and I added it to https://github.com/adafruit/Adafruit-Ra ... CDPlate.py
I modified the code like so, adding the run_cmd function and made the original test code into comments.
Code: Select all
def message(self, text):
""" Send string to LCD. Newline wraps to second line"""
lines = str(text).split('\n') # Split at newline(s)
for i, line in enumerate(lines): # For each substring...
if i > 0: # If newline(s),
self.write(0xC0) # set DDRAM address to 2nd line
self.write(line, True) # Issue substring
def backlight(self, color):
c = ~color
self.porta = (self.porta & 0b00111111) | ((c & 0b011) << 6)
self.portb = (self.portb & 0b11111110) | ((c & 0b100) >> 2)
# Has to be done as two writes because sequential operation is off.
self.i2c.bus.write_byte_data(
self.i2c.address, self.MCP23017_GPIOA, self.porta)
self.i2c.bus.write_byte_data(
self.i2c.address, self.MCP23017_GPIOB, self.portb)
def run_cmd(cmd):
p = Popen(cmd, shell=True, stdout=PIPE)
output = p.communicate()[0]
return output
# Read state of single button
def buttonPressed(self, b):
return (self.i2c.readU8(self.MCP23017_GPIOA) >> b) & 1
# Read and return bitmask of combined button state
def buttons(self):
return self.i2c.readU8(self.MCP23017_GPIOA) & 0b11111
# ----------------------------------------------------------------------
# Test code
"""
if __name__ == '__main__':
lcd = Adafruit_CharLCDPlate()
lcd.begin(16, 2)
lcd.clear()
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
sleep(1)
col = (('Red' , lcd.RED) , ('Yellow', lcd.YELLOW), ('Green' , lcd.GREEN),
('Teal', lcd.TEAL), ('Blue' , lcd.BLUE) , ('Violet', lcd.VIOLET),
('Off' , lcd.OFF) , ('On' , lcd.ON))
print "Cycle thru backlight colors"
for c in col:
print c[0]
lcd.clear()
lcd.message(c[0])
lcd.backlight(c[1])
sleep(0.5)
btn = ((lcd.SELECT, 'Select', lcd.ON),
(lcd.LEFT , 'Left' , lcd.RED),
(lcd.UP , 'Up' , lcd.BLUE),
(lcd.DOWN , 'Down' , lcd.GREEN),
(lcd.RIGHT , 'Right' , lcd.VIOLET))
print "Try buttons on plate"
lcd.clear()
lcd.message("Try buttons")
prev = -1
while True:
for b in btn:
if lcd.buttonPressed(b[0]):
if b is not prev:
print b[1]
lcd.clear()
lcd.message(b[1])
lcd.backlight(b[2])
prev = b
break
"""
if __name__ == '__main__':
lcd = Adafruit_CharLCDPlate()
lcd.begin(16, 2)
lcd.clear()
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
while 1:
lcd.clear()
ipaddr = run_cmd(cmd)
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
lcd.message('IP %s' % ( ipaddr ) )
sleep(2)
Code: Select all
Traceback (most recent call last):
File "displayip.py", line 489, in <module>
lcd = Adafruit_CharLCDPlate()
File "displayip.py", line 93, in __init__
self.i2c.address, self.MCP23017_IOCON_BANK1, 0)
IOError: [Errno 5] Input/output error