-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcd.py
162 lines (137 loc) · 3.73 KB
/
lcd.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
#!/usr/bin/python
# Copyright (c) 2015 Aaron Soto
# Released under the MIT license
# Incorporates libraries from AdaFruit, also released under the MIT license
import Adafruit_CharLCDPlate # to interface with the Adafruit i2c 16x2 RGB LCD Pi Plate
import os # to check UID for root
import sys # to exit with error status code
import time # to sleep while waiting for user action (i.e. awaiting button press)
import core
SELECT = 1
RIGHT = 2
DOWN = 4
UP = 8
LEFT = 16
ANY = 255
OFF = 0x00
RED = 0x01
GREEN = 0x02
BLUE = 0x04
YELLOW = RED + GREEN
TEAL = GREEN + BLUE
VIOLET = RED + BLUE
WHITE = RED + GREEN + BLUE
ON = RED + GREEN + BLUE
currentColor = ON
currentText = [' ',' ']
charLCD = None
def setColor(color):
charlcd.backlight(color)
currentColor = color
def setCharacter(row,col,char):
currentText[row] = currentText[row][:col] + char[0] + currentText[row][col:]
output()
def setString(row,col,string):
currentText[row] = currentText[row][:col] + string
output()
def setTitle(string):
currentText[0] = string
output()
def setContent(string):
if len(string)<=15:
currentText[1] = " " + string
else:
currentText[1] = " " + string[:14] + ">"
output()
def showError(string,redraw=True):
clear()
charlcd.backlight(RED)
charlcd.message("ERROR\n")
charlcd.message(" " + string)
if redraw:
waitForButton()
output()
def showMessage(string,length=2,color=None):
# Wipe screen and fill with provided message and color
clear()
if color:
charlcd.backlight(color)
charlcd.message(string)
time.sleep(length)
# Reset to previous screen
if color:
charlcd.backlight(currentColor)
clear()
output()
def off():
charlcd.clear()
setColor(OFF)
def clear(color=None):
# Wipes the screen and resets the color, but doesn't touch currentText
global currentColor
charlcd.clear()
charlcd.home()
if color:
setColor(color)
elif currentColor:
setColor(currentColor)
def redraw():
output()
def output():
clear()
charlcd.message(currentText[0])
charlcd.message("\n")
charlcd.message(currentText[1])
def waitForButton(maskList=ANY):
# Delay to wait for the user to release the previous button.
while charlcd.buttons() != 0:
time.sleep(0.05)
# Excellent. Now, let's just wait for someone to push a button.
currentButton = 0
while True:
time.sleep(0.05)
currentButton = charlcd.buttons()
if currentButton > 0:
if maskList == ANY: return currentButton
try:
if currentButton in maskList:
return currentButton
except TypeError:
if currentButton == maskList:
return currentButton
else:
# USER PRESSED THE WRONG BUTTON. Keep waiting
pass
def checkForButton(maskList=ANY):
return (charlcd.buttons() & maskList)
def menu(title, options, funcs=None):
optionNumber = 0
clear()
if funcs==None:
# This is the main menu.
main=True
else :
# This is a submenu.
main=False
options.append("<- BACK")
while True:
setTitle(title)
setContent(options[optionNumber])
button = waitForButton((ANY))
if button == DOWN:
if optionNumber < len(options)-1: optionNumber += 1
if button == UP:
if optionNumber > 0: optionNumber -= 1
if button == LEFT and not main:
return
if button == RIGHT or button == SELECT:
if main:
core.module_run(options[optionNumber])
elif not main and optionNumber == len(options)-1:
return
else:
funcs[optionNumber]()
if os.getuid() != 0:
print "MUST RUN AS ROOT!"
sys.exit(-1)
charlcd = Adafruit_CharLCDPlate.Adafruit_CharLCDPlate()