From 4a4d3a537437ecb963a8b55c5c205369e53cb3fd Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:37:31 +0900 Subject: [PATCH 01/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 60 ++++++++++++++-------------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 22060c94..87d842b1 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -57,36 +57,38 @@ class Adafruit_CharLCD: def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 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: - import RPi.GPIO as GPIO - self.GPIO = GPIO - self.pin_rs = pin_rs - self.pin_e = pin_e - self.pins_db = pins_db - - self.GPIO.setmode(GPIO.BCM) - self.GPIO.setup(self.pin_e, GPIO.OUT) - self.GPIO.setup(self.pin_rs, GPIO.OUT) - - for pin in self.pins_db: - self.GPIO.setup(pin, GPIO.OUT) - - self.write4bits(0x33) # initialization - self.write4bits(0x32) # initialization - self.write4bits(0x28) # 2 line 5x7 matrix - self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor - self.write4bits(0x06) # shift cursor right - - self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF - - self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS - self.displayfunction |= self.LCD_2LINE - - """ Initialize to default text direction (for romance languages) """ - self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode - + try: + if not GPIO: + import RPi.GPIO as GPIO + self.GPIO = GPIO + self.pin_rs = pin_rs + self.pin_e = pin_e + self.pins_db = pins_db + + self.GPIO.setmode(GPIO.BCM) + self.GPIO.setup(self.pin_e, GPIO.OUT) + self.GPIO.setup(self.pin_rs, GPIO.OUT) + + for pin in self.pins_db: + self.GPIO.setup(pin, GPIO.OUT) + + self.write4bits(0x33) # initialization + self.write4bits(0x32) # initialization + self.write4bits(0x28) # 2 line 5x7 matrix + self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor + self.write4bits(0x06) # shift cursor right + + self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF + + self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS + self.displayfunction |= self.LCD_2LINE + + """ Initialize to default text direction (for romance languages) """ + self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode + self.clear() + finally: GPIO.cleanup() def begin(self, cols, lines): From 3df4356a9a5b79d6c036f12b71aa3b409c98d465 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:38:43 +0900 Subject: [PATCH 02/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 87d842b1..1872791c 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -87,8 +87,8 @@ 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() - finally: GPIO.cleanup() + self.clear() + finally: GPIO.cleanup() def begin(self, cols, lines): From 20332ed1e5c252334ceba46c535e17ac91c5ebb0 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:40:38 +0900 Subject: [PATCH 03/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 1872791c..69840672 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -88,7 +88,8 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode self.clear() - finally: GPIO.cleanup() + except KeyboardInterrupt: + GPIO.cleanup() def begin(self, cols, lines): From c923dd9283b9c002be0af0a39ad58ac39f551bc3 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:42:04 +0900 Subject: [PATCH 04/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 69840672..1fc0da6b 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -88,7 +88,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode self.clear() - except KeyboardInterrupt: + except: GPIO.cleanup() From 122b143810bcdc79c62ce32c78b982ae9ffc5a28 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:43:41 +0900 Subject: [PATCH 05/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 1fc0da6b..830c1d96 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -66,6 +66,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.pins_db = pins_db self.GPIO.setmode(GPIO.BCM) + GPIO.cleanup() self.GPIO.setup(self.pin_e, GPIO.OUT) self.GPIO.setup(self.pin_rs, GPIO.OUT) From 54596901fed0171f92815704436820c6c639c42a Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:48:01 +0900 Subject: [PATCH 06/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 830c1d96..ad23d566 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -89,7 +89,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode self.clear() - except: + finally: GPIO.cleanup() From 504a2fcb6140903f9d14c0ef2d1d8c36f6ad9ad7 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:50:41 +0900 Subject: [PATCH 07/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 1 - 1 file changed, 1 deletion(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index ad23d566..ff010155 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -66,7 +66,6 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): self.pins_db = pins_db self.GPIO.setmode(GPIO.BCM) - GPIO.cleanup() self.GPIO.setup(self.pin_e, GPIO.OUT) self.GPIO.setup(self.pin_rs, GPIO.OUT) From 6d7ae7a02bcd83eed75a058cfe3742f72dc96be4 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:52:24 +0900 Subject: [PATCH 08/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 68 ++++++++++++++-------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index ff010155..752b2d4a 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -57,39 +57,36 @@ class Adafruit_CharLCD: def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None): # Emulate the old behavior of using RPi.GPIO if we haven't been given # an explicit GPIO interface to use - try: - if not GPIO: - import RPi.GPIO as GPIO - self.GPIO = GPIO - self.pin_rs = pin_rs - self.pin_e = pin_e - self.pins_db = pins_db - - self.GPIO.setmode(GPIO.BCM) - self.GPIO.setup(self.pin_e, GPIO.OUT) - self.GPIO.setup(self.pin_rs, GPIO.OUT) - - for pin in self.pins_db: - self.GPIO.setup(pin, GPIO.OUT) - - self.write4bits(0x33) # initialization - self.write4bits(0x32) # initialization - self.write4bits(0x28) # 2 line 5x7 matrix - self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor - self.write4bits(0x06) # shift cursor right - - self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF - - self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS - self.displayfunction |= self.LCD_2LINE - - """ Initialize to default text direction (for romance languages) """ - self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT - self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode - - self.clear() - finally: - GPIO.cleanup() + if not GPIO: + import RPi.GPIO as GPIO + self.GPIO = GPIO + self.pin_rs = pin_rs + self.pin_e = pin_e + self.pins_db = pins_db + + self.GPIO.setmode(GPIO.BCM) + self.GPIO.setup(self.pin_e, GPIO.OUT) + self.GPIO.setup(self.pin_rs, GPIO.OUT) + + for pin in self.pins_db: + self.GPIO.setup(pin, GPIO.OUT) + + self.write4bits(0x33) # initialization + self.write4bits(0x32) # initialization + self.write4bits(0x28) # 2 line 5x7 matrix + self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor + self.write4bits(0x06) # shift cursor right + + self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF + + self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS + self.displayfunction |= self.LCD_2LINE + + """ Initialize to default text direction (for romance languages) """ + self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT + self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode + + self.clear() def begin(self, cols, lines): @@ -253,6 +250,9 @@ def message(self, text): self.write4bits(0xC0) # next line else: self.write4bits(ord(char),True) + + def exit(self): + self.GPIO.cleanup(); if __name__ == '__main__': @@ -261,4 +261,4 @@ def message(self, text): lcd.clear() lcd.message(" Adafruit 16x2\n Standard LCD") - + lcd.exit(); From e3b8970855709dabcbec56e21ccc097516b8e0b5 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 14:55:31 +0900 Subject: [PATCH 09/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 752b2d4a..b2d5d2a4 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -257,8 +257,10 @@ def exit(self): if __name__ == '__main__': - lcd = Adafruit_CharLCD() - - lcd.clear() - lcd.message(" Adafruit 16x2\n Standard LCD") - lcd.exit(); + try: + lcd = Adafruit_CharLCD() + + lcd.clear() + lcd.message(" Adafruit 16x2\n Standard LCD") + except KeyboardInterrupt: + lcd.exit(); From ed2fdbb22fbe686dd3b14671f6cf0b12230f9e07 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 15:00:13 +0900 Subject: [PATCH 10/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index b2d5d2a4..b25a18a1 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: From f95b85239d58f4f83e61e773ed764a32f941615c Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 15:05:54 +0900 Subject: [PATCH 11/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index b25a18a1..7fc11a27 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -264,3 +264,5 @@ def exit(self): lcd.message(" Adafruit 16x2\n Standard LCD") except KeyboardInterrupt: lcd.exit(); + finally: + lcd.exit(); From 4a5713c58f23ec072a5a5dd9d687183e3f590c15 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 15:07:13 +0900 Subject: [PATCH 12/14] Update Adafruit_CharLCD_IPclock_example.py --- .../Adafruit_CharLCD_IPclock_example.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py index d3f6958f..19503a53 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py @@ -16,9 +16,15 @@ 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() +finally: + lcd.exit() From 3e4c90fc2ccc1e0d81fe05c79519c96eff272021 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 15:11:10 +0900 Subject: [PATCH 13/14] Update Adafruit_CharLCD_IPclock_example.py --- Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py index 19503a53..e6d24931 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py @@ -26,5 +26,3 @@ def run_cmd(cmd): sleep(2) except KeyboardInterrupt: lcd.exit() -finally: - lcd.exit() From af10968e13eac96a71867ced8282b2376195a128 Mon Sep 17 00:00:00 2001 From: Denny Lim Date: Thu, 13 Mar 2014 15:18:18 +0900 Subject: [PATCH 14/14] Update Adafruit_CharLCD.py --- Adafruit_CharLCD/Adafruit_CharLCD.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Adafruit_CharLCD/Adafruit_CharLCD.py b/Adafruit_CharLCD/Adafruit_CharLCD.py index 7fc11a27..498f4255 100755 --- a/Adafruit_CharLCD/Adafruit_CharLCD.py +++ b/Adafruit_CharLCD/Adafruit_CharLCD.py @@ -64,6 +64,7 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 27, 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)