-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcd.py
More file actions
executable file
·215 lines (182 loc) · 4.38 KB
/
lcd.py
File metadata and controls
executable file
·215 lines (182 loc) · 4.38 KB
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
#!/usr/bin/python
import lcdHelper
import time
import signal
import sys
import Adafruit_CharLCD as LCD
def signal_handler(signal, frame):
global go
print('Exiting gracefully...')
lcdReset()
lcdOff()
go = False
def lcdToggle():
global screenOn
if screenOn:
lcdOff()
else:
lcdOn()
def showDone(myWait=0.2):
lcd.set_backlight(.5)
lcd.set_color(0, 0, 1)
time.sleep(myWait)
lcd.set_color(1, 1, 1)
lcd.set_backlight(1)
def showError(myWait=0.2):
lcd.set_backlight(.5)
lcd.set_color(1, 0, 0)
time.sleep(myWait)
lcd.set_color(1, 1, 1)
lcd.set_backlight(1)
def lcdOn():
global screenOn
screenOn = True
lcd.set_color(1, 1, 1)
lcd.enable_display(True)
lcd.set_backlight(1)
scrollLines()
def lcdOff():
global screenOn
screenOn = False
lcd.enable_display(False)
lcd.set_backlight(0)
def lcdReset():
lcd.clear()
lcd.set_cursor(0,0) # col, row
def showTopLine(myColPos, myLinePos):
global menu
lcd.set_cursor(0,0)
lcd.message(menu[myColPos][myLinePos][0])
def showBottLine(myColPos, myLinePos):
global menu, lastUpdateTime
lcd.set_cursor(0,1)
lcd.message(getMessage(menu[myColPos][myLinePos][1]))
lastUpdateTime = time.time()
def scrollLines():
global linePos, colPos
lcd.clear()
showTopLine(colPos,linePos)
showBottLine(colPos, linePos)
def buttonPressed(button):
global linePos, colPos, menu
if button == LCD.UP:
if linePos > 0:
linePos -=1
scrollLines()
else:
showDone()
elif button == LCD.DOWN:
if linePos < len(menu[colPos])-1:
linePos +=1
scrollLines()
else:
showDone()
elif button == LCD.LEFT:
if colPos > 0:
colPos -=1
linePos = 0
scrollLines()
else:
showDone()
elif button == LCD.RIGHT:
menu = lcdHelper.buildMenu()
if colPos < len(menu) - 1:
colPos +=1
linePos = 0
scrollLines()
else:
showDone()
elif button == LCD.SELECT:
executeAction(menu[colPos][linePos][2])
def getMessage(myAction):
print (myAction)
myAction = myAction.strip()
result = ""
if len(myAction) > 0:
myActions = myAction.split()
funcName = getattr(lcdHelper, myActions[0])
if len(myActions) == 1:
result = funcName()
else:
result = funcName(myActions[1])
if len(result) > 0:
result = "> " + result
spaces = 16 - len(result)
for i in range(0, spaces-1): result = result + " "
return result
def executeAction(myAction):
if len(myAction) > 0:
if confirm():
lcdOff()
funcName = getattr(lcdHelper, myAction)
result = funcName()
else:
scrollLines()
else:
showError()
return
def confirm():
global lastButton, lastButtonTime
lcd.set_cursor(0,1)
lcd.message(">> Are you sure?")
startTime = nowTime = time.time()
while startTime > nowTime -3:
for button in lcdHelper.BUTTONS:
nowTime = time.time()
if lcd.is_pressed(button):
if button != lastButton or nowTime > lastButtonTime + .2:
lastButton = button
lastButtonTime = nowTime
if button == LCD.SELECT:
lcd.set_cursor(0,1)
lcd.message(">> CONFIRMED ")
time.sleep(1)
return True
else:
lcd.set_cursor(0,1)
lcd.message(">> CANCELLED ")
showError(.5)
return False
lcd.set_cursor(0,1)
lcd.message(">> TIME UP ")
lastButtonTime = nowTime
showError(1)
return False
############### START HERE ###############
# the LCD object
lcd = LCD.Adafruit_CharLCDPlate()
# these are our position in the menu array
menu = lcdHelper.buildMenu()
linePos = 0
colPos = 0
screenOn = False
# the last button pnd time it was pressed - for debouncing
lastButton = -1
lastButtonTime = time.time()
# last time the bottom line was refreshed
lastUpdateTime = time.time()
# listen for SIGINT
signal.signal(signal.SIGINT, signal_handler)
go = True #this will be set to alse if we receive a sig
# make sure LCD is cleared up
lcdReset()
lcdOn()
while True == go:
timeNow = time.time()
# if more than a second since last update - refresh
if screenOn and timeNow - lastUpdateTime > 1 and menu[colPos][linePos][3] :
showBottLine(colPos,linePos)
# Loop through each button and check if it is pressed.
for button in lcdHelper.BUTTONS:
if lcd.is_pressed(button):
if not screenOn: lcdOn()
else:
if button != lastButton or timeNow > lastButtonTime + .2:
lastButton = button
buttonPressed(button)
else: pass
lastButtonTime = timeNow
time.sleep(0.1)
# put display to sleep if 10 seconds passed since button pressed
if screenOn and timeNow > lastButtonTime + 10: lcdOff()
sys.exit(0)