Skip to content

Commit f8085fe

Browse files
committedJul 15, 2014
Make char LCD spaces/tabs consistent and clean up to be close to pep8 compliance.
1 parent 4de28dd commit f8085fe

File tree

2 files changed

+124
-173
lines changed

2 files changed

+124
-173
lines changed
 

‎Adafruit_CharLCD/Adafruit_CharLCD.py

+113-163
Original file line numberDiff line numberDiff line change
@@ -8,58 +8,58 @@
88

99
from time import sleep
1010

11-
class Adafruit_CharLCD:
11+
12+
class Adafruit_CharLCD(object):
1213

1314
# commands
14-
LCD_CLEARDISPLAY = 0x01
15-
LCD_RETURNHOME = 0x02
16-
LCD_ENTRYMODESET = 0x04
17-
LCD_DISPLAYCONTROL = 0x08
18-
LCD_CURSORSHIFT = 0x10
19-
LCD_FUNCTIONSET = 0x20
20-
LCD_SETCGRAMADDR = 0x40
21-
LCD_SETDDRAMADDR = 0x80
15+
LCD_CLEARDISPLAY = 0x01
16+
LCD_RETURNHOME = 0x02
17+
LCD_ENTRYMODESET = 0x04
18+
LCD_DISPLAYCONTROL = 0x08
19+
LCD_CURSORSHIFT = 0x10
20+
LCD_FUNCTIONSET = 0x20
21+
LCD_SETCGRAMADDR = 0x40
22+
LCD_SETDDRAMADDR = 0x80
2223

2324
# flags for display entry mode
24-
LCD_ENTRYRIGHT = 0x00
25-
LCD_ENTRYLEFT = 0x02
26-
LCD_ENTRYSHIFTINCREMENT = 0x01
27-
LCD_ENTRYSHIFTDECREMENT = 0x00
25+
LCD_ENTRYRIGHT = 0x00
26+
LCD_ENTRYLEFT = 0x02
27+
LCD_ENTRYSHIFTINCREMENT = 0x01
28+
LCD_ENTRYSHIFTDECREMENT = 0x00
2829

2930
# flags for display on/off control
30-
LCD_DISPLAYON = 0x04
31-
LCD_DISPLAYOFF = 0x00
32-
LCD_CURSORON = 0x02
33-
LCD_CURSOROFF = 0x00
34-
LCD_BLINKON = 0x01
35-
LCD_BLINKOFF = 0x00
31+
LCD_DISPLAYON = 0x04
32+
LCD_DISPLAYOFF = 0x00
33+
LCD_CURSORON = 0x02
34+
LCD_CURSOROFF = 0x00
35+
LCD_BLINKON = 0x01
36+
LCD_BLINKOFF = 0x00
3637

3738
# flags for display/cursor shift
38-
LCD_DISPLAYMOVE = 0x08
39-
LCD_CURSORMOVE = 0x00
39+
LCD_DISPLAYMOVE = 0x08
40+
LCD_CURSORMOVE = 0x00
4041

4142
# flags for display/cursor shift
42-
LCD_DISPLAYMOVE = 0x08
43-
LCD_CURSORMOVE = 0x00
44-
LCD_MOVERIGHT = 0x04
45-
LCD_MOVELEFT = 0x00
43+
LCD_DISPLAYMOVE = 0x08
44+
LCD_CURSORMOVE = 0x00
45+
LCD_MOVERIGHT = 0x04
46+
LCD_MOVELEFT = 0x00
4647

4748
# flags for function set
48-
LCD_8BITMODE = 0x10
49-
LCD_4BITMODE = 0x00
50-
LCD_2LINE = 0x08
51-
LCD_1LINE = 0x00
52-
LCD_5x10DOTS = 0x04
53-
LCD_5x8DOTS = 0x00
54-
55-
56-
57-
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):
58-
# Emulate the old behavior of using RPi.GPIO if we haven't been given
59-
# an explicit GPIO interface to use
60-
if not GPIO:
61-
import RPi.GPIO as GPIO
62-
self.GPIO = GPIO
49+
LCD_8BITMODE = 0x10
50+
LCD_4BITMODE = 0x00
51+
LCD_2LINE = 0x08
52+
LCD_1LINE = 0x00
53+
LCD_5x10DOTS = 0x04
54+
LCD_5x8DOTS = 0x00
55+
56+
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO=None):
57+
# Emulate the old behavior of using RPi.GPIO if we haven't been given
58+
# an explicit GPIO interface to use
59+
if not GPIO:
60+
import RPi.GPIO as GPIO
61+
GPIO.setwarnings(False)
62+
self.GPIO = GPIO
6363
self.pin_rs = pin_rs
6464
self.pin_e = pin_e
6565
self.pins_db = pins_db
@@ -71,190 +71,140 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22], GPIO = None):
7171
for pin in self.pins_db:
7272
self.GPIO.setup(pin, GPIO.OUT)
7373

74-
self.write4bits(0x33) # initialization
75-
self.write4bits(0x32) # initialization
76-
self.write4bits(0x28) # 2 line 5x7 matrix
77-
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
78-
self.write4bits(0x06) # shift cursor right
74+
self.write4bits(0x33) # initialization
75+
self.write4bits(0x32) # initialization
76+
self.write4bits(0x28) # 2 line 5x7 matrix
77+
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
78+
self.write4bits(0x06) # shift cursor right
7979

80-
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
80+
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
8181

82-
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
83-
self.displayfunction |= self.LCD_2LINE
82+
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
83+
self.displayfunction |= self.LCD_2LINE
8484

85-
""" Initialize to default text direction (for romance languages) """
86-
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
87-
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
85+
# Initialize to default text direction (for romance languages)
86+
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
87+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
8888

8989
self.clear()
9090

91-
9291
def begin(self, cols, lines):
93-
94-
if (lines > 1):
95-
self.numlines = lines
96-
self.displayfunction |= self.LCD_2LINE
97-
92+
if (lines > 1):
93+
self.numlines = lines
94+
self.displayfunction |= self.LCD_2LINE
9895

9996
def home(self):
100-
101-
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
102-
self.delayMicroseconds(3000) # this command takes a long time!
103-
97+
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
98+
self.delayMicroseconds(3000) # this command takes a long time!
10499

105100
def clear(self):
106-
107-
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
108-
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
109-
101+
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
102+
self.delayMicroseconds(3000) # 3000 microsecond sleep, clearing the display takes a long time
110103

111104
def setCursor(self, col, row):
105+
self.row_offsets = [0x00, 0x40, 0x14, 0x54]
106+
if row > self.numlines:
107+
row = self.numlines - 1 # we count rows starting w/0
108+
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
112109

113-
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
114-
115-
if ( row > self.numlines ):
116-
row = self.numlines - 1 # we count rows starting w/0
117-
118-
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
119-
120-
121-
def noDisplay(self):
122-
""" Turn the display off (quickly) """
123-
124-
self.displaycontrol &= ~self.LCD_DISPLAYON
125-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
126-
110+
def noDisplay(self):
111+
""" Turn the display off (quickly) """
112+
self.displaycontrol &= ~self.LCD_DISPLAYON
113+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
127114

128115
def display(self):
129-
""" Turn the display on (quickly) """
130-
131-
self.displaycontrol |= self.LCD_DISPLAYON
132-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
133-
116+
""" Turn the display on (quickly) """
117+
self.displaycontrol |= self.LCD_DISPLAYON
118+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
134119

135120
def noCursor(self):
136-
""" Turns the underline cursor off """
137-
138-
self.displaycontrol &= ~self.LCD_CURSORON
139-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
140-
121+
""" Turns the underline cursor off """
122+
self.displaycontrol &= ~self.LCD_CURSORON
123+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
141124

142125
def cursor(self):
143-
""" Turns the underline cursor on """
144-
145-
self.displaycontrol |= self.LCD_CURSORON
146-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
147-
126+
""" Turns the underline cursor on """
127+
self.displaycontrol |= self.LCD_CURSORON
128+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
148129

149130
def noBlink(self):
150-
""" Turn the blinking cursor off """
151-
152-
self.displaycontrol &= ~self.LCD_BLINKON
153-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
154-
131+
""" Turn the blinking cursor off """
132+
self.displaycontrol &= ~self.LCD_BLINKON
133+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
155134

156135
def blink(self):
157-
""" Turn the blinking cursor on """
158-
159-
self.displaycontrol |= self.LCD_BLINKON
160-
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
161-
136+
""" Turn the blinking cursor on """
137+
self.displaycontrol |= self.LCD_BLINKON
138+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
162139

163140
def DisplayLeft(self):
164-
""" These commands scroll the display without changing the RAM """
165-
166-
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
167-
141+
""" These commands scroll the display without changing the RAM """
142+
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
168143

169144
def scrollDisplayRight(self):
170-
""" These commands scroll the display without changing the RAM """
171-
172-
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
173-
145+
""" These commands scroll the display without changing the RAM """
146+
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT)
174147

175148
def leftToRight(self):
176-
""" This is for text that flows Left to Right """
177-
178-
self.displaymode |= self.LCD_ENTRYLEFT
179-
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
180-
149+
""" This is for text that flows Left to Right """
150+
self.displaymode |= self.LCD_ENTRYLEFT
151+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
181152

182153
def rightToLeft(self):
183-
""" This is for text that flows Right to Left """
184-
self.displaymode &= ~self.LCD_ENTRYLEFT
185-
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
186-
154+
""" This is for text that flows Right to Left """
155+
self.displaymode &= ~self.LCD_ENTRYLEFT
156+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
187157

188158
def autoscroll(self):
189-
""" This will 'right justify' text from the cursor """
190-
191-
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
192-
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
193-
194-
195-
def noAutoscroll(self):
196-
""" This will 'left justify' text from the cursor """
197-
198-
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
199-
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
159+
""" This will 'right justify' text from the cursor """
160+
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
161+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
200162

163+
def noAutoscroll(self):
164+
""" This will 'left justify' text from the cursor """
165+
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
166+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
201167

202168
def write4bits(self, bits, char_mode=False):
203169
""" Send command to LCD """
204-
205-
self.delayMicroseconds(1000) # 1000 microsecond sleep
206-
207-
bits=bin(bits)[2:].zfill(8)
208-
170+
self.delayMicroseconds(1000) # 1000 microsecond sleep
171+
bits = bin(bits)[2:].zfill(8)
209172
self.GPIO.output(self.pin_rs, char_mode)
210-
211173
for pin in self.pins_db:
212174
self.GPIO.output(pin, False)
213-
214175
for i in range(4):
215176
if bits[i] == "1":
216177
self.GPIO.output(self.pins_db[::-1][i], True)
217-
218-
self.pulseEnable()
219-
178+
self.pulseEnable()
220179
for pin in self.pins_db:
221180
self.GPIO.output(pin, False)
222-
223-
for i in range(4,8):
181+
for i in range(4, 8):
224182
if bits[i] == "1":
225183
self.GPIO.output(self.pins_db[::-1][i-4], True)
226-
227-
self.pulseEnable()
228-
184+
self.pulseEnable()
229185

230186
def delayMicroseconds(self, microseconds):
231-
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
232-
sleep(seconds)
233-
187+
seconds = microseconds / float(1000000) # divide microseconds by 1 million for seconds
188+
sleep(seconds)
234189

235190
def pulseEnable(self):
236-
self.GPIO.output(self.pin_e, False)
237-
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
238-
self.GPIO.output(self.pin_e, True)
239-
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
240-
self.GPIO.output(self.pin_e, False)
241-
self.delayMicroseconds(1) # commands need > 37us to settle
242-
191+
self.GPIO.output(self.pin_e, False)
192+
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
193+
self.GPIO.output(self.pin_e, True)
194+
self.delayMicroseconds(1) # 1 microsecond pause - enable pulse must be > 450ns
195+
self.GPIO.output(self.pin_e, False)
196+
self.delayMicroseconds(1) # commands need > 37us to settle
243197

244198
def message(self, text):
245199
""" Send string to LCD. Newline wraps to second line"""
246-
247200
for char in text:
248201
if char == '\n':
249-
self.write4bits(0xC0) # next line
202+
self.write4bits(0xC0) # next line
250203
else:
251-
self.write4bits(ord(char),True)
204+
self.write4bits(ord(char), True)
252205

253206

254207
if __name__ == '__main__':
255-
256208
lcd = Adafruit_CharLCD()
257-
258209
lcd.clear()
259210
lcd.message(" Adafruit 16x2\n Standard LCD")
260-
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
#!/usr/bin/python
22

33
from Adafruit_CharLCD import Adafruit_CharLCD
4-
from subprocess import *
4+
from subprocess import *
55
from time import sleep, strftime
66
from datetime import datetime
77

88
lcd = Adafruit_CharLCD()
99

1010
cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
1111

12-
lcd.begin(16,1)
12+
lcd.begin(16, 1)
13+
1314

1415
def run_cmd(cmd):
15-
p = Popen(cmd, shell=True, stdout=PIPE)
16-
output = p.communicate()[0]
17-
return output
16+
p = Popen(cmd, shell=True, stdout=PIPE)
17+
output = p.communicate()[0]
18+
return output
1819

1920
while 1:
20-
lcd.clear()
21-
ipaddr = run_cmd(cmd)
22-
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
23-
lcd.message('IP %s' % ( ipaddr ) )
24-
sleep(2)
21+
lcd.clear()
22+
ipaddr = run_cmd(cmd)
23+
lcd.message(datetime.now().strftime('%b %d %H:%M:%S\n'))
24+
lcd.message('IP %s' % (ipaddr))
25+
sleep(2)

0 commit comments

Comments
 (0)