Skip to content

Commit 9d03735

Browse files
committed
Addition Routines and corrections added conforming to pep8 standard
1 parent d957bcb commit 9d03735

File tree

1 file changed

+155
-59
lines changed

1 file changed

+155
-59
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

Lines changed: 155 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#!/usr/bin/pythonhttp://raspberrypi.local/editor
1+
#!/usr/bin/pythonhttp://raspberrypi.local/editor
22

33
#
44
# based on code from lrvick and LiquidCrystal
55
# lrvic - https://github.com/lrvick/raspi-hd44780/blob/master/hd44780.py
66
# LiquidCrystal - https://github.com/arduino/Arduino/blob/master/libraries/LiquidCrystal/LiquidCrystal.cpp
7-
#
7+
88

99
from time import sleep
1010
from Adafruit_I2C import Adafruit_I2C
@@ -35,71 +35,75 @@ 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

8181

82-
def __init__(self, busnum=0, pin_rs=15, pin_e=13, pins_db=[12, 11, 10, 9], pin_rw=14):
82+
def __init__(self, busnum=1, pin_rs=15, pin_e=13, pins_db=[12, 11, 10, 9], pin_rw=14):
8383
self.pin_rs = pin_rs
8484
self.pin_e = pin_e
8585
self.pin_rw = pin_rw
8686
self.pins_db = pins_db
8787

8888
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)
98+
9699
self.write4bits(0x33) # initialization
97100
self.write4bits(0x32) # initialization
98-
self.write4bits(0x28) # 2 line 5x7 matrix
101+
self.write4bits(0x28) # 2 line 5x8 matrix
99102
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
100-
self.write4bits(0x06) # shift cursor right
103+
self.write4bits(0x06) # set cursor incrementing and no shift of display
101104

102105
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
106+
103107
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
104108
self.displayfunction |= self.LCD_2LINE
105109

@@ -136,12 +140,13 @@ def begin(self, cols, lines):
136140

137141
def home(self):
138142
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
139-
self.delayMicroseconds(2000) # this command takes a long time!
143+
self.waitBFlow() #wait for Busy flag low
144+
140145

141146
def clear(self):
142147
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
143-
self.delayMicroseconds(2000) # 2000 microsecond sleep, clearing the display takes a long time
144-
148+
self.waitBFlow() #wait for Busy flag low
149+
145150
def setCursor(self, col, row):
146151
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
147152
if ( row > self.numlines ):
@@ -159,26 +164,39 @@ def display(self):
159164
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
160165

161166
def noCursor(self):
162-
""" Turns the underline cursor off """
167+
""" underline cursor off """
163168
self.displaycontrol &= ~self.LCD_CURSORON
164169
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
165170

166171

167172
def cursor(self):
168-
""" Turns the underline cursor On """
173+
""" underline Cursor On """
169174
self.displaycontrol |= self.LCD_CURSORON
170175
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
171176

177+
def ToggleCursor(self):
178+
""" Toggles the underline cursor On/Off bb"""
179+
self.displaycontrol ^= self.LCD_CURSORON
180+
self.delayMicroseconds(200000)
181+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
182+
172183
def noBlink(self):
173-
""" Turn on and off the blinking cursor """
184+
""" Turn off the blinking cursor """
174185
self.displaycontrol &= ~self.LCD_BLINKON
175186
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
176187

177188
def blink(self):
178-
""" Turn on and off the blinking cursor """
189+
""" Turn on the blinking cursor"""
179190
self.displaycontrol |= self.LCD_BLINKON
180191
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
181192

193+
def ToggleBlink(self):
194+
""" Toggles the blinking cursor"""
195+
self.displaycontrol ^= self.LCD_BLINKON
196+
self.delayMicroseconds(200000)
197+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
198+
199+
182200
def DisplayLeft(self):
183201
""" These commands scroll the display without changing the RAM """
184202
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
@@ -212,18 +230,78 @@ def write4bits(self, bits, char_mode=False):
212230
#self.delayMicroseconds(1000) # 1000 microsecond sleep
213231
bits=bin(bits)[2:].zfill(8)
214232
self.mcp.output(self.pin_rs, char_mode)
233+
215234
for i in range(4):
216235
if bits[i] == "1":
217236
self.mcp.output(self.pins_db[::-1][i], True)
218237
else:
219238
self.mcp.output(self.pins_db[::-1][i], False)
220239
self.pulseEnable()
240+
221241
for i in range(4,8):
222242
if bits[i] == "1":
223243
self.mcp.output(self.pins_db[::-1][i-4], True)
224244
else:
225245
self.mcp.output(self.pins_db[::-1][i-4], False)
226246
self.pulseEnable()
247+
248+
#See pg. 24 and 58 HD44780.pdf and http://www.avrbeginners.net/ Standard LCD bit mode
249+
def read4bits(self, char_mode=False):
250+
""" Get Data from LCD, when char_mode = 0 get Busy Flag and Address Counter, else get RAM data """
251+
# print "First:", bin(self.mcp.direction)[2:].zfill(16)
252+
self.mcp.output(self.pin_rs, char_mode) # when char_mode = 0 get Busy Flag and Address Counter, else get RAM data
253+
self.mcp.output(self.pin_rw, 1) #set rw to 1
254+
# Configure pins for input
255+
makeinput=True
256+
if makeinput:
257+
for pin in self.pins_db:
258+
self.mcp.config(pin, self.INPUT)
259+
self.mcp.output(self.pin_e, True) # set Enable high and keep while read first nibble
260+
bits = range(8)
261+
# get the pins values
262+
for i in range(4):
263+
bt = self.mcp.input(self.pins_db[::-1][i], makeinput) # if makeinput is False, pin direction checking is supressed in input
264+
bits[i]=bt
265+
#print i,bt,bits[i]
266+
self.mcp.output(self.pin_e, False) # set Enable low to finish first nibble
267+
self.mcp.output(self.pin_e, True) # set Enable high and keep while read 2nd nibble
268+
# get the pins values
269+
for i in range(4,8):
270+
bt = self.mcp.input(self.pins_db[::-1][i-4],makeinput)
271+
bits[i]=bt
272+
#print i,bt,bits[i]
273+
self.mcp.output(self.pin_e, False) # set Enable low to finish 2nd nibble
274+
# restore the pins to output and rw to 0
275+
for pin in self.pins_db:
276+
self.mcp.config(pin, self.OUTPUT)
277+
self.mcp.output(self.pin_rw, 0) # return rw to 0
278+
# print "Last :", bin(self.mcp.direction)[2:].zfill(16)
279+
return bits;
280+
281+
def readBF(self):
282+
""" Get Data from LCD, when char_mode = 0 get Busy Flag and Address Counter, else get RAM data """
283+
# print "First:", bin(self.mcp.direction)[2:].zfill(16)
284+
self.mcp.output(self.pin_rs, 0) # when char_mode = 0 get Busy Flag and Address Counter, else get RAM data
285+
self.mcp.output(self.pin_rw, 1) #set rw to 1
286+
# Configure pins for input
287+
makeinput=True
288+
self.mcp.config(self.pins_db[::-1][1], self.INPUT)
289+
self.mcp.output(self.pin_e, True) # set Enable high and keep while read first nibble
290+
# get the pins values
291+
bt = self.mcp.input(self.pins_db[::-1][1], makeinput) # if makeinput is False, pin direction checking is supressed in input
292+
self.mcp.output(self.pin_e, False) # set Enable low to finish first nibble
293+
self.pulseEnable() # one more pulse to get (but ingore the 2nd nibble)
294+
# restore the pins to output and rw to 0
295+
self.mcp.config(self.pins_db[::-1][1], self.OUTPUT)
296+
self.mcp.output(self.pin_rw, 0) # return rw to 0
297+
return bt==1
298+
299+
def waitBFlow(self):
300+
for cnt in range(100000):
301+
if not self.readBF():
302+
#print cnt
303+
return
304+
print "timed out of waitBFlow"
227305

228306
def delayMicroseconds(self, microseconds):
229307
seconds = microseconds / 1000000 # divide microseconds by 1 million for seconds
@@ -239,7 +317,7 @@ def message(self, text):
239317
""" Send string to LCD. Newline wraps to second line"""
240318
for char in text:
241319
if char == '\n':
242-
self.write4bits(0xC0) # next line
320+
self.write4bits(0xC0) # set DDRAM address 0x40 start of second line
243321
else:
244322
self.write4bits(ord(char),True)
245323

@@ -251,42 +329,60 @@ def backlight(self, color):
251329
def buttonPressed(self, buttonname):
252330
if (buttonname > self.LEFT):
253331
return false
332+
254333
return not self.mcp.input(buttonname)
255334

256335

257336
if __name__ == '__main__':
258337

259-
lcd = Adafruit_CharLCDPlate(busnum = 0)
338+
lcd = Adafruit_CharLCDPlate(busnum = 1)
260339
lcd.clear()
261340
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
262341
sleep(1)
263-
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)
274342

343+
print " Cycle thru backlight colors 3 times "
275344

276-
while 1:
345+
for i in range(3):
346+
print "red"
277347
lcd.backlight(lcd.RED)
278348
sleep(1)
349+
print "yellow"
279350
lcd.backlight(lcd.YELLOW)
280351
sleep(1)
352+
print "green"
281353
lcd.backlight(lcd.GREEN)
282354
sleep(1)
355+
print "teal"
283356
lcd.backlight(lcd.TEAL)
284357
sleep(1)
358+
print "blue"
285359
lcd.backlight(lcd.BLUE)
286360
sleep(1)
361+
print "violet"
287362
lcd.backlight(lcd.VIOLET)
288363
sleep(1)
289-
lcd.backlight(lcd.ON)
290-
sleep(1)
364+
print "off"
291365
lcd.backlight(lcd.OFF)
292366
sleep(1)
367+
print "on"
368+
lcd.backlight(lcd.ON)
369+
sleep(1)
370+
371+
print " Try buttons on plate"
372+
373+
while 1:
374+
if (lcd.buttonPressed(lcd.LEFT)):
375+
lcd.backlight(lcd.RED)
376+
377+
if (lcd.buttonPressed(lcd.UP)):
378+
lcd.backlight(lcd.BLUE)
379+
380+
if (lcd.buttonPressed(lcd.DOWN)):
381+
lcd.backlight(lcd.GREEN)
382+
383+
if (lcd.buttonPressed(lcd.RIGHT)):
384+
lcd.backlight(lcd.VIOLET)
385+
386+
if (lcd.buttonPressed(lcd.SELECT)):
387+
lcd.backlight(lcd.ON)
388+

0 commit comments

Comments
 (0)