forked from sleeepyjack/RPi_AdafruitLCD_Menu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMenu.py
218 lines (196 loc) · 5.6 KB
/
Menu.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
#Daniel Juenger, github.com/sleeepyjack
import Adafruit_CharLCD as LCD
from time import sleep
import commands
import psutil
class Menu():
menu = list()
top = 0
sub = 0
count = 1000
element = None
isInterrupted = False
isOn = True
isOnCount = 0
stepScroll = 0
def topElement(self, name, type, content):
subList = list()
sEl = self.subElement(name,type,content)
subList.append(sEl)
return {
"Name" : name,
"Sub" : subList,
"Type" : type,
"Content" : content}
def subElement(self, name, type, content):
return {
"Name" : name,
"Type" : type,
"Content" : content}
def buttonPressed(self, lcd):
boo = False
if (lcd.is_pressed(LCD.SELECT) | lcd.is_pressed(LCD.UP) | lcd.is_pressed(LCD.DOWN) | lcd.is_pressed(LCD.LEFT) | lcd.is_pressed(LCD.RIGHT)):
boo = True
return boo
def clearMenuRight(self, lcd):
j = 0
while(j < 16):
lcd.move_right()
sleep(.03)
j += 1
def clearMenuLeft(self, lcd):
i = 0
while(i < 16):
lcd.move_left()
sleep(.03)
i += 1
def returnToTopElement(self):
global element
self.sub = 0
self.element = self.menu[self.top]
def firstTopElement(self):
global element
self.top = 0
self.sub = 0
self.element = self.menu[self.top]
return self.element
def addTopElement(self, topEl):
if not topEl in self.menu:
self.menu.append(topEl)
def addSubElement(self, topEl, subEl):
if not subEl in topEl["Sub"]:
topEl["Sub"].append(subEl)
def returnElement():
global element
return self.element
def scroll(self, lcd, msg):
global count
global stepScroll
if len(msg) > 16:
self.stepScroll = len(msg) - 16
if self.count <= 10 & self.stepScroll <= 0:
lcd.scrollDisplayLeft()
self.stepScroll -= 1
if self.count > 10:
print "la"
def nextTopElement(self, lcd):
global element
global count
self.clearMenuLeft(lcd)
self.count = 1000
if len(self.menu) > 0:
self.top = (self.top + 1) % len(self.menu)
self.sub = 0
self.element = self.menu[self.top]
self.handleMenu(lcd)
def prevTopElement(self, lcd):
global element
global count
self.clearMenuRight(lcd)
self.count = 1000
if len(self.menu) > 0:
self.top -= 1
self.sub = 0
if self.top < 0:
self.top = len(self.menu)-1
self.element = self.menu[self.top]
self.handleMenu(lcd)
def nextSubElement(self, lcd):
global element
global count
topEl = self.menu[self.top]
self.count = 1000
if (len(topEl["Sub"]) > 0):
self.sub += 1
if (self.sub >= len(topEl["Sub"])):
self.sub = 0
self.element = topEl["Sub"][self.sub]
self.handleMenu(lcd)
def prevSubElement(self, lcd):
global element
global count
topEl = self.menu[self.top]
self.count = 1000
if (len(topEl["Sub"]) > 0):
self.sub -= 1
if (self.sub < 0):
self.sub = len(topEl["Sub"])-1
self.element = topEl["Sub"][self.sub]
self.handleMenu(lcd)
def handleMenu(self,lcd):
global element
global count
global isOn
msg = ""
if (self.count > 10) & self.isOn:
if self.element["Type"] == "STRING":
msg = self.element["Content"]
elif self.element["Type"] == "PYTHON":
msg = str(eval(self.element["Content"]))
elif self.element["Type"] == "BASH":
msg = commands.getoutput(self.element["Content"])
self.count = 0
lcd.clear()
lcd.message(self.element["Name"]+"\n"+msg)
self.scroll(lcd, msg)
self.count += 1
def startMenu(self, lcd):
global isInterrupted
global isOn
global isOnCount
lcd.clear()
lcd.set_color(0,1,1)
selfisOnCount = 0
self.firstTopElement()
self.handleMenu(lcd)
self.isOn = True
self.isOnCount = 0
self.isInterrupted = False
while not self.isInterrupted:
try:
if self.isOn == True:
if lcd.is_pressed(LCD.RIGHT):
self.nextTopElement(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(LCD.LEFT):
self.prevTopElement(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(LCD.DOWN):
self.nextSubElement(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(LCD.UP):
self.prevSubElement(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(LCD.SELECT):
self.returnToTopElement()
self.isOnCount = 0
print "s"
sleep(.3)
if self.isOnCount > 100:
lcd.set_backlight(0.0)
lcd.enable_display(False)
self.isOnCount = 0
self.isOn = False
sleep(.3)
else:
if self.buttonPressed(lcd):
lcd.enable_display(True)
lcd.set_color(0,1,1)
self.isOnCount = 0
self.isOn = True
self.handleMenu(lcd)
sleep(.3)
self.handleMenu(lcd)
sleep(.1)
self.isOnCount += 1
except KeyboardInterrupt:
self.stopMenu(lcd)
def stopMenu(self, lcd):
global isInterrupted
lcd.set_backlight(0.0)
lcd.enable_display(False)
self.isInterrupted = True