11
11
from Adafruit_MCP230xx import Adafruit_MCP230XX
12
12
import smbus
13
13
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 )
16
14
17
- class Adafruit_CharLCD :
15
+ class Adafruit_CharLCDPlate :
18
16
19
17
OUTPUT = 0
20
18
INPUT = 1
@@ -80,20 +78,23 @@ class Adafruit_CharLCD:
80
78
81
79
82
80
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 ):
84
83
self .pin_rs = pin_rs
85
84
self .pin_e = pin_e
86
85
self .pin_rw = pin_rw
87
86
self .pins_db = pins_db
88
87
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 )
94
95
95
96
for pin in self .pins_db :
96
- mcp .config (pin , self .OUTPUT )
97
+ self . mcp .config (pin , self .OUTPUT )
97
98
98
99
self .write4bits (0x33 ) # initialization
99
100
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):
111
112
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
112
113
113
114
# 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
120
121
121
122
# 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 )
132
133
133
134
def begin (self , cols , lines ):
134
135
if (lines > 1 ):
@@ -214,30 +215,30 @@ def write4bits(self, bits, char_mode=False):
214
215
""" Send command to LCD """
215
216
#self.delayMicroseconds(1000) # 1000 microsecond sleep
216
217
bits = bin (bits )[2 :].zfill (8 )
217
- mcp .output (self .pin_rs , char_mode )
218
+ self . mcp .output (self .pin_rs , char_mode )
218
219
219
220
for i in range (4 ):
220
221
if bits [i ] == "1" :
221
- mcp .output (self .pins_db [::- 1 ][i ], True )
222
+ self . mcp .output (self .pins_db [::- 1 ][i ], True )
222
223
else :
223
- mcp .output (self .pins_db [::- 1 ][i ], False )
224
+ self . mcp .output (self .pins_db [::- 1 ][i ], False )
224
225
self .pulseEnable ()
225
226
226
227
for i in range (4 ,8 ):
227
228
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 )
229
230
else :
230
- mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231
+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231
232
self .pulseEnable ()
232
233
233
234
def delayMicroseconds (self , microseconds ):
234
235
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
235
236
sleep (seconds )
236
237
237
238
def pulseEnable (self ):
238
- mcp .output (self .pin_e , True )
239
+ self . mcp .output (self .pin_e , True )
239
240
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 )
241
242
#self.delayMicroseconds(1) # commands need > 37us to settle
242
243
243
244
def message (self , text ):
@@ -249,39 +250,37 @@ def message(self, text):
249
250
self .write4bits (ord (char ),True )
250
251
251
252
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 )
255
256
257
+ def buttonPressed (self , buttonname ):
258
+ if (buttonname > self .LEFT ):
259
+ return false
256
260
257
- if __name__ == '__main__' :
261
+ return not self . mcp . input ( buttonname )
258
262
259
263
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__' :
266
265
267
- lcd = Adafruit_CharLCD ( 15 , 13 , [ 12 , 11 , 10 , 9 ], 14 )
266
+ lcd = Adafruit_CharLCDPlate ( busnum = 0 )
268
267
lcd .clear ()
269
268
lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
270
269
sleep (1 )
271
270
while 1 :
272
- if (not mcp . input (lcd .LEFT )):
271
+ if (lcd . buttonPressed (lcd .LEFT )):
273
272
lcd .backlight (lcd .RED )
274
273
275
- if (not mcp . input (lcd .UP )):
274
+ if (lcd . buttonPressed (lcd .UP )):
276
275
lcd .backlight (lcd .BLUE )
277
276
278
- if (not mcp . input (lcd .DOWN )):
277
+ if (lcd . buttonPressed (lcd .DOWN )):
279
278
lcd .backlight (lcd .GREEN )
280
279
281
- if (not mcp . input (lcd .RIGHT )):
280
+ if (lcd . buttonPressed (lcd .RIGHT )):
282
281
lcd .backlight (lcd .VIOLET )
283
282
284
- if (not mcp . input (lcd .SELECT )):
283
+ if (lcd . buttonPressed (lcd .SELECT )):
285
284
lcd .backlight (lcd .ON )
286
285
287
286
0 commit comments