11
11
from Adafruit_MCP230xx import Adafruit_MCP230XX
12
12
import smbus
13
13
14
- mcp = Adafruit_MCP230XX (address = 0x20 , num_gpios = 16 )
15
14
16
- class Adafruit_CharLCD :
15
+ class Adafruit_CharLCDPlate :
17
16
18
17
OUTPUT = 0
19
18
INPUT = 1
@@ -79,20 +78,23 @@ class Adafruit_CharLCD:
79
78
80
79
81
80
82
- 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 ):
83
83
self .pin_rs = pin_rs
84
84
self .pin_e = pin_e
85
85
self .pin_rw = pin_rw
86
86
self .pins_db = pins_db
87
87
88
- mcp .config (self .pin_e , self .OUTPUT )
89
- mcp .config (self .pin_rs , self .OUTPUT )
90
- mcp .config (self .pin_rw , self .OUTPUT )
91
- mcp .output (self .pin_rw , 0 )
92
- 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 )
93
95
94
96
for pin in self .pins_db :
95
- mcp .config (pin , self .OUTPUT )
97
+ self . mcp .config (pin , self .OUTPUT )
96
98
97
99
self .write4bits (0x33 ) # initialization
98
100
self .write4bits (0x32 ) # initialization
@@ -110,24 +112,24 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
110
112
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
111
113
112
114
# turn on backlights!
113
- mcp .config (6 , mcp .OUTPUT )
114
- mcp .config (7 , mcp .OUTPUT )
115
- mcp .config (8 , mcp .OUTPUT )
116
- mcp .output (6 , 0 ) # red
117
- mcp .output (7 , 0 ) # green
118
- 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
119
121
120
122
# turn on pullups
121
- mcp .pullup (self .SELECT , True )
122
- mcp .pullup (self .LEFT , True )
123
- mcp .pullup (self .RIGHT , True )
124
- mcp .pullup (self .UP , True )
125
- mcp .pullup (self .DOWN , True )
126
- mcp .config (self .SELECT , mcp .INPUT )
127
- mcp .config (self .LEFT , mcp .INPUT )
128
- mcp .config (self .RIGHT , mcp .INPUT )
129
- mcp .config (self .DOWN , mcp .INPUT )
130
- 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 )
131
133
132
134
def begin (self , cols , lines ):
133
135
if (lines > 1 ):
@@ -213,30 +215,30 @@ def write4bits(self, bits, char_mode=False):
213
215
""" Send command to LCD """
214
216
#self.delayMicroseconds(1000) # 1000 microsecond sleep
215
217
bits = bin (bits )[2 :].zfill (8 )
216
- mcp .output (self .pin_rs , char_mode )
218
+ self . mcp .output (self .pin_rs , char_mode )
217
219
218
220
for i in range (4 ):
219
221
if bits [i ] == "1" :
220
- mcp .output (self .pins_db [::- 1 ][i ], True )
222
+ self . mcp .output (self .pins_db [::- 1 ][i ], True )
221
223
else :
222
- mcp .output (self .pins_db [::- 1 ][i ], False )
224
+ self . mcp .output (self .pins_db [::- 1 ][i ], False )
223
225
self .pulseEnable ()
224
226
225
227
for i in range (4 ,8 ):
226
228
if bits [i ] == "1" :
227
- mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
229
+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
228
230
else :
229
- mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
231
+ self . mcp .output (self .pins_db [::- 1 ][i - 4 ], False )
230
232
self .pulseEnable ()
231
233
232
234
def delayMicroseconds (self , microseconds ):
233
235
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
234
236
sleep (seconds )
235
237
236
238
def pulseEnable (self ):
237
- mcp .output (self .pin_e , True )
239
+ self . mcp .output (self .pin_e , True )
238
240
self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
239
- mcp .output (self .pin_e , False )
241
+ self . mcp .output (self .pin_e , False )
240
242
#self.delayMicroseconds(1) # commands need > 37us to settle
241
243
242
244
def message (self , text ):
@@ -248,28 +250,20 @@ def message(self, text):
248
250
self .write4bits (ord (char ),True )
249
251
250
252
def backlight (self , color ):
251
- mcp .output (6 , not color & 0x01 )
252
- mcp .output (7 , not color & 0x02 )
253
- 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 )
254
256
255
257
def buttonPressed (self , buttonname ):
256
258
if (buttonname > self .LEFT ):
257
259
return false
258
260
259
- return not mcp .input (buttonname )
261
+ return not self . mcp .input (buttonname )
260
262
261
263
262
264
if __name__ == '__main__' :
263
265
264
-
265
- # input test
266
- # for i in range(16):
267
- # mcp.pullup(i, 1)
268
- # while (True):
269
- # for i in range(16):
270
- # print "%d: %x" % (i, mcp.input(i) >> i)
271
-
272
- lcd = Adafruit_CharLCD (15 , 13 , [12 ,11 ,10 ,9 ], 14 )
266
+ lcd = Adafruit_CharLCDPlate (busnum = 0 )
273
267
lcd .clear ()
274
268
lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
275
269
sleep (1 )
0 commit comments