This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathlcd_i2c_pcf8574.py
299 lines (243 loc) · 8.16 KB
/
lcd_i2c_pcf8574.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
#!/usr/bin/env python
#
# LCD test program for the lcd_i2c_class.py class
# $Id: lcd_i2c_pcf8574.py,v 1.3 2016/12/01 14:17:31 bob Exp $
# PCF8574 I2C LCD Backpack LCD class
# Use this program to test I2C Backpack LCD wiring
# Adapted from RpiLcdBackpack from Paul Knox-Kennedy by Lubos Ruckl and Bob Rathbone
# at Adafruit Industries
#
# Author : Bob Rathbone
# Site : http://www.bobrathbone.com
#
# License: GNU V3, See https://www.gnu.org/copyleft/gpl.html
#
# Disclaimer: Software is provided as is and absolutly no warranties are implied or given.
# The authors shall not be liable for any loss or damage however caused.
#
import smbus, time
from subprocess import *
from time import sleep, strftime
from datetime import datetime
from config_class import Configuration
config = Configuration()
class lcd_i2c_pcf8574:
ADDRESS = 0x27 #for "Serial Interface IIC I2C TWI Adapter Module" (eBay)
i2c_address = ADDRESS
#Bus = None
# commands
__CLEARDISPLAY=0x01
__RETURNHOME=0x02
__ENTRYMODESET=0x04
__DISPLAYCONTROL=0x08
__CURSORSHIFT=0x10
__FUNCTIONSET=0x20
__SETCGRAMADDR=0x40
__SETDDRAMADDR=0x80
# flags for display entry mode
__ENTRYRIGHT=0x00
__ENTRYLEFT=0x02
__ENTRYSHIFTINCREMENT=0x01
__ENTRYSHIFTDECREMENT=0x00
# flags for display on/off control
__DISPLAYON=0x04
__DISPLAYOFF=0x00
__CURSORON=0x02
__CURSOROFF=0x00
__BLINKON=0x01
__BLINKOFF=0x00
# flags for display/cursor shift
__DISPLAYMOVE=0x08
__CURSORMOVE=0x00
__MOVERIGHT=0x04
__MOVELEFT=0x00
# flags for function set
__8BITMODE=0x10
__4BITMODE=0x00
__2LINE=0x08
__1LINE=0x00
__5x10DOTS=0x04
__5x8DOTS=0x00
# flags for backlight control
LCD_BACKLIGHT = 0x08
LCD_NOBACKLIGHT = 0x00
# control bits
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit
Rs = 0b00000001 # Register select bit
# Define LCD device constants
LCD_WIDTH = 16 # Default characters per line
LCD_LINE_1 = 0x80 # LCD RAM address for the 1st line
LCD_LINE_2 = 0xC0 # LCD RAM address for the 2nd line
LCD_LINE_3 = 0x94 # LCD RAM address for the 3rd line
LCD_LINE_4 = 0xD4 # LCD RAM address for the 4th line
# Some LCDs use different addresses (16 x 4 line LCDs)
LCD_LINE_3a = 0x90 # LCD RAM address for the 3rd line (16 char display)
LCD_LINE_4a = 0xD0 # LCD RAM address for the 4th line (16 char display)
lcd_line3 = LCD_LINE_3
lcd_line4 = LCD_LINE_4
width = LCD_WIDTH
ScrollSpeed = 0.3 # Default scroll speed
def write_cmd(self, cmd):
#self.__bus.write_byte(self.ADDRESS, cmd) # Now configurable
self.__bus.write_byte(self.i2c_address, cmd)
sleep(0.0001)
# clocks EN to latch command
def lcd_strobe(self, data):
self.write_cmd(data | self.En | self._backlight)
sleep(.0005)
self.write_cmd(((data & ~self.En) | self._backlight))
sleep(.0001)
def lcd_write_four_bits(self, data):
self.write_cmd(data | self._backlight)
self.lcd_strobe(data)
# write a command to lcd
def writeCommand(self, cmd, mode=0):
self.lcd_write_four_bits(mode | (cmd & 0xF0))
#self.lcd_write_four_bits(data)
#self.lcd_write_four_bits(mode | (cmd & 0xF0))
self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
# __init__
def __init__(self):
self._backlight = self.LCD_BACKLIGHT
# Initialisation routine
def init(self, board_rev=2, address=0x27):
bus = 1
if board_rev == 1:
bus = 0
if address > 0x00:
self.i2c_address = address
self.__bus=smbus.SMBus(bus)
self.writeCommand(0x03)
self.writeCommand(0x03)
self.writeCommand(0x03)
self.writeCommand(0x02)
self.__displayfunction = self.__FUNCTIONSET | self.__2LINE | self.__5x8DOTS | self.__4BITMODE
self.__displaycontrol = self.__DISPLAYCONTROL | self.__DISPLAYON | self.__CURSORON | self.__BLINKON
self.writeCommand(self.__displayfunction)
self.writeCommand(self.__DISPLAYCONTROL | self.__DISPLAYON)
self.writeCommand(self.__CLEARDISPLAY)
self.writeCommand(self.__ENTRYMODESET | self.__ENTRYLEFT)
sleep(0.2)
# Display Line 1 on LCD
def line1(self,text):
self._writeLine(self.LCD_LINE_1,text)
return
# Display Line 2 on LCD
def line2(self,text):
self._writeLine(self.LCD_LINE_2,text)
return
# Display Line 3 on LCD
def line3(self,text):
self._writeLine(self.lcd_line3,text)
return
# Display Line 4 on LCD
def line4(self,text):
self._writeLine(self.lcd_line4,text)
return
# Write a single line to the LCD
def _writeLine(self, line, text):
self.writeCommand(line)
if len(text) < self.width:
text = text.ljust(self.width, ' ')
self.message(text[:self.width])
return
# Scroll message on line 1
def scroll1(self,mytext,interrupt):
self._scroll(mytext,self.LCD_LINE_1,interrupt)
return
# Scroll message on line 2
def scroll2(self,mytext,interrupt):
self._scroll(mytext,self.LCD_LINE_2,interrupt)
return
# Scroll message on line 3
def scroll3(self,mytext,interrupt):
self._scroll(mytext,self.lcd_line3,interrupt)
return
# Scroll message on line 4
def scroll4(self,mytext,interrupt):
self._scroll(mytext,self.lcd_line4,interrupt)
return
# Scroll line - interrupt() breaks out routine if True
def _scroll(self,mytext,line,interrupt):
ilen = len(mytext)
skip = False
self._writeLine(line,mytext[0:self.width + 1])
if (ilen <= self.width):
skip = True
if not skip:
for i in range(0, 5):
time.sleep(0.2)
if interrupt():
skip = True
break
if not skip:
for i in range(0, ilen - self.width + 1 ):
self._writeLine(line,mytext[i:i+self.width])
if interrupt():
skip = True
break
time.sleep(self.ScrollSpeed)
if not skip:
for i in range(0, 5):
time.sleep(0.2)
if interrupt():
break
return
# Switch back light on and off
def backlight(self,on):
if on:
self._backlight = self.LCD_BACKLIGHT
else:
self._backlight = self.LCD_NOBACKLIGHT
self.writeCommand(self.__displayfunction)
# Clear display
def clear(self):
self.writeCommand(self.__CLEARDISPLAY)
time.sleep(0.002)
# Blink cursor
def blink(self, on):
if on:
self.__displaycontrol |= self.__BLINKON
else:
self.__displaycontrol &= ~self.__BLINKON
self.writeCommand(self.__displaycontrol)
def noCursor(self):
self.writeCommand(self.__displaycontrol)
def cursor(self, on):
if on:
self.__displaycontrol |= self.__CURSORON
else:
self.__displaycontrol &= ~self.__CURSORON
self.writeCommand(self.__displaycontrol)
def message(self, text):
for char in text:
if char == '\n':
self.writeCommand(0xC0)
else:
self.writeCommand(ord(char), self.Rs)
# Set the display width
def setWidth(self,width):
self.width = width
return
# Get LCD width 0 = use default for program
def getWidth(self):
return config.getWidth()
# Set the display width
def setWidth(self,width):
self.width = width
# Adjust line offsets if 16 char display
if width is 16:
self.lcd_line3 = self.LCD_LINE_3a
self.lcd_line4 = self.LCD_LINE_4a
return
# Set Scroll line speed - Best values are 0.2 and 0.3
# Limit to between 0.05 and 1.0
def setScrollSpeed(self,speed):
if speed < 0.05:
speed = 0.2
elif speed > 1.0:
speed = 0.3
self.ScrollSpeed = speed
return
# End of lcd_i2c_class