@@ -35,46 +35,46 @@ class Adafruit_CharLCDPlate:
35
35
LEFT = 4
36
36
37
37
# commands
38
- LCD_CLEARDISPLAY = 0x01
39
- LCD_RETURNHOME = 0x02
40
- LCD_ENTRYMODESET = 0x04
41
- LCD_DISPLAYCONTROL = 0x08
42
- LCD_CURSORSHIFT = 0x10
43
- LCD_FUNCTIONSET = 0x20
44
- LCD_SETCGRAMADDR = 0x40
45
- LCD_SETDDRAMADDR = 0x80
38
+ LCD_CLEARDISPLAY = 0x01
39
+ LCD_RETURNHOME = 0x02
40
+ LCD_ENTRYMODESET = 0x04
41
+ LCD_DISPLAYCONTROL = 0x08
42
+ LCD_CURSORSHIFT = 0x10
43
+ LCD_FUNCTIONSET = 0x20
44
+ LCD_SETCGRAMADDR = 0x40
45
+ LCD_SETDDRAMADDR = 0x80
46
46
47
47
# flags for display entry mode
48
- LCD_ENTRYRIGHT = 0x00
49
- LCD_ENTRYLEFT = 0x02
50
- LCD_ENTRYSHIFTINCREMENT = 0x01
51
- LCD_ENTRYSHIFTDECREMENT = 0x00
48
+ LCD_ENTRYRIGHT = 0x00
49
+ LCD_ENTRYLEFT = 0x02
50
+ LCD_ENTRYSHIFTINCREMENT = 0x01
51
+ LCD_ENTRYSHIFTDECREMENT = 0x00
52
52
53
53
# flags for display on/off control
54
- LCD_DISPLAYON = 0x04
55
- LCD_DISPLAYOFF = 0x00
56
- LCD_CURSORON = 0x02
57
- LCD_CURSOROFF = 0x00
58
- LCD_BLINKON = 0x01
59
- LCD_BLINKOFF = 0x00
54
+ LCD_DISPLAYON = 0x04
55
+ LCD_DISPLAYOFF = 0x00
56
+ LCD_CURSORON = 0x02
57
+ LCD_CURSOROFF = 0x00
58
+ LCD_BLINKON = 0x01
59
+ LCD_BLINKOFF = 0x00
60
60
61
61
# flags for display/cursor shift
62
- LCD_DISPLAYMOVE = 0x08
63
- LCD_CURSORMOVE = 0x00
62
+ LCD_DISPLAYMOVE = 0x08
63
+ LCD_CURSORMOVE = 0x00
64
64
65
65
# flags for display/cursor shift
66
- LCD_DISPLAYMOVE = 0x08
67
- LCD_CURSORMOVE = 0x00
68
- LCD_MOVERIGHT = 0x04
69
- LCD_MOVELEFT = 0x00
66
+ LCD_DISPLAYMOVE = 0x08
67
+ LCD_CURSORMOVE = 0x00
68
+ LCD_MOVERIGHT = 0x04
69
+ LCD_MOVELEFT = 0x00
70
70
71
71
# flags for function set
72
- LCD_8BITMODE = 0x10
73
- LCD_4BITMODE = 0x00
74
- LCD_2LINE = 0x08
75
- LCD_1LINE = 0x00
76
- LCD_5x10DOTS = 0x04
77
- LCD_5x8DOTS = 0x00
72
+ LCD_8BITMODE = 0x10
73
+ LCD_4BITMODE = 0x00
74
+ LCD_2LINE = 0x08
75
+ LCD_1LINE = 0x00
76
+ LCD_5x10DOTS = 0x04
77
+ LCD_5x8DOTS = 0x00
78
78
79
79
80
80
@@ -85,47 +85,51 @@ def __init__(self, busnum=0, pin_rs=15, pin_e=13, pins_db=[12, 11, 10, 9], pin_r
85
85
self .pin_rw = pin_rw
86
86
self .pins_db = pins_db
87
87
88
- self .mcp = Adafruit_MCP230XX (busnum = busnum , address = 0x20 , num_gpios = 16 )
88
+ self .mcp = Adafruit_MCP230XX (busnum = busnum , address = 0x20 , num_gpios = 16 )
89
+
89
90
self .mcp .config (self .pin_e , self .OUTPUT )
90
91
self .mcp .config (self .pin_rs , self .OUTPUT )
91
92
self .mcp .config (self .pin_rw , self .OUTPUT )
92
93
self .mcp .output (self .pin_rw , 0 )
93
- self .mcp .output (self .pin_e , 0 )
94
+ self .mcp .output (self .pin_e , 0 )
95
+
94
96
for pin in self .pins_db :
95
97
self .mcp .config (pin , self .OUTPUT )
96
- self .write4bits (0x33 ) # initialization
97
- self .write4bits (0x32 ) # initialization
98
- self .write4bits (0x28 ) # 2 line 5x7 matrix
99
- self .write4bits (0x0C ) # turn cursor off 0x0E to enable cursor
100
- self .write4bits (0x06 ) # shift cursor right
101
-
102
- self .displaycontrol = self .LCD_DISPLAYON | self .LCD_CURSOROFF | self .LCD_BLINKOFF
103
- self .displayfunction = self .LCD_4BITMODE | self .LCD_1LINE | self .LCD_5x8DOTS
104
- self .displayfunction |= self .LCD_2LINE
105
-
106
- """ Initialize to default text direction (for romance languages) """
107
- self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
108
- self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
109
-
110
- # turn on backlights!
111
- self .mcp .config (6 , self .mcp .OUTPUT )
112
- self .mcp .config (7 , self .mcp .OUTPUT )
113
- self .mcp .config (8 , self .mcp .OUTPUT )
114
- self .mcp .output (6 , 0 ) # red
115
- self .mcp .output (7 , 0 ) # green
116
- self .mcp .output (8 , 0 ) # blue
117
-
118
- # turn on pullups
98
+
99
+ self .write4bits (0x33 ) # initialization
100
+ self .write4bits (0x32 ) # initialization
101
+ self .write4bits (0x28 ) # 2 line 5x7 matrix
102
+ self .write4bits (0x0C ) # turn cursor off 0x0E to enable cursor
103
+ self .write4bits (0x06 ) # shift cursor right
104
+
105
+ self .displaycontrol = self .LCD_DISPLAYON | self .LCD_CURSOROFF | self .LCD_BLINKOFF
106
+
107
+ self .displayfunction = self .LCD_4BITMODE | self .LCD_1LINE | self .LCD_5x8DOTS
108
+ self .displayfunction |= self .LCD_2LINE
109
+
110
+ """ Initialize to default text direction (for romance languages) """
111
+ self .displaymode = self .LCD_ENTRYLEFT | self .LCD_ENTRYSHIFTDECREMENT
112
+ self .write4bits (self .LCD_ENTRYMODESET | self .displaymode ) # set the entry mode
113
+
114
+ # turn on backlights!
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
121
+
122
+ # turn on pullups
119
123
self .mcp .pullup (self .SELECT , True )
120
124
self .mcp .pullup (self .LEFT , True )
121
125
self .mcp .pullup (self .RIGHT , True )
122
126
self .mcp .pullup (self .UP , True )
123
127
self .mcp .pullup (self .DOWN , True )
124
- self .mcp .config (self .SELECT , self .mcp .INPUT )
125
- self .mcp .config (self .LEFT , self .mcp .INPUT )
126
- self .mcp .config (self .RIGHT , self .mcp .INPUT )
127
- self .mcp .config (self .DOWN , self .mcp .INPUT )
128
- self .mcp .config (self .UP , self .mcp .INPUT )
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 )
129
133
130
134
def begin (self , cols , lines ):
131
135
if (lines > 1 ):
@@ -140,12 +144,12 @@ def home(self):
140
144
141
145
def clear (self ):
142
146
self .write4bits (self .LCD_CLEARDISPLAY ) # command to clear display
143
- self .delayMicroseconds (2000 ) # 2000 microsecond sleep, clearing the display takes a long time
147
+ self .delayMicroseconds (2000 ) # 2000 microsecond sleep, clearing the display takes a long time
144
148
145
149
def setCursor (self , col , row ):
146
150
self .row_offsets = [ 0x00 , 0x40 , 0x14 , 0x54 ]
147
151
if ( row > self .numlines ):
148
- row = self .numlines - 1 # we count rows starting w/0
152
+ row = self .numlines - 1 # we count rows starting w/0
149
153
self .write4bits (self .LCD_SETDDRAMADDR | (col + self .row_offsets [row ]))
150
154
151
155
def noDisplay (self ):
@@ -159,51 +163,51 @@ def display(self):
159
163
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
160
164
161
165
def noCursor (self ):
162
- """ Turns the underline cursor off """
166
+ """ Turns the underline cursor on/ off """
163
167
self .displaycontrol &= ~ self .LCD_CURSORON
164
168
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
165
169
166
170
167
171
def cursor (self ):
168
- """ Turns the underline cursor On """
172
+ """ Cursor On """
169
173
self .displaycontrol |= self .LCD_CURSORON
170
174
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
171
175
172
176
def noBlink (self ):
173
- """ Turn on and off the blinking cursor """
177
+ """ Turn on and off the blinking cursor """
174
178
self .displaycontrol &= ~ self .LCD_BLINKON
175
179
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
176
180
177
- def blink (self ):
178
- """ Turn on and off the blinking cursor """
179
- self .displaycontrol |= self .LCD_BLINKON
181
+ def noBlink (self ):
182
+ """ Turn on and off the blinking cursor """
183
+ self .displaycontrol &= ~ self .LCD_BLINKON
180
184
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
181
185
182
186
def DisplayLeft (self ):
183
187
""" These commands scroll the display without changing the RAM """
184
188
self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVELEFT )
185
189
186
190
def scrollDisplayRight (self ):
187
- """ These commands scroll the display without changing the RAM """
191
+ """ These commands scroll the display without changing the RAM """
188
192
self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVERIGHT );
189
193
190
194
def leftToRight (self ):
191
- """ This is for text that flows Left to Right """
195
+ """ This is for text that flows Left to Right """
192
196
self .displaymode |= self .LCD_ENTRYLEFT
193
197
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode );
194
198
195
199
def rightToLeft (self ):
196
- """ This is for text that flows Right to Left """
200
+ """ This is for text that flows Right to Left """
197
201
self .displaymode &= ~ self .LCD_ENTRYLEFT
198
202
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
199
203
200
204
def autoscroll (self ):
201
- """ This will 'right justify' text from the cursor """
205
+ """ This will 'right justify' text from the cursor """
202
206
self .displaymode |= self .LCD_ENTRYSHIFTINCREMENT
203
207
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
204
208
205
209
def noAutoscroll (self ):
206
- """ This will 'left justify' text from the cursor """
210
+ """ This will 'left justify' text from the cursor """
207
211
self .displaymode &= ~ self .LCD_ENTRYSHIFTINCREMENT
208
212
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
209
213
@@ -212,12 +216,14 @@ def write4bits(self, bits, char_mode=False):
212
216
#self.delayMicroseconds(1000) # 1000 microsecond sleep
213
217
bits = bin (bits )[2 :].zfill (8 )
214
218
self .mcp .output (self .pin_rs , char_mode )
219
+
215
220
for i in range (4 ):
216
221
if bits [i ] == "1" :
217
222
self .mcp .output (self .pins_db [::- 1 ][i ], True )
218
223
else :
219
224
self .mcp .output (self .pins_db [::- 1 ][i ], False )
220
225
self .pulseEnable ()
226
+
221
227
for i in range (4 ,8 ):
222
228
if bits [i ] == "1" :
223
229
self .mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
@@ -226,14 +232,14 @@ def write4bits(self, bits, char_mode=False):
226
232
self .pulseEnable ()
227
233
228
234
def delayMicroseconds (self , microseconds ):
229
- seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
235
+ seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
230
236
sleep (seconds )
231
237
232
238
def pulseEnable (self ):
233
239
self .mcp .output (self .pin_e , True )
234
- self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
240
+ self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
235
241
self .mcp .output (self .pin_e , False )
236
- #self.delayMicroseconds(1) # commands need > 37us to settle
242
+ #self.delayMicroseconds(1) # commands need > 37us to settle
237
243
238
244
def message (self , text ):
239
245
""" Send string to LCD. Newline wraps to second line"""
@@ -244,14 +250,15 @@ def message(self, text):
244
250
self .write4bits (ord (char ),True )
245
251
246
252
def backlight (self , color ):
247
- self .mcp .output (6 , not color & 0x01 )
248
- self .mcp .output (7 , not color & 0x02 )
249
- self .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 )
250
256
251
257
def buttonPressed (self , buttonname ):
252
- if (buttonname > self .LEFT ):
253
- return false
254
- return not self .mcp .input (buttonname )
258
+ if (buttonname > self .LEFT ):
259
+ return false
260
+
261
+ return not self .mcp .input (buttonname )
255
262
256
263
257
264
if __name__ == '__main__' :
@@ -261,33 +268,37 @@ def buttonPressed(self, buttonname):
261
268
lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
262
269
sleep (1 )
263
270
while 1 :
264
- if (lcd .buttonPressed (lcd .LEFT )):
265
- lcd .backlight (lcd .RED )
266
- if (lcd .buttonPressed (lcd .UP )):
267
- lcd .backlight (lcd .BLUE )
268
- if (lcd .buttonPressed (lcd .DOWN )):
269
- lcd .backlight (lcd .GREEN )
270
- if (lcd .buttonPressed (lcd .RIGHT )):
271
- lcd .backlight (lcd .VIOLET )
272
- if (lcd .buttonPressed (lcd .SELECT )):
273
- lcd .backlight (lcd .ON )
271
+ if (lcd .buttonPressed (lcd .LEFT )):
272
+ lcd .backlight (lcd .RED )
273
+
274
+ if (lcd .buttonPressed (lcd .UP )):
275
+ lcd .backlight (lcd .BLUE )
276
+
277
+ if (lcd .buttonPressed (lcd .DOWN )):
278
+ lcd .backlight (lcd .GREEN )
279
+
280
+ if (lcd .buttonPressed (lcd .RIGHT )):
281
+ lcd .backlight (lcd .VIOLET )
282
+
283
+ if (lcd .buttonPressed (lcd .SELECT )):
284
+ lcd .backlight (lcd .ON )
274
285
275
286
276
287
while 1 :
277
- lcd .backlight (lcd .RED )
278
- sleep (1 )
279
- lcd .backlight (lcd .YELLOW )
280
- sleep (1 )
281
- lcd .backlight (lcd .GREEN )
282
- sleep (1 )
283
- lcd .backlight (lcd .TEAL )
284
- sleep (1 )
285
- lcd .backlight (lcd .BLUE )
286
- sleep (1 )
287
- lcd .backlight (lcd .VIOLET )
288
- sleep (1 )
289
- lcd .backlight (lcd .ON )
290
- sleep (1 )
291
- lcd .backlight (lcd .OFF )
292
- sleep (1 )
288
+ lcd .backlight (lcd .RED )
289
+ sleep (1 )
290
+ lcd .backlight (lcd .YELLOW )
291
+ sleep (1 )
292
+ lcd .backlight (lcd .GREEN )
293
+ sleep (1 )
294
+ lcd .backlight (lcd .TEAL )
295
+ sleep (1 )
296
+ lcd .backlight (lcd .BLUE )
297
+ sleep (1 )
298
+ lcd .backlight (lcd .VIOLET )
299
+ sleep (1 )
300
+ lcd .backlight (lcd .ON )
301
+ sleep (1 )
302
+ lcd .backlight (lcd .OFF )
303
+ sleep (1 )
293
304
0 commit comments