class ArduinoBedroom(object): def __init__(self, port="/dev/ttyACM0"): self.ser = LoggingSerial(port=port, baudrate=115200, timeout=1) self.ser.flush() def ping(self): self.ser.write("\x60\x00\x00") msg = self.ser.readJson() assert msg == {"ok": True}, msg def poll(self): self.ser.write("\x60\x01\x00") ret = self.ser.readJson() ret["motion"] = int(ret["motion"] > 100) return ret def setSpeakerChoice(self, pillow): self.ser.write("\x60\x02" + chr(pillow)) return self.ser.readJson() def setLeds(self, colors): """ shift out this sequence of (r,g,b) triples of 10-bit ints The nearest led gets color[0], etc. """ resetCurrent = "".join( bitstring.pack("0b01, uint:10, uint:10, uint:10", 127, 127, 127).bytes for loop in range(len(colors)) ) out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10", b, r, g).bytes for r, g, b in reversed(colors)) out = resetCurrent + out self.ser.write("\x60\x03" + chr(len(out)) + out) msg = self.ser.readJson() assert msg == {"ok": 1}, msg
class Board(object): """ arduino board actions, plus the last values we wrote to it """ def __init__(self, port): self.ser = LoggingSerial(port=port) self.ser.flush() self.setLcd("") self.setLcdBrightness(0) self.setYardLight(0) def ping(self): self.getDoor() def getDoor(self): self.ser.write("\xff\x01") ret = self.ser.readJson() return ret['door'] def setYardLight(self, level): self.currentYardLight = bool(level) self.ser.write("\xff\x04" + chr(bool(self.currentYardLight))) def getYardLight(self): return self.currentYardLight def getLcd(self): return self.currentText def setLcd(self, txt): """ up to 8*21 chars """ self.currentText = txt self.ser.write("\xff\x00" + txt + "\x00") def getLcdBrightness(self): return self.currentBrightness def setLcdBrightness(self, b): """b in 0 to 255""" self.currentBrightness = b self.ser.write("\xff\x03" + chr(b)) def getTemperature(self): """returns parsed json from the board""" self.ser.write("\xff\x02") # this can take 1.25 seconds per retry f = self.ser.readJson() if f['temp'] > 184 or f['temp'] < -100: # this fails a lot, maybe 50% of the time. retry if # you want raise ValueError("out of range temp value (%s)" % f) return f
class ArduinoDesk(object): def __init__(self, ports=['/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A900gbcG-if00-port0']): self.ser = LoggingSerial(ports=ports, baudrate=115200, timeout=1) def ping(self): self.ser.write("\x60\x00") msg = self.ser.readJson() assert msg == {"ok":"ping"}, msg def shiftbrite(self, colors): """ shift out this sequence of (r,g,b) triples of 10-bit ints """ out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10", b, r, g).bytes for r,g,b in colors) self.ser.write("\x60\x01" + chr(len(out)) + out) msg = self.ser.readJson() assert msg == {"ok":1}, msg
class ArduinoGarage(object): def __init__(self, port='/dev/ttyACM0'): self.ser = LoggingSerial(port=port, baudrate=115200, timeout=1) time.sleep(2) # wait for a arduino reset to pass self.ser.flush() self.ping() def ping(self): self.ser.write("\x60\x00\x00") msg = self.ser.readJson() assert msg == {"ok":True}, msg def poll(self): self.ser.write("\x60\x01\x00") ret = self.ser.readJson() return ret def lastLevel(self): self.ser.write("\x60\x02\x00") return self.ser.readJson()['z'] def setThreshold(self, t): """set 10-bit threshold""" self.ser.write("\x60\x03"+chr(max(1 << 2, t) >> 2)) return self.ser.readJson()['threshold'] def setGarage(self, level): """set garage door opener pin""" self.ser.write("\x60\x04"+chr(int(bool(level)))) return self.ser.readJson()['garage'] def setVideoSelect(self, chan): """set video select bits from 0..3""" self.ser.write("\x60\x05"+chr(chan)) return self.ser.readJson()['videoSelect'] def shiftbrite(self, colors): """ shift out this sequence of (r,g,b) triples of 10-bit ints """ resetCurrent = "".join(bitstring.pack("0b01, uint:10, uint:10, uint:10", 127, 127, 127).bytes for loop in range(len(colors))) out = "".join(bitstring.pack("0b00, uint:10, uint:10, uint:10", b, r, g).bytes for r,g,b in colors) out = resetCurrent + out self.ser.write("\x60\x06" + chr(len(out)) + out) msg = self.ser.readJson() assert msg == {"ok":1}, msg def virtualwire(self, colors): """ send this sequence of (r,g,b) 8-bit triples """ numLeds = 4 # vw receiver wants data for all leds every time colors = (list(colors) + [(0,0,0)] * numLeds)[:numLeds] msg = "".join("%s%s%s" % (chr(r), chr(g), chr(b)) for r,g,b in colors) self.ser.write("\x60\x07" + chr(len(msg)) + msg) msg = self.ser.readJson() assert msg == {"sent": 12}, msg