@@ -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,51 +85,47 @@ 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 )
89
-
88
+ self .mcp = Adafruit_MCP230XX (busnum = busnum , address = 0x20 , num_gpios = 16 )
90
89
self .mcp .config (self .pin_e , self .OUTPUT )
91
90
self .mcp .config (self .pin_rs , self .OUTPUT )
92
91
self .mcp .config (self .pin_rw , self .OUTPUT )
93
92
self .mcp .output (self .pin_rw , 0 )
94
- self .mcp .output (self .pin_e , 0 )
95
-
93
+ self .mcp .output (self .pin_e , 0 )
96
94
for pin in self .pins_db :
97
95
self .mcp .config (pin , self .OUTPUT )
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
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
123
119
self .mcp .pullup (self .SELECT , True )
124
120
self .mcp .pullup (self .LEFT , True )
125
121
self .mcp .pullup (self .RIGHT , True )
126
122
self .mcp .pullup (self .UP , True )
127
123
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 )
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 )
133
129
134
130
def begin (self , cols , lines ):
135
131
if (lines > 1 ):
@@ -144,12 +140,12 @@ def home(self):
144
140
145
141
def clear (self ):
146
142
self .write4bits (self .LCD_CLEARDISPLAY ) # command to clear display
147
- self .delayMicroseconds (2000 ) # 2000 microsecond sleep, clearing the display takes a long time
143
+ self .delayMicroseconds (2000 ) # 2000 microsecond sleep, clearing the display takes a long time
148
144
149
145
def setCursor (self , col , row ):
150
146
self .row_offsets = [ 0x00 , 0x40 , 0x14 , 0x54 ]
151
147
if ( row > self .numlines ):
152
- row = self .numlines - 1 # we count rows starting w/0
148
+ row = self .numlines - 1 # we count rows starting w/0
153
149
self .write4bits (self .LCD_SETDDRAMADDR | (col + self .row_offsets [row ]))
154
150
155
151
def noDisplay (self ):
@@ -163,51 +159,51 @@ def display(self):
163
159
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
164
160
165
161
def noCursor (self ):
166
- """ Turns the underline cursor on/ off """
162
+ """ Turns the underline cursor off """
167
163
self .displaycontrol &= ~ self .LCD_CURSORON
168
164
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
169
165
170
166
171
167
def cursor (self ):
172
- """ Cursor On """
168
+ """ Turns the underline cursor On """
173
169
self .displaycontrol |= self .LCD_CURSORON
174
170
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
175
171
176
172
def noBlink (self ):
177
- """ Turn on and off the blinking cursor """
173
+ """ Turn on and off the blinking cursor """
178
174
self .displaycontrol &= ~ self .LCD_BLINKON
179
175
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
180
176
181
- def noBlink (self ):
182
- """ Turn on and off the blinking cursor """
183
- self .displaycontrol &= ~ self .LCD_BLINKON
177
+ def blink (self ):
178
+ """ Turn on and off the blinking cursor """
179
+ self .displaycontrol |= self .LCD_BLINKON
184
180
self .write4bits (self .LCD_DISPLAYCONTROL | self .displaycontrol )
185
181
186
182
def DisplayLeft (self ):
187
183
""" These commands scroll the display without changing the RAM """
188
184
self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVELEFT )
189
185
190
186
def scrollDisplayRight (self ):
191
- """ These commands scroll the display without changing the RAM """
187
+ """ These commands scroll the display without changing the RAM """
192
188
self .write4bits (self .LCD_CURSORSHIFT | self .LCD_DISPLAYMOVE | self .LCD_MOVERIGHT );
193
189
194
190
def leftToRight (self ):
195
- """ This is for text that flows Left to Right """
191
+ """ This is for text that flows Left to Right """
196
192
self .displaymode |= self .LCD_ENTRYLEFT
197
193
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode );
198
194
199
195
def rightToLeft (self ):
200
- """ This is for text that flows Right to Left """
196
+ """ This is for text that flows Right to Left """
201
197
self .displaymode &= ~ self .LCD_ENTRYLEFT
202
198
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
203
199
204
200
def autoscroll (self ):
205
- """ This will 'right justify' text from the cursor """
201
+ """ This will 'right justify' text from the cursor """
206
202
self .displaymode |= self .LCD_ENTRYSHIFTINCREMENT
207
203
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
208
204
209
205
def noAutoscroll (self ):
210
- """ This will 'left justify' text from the cursor """
206
+ """ This will 'left justify' text from the cursor """
211
207
self .displaymode &= ~ self .LCD_ENTRYSHIFTINCREMENT
212
208
self .write4bits (self .LCD_ENTRYMODESET | self .displaymode )
213
209
@@ -216,14 +212,12 @@ def write4bits(self, bits, char_mode=False):
216
212
#self.delayMicroseconds(1000) # 1000 microsecond sleep
217
213
bits = bin (bits )[2 :].zfill (8 )
218
214
self .mcp .output (self .pin_rs , char_mode )
219
-
220
215
for i in range (4 ):
221
216
if bits [i ] == "1" :
222
217
self .mcp .output (self .pins_db [::- 1 ][i ], True )
223
218
else :
224
219
self .mcp .output (self .pins_db [::- 1 ][i ], False )
225
220
self .pulseEnable ()
226
-
227
221
for i in range (4 ,8 ):
228
222
if bits [i ] == "1" :
229
223
self .mcp .output (self .pins_db [::- 1 ][i - 4 ], True )
@@ -232,14 +226,14 @@ def write4bits(self, bits, char_mode=False):
232
226
self .pulseEnable ()
233
227
234
228
def delayMicroseconds (self , microseconds ):
235
- seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
229
+ seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
236
230
sleep (seconds )
237
231
238
232
def pulseEnable (self ):
239
233
self .mcp .output (self .pin_e , True )
240
- self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
234
+ self .delayMicroseconds (1 ) # 1 microsecond pause - enable pulse must be > 450ns
241
235
self .mcp .output (self .pin_e , False )
242
- #self.delayMicroseconds(1) # commands need > 37us to settle
236
+ #self.delayMicroseconds(1) # commands need > 37us to settle
243
237
244
238
def message (self , text ):
245
239
""" Send string to LCD. Newline wraps to second line"""
@@ -250,15 +244,14 @@ def message(self, text):
250
244
self .write4bits (ord (char ),True )
251
245
252
246
def backlight (self , color ):
253
- self .mcp .output (6 , not color & 0x01 )
254
- self .mcp .output (7 , not color & 0x02 )
255
- self .mcp .output (8 , not color & 0x04 )
247
+ self .mcp .output (6 , not color & 0x01 )
248
+ self .mcp .output (7 , not color & 0x02 )
249
+ self .mcp .output (8 , not color & 0x04 )
256
250
257
251
def buttonPressed (self , buttonname ):
258
- if (buttonname > self .LEFT ):
259
- return false
260
-
261
- return not self .mcp .input (buttonname )
252
+ if (buttonname > self .LEFT ):
253
+ return false
254
+ return not self .mcp .input (buttonname )
262
255
263
256
264
257
if __name__ == '__main__' :
@@ -268,36 +261,32 @@ def buttonPressed(self, buttonname):
268
261
lcd .message ("Adafruit RGB LCD\n Plate w/Keypad!" )
269
262
sleep (1 )
270
263
while 1 :
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 )
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 )
285
274
286
275
287
276
while 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 )
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 )
0 commit comments