16x2 char LCD plus keypad for pi works with test code but re

EL Wire/Tape/Panels, LEDs, pixels and strips, LCDs and TFTs, etc products from Adafruit

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
matthew s
 
Posts: 3
Joined: Sat Aug 24, 2013 9:34 pm

16x2 char LCD plus keypad for pi works with test code but re

Post by matthew s »

I have my version 1 raspberry pi with the Adafruit 16x2 char LCD. I followed the tutorials to get it to work and the test code worked fine. My next step was to add some code in order for it to display the IP address. I took code from https://github.com/adafruit/Adafruit-Ra ... example.py

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)

When ran, I get this error:

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
I am not sure what is causing the error. I've double checked the line 93 and it is the same as the original code.

User avatar
matthew s
 
Posts: 3
Joined: Sat Aug 24, 2013 9:34 pm

Re: 16x2 char LCD plus keypad for pi works with test code bu

Post by matthew s »

Okay, So I got the program to run but it will not print the IP. it's aparently returning a none type. The code is the run_cmd(cmd) snippet.

User avatar
matthew s
 
Posts: 3
Joined: Sat Aug 24, 2013 9:34 pm

Re: 16x2 char LCD plus keypad for pi works with test code bu

Post by matthew s »

Edit: not nonetype but class 'subprocess.Popen'

Locked
Please be positive and constructive with your questions and comments.

Return to “Glowy things (LCD, LED, TFT, EL) purchased at Adafruit”