Skip to content

Commit 22c8932

Browse files
committed
Merge pull request adafruit#6 from dberlin/master
Make CharLCD work over anything that supports the GPIO interface (such as bus expanders)
2 parents 6a8ed9c + 522c945 commit 22c8932

File tree

1 file changed

+18
-15
lines changed

1 file changed

+18
-15
lines changed

Adafruit_CharLCD/Adafruit_CharLCD.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
77
#
88

9-
import RPi.GPIO as GPIO
109
from time import sleep
1110

1211
class Adafruit_CharLCD:
@@ -55,18 +54,22 @@ class Adafruit_CharLCD:
5554

5655

5756

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
6063
self.pin_rs = pin_rs
6164
self.pin_e = pin_e
6265
self.pins_db = pins_db
6366

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)
6770

6871
for pin in self.pins_db:
69-
GPIO.setup(pin, GPIO.OUT)
72+
self.GPIO.setup(pin, GPIO.OUT)
7073

7174
self.write4bits(0x33) # initialization
7275
self.write4bits(0x32) # initialization
@@ -204,23 +207,23 @@ def write4bits(self, bits, char_mode=False):
204207

205208
bits=bin(bits)[2:].zfill(8)
206209

207-
GPIO.output(self.pin_rs, char_mode)
210+
self.GPIO.output(self.pin_rs, char_mode)
208211

209212
for pin in self.pins_db:
210-
GPIO.output(pin, False)
213+
self.GPIO.output(pin, False)
211214

212215
for i in range(4):
213216
if bits[i] == "1":
214-
GPIO.output(self.pins_db[::-1][i], True)
217+
self.GPIO.output(self.pins_db[::-1][i], True)
215218

216219
self.pulseEnable()
217220

218221
for pin in self.pins_db:
219-
GPIO.output(pin, False)
222+
self.GPIO.output(pin, False)
220223

221224
for i in range(4,8):
222225
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)
224227

225228
self.pulseEnable()
226229

@@ -231,11 +234,11 @@ def delayMicroseconds(self, microseconds):
231234

232235

233236
def pulseEnable(self):
234-
GPIO.output(self.pin_e, False)
237+
self.GPIO.output(self.pin_e, False)
235238
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)
237240
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)
239242
self.delayMicroseconds(1) # commands need > 37us to settle
240243

241244

0 commit comments

Comments
 (0)