6
6
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
7
7
#
8
8
9
- import RPi .GPIO as GPIO
10
9
from time import sleep
11
10
12
11
class Adafruit_CharLCD :
@@ -55,18 +54,22 @@ class Adafruit_CharLCD:
55
54
56
55
57
56
58
- def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ]):
59
-
57
+ def __init__ (self , pin_rs = 25 , pin_e = 24 , pins_db = [23 , 17 , 21 , 22 ], GPIO = None ):
58
+ # Emulate the old behavior of using RPi.GPIO if we haven't been given
59
+ # an explicit GPIO interface to use
60
+ if not GPIO :
61
+ import RPi .GPIO as GPIO
62
+ self .GPIO = GPIO
60
63
self .pin_rs = pin_rs
61
64
self .pin_e = pin_e
62
65
self .pins_db = pins_db
63
66
64
- GPIO .setmode (GPIO .BCM )
65
- GPIO .setup (self .pin_e , GPIO .OUT )
66
- GPIO .setup (self .pin_rs , GPIO .OUT )
67
+ self . GPIO .setmode (GPIO .BCM )
68
+ self . GPIO .setup (self .pin_e , GPIO .OUT )
69
+ self . GPIO .setup (self .pin_rs , GPIO .OUT )
67
70
68
71
for pin in self .pins_db :
69
- GPIO .setup (pin , GPIO .OUT )
72
+ self . GPIO .setup (pin , GPIO .OUT )
70
73
71
74
self .write4bits (0x33 ) # initialization
72
75
self .write4bits (0x32 ) # initialization
@@ -204,23 +207,23 @@ def write4bits(self, bits, char_mode=False):
204
207
205
208
bits = bin (bits )[2 :].zfill (8 )
206
209
207
- GPIO .output (self .pin_rs , char_mode )
210
+ self . GPIO .output (self .pin_rs , char_mode )
208
211
209
212
for pin in self .pins_db :
210
- GPIO .output (pin , False )
213
+ self . GPIO .output (pin , False )
211
214
212
215
for i in range (4 ):
213
216
if bits [i ] == "1" :
214
- GPIO .output (self .pins_db [::- 1 ][i ], True )
217
+ self . GPIO .output (self .pins_db [::- 1 ][i ], True )
215
218
216
219
self .pulseEnable ()
217
220
218
221
for pin in self .pins_db :
219
- GPIO .output (pin , False )
222
+ self . GPIO .output (pin , False )
220
223
221
224
for i in range (4 ,8 ):
222
225
if bits [i ] == "1" :
223
- GPIO .output (self .pins_db [::- 1 ][i - 4 ], True )
226
+ self . GPIO .output (self .pins_db [::- 1 ][i - 4 ], True )
224
227
225
228
self .pulseEnable ()
226
229
@@ -231,11 +234,11 @@ def delayMicroseconds(self, microseconds):
231
234
232
235
233
236
def pulseEnable (self ):
234
- GPIO .output (self .pin_e , False )
237
+ self . GPIO .output (self .pin_e , False )
235
238
self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
236
- GPIO .output (self .pin_e , True )
239
+ self . GPIO .output (self .pin_e , True )
237
240
self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
238
- GPIO .output (self .pin_e , False )
241
+ self . GPIO .output (self .pin_e , False )
239
242
self .delayMicroseconds (1 ) # commands need > 37us to settle
240
243
241
244
0 commit comments