-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
290 lines (216 loc) · 9.01 KB
/
main.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
from pyfirmata import OUTPUT
from util import BoardWrapper
LOW = 0
HIGH = 1
# commands
LCD_CLEARDISPLAY = 0x01
LCD_RETURNHOME = 0x02
LCD_ENTRYMODESET = 0x04
LCD_DISPLAYCONTROL = 0x08
LCD_CURSORSHIFT = 0x10
LCD_FUNCTIONSET = 0x20
LCD_SETCGRAMADDR = 0x40
LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00
LCD_ENTRYLEFT = 0x02
LCD_ENTRYSHIFTINCREMENT = 0x01
LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04
LCD_DISPLAYOFF = 0x00
LCD_CURSORON = 0x02
LCD_CURSOROFF = 0x00
LCD_BLINKON = 0x01
LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08
LCD_CURSORMOVE = 0x00
LCD_MOVERIGHT = 0x04
LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10
LCD_4BITMODE = 0x00
LCD_2LINE = 0x08
LCD_1LINE = 0x00
LCD_5x10DOTS = 0x04
LCD_5x8DOTS = 0x00
class LiquidCrystal:
def __init__(self, board, fourbitmode, rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7):
self.wrapper = BoardWrapper(board)
self._rs_pin = rs
self._rw_pin = rw
self._enable_pin = enable
self._data_pins = [d0,d1,d2,d3,d4,d5,d6,d7]
if fourbitmode:
self._displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS
else:
self._displayfunction = LCD_8BITMODE | LCD_1LINE | LCD_5x8DOTS
self.begin(16, 1)
def begin(self, cols, lines, dotsize=LCD_5x8DOTS):
if lines > 1:
self._displayfunction |= LCD_2LINE
_numlines = lines
self.setRowOffsets(0x00, 0x40, 0x00 + cols, 0x40 + cols)
# for some 1 line displays you can select a 10 pixel high font
if dotsize != LCD_5x8DOTS and lines == 1:
self._displayfunction |= LCD_5x10DOTS
self.wrapper.pinMode(self._rs_pin, OUTPUT)
# we can save 1 pin by not using RW. Indicate by passing 255 instead of pin#
if self._rw_pin != 255:
self.wrapper.pinMode(self._rw_pin, OUTPUT)
self.wrapper.pinMode(self._enable_pin, OUTPUT)
npins = 8 if self._displayfunction & LCD_8BITMODE else 4
for i in range(npins):
self.wrapper.pinMode(self._data_pins[i], OUTPUT)
# SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
# according to datasheet, we need at least 40ms after power rises above 2.7V
# before sending commands. Arduino can turn on way before 4.5V so we'll wait 50
self.wrapper.delayMicroseconds(50000)
# Now we pull both RS and R/W low to begin commands
self.wrapper.digitalWrite(self._rs_pin, LOW)
self.wrapper.digitalWrite(self._enable_pin, LOW)
if self._rw_pin != 255:
self.wrapper.digitalWrite(self._rw_pin, LOW)
#put the LCD into 4 bit or 8 bit mode
if ~ (self._displayfunction & LCD_8BITMODE):
# this is according to the hitachi HD44780 datasheet
# figure 24, pg 46
# we start in 8bit mode, try to set 4 bit mode
self.write4bits(0x03)
self.wrapper.delayMicroseconds(4500) # wait min 4.1ms
# second try
self.write4bits(0x03)
self.wrapper.delayMicroseconds(4500) # wait min 4.1ms
# third go!
self.write4bits(0x03)
self.wrapper.delayMicroseconds(150)
# finally, set to 4-bit interface
self.write4bits(0x02)
else:
# this is according to the hitachi HD44780 datasheet
# page 45 figure 23
# Send function set command sequence
self.command(LCD_FUNCTIONSET | self._displayfunction)
self.wrapper.delayMicroseconds(4500) # wait more than 4.1ms
# second try
self.command(LCD_FUNCTIONSET | self._displayfunction)
self.wrapper.delayMicroseconds(150)
# third go
self.command(LCD_FUNCTIONSET | self._displayfunction)
# finally, set # lines, font size, etc.
self.command(LCD_FUNCTIONSET | self._displayfunction)
# turn the display on with no cursor or blinking default
self._displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF
self.display()
# clear it off
self.clear()
# Initialize to default text direction (for romance languages)
self._displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT
# set the entry mode
self.command(LCD_ENTRYMODESET | self._displaymode)
def setRowOffsets(self, row0, row1, row2, row3):
self._row_offsets = [row0, row1, row2, row3]
def clear(self):
# clear display, set cursor position to zero
self.command(LCD_CLEARDISPLAY)
# this command takes a long time!
self.wrapper.delayMicroseconds(2000)
def home(self):
# set cursor position to zero
self.command(LCD_RETURNHOME)
# this command takes a long time!
self.wrapper.delayMicroseconds(2000)
def setCursor(col, row):
max_lines = len(row_offsets) #XXX
if row >= max_lines:
row = max_lines - 1 # we count rows starting w/0
if row >= self._numlines:
row = self._numlines - 1 # we count rows starting w/0
self.command(LCD_SETDDRAMADDR | (col + self._row_offsets[row]))
def noDisplay(self):
"""Turn the display on/off (quickly)"""
self._displaycontrol &= ~LCD_DISPLAYON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
def display(self):
self._displaycontrol |= LCD_DISPLAYON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
def noCursor(self):
"""Turns the underline cursor off"""
self._displaycontrol &= ~LCD_CURSORON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
def cursor(self):
"""Turns the underline cursor on"""
self._displaycontrol |= LCD_CURSORON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
def noBlink(self):
"""Turns the underline cursor off"""
self._displaycontrol &= ~LCD_BLINKON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
def blink(self):
"""Turns the underline cursor on"""
self._displaycontrol |= LCD_BLINKON
self.command(LCD_DISPLAYCONTROL | self._displaycontrol)
# These commands scroll the display without changing the RAM
def scrollDisplayLeft(self):
self.command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT)
def scrollDisplayRight(self):
self.command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT)
def leftToRight(self):
"""This is for text that flows Left to Right"""
self._displaymode |= LCD_ENTRYLEFT
self.command(LCD_ENTRYMODESET | self._displaymode)
def rightToLeft(self):
"""This is for text that flows Right to Left"""
self._displaymode &= ~LCD_ENTRYLEFT
self.command(LCD_ENTRYMODESET | self._displaymode)
def autoscroll(self):
"""This will 'right justify' text from the cursor"""
self._displaymode |= LCD_ENTRYSHIFTINCREMENT
self.command(LCD_ENTRYMODESET | self._displaymode)
def noAutoscroll(self):
"""This will 'left justify' text from the cursor"""
self._displaymode &= ~LCD_ENTRYSHIFTINCREMENT
self.command(LCD_ENTRYMODESET | self._displaymode)
def createChar(location, charmap):
"""Allows us to fill the first 8 CGRAM locations with custom characters"""
location &= 0x7 # we only have 8 locations 0-7
self.command(LCD_SETCGRAMADDR | (location << 3))
for i in range(8):
self.write(charmap[i])
# mid level commands, for sending data/cmds
def command(self, value):
self.send(value, LOW)
def write(self, value):
self.send(value, HIGH)
return 1 # assume success
def write_string(self, s):
for c in s:
self.write(ord(c))
# low level data pushing commands
def send(self, value, mode):
"""write either command or data, with automatic 4/8-bit selection"""
self.wrapper.digitalWrite(self._rs_pin, mode)
# if there is a RW pin indicated, set it low to Write
if self._rw_pin != 255:
self.wrapper.digitalWrite(self._rw_pin, LOW)
if self._displayfunction & LCD_8BITMODE:
self.write8bits(value)
else:
self.write4bits(value>>4)
self.write4bits(value)
def pulseEnable(self):
self.wrapper.digitalWrite(self._enable_pin, LOW)
self.wrapper.delayMicroseconds(1);
self.wrapper.digitalWrite(self._enable_pin, HIGH)
self.wrapper.delayMicroseconds(1) # enable pulse must be >450ns
self.wrapper.digitalWrite(self._enable_pin, LOW)
self.wrapper.delayMicroseconds(100); # commands need > 37us to settle
def write4bits(self, value):
for i in range(4):
self.wrapper.digitalWrite(self._data_pins[i], (value >> i) & 0x1)
self.pulseEnable()
def write8bits(self, value):
for i in range(8):
self.wrapper.digitalWrite(self._data_pins[i], (value >> i) & 0x1)
self.pulseEnable()