Skip to content

Commit 466f773

Browse files
committed
Original Code
1 parent 466ef2f commit 466f773

File tree

1 file changed

+119
-108
lines changed

1 file changed

+119
-108
lines changed

Adafruit_CharLCDPlate/ACLP4spaceindent.py

Lines changed: 119 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -35,46 +35,46 @@ class Adafruit_CharLCDPlate:
3535
LEFT = 4
3636

3737
# 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
4646

4747
# 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
5252

5353
# 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
6060

6161
# flags for display/cursor shift
62-
LCD_DISPLAYMOVE = 0x08
63-
LCD_CURSORMOVE = 0x00
62+
LCD_DISPLAYMOVE = 0x08
63+
LCD_CURSORMOVE = 0x00
6464

6565
# 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
7070

7171
# 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
7878

7979

8080

@@ -85,47 +85,51 @@ def __init__(self, busnum=0, pin_rs=15, pin_e=13, pins_db=[12, 11, 10, 9], pin_r
8585
self.pin_rw = pin_rw
8686
self.pins_db = pins_db
8787

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+
8990
self.mcp.config(self.pin_e, self.OUTPUT)
9091
self.mcp.config(self.pin_rs, self.OUTPUT)
9192
self.mcp.config(self.pin_rw, self.OUTPUT)
9293
self.mcp.output(self.pin_rw, 0)
93-
self.mcp.output(self.pin_e, 0)
94+
self.mcp.output(self.pin_e, 0)
95+
9496
for pin in self.pins_db:
9597
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
119123
self.mcp.pullup(self.SELECT, True)
120124
self.mcp.pullup(self.LEFT, True)
121125
self.mcp.pullup(self.RIGHT, True)
122126
self.mcp.pullup(self.UP, True)
123127
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)
129133

130134
def begin(self, cols, lines):
131135
if (lines > 1):
@@ -140,12 +144,12 @@ def home(self):
140144

141145
def clear(self):
142146
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
144148

145149
def setCursor(self, col, row):
146150
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
147151
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
149153
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
150154

151155
def noDisplay(self):
@@ -159,51 +163,51 @@ def display(self):
159163
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
160164

161165
def noCursor(self):
162-
""" Turns the underline cursor off """
166+
""" Turns the underline cursor on/off """
163167
self.displaycontrol &= ~self.LCD_CURSORON
164168
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
165169

166170

167171
def cursor(self):
168-
""" Turns the underline cursor On """
172+
""" Cursor On """
169173
self.displaycontrol |= self.LCD_CURSORON
170174
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
171175

172176
def noBlink(self):
173-
""" Turn on and off the blinking cursor """
177+
""" Turn on and off the blinking cursor """
174178
self.displaycontrol &= ~self.LCD_BLINKON
175179
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
176180

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
180184
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
181185

182186
def DisplayLeft(self):
183187
""" These commands scroll the display without changing the RAM """
184188
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
185189

186190
def scrollDisplayRight(self):
187-
""" These commands scroll the display without changing the RAM """
191+
""" These commands scroll the display without changing the RAM """
188192
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
189193

190194
def leftToRight(self):
191-
""" This is for text that flows Left to Right """
195+
""" This is for text that flows Left to Right """
192196
self.displaymode |= self.LCD_ENTRYLEFT
193197
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
194198

195199
def rightToLeft(self):
196-
""" This is for text that flows Right to Left """
200+
""" This is for text that flows Right to Left """
197201
self.displaymode &= ~self.LCD_ENTRYLEFT
198202
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
199203

200204
def autoscroll(self):
201-
""" This will 'right justify' text from the cursor """
205+
""" This will 'right justify' text from the cursor """
202206
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
203207
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
204208

205209
def noAutoscroll(self):
206-
""" This will 'left justify' text from the cursor """
210+
""" This will 'left justify' text from the cursor """
207211
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
208212
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
209213

@@ -212,12 +216,14 @@ def write4bits(self, bits, char_mode=False):
212216
#self.delayMicroseconds(1000) # 1000 microsecond sleep
213217
bits=bin(bits)[2:].zfill(8)
214218
self.mcp.output(self.pin_rs, char_mode)
219+
215220
for i in range(4):
216221
if bits[i] == "1":
217222
self.mcp.output(self.pins_db[::-1][i], True)
218223
else:
219224
self.mcp.output(self.pins_db[::-1][i], False)
220225
self.pulseEnable()
226+
221227
for i in range(4,8):
222228
if bits[i] == "1":
223229
self.mcp.output(self.pins_db[::-1][i-4], True)
@@ -226,14 +232,14 @@ def write4bits(self, bits, char_mode=False):
226232
self.pulseEnable()
227233

228234
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
230236
sleep(seconds)
231237

232238
def pulseEnable(self):
233239
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
235241
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
237243

238244
def message(self, text):
239245
""" Send string to LCD. Newline wraps to second line"""
@@ -244,14 +250,15 @@ def message(self, text):
244250
self.write4bits(ord(char),True)
245251

246252
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)
250256

251257
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)
255262

256263

257264
if __name__ == '__main__':
@@ -261,33 +268,37 @@ def buttonPressed(self, buttonname):
261268
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
262269
sleep(1)
263270
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)
274285

275286

276287
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)
293304

0 commit comments

Comments
 (0)