Skip to content

Commit c973141

Browse files
author
mikeysklar
committed
merged 200 lines from liquid crystal into Adafruit_CharLCD.py
code syntax is a good and most of the methods were tested to confirm they performed properly on a LCD
1 parent 7b26f75 commit c973141

File tree

5 files changed

+449
-10
lines changed

5 files changed

+449
-10
lines changed

Adafruit_BMP085/Adafruit_I2C.py

-1
This file was deleted.

Adafruit_BMP085/Adafruit_I2C.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/python
2+
3+
import smbus
4+
5+
# ===========================================================================
6+
# Adafruit_I2C Base Class
7+
# ===========================================================================
8+
9+
class Adafruit_I2C :
10+
11+
def __init__(self, address, bus=smbus.SMBus(0), debug=False):
12+
self.address = address
13+
self.bus = bus
14+
self.debug = debug
15+
16+
def reverseByteOrder(self, data):
17+
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
18+
# Courtesy Vishal Sapre
19+
dstr = hex(data)[2:].replace('L','')
20+
byteCount = len(dstr[::2])
21+
val = 0
22+
for i, n in enumerate(range(byteCount)):
23+
d = data & 0xFF
24+
val |= (d << (8 * (byteCount - i - 1)))
25+
data >>= 8
26+
return val
27+
28+
def write8(self, reg, value):
29+
"Writes an 8-bit value to the specified register/address"
30+
try:
31+
self.bus.write_byte_data(self.address, reg, value)
32+
if (self.debug):
33+
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
34+
except IOError, err:
35+
print "Error accessing 0x%02X: Check your I2C address" % self.address
36+
return -1
37+
38+
def writeList(self, reg, list):
39+
"Writes an array of bytes using I2C format"
40+
try:
41+
self.bus.write_i2c_block_data(self.address, reg, list)
42+
except IOError, err:
43+
print "Error accessing 0x%02X: Check your I2C address" % self.address
44+
return -1
45+
46+
def readU8(self, reg):
47+
"Read an unsigned byte from the I2C device"
48+
try:
49+
result = self.bus.read_byte_data(self.address, reg)
50+
if (self.debug):
51+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
52+
return result
53+
except IOError, err:
54+
print "Error accessing 0x%02X: Check your I2C address" % self.address
55+
return -1
56+
57+
def readS8(self, reg):
58+
"Reads a signed byte from the I2C device"
59+
try:
60+
result = self.bus.read_byte_data(self.address, reg)
61+
if (self.debug):
62+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
63+
if (result > 127):
64+
return result - 256
65+
else:
66+
return result
67+
except IOError, err:
68+
print "Error accessing 0x%02X: Check your I2C address" % self.address
69+
return -1
70+
71+
def readU16(self, reg):
72+
"Reads an unsigned 16-bit value from the I2C device"
73+
try:
74+
hibyte = self.bus.read_byte_data(self.address, reg)
75+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
76+
if (self.debug):
77+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
78+
return result
79+
except IOError, err:
80+
print "Error accessing 0x%02X: Check your I2C address" % self.address
81+
return -1
82+
83+
def readS16(self, reg):
84+
"Reads a signed 16-bit value from the I2C device"
85+
try:
86+
hibyte = self.bus.read_byte_data(self.address, reg)
87+
if (hibyte > 127):
88+
hibyte -= 256
89+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
90+
if (self.debug):
91+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
92+
return result
93+
except IOError, err:
94+
print "Error accessing 0x%02X: Check your I2C address" % self.address
95+
return -1

Adafruit_CharLCD/Adafruit_CharLCD.py

+162-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,50 @@
1111

1212
class Adafruit_CharLCD:
1313

14+
# commands
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
23+
24+
# flags for display entry mode
25+
LCD_ENTRYRIGHT = 0x00
26+
LCD_ENTRYLEFT = 0x02
27+
LCD_ENTRYSHIFTINCREMENT = 0x01
28+
LCD_ENTRYSHIFTDECREMENT = 0x00
29+
30+
# flags for display on/off control
31+
LCD_DISPLAYON = 0x04
32+
LCD_DISPLAYOFF = 0x00
33+
LCD_CURSORON = 0x02
34+
LCD_CURSOROFF = 0x00
35+
LCD_BLINKON = 0x01
36+
LCD_BLINKOFF = 0x00
37+
38+
# flags for display/cursor shift
39+
LCD_DISPLAYMOVE = 0x08
40+
LCD_CURSORMOVE = 0x00
41+
42+
# flags for display/cursor shift
43+
LCD_DISPLAYMOVE = 0x08
44+
LCD_CURSORMOVE = 0x00
45+
LCD_MOVERIGHT = 0x04
46+
LCD_MOVELEFT = 0x00
47+
48+
# flags for function set
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+
57+
1458
def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
1559

1660
self.pin_rs = pin_rs
@@ -29,23 +73,134 @@ def __init__(self, pin_rs=25, pin_e=24, pins_db=[23, 17, 21, 22]):
2973
self.write4bits(0x28) # 2 line 5x7 matrix
3074
self.write4bits(0x0C) # turn cursor off 0x0E to enable cursor
3175
self.write4bits(0x06) # shift cursor right
76+
77+
self.displaycontrol = self.LCD_DISPLAYON | self.LCD_CURSOROFF | self.LCD_BLINKOFF
78+
79+
self.displayfunction = self.LCD_4BITMODE | self.LCD_1LINE | self.LCD_5x8DOTS
80+
self.displayfunction |= self.LCD_2LINE
81+
82+
""" Initialize to default text direction (for romance languages) """
83+
self.displaymode = self.LCD_ENTRYLEFT | self.LCD_ENTRYSHIFTDECREMENT
84+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode) # set the entry mode
85+
3286
self.clear()
87+
88+
89+
def begin(self, cols, lines):
90+
91+
if (lines > 1):
92+
self.numlines = lines
93+
self.displayfunction |= self.LCD_2LINE
94+
self.currline = 0
95+
96+
97+
def home(self):
98+
99+
self.write4bits(self.LCD_RETURNHOME) # set cursor position to zero
100+
self.delayMicroseconds(2000) # this command takes a long time!
33101

34102

35103
def clear(self):
36104

37-
self.write4bits(0x01) # command to clear display
105+
self.write4bits(self.LCD_CLEARDISPLAY) # command to clear display
106+
self.delayMicroseconds(2000) # 2000 microsecond sleep, clearing the display takes a long time
107+
108+
109+
def setCursor(self, col, row):
110+
111+
self.row_offsets = [ 0x00, 0x40, 0x14, 0x54 ]
112+
113+
if ( row > self.numlines ):
114+
row = self.numlines - 1 # we count rows starting w/0
115+
116+
self.write4bits(self.LCD_SETDDRAMADDR | (col + self.row_offsets[row]))
117+
118+
119+
def noDisplay(self):
120+
""" Turn the display off (quickly) """
121+
122+
self.displaycontrol &= ~self.LCD_DISPLAYON
123+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
124+
125+
126+
def display(self):
127+
""" Turn the display on (quickly) """
128+
129+
self.displaycontrol |= self.LCD_DISPLAYON
130+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
131+
132+
133+
def noCursor(self):
134+
""" Turns the underline cursor on/off """
135+
136+
self.displaycontrol &= ~self.LCD_CURSORON
137+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
138+
139+
140+
def cursor(self):
141+
""" Cursor On """
142+
143+
self.displaycontrol |= self.LCD_CURSORON
144+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
145+
146+
147+
def noBlink(self):
148+
""" Turn on and off the blinking cursor """
149+
150+
self.displaycontrol &= ~self.LCD_BLINKON
151+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
152+
153+
154+
def noBlink(self):
155+
""" Turn on and off the blinking cursor """
156+
157+
self.displaycontrol &= ~self.LCD_BLINKON
158+
self.write4bits(self.LCD_DISPLAYCONTROL | self.displaycontrol)
159+
160+
161+
def DisplayLeft(self):
162+
""" These commands scroll the display without changing the RAM """
163+
164+
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVELEFT)
165+
166+
167+
def scrollDisplayRight(self):
168+
""" These commands scroll the display without changing the RAM """
169+
170+
self.write4bits(self.LCD_CURSORSHIFT | self.LCD_DISPLAYMOVE | self.LCD_MOVERIGHT);
171+
172+
173+
def leftToRight(self):
174+
""" This is for text that flows Left to Right """
175+
176+
self.displaymode |= self.LCD_ENTRYLEFT
177+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode);
178+
179+
180+
def rightToLeft(self):
181+
""" This is for text that flows Right to Left """
182+
self.displaymode &= ~self.LCD_ENTRYLEFT
183+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
184+
185+
186+
def autoscroll(self):
187+
""" This will 'right justify' text from the cursor """
188+
189+
self.displaymode |= self.LCD_ENTRYSHIFTINCREMENT
190+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
191+
192+
193+
def noAutoscroll(self):
194+
""" This will 'left justify' text from the cursor """
195+
196+
self.displaymode &= ~self.LCD_ENTRYSHIFTINCREMENT
197+
self.write4bits(self.LCD_ENTRYMODESET | self.displaymode)
38198

39-
# 2000 microsecond sleep, clearing the display takes a long time
40-
sleep(.002)
41-
#self.delayMicroseconds(2000)
42199

43200
def write4bits(self, bits, char_mode=False):
44201
""" Send command to LCD """
45202

46-
# 1000 microseconds sleep
47-
# sleep(.001)
48-
self.delayMicroseconds(1000)
203+
self.delayMicroseconds(1000) # 1000 microsecond sleep
49204

50205
bits=bin(bits)[2:].zfill(8)
51206

Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

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

12+
lcd.begin(16,1)
13+
1214
def run_cmd(cmd):
1315
p = Popen(cmd, shell=True, stdout=PIPE)
1416
output = p.communicate()[0]

Adafruit_MCP4725/Adafruit_I2C.py

-1
This file was deleted.

Adafruit_MCP4725/Adafruit_I2C.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#!/usr/bin/python
2+
3+
import smbus
4+
5+
# ===========================================================================
6+
# Adafruit_I2C Base Class
7+
# ===========================================================================
8+
9+
class Adafruit_I2C :
10+
11+
def __init__(self, address, bus=smbus.SMBus(0), debug=False):
12+
self.address = address
13+
self.bus = bus
14+
self.debug = debug
15+
16+
def reverseByteOrder(self, data):
17+
"Reverses the byte order of an int (16-bit) or long (32-bit) value"
18+
# Courtesy Vishal Sapre
19+
dstr = hex(data)[2:].replace('L','')
20+
byteCount = len(dstr[::2])
21+
val = 0
22+
for i, n in enumerate(range(byteCount)):
23+
d = data & 0xFF
24+
val |= (d << (8 * (byteCount - i - 1)))
25+
data >>= 8
26+
return val
27+
28+
def write8(self, reg, value):
29+
"Writes an 8-bit value to the specified register/address"
30+
try:
31+
self.bus.write_byte_data(self.address, reg, value)
32+
if (self.debug):
33+
print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg))
34+
except IOError, err:
35+
print "Error accessing 0x%02X: Check your I2C address" % self.address
36+
return -1
37+
38+
def writeList(self, reg, list):
39+
"Writes an array of bytes using I2C format"
40+
try:
41+
self.bus.write_i2c_block_data(self.address, reg, list)
42+
except IOError, err:
43+
print "Error accessing 0x%02X: Check your I2C address" % self.address
44+
return -1
45+
46+
def readU8(self, reg):
47+
"Read an unsigned byte from the I2C device"
48+
try:
49+
result = self.bus.read_byte_data(self.address, reg)
50+
if (self.debug):
51+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
52+
return result
53+
except IOError, err:
54+
print "Error accessing 0x%02X: Check your I2C address" % self.address
55+
return -1
56+
57+
def readS8(self, reg):
58+
"Reads a signed byte from the I2C device"
59+
try:
60+
result = self.bus.read_byte_data(self.address, reg)
61+
if (self.debug):
62+
print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg)
63+
if (result > 127):
64+
return result - 256
65+
else:
66+
return result
67+
except IOError, err:
68+
print "Error accessing 0x%02X: Check your I2C address" % self.address
69+
return -1
70+
71+
def readU16(self, reg):
72+
"Reads an unsigned 16-bit value from the I2C device"
73+
try:
74+
hibyte = self.bus.read_byte_data(self.address, reg)
75+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
76+
if (self.debug):
77+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
78+
return result
79+
except IOError, err:
80+
print "Error accessing 0x%02X: Check your I2C address" % self.address
81+
return -1
82+
83+
def readS16(self, reg):
84+
"Reads a signed 16-bit value from the I2C device"
85+
try:
86+
hibyte = self.bus.read_byte_data(self.address, reg)
87+
if (hibyte > 127):
88+
hibyte -= 256
89+
result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1)
90+
if (self.debug):
91+
print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg)
92+
return result
93+
except IOError, err:
94+
print "Error accessing 0x%02X: Check your I2C address" % self.address
95+
return -1

0 commit comments

Comments
 (0)