11
11
from Adafruit_MCP230xx import Adafruit_MCP230XX
12
12
import smbus
13
13
14
- OUTPUT = 0
15
- INPUT = 1
14
+ # change busnum = 0 to bbusnum = 1 if you have a rev 2 Pi!
16
15
mcp = Adafruit_MCP230XX (busnum = 0 , address = 0x20 , num_gpios = 16 )
17
16
18
-
19
17
class Adafruit_CharLCD :
20
18
19
+ OUTPUT = 0
20
+ INPUT = 1
21
+
22
+ # LED colors
23
+ RED = 0x01
24
+ GREEN = 0x02
25
+ BLUE = 0x04
26
+ YELLOW = 0x03
27
+ TEAL = 0x06
28
+ VIOLET = 0x05
29
+ ON = 0x07
30
+ OFF = 0x0
31
+
21
32
# commands
22
33
LCD_CLEARDISPLAY = 0x01
23
34
LCD_RETURNHOME = 0x02
@@ -68,14 +79,14 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
68
79
self .pin_rw = pin_rw
69
80
self .pins_db = pins_db
70
81
71
- mcp .config (self .pin_e , OUTPUT )
72
- mcp .config (self .pin_rs , OUTPUT )
73
- mcp .config (self .pin_rw , OUTPUT )
82
+ mcp .config (self .pin_e , self . OUTPUT )
83
+ mcp .config (self .pin_rs , self . OUTPUT )
84
+ mcp .config (self .pin_rw , self . OUTPUT )
74
85
mcp .output (self .pin_rw , 0 )
75
86
mcp .output (self .pin_e , 0 )
76
87
77
88
for pin in self .pins_db :
78
- mcp .config (pin , OUTPUT )
89
+ mcp .config (pin , self . OUTPUT )
79
90
80
91
self .write4bits (0x33 ) # initialization
81
92
self .write4bits (0x32 ) # initialization
@@ -92,6 +103,14 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], pin_rw=0):
92
103
self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
93
104
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
94
105
106
+ # turn on backlights!
107
+ mcp .config (6 , mcp .OUTPUT )
108
+ mcp .config (7 , mcp .OUTPUT )
109
+ mcp .config (8 , mcp .OUTPUT )
110
+ mcp .output (6 , 0 ) # red
111
+ mcp .output (7 , 0 ) # green
112
+ mcp .output (8 , 0 ) # blue
113
+
95
114
96
115
def begin (self , cols , lines ):
97
116
if (lines > 1 ):
@@ -211,6 +230,10 @@ def message(self, text):
211
230
else :
212
231
self .write4bits (ord (char ),True )
213
232
233
+ def backlight (self , color ):
234
+ mcp .output (6 , not color & 0x01 )
235
+ mcp .output (7 , not color & 0x02 )
236
+ mcp .output (8 , not color & 0x04 )
214
237
215
238
216
239
if __name__ == '__main__' :
@@ -222,29 +245,25 @@ def message(self, text):
222
245
# while (True):
223
246
# for i in range(16):
224
247
# print "%d: %x" % (i, mcp.input(i) >> i)
225
- mcp .config (6 , OUTPUT )
226
- mcp .config (7 , OUTPUT )
227
- mcp .config (8 , OUTPUT )
228
- mcp .output (6 , 0 ) # red
229
- mcp .output (7 , 0 ) # green
230
- mcp .output (8 , 0 ) # blue
231
248
232
249
lcd = Adafruit_CharLCD (15 , 13 , [12 ,11 ,10 ,9 ], 14 )
233
250
lcd .clear ()
234
251
lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
252
+ sleep (1 )
235
253
while 1 :
236
- mcp .output (6 , 0 ) # red
237
- mcp .output (7 , 1 ) # green
238
- mcp .output (8 , 1 ) # blue
239
- sleep (1 )
240
- mcp .output (7 , 0 ) # green
241
- sleep (1 )
242
- mcp .output (6 , 1 ) # red
243
- sleep (1 )
244
- mcp .output (8 , 0 ) # blue
245
- sleep (1 )
246
- mcp .output (7 , 1 ) # green
247
- sleep (1 )
248
- mcp .output (6 , 0 ) # red
249
- sleep (1 )
250
-
254
+ lcd .backlight (lcd .RED )
255
+ sleep (1 )
256
+ lcd .backlight (lcd .YELLOW )
257
+ sleep (1 )
258
+ lcd .backlight (lcd .GREEN )
259
+ sleep (1 )
260
+ lcd .backlight (lcd .TEAL )
261
+ sleep (1 )
262
+ lcd .backlight (lcd .BLUE )
263
+ sleep (1 )
264
+ lcd .backlight (lcd .VIOLET )
265
+ sleep (1 )
266
+ lcd .backlight (lcd .ON )
267
+ sleep (1 )
268
+ lcd .backlight (lcd .OFF )
269
+ sleep (1 )
0 commit comments