-
Notifications
You must be signed in to change notification settings - Fork 18
/
Menu.py
285 lines (263 loc) · 7.71 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
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
# coding=utf-8
# Daniel Juenger, github.com/sleeepyjack
# updated to 3.x - James L. Key
import subprocess
from time import sleep
import Adafruit_CharLCD as Lcd
class Menu:
menu = list()
top = 0
sub = 0
count = 1000
element = None
isInterrupted = False
isOn = True
isOnCount = 0
stepScroll = 0
def top_element(self, name, element_type, content):
"""
:param name:
:param element_type:
:param content:
:return List {Name: Sub: Element_type: Content}:
"""
sublist = list()
subelement = self.sub_element(name, element_type, content)
sublist.append(subelement)
return {
"Name": name,
"Sub": sublist,
"Type": element_type,
"Content": content}
@staticmethod
def sub_element(name, element_type, content):
"""
:param name:
:param element_type:
:param content:
:return: List {Name: Element_type: Content}:
"""
return {
"Name": name,
"Type": element_type,
"Content": content}
@staticmethod
def button_pressed(lcd):
"""
:param lcd:
:return: Button Presses Boolean
"""
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
@staticmethod
def clear_menu_right(lcd):
"""
:param lcd:
"""
j = 0
while j < 16:
lcd.move_right()
sleep(.03)
j += 1
@staticmethod
def clear_menu_left(lcd):
"""
:param lcd:
"""
i = 0
while i < 16:
lcd.move_left()
sleep(.03)
i += 1
def return_to_top_element(self):
global element
self.sub = 0
self.element = self.menu[self.top]
def first_top_element(self):
"""
:return: Element
"""
global element
self.top = 0
self.sub = 0
self.element = self.menu[self.top]
return self.element
def add_top_element(self, top_element):
"""
:param top_element:
"""
if top_element not in self.menu:
self.menu.append(top_element)
@staticmethod
def add_sub_element(top_element, sub_element):
"""
:param top_element:
:param sub_element:
"""
if sub_element not in top_element["Sub"]:
top_element["Sub"].append(sub_element)
def return_element(self):
"""
:return: Element
"""
global element
return self.element
def scroll(self, lcd, msg):
"""
:param lcd:
:param 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 next_top_element(self, lcd):
"""
:param lcd:
"""
global element
global count
self.clear_menu_left(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.handle_menu(lcd)
def prev_top_element(self, lcd):
"""
:param lcd:
"""
global element
global count
self.clear_menu_right(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.handle_menu(lcd)
def next_sub_element(self, lcd):
"""
:param lcd:
"""
global element
global count
top_el = self.menu[self.top]
self.count = 1000
if len(top_el["Sub"]) > 0:
self.sub += 1
if self.sub >= len(top_el["Sub"]):
self.sub = 0
self.element = top_el["Sub"][self.sub]
self.handle_menu(lcd)
def prev_sub_element(self, lcd):
"""
:param lcd:
"""
global element
global count
top_el = self.menu[self.top]
self.count = 1000
if len(top_el["Sub"]) > 0:
self.sub -= 1
if self.sub < 0:
self.sub = len(top_el["Sub"]) - 1
self.element = top_el["Sub"][self.sub]
self.handle_menu(lcd)
def handle_menu(self, lcd):
"""
:param 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 = subprocess.getoutput(self.element["Content"])
self.count = 0
lcd.clear()
lcd.message(self.element["Name"] + "\n" + msg)
self.scroll(lcd, msg)
self.count += 1
def start_menu(self, lcd):
"""
:param lcd:
"""
global isInterrupted
global isOn
global isOnCount
lcd.clear()
lcd.set_color(0, 1, 1)
self.isOnCount = 0
self.first_top_element()
self.handle_menu(lcd)
self.isOn = True
self.isOnCount = 0
self.isInterrupted = False
while not self.isInterrupted:
try:
if self.isOn:
if lcd.is_pressed(Lcd.RIGHT):
self.next_top_element(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(Lcd.LEFT):
self.prev_top_element(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(Lcd.DOWN):
self.next_sub_element(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(Lcd.UP):
self.prev_sub_element(lcd)
self.isOnCount = 0
sleep(.3)
if lcd.is_pressed(Lcd.SELECT):
self.return_to_top_element()
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.button_pressed(lcd):
lcd.enable_display(True)
lcd.set_color(0, 1, 1)
self.isOnCount = 0
self.isOn = True
self.handle_menu(lcd)
sleep(.3)
self.handle_menu(lcd)
sleep(.1)
self.isOnCount += 1
except KeyboardInterrupt:
self.stop_menu(lcd)
def stop_menu(self, lcd):
"""
:param lcd:
"""
global isInterrupted
lcd.set_backlight(0.0)
lcd.enable_display(False)
self.isInterrupted = True