Skip to content

Commit f1cf300

Browse files
author
K. Townsend
committed
2 parents e04798d + 1d112c2 commit f1cf300

File tree

3 files changed

+114
-52
lines changed

3 files changed

+114
-52
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

+48-49
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111
from Adafruit_MCP230xx import Adafruit_MCP230XX
1212
import smbus
1313

14-
# change busnum = 0 to bbusnum = 1 if you have a rev 2 Pi!
15-
mcp = Adafruit_MCP230XX(busnum = 0, address = 0x20, num_gpios = 16)
1614

17-
class Adafruit_CharLCD:
15+
class Adafruit_CharLCDPlate:
1816

1917
OUTPUT = 0
2018
INPUT = 1
@@ -80,20 +78,23 @@ class Adafruit_CharLCD:
8078

8179

8280

83-
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
81+
82+
def __init__(self, busnum=0, pin_rs=15, pin_e=13, pins_db=[12, 11, 10, 9], pin_rw=14):
8483
self.pin_rs = pin_rs
8584
self.pin_e = pin_e
8685
self.pin_rw = pin_rw
8786
self.pins_db = pins_db
8887

89-
mcp.config(self.pin_e, self.OUTPUT)
90-
mcp.config(self.pin_rs, self.OUTPUT)
91-
mcp.config(self.pin_rw, self.OUTPUT)
92-
mcp.output(self.pin_rw, 0)
93-
mcp.output(self.pin_e, 0)
88+
self.mcp = Adafruit_MCP230XX(busnum = busnum, address = 0x20, num_gpios = 16)
89+
90+
self.mcp.config(self.pin_e, self.OUTPUT)
91+
self.mcp.config(self.pin_rs, self.OUTPUT)
92+
self.mcp.config(self.pin_rw, self.OUTPUT)
93+
self.mcp.output(self.pin_rw, 0)
94+
self.mcp.output(self.pin_e, 0)
9495

9596
for pin in self.pins_db:
96-
mcp.config(pin, self.OUTPUT)
97+
self.mcp.config(pin, self.OUTPUT)
9798

9899
self.write4bits(0x33) # initialization
99100
self.write4bits(0x32) # initialization
@@ -111,24 +112,24 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
111112
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
112113

113114
# turn on backlights!
114-
mcp.config(6, mcp.OUTPUT)
115-
mcp.config(7, mcp.OUTPUT)
116-
mcp.config(8, mcp.OUTPUT)
117-
mcp.output(6, 0) # red
118-
mcp.output(7, 0) # green
119-
mcp.output(8, 0) # blue
115+
self.mcp.config(6, self.mcp.OUTPUT)
116+
self.mcp.config(7, self.mcp.OUTPUT)
117+
self.mcp.config(8, self.mcp.OUTPUT)
118+
self.mcp.output(6, 0) # red
119+
self.mcp.output(7, 0) # green
120+
self.mcp.output(8, 0) # blue
120121

121122
# turn on pullups
122-
mcp.pullup(self.SELECT, True)
123-
mcp.pullup(self.LEFT, True)
124-
mcp.pullup(self.RIGHT, True)
125-
mcp.pullup(self.UP, True)
126-
mcp.pullup(self.DOWN, True)
127-
mcp.config(self.SELECT, mcp.INPUT)
128-
mcp.config(self.LEFT, mcp.INPUT)
129-
mcp.config(self.RIGHT, mcp.INPUT)
130-
mcp.config(self.DOWN, mcp.INPUT)
131-
mcp.config(self.UP, mcp.INPUT)
123+
self.mcp.pullup(self.SELECT, True)
124+
self.mcp.pullup(self.LEFT, True)
125+
self.mcp.pullup(self.RIGHT, True)
126+
self.mcp.pullup(self.UP, True)
127+
self.mcp.pullup(self.DOWN, True)
128+
self.mcp.config(self.SELECT, self.mcp.INPUT)
129+
self.mcp.config(self.LEFT, self.mcp.INPUT)
130+
self.mcp.config(self.RIGHT, self.mcp.INPUT)
131+
self.mcp.config(self.DOWN, self.mcp.INPUT)
132+
self.mcp.config(self.UP, self.mcp.INPUT)
132133

133134
def begin(self, cols, lines):
134135
if (lines > 1):
@@ -214,30 +215,30 @@ def write4bits(self, bits, char_mode=False):
214215
""" Send command to LCD """
215216
#self.delayMicroseconds(1000) # 1000 microsecond sleep
216217
bits=bin(bits)[2:].zfill(8)
217-
mcp.output(self.pin_rs, char_mode)
218+
self.mcp.output(self.pin_rs, char_mode)
218219

219220
for i in range(4):
220221
if bits[i] == "1":
221-
mcp.output(self.pins_db[::-1][i], True)
222+
self.mcp.output(self.pins_db[::-1][i], True)
222223
else:
223-
mcp.output(self.pins_db[::-1][i], False)
224+
self.mcp.output(self.pins_db[::-1][i], False)
224225
self.pulseEnable()
225226

226227
for i in range(4,8):
227228
if bits[i] == "1":
228-
mcp.output(self.pins_db[::-1][i-4], True)
229+
self.mcp.output(self.pins_db[::-1][i-4], True)
229230
else:
230-
mcp.output(self.pins_db[::-1][i-4], False)
231+
self.mcp.output(self.pins_db[::-1][i-4], False)
231232
self.pulseEnable()
232233

233234
def delayMicroseconds(self, microseconds):
234235
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
235236
sleep(seconds)
236237

237238
def pulseEnable(self):
238-
mcp.output(self.pin_e, True)
239+
self.mcp.output(self.pin_e, True)
239240
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
240-
mcp.output(self.pin_e, False)
241+
self.mcp.output(self.pin_e, False)
241242
#self.delayMicroseconds(1) # commands need > 37us to settle
242243

243244
def message(self, text):
@@ -249,39 +250,37 @@ def message(self, text):
249250
self.write4bits(ord(char),True)
250251

251252
def backlight(self, color):
252-
mcp.output(6, not color & 0x01)
253-
mcp.output(7, not color & 0x02)
254-
mcp.output(8, not color & 0x04)
253+
self.mcp.output(6, not color & 0x01)
254+
self.mcp.output(7, not color & 0x02)
255+
self.mcp.output(8, not color & 0x04)
255256

257+
def buttonPressed(self, buttonname):
258+
if (buttonname > self.LEFT):
259+
return false
256260

257-
if __name__ == '__main__':
261+
return not self.mcp.input(buttonname)
258262

259263

260-
# input test
261-
# for i in range(16):
262-
# mcp.pullup(i, 1)
263-
# while (True):
264-
# for i in range(16):
265-
# print "%d: %x" % (i, mcp.input(i) >> i)
264+
if __name__ == '__main__':
266265

267-
lcd = Adafruit_CharLCD(15, 13, [12,11,10,9], 14)
266+
lcd = Adafruit_CharLCDPlate(busnum = 0)
268267
lcd.clear()
269268
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
270269
sleep(1)
271270
while 1:
272-
if (not mcp.input(lcd.LEFT)):
271+
if (lcd.buttonPressed(lcd.LEFT)):
273272
lcd.backlight(lcd.RED)
274273

275-
if (not mcp.input(lcd.UP)):
274+
if (lcd.buttonPressed(lcd.UP)):
276275
lcd.backlight(lcd.BLUE)
277276

278-
if (not mcp.input(lcd.DOWN)):
277+
if (lcd.buttonPressed(lcd.DOWN)):
279278
lcd.backlight(lcd.GREEN)
280279

281-
if (not mcp.input(lcd.RIGHT)):
280+
if (lcd.buttonPressed(lcd.RIGHT)):
282281
lcd.backlight(lcd.VIOLET)
283282

284-
if (not mcp.input(lcd.SELECT)):
283+
if (lcd.buttonPressed(lcd.SELECT)):
285284
lcd.backlight(lcd.ON)
286285

287286

Adafruit_CharLCDPlate/LCDtest.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/python
2+
3+
from time import sleep
4+
from Adafruit_I2C import Adafruit_I2C
5+
from Adafruit_MCP230xx import Adafruit_MCP230XX
6+
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
7+
8+
import smbus
9+
10+
11+
# initialize the LCD plate
12+
# use busnum = 0 for raspi version 1 (256MB) and busnum = 1 for version 2
13+
lcd = Adafruit_CharLCDPlate(busnum = 0)
14+
15+
# clear display
16+
lcd.clear()
17+
# hello!
18+
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
19+
sleep(1)
20+
21+
# first loop, just changes the color
22+
lcd.backlight(lcd.RED)
23+
sleep(.5)
24+
lcd.backlight(lcd.YELLOW)
25+
sleep(.5)
26+
lcd.backlight(lcd.GREEN)
27+
sleep(.5)
28+
lcd.backlight(lcd.TEAL)
29+
sleep(.5)
30+
lcd.backlight(lcd.BLUE)
31+
sleep(.5)
32+
lcd.backlight(lcd.VIOLET)
33+
sleep(.5)
34+
lcd.backlight(lcd.ON)
35+
sleep(.5)
36+
lcd.backlight(lcd.OFF)
37+
sleep(.5)
38+
39+
while 1:
40+
if (lcd.buttonPressed(lcd.LEFT)):
41+
lcd.clear()
42+
lcd.message("Red Red Wine")
43+
lcd.backlight(lcd.RED)
44+
45+
if (lcd.buttonPressed(lcd.UP)):
46+
lcd.clear()
47+
lcd.message("Sita Sings \nthe blues")
48+
lcd.backlight(lcd.BLUE)
49+
50+
if (lcd.buttonPressed(lcd.DOWN)):
51+
lcd.clear()
52+
lcd.message("I see fields\nof green");
53+
lcd.backlight(lcd.GREEN)
54+
55+
if (lcd.buttonPressed(lcd.RIGHT)):
56+
lcd.clear()
57+
lcd.message("Purple mountain\n majesties");
58+
lcd.backlight(lcd.VIOLET)
59+
60+
if (lcd.buttonPressed(lcd.SELECT)):
61+
lcd.clear()
62+
lcd.backlight(lcd.ON)
63+
64+

Adafruit_MCP230xx/Adafruit_MCP230xx.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,9 @@ class Adafruit_MCP230XX(object):
4040
OUTPUT = 0
4141
INPUT = 1
4242

43-
44-
def __init__(self, address, num_gpios):
43+
def __init__(self, address, num_gpios, busnum = 0):
4544
assert num_gpios >= 0 and num_gpios <= 16, "Number of GPIOs must be between 0 and 16"
46-
self.i2c = Adafruit_I2C(address)
45+
self.i2c = Adafruit_I2C(address=address, bus=smbus.SMBus(busnum))
4746
self.address = address
4847
self.num_gpios = num_gpios
4948

0 commit comments

Comments
 (0)