Skip to content

Commit d957bcb

Browse files
committed
Original Code from Adafruit made Pep8 with all tabs removed
so indent = 4 spaces
1 parent 6af2219 commit d957bcb

File tree

1 file changed

+108
-119
lines changed

1 file changed

+108
-119
lines changed

Adafruit_CharLCDPlate/Adafruit_CharLCDPlate.py

Lines changed: 108 additions & 119 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,51 +85,47 @@ 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)
89-
88+
self.mcp = Adafruit_MCP230XX(busnum = busnum, address = 0x20, num_gpios = 16)
9089
self.mcp.config(self.pin_e, self.OUTPUT)
9190
self.mcp.config(self.pin_rs, self.OUTPUT)
9291
self.mcp.config(self.pin_rw, self.OUTPUT)
9392
self.mcp.output(self.pin_rw, 0)
94-
self.mcp.output(self.pin_e, 0)
95-
93+
self.mcp.output(self.pin_e, 0)
9694
for pin in self.pins_db:
9795
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
123119
self.mcp.pullup(self.SELECT, True)
124120
self.mcp.pullup(self.LEFT, True)
125121
self.mcp.pullup(self.RIGHT, True)
126122
self.mcp.pullup(self.UP, True)
127123
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)
133129

134130
def begin(self, cols, lines):
135131
if (lines > 1):
@@ -144,12 +140,12 @@ def home(self):
144140

145141
def clear(self):
146142
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
148144

149145
def setCursor(self, col, row):
150146
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
151147
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
153149
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
154150

155151
def noDisplay(self):
@@ -163,51 +159,51 @@ def display(self):
163159
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
164160

165161
def noCursor(self):
166-
""" Turns the underline cursor on/off """
162+
""" Turns the underline cursor off """
167163
self.displaycontrol &= ~self.LCD_CURSORON
168164
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
169165

170166

171167
def cursor(self):
172-
""" Cursor On """
168+
""" Turns the underline cursor On """
173169
self.displaycontrol |= self.LCD_CURSORON
174170
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
175171

176172
def noBlink(self):
177-
""" Turn on and off the blinking cursor """
173+
""" Turn on and off the blinking cursor """
178174
self.displaycontrol &= ~self.LCD_BLINKON
179175
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
180176

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
184180
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
185181

186182
def DisplayLeft(self):
187183
""" These commands scroll the display without changing the RAM """
188184
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
189185

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

194190
def leftToRight(self):
195-
""" This is for text that flows Left to Right """
191+
""" This is for text that flows Left to Right """
196192
self.displaymode |= self.LCD_ENTRYLEFT
197193
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
198194

199195
def rightToLeft(self):
200-
""" This is for text that flows Right to Left """
196+
""" This is for text that flows Right to Left """
201197
self.displaymode &= ~self.LCD_ENTRYLEFT
202198
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
203199

204200
def autoscroll(self):
205-
""" This will 'right justify' text from the cursor """
201+
""" This will 'right justify' text from the cursor """
206202
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
207203
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
208204

209205
def noAutoscroll(self):
210-
""" This will 'left justify' text from the cursor """
206+
""" This will 'left justify' text from the cursor """
211207
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
212208
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
213209

@@ -216,14 +212,12 @@ def write4bits(self, bits, char_mode=False):
216212
#self.delayMicroseconds(1000) # 1000 microsecond sleep
217213
bits=bin(bits)[2:].zfill(8)
218214
self.mcp.output(self.pin_rs, char_mode)
219-
220215
for i in range(4):
221216
if bits[i] == "1":
222217
self.mcp.output(self.pins_db[::-1][i], True)
223218
else:
224219
self.mcp.output(self.pins_db[::-1][i], False)
225220
self.pulseEnable()
226-
227221
for i in range(4,8):
228222
if bits[i] == "1":
229223
self.mcp.output(self.pins_db[::-1][i-4], True)
@@ -232,14 +226,14 @@ def write4bits(self, bits, char_mode=False):
232226
self.pulseEnable()
233227

234228
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
236230
sleep(seconds)
237231

238232
def pulseEnable(self):
239233
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
241235
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
243237

244238
def message(self, text):
245239
""" Send string to LCD. Newline wraps to second line"""
@@ -250,15 +244,14 @@ def message(self, text):
250244
self.write4bits(ord(char),True)
251245

252246
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)
256250

257251
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)
262255

263256

264257
if __name__ == '__main__':
@@ -268,36 +261,32 @@ def buttonPressed(self, buttonname):
268261
lcd.message("Adafruit RGB LCD\nPlate w/Keypad!")
269262
sleep(1)
270263
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)
285274

286275

287276
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

Comments
 (0)