diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 22060c94..498f4255 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -54,7 +54,7 @@ class Adafruit_CharLCD: - def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): + def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 22], GPIO = None): # Emulate the old behavior of using RPi.GPIO if we haven't been given # an explicit GPIO interface to use if not GPIO: @@ -64,6 +64,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.pin_e = pin_e self.pins_db = pins_db + self.GPIO.cleanup() self.GPIO.setmode(GPIO.BCM) self.GPIO.setup(self.pin_e, GPIO.OUT) self.GPIO.setup(self.pin_rs, GPIO.OUT) @@ -86,7 +87,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode - self.clear() + self.clear() def begin(self, cols, lines): @@ -250,12 +251,19 @@ def message(self, text): self.write4bits(0xC0) # next line else: self.write4bits(ord(char),True) + + def exit(self): + self.GPIO.cleanup(); if __name__ == '__main__': - lcd = Adafruit_CharLCD() - - lcd.clear() - lcd.message(" Adafruit 16x2\n Standard LCD") - + try: + lcd = Adafruit_CharLCD() + + lcd.clear() + lcd.message(" Adafruit 16x2\n Standard LCD") + except KeyboardInterrupt: + lcd.exit(); + finally: + lcd.exit(); diff --git a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py index d3f6958f..e6d24931 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py @@ -16,9 +16,13 @@ def run_cmd(cmd): output = p.communicate()[0] return output -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) +try: + + 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) +except KeyboardInterrupt: + lcd.exit()