def lchange(self, buffer, dataIn): buffer[headerOffsets["scmd"]] = self.subcommands["cpin"] buffer[headerOffsets["out_0"]] = b"\x01" buffer.append(to_bytes(dataIn["data"]["pin"], 2, 0)) buffer.append(to_bytes(dataIn["data"]["direction"], 1, 1)) buffer.append(to_bytes(dataIn["data"]["class"], 1, 1)) return buffer
def ldel(self, buffer, dataIn): buffer[headerOffsets["scmd"]] = self.subcommands["del"] buffer[headerOffsets["out_0"]] = "\x01" buffer.append(to_bytes(dataIn["data"]["pin"], 2, 0)) buffer.append(to_bytes(dataIn["data"]["pin"], 2, 0)) if dataIn["data"]["pin"] in self.pins: del self.pins[dataIn["data"]["pin"]] return buffer
def lget(self, buffer, dataIn): buffer[headerOffsets["scmd"]] = self.subcommands["get"] outLen = to_bytes(1, 2, "little") inLen = to_bytes(1, 2, "little") buffer[headerOffsets["out_0"]] = outLen[0] buffer[headerOffsets["out_1"]] = outLen[1] buffer[headerOffsets["in_0"]] = inLen[0] buffer[headerOffsets["in_1"]] = inLen[1] for pinpart in to_bytes(int(dataIn["data"]["pin"]), 2): buffer.append(pinpart) return buffer
def lset(self, buffer, dataIn, state=0): buffer[headerOffsets["scmd"]] = self.subcommands["set"] # Right now, we only support 1 pin at a time... but this will exist for the future. outLen = to_bytes(1, 2, "little") buffer[headerOffsets["out_0"]] = outLen[0] buffer[headerOffsets["out_1"]] = outLen[1] for pinpart in to_bytes(int(dataIn["data"]["pin"]), 2): buffer.append(pinpart) for setpart in to_bytes(int(dataIn["data"]["state"]), 2): buffer.append(setpart) return buffer
def ladd(self, buffer, dataIn): buffer[headerOffsets["scmd"]] = self.subcommands["add"] buffer[headerOffsets["out_0"]] = b"\x01" buffer.append(to_bytes(dataIn["data"]["pin"], 2, 0)) buffer.append(to_bytes(dataIn["data"]["direction"], 1, 1)) buffer.append(to_bytes(dataIn["data"]["class"], 1, 1)) self.pins[dataIn["data"]["pin"]] = { "state": None, "pin": dataIn["data"]["pin"], "direction": dataIn["data"]["direction"], "class": dataIn["data"]["class"], } return buffer
def ladd(self, buffer, dataIn): buffer[headerOffsets["scmd"]] = self.subcommands["add"] buffer[headerOffsets["out_0"]] = '\x01' buffer.append(to_bytes(dataIn['data']['pin'], 1, 1)) for b in to_bytes(dataIn['data']['length'], 2, 1): buffer.append(b) if dataIn["id"] not in self.strips or not isinstance(self.strips[dataIn["id"]], dict): self.strips[dataIn["id"]] = {} self.strips[dataIn["id"]]["pin"] = dataIn['data']['pin'] self.strips[dataIn["id"]]["length"] = dataIn['data']['length'] return buffer
def lset(self, buffer, dataIn, state=0): if state: buffer[headerOffsets["scmd"]] = self.subcommands["ctrli"] else: buffer[headerOffsets["scmd"]] = self.subcommands["ctrl"] outLen = to_bytes(len(dataIn['data']['leds']), 2, "little") buffer[headerOffsets['out_0']] = outLen[0] buffer[headerOffsets['out_1']] = outLen[1] for idx in dataIn['data']['leds']: for pixelp in to_bytes(int(idx), 2, 1): buffer.append(pixelp) for color in dataIn['data']['leds'][idx]: buffer.append(to_bytes(color, 1, 1)) return buffer
def createMessage(self, dataIn): try: if "type" not in dataIn: #This probably should be removed. return 1 if "data" not in dataIn: return 2 if "command" not in dataIn or dataIn["command"] not in self.subcommands: return 3 buffer = self.assembleHeader(dataIn["type"]) buffer.append(to_bytes(dataIn["id"], 1, 1)) if dataIn["command"] == "ctrl": buffer = self.lset(buffer, dataIn) elif dataIn["command"] == "ctrli": buffer = self.lset(buffer, dataIn, 1) elif dataIn["command"] == "get": buffer = self.lget(buffer, dataIn) elif dataIn["command"] == "get_all": buffer = self.lget(buffer, dataIn) elif dataIn["command"] == "clear": buffer = self.lclear(buffer, dataIn) elif dataIn["command"] == "manage": buffer = self.lmanage(buffer) return buffer #this is the only special case. elif dataIn["command"] == "add": buffer = self.ladd(buffer, dataIn) elif dataIn["command"] == "del": buffer = self.ldelete(buffer, dataIn) else: self.log("UARTNeopixel.createMessage(), Unknown command was provided: '%s'" % str(dataIn["command"])) return None buffer = self.finishMessage(buffer) return buffer except: self.log ("UARTNeopixel.createMessage(), exception with data: '%s'" % str(dataIn)) return None