-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlcdmenu.py
More file actions
executable file
·348 lines (308 loc) · 10.7 KB
/
Copy pathlcdmenu.py
File metadata and controls
executable file
·348 lines (308 loc) · 10.7 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
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
#!/usr/bin/python
#
# Created by Alan Aufderheide, February 2013
#
# This provides a menu driven application using the LCD Plates
# from Adafruit Electronics.
#
# Modified by Phill W.J. Rogers for stfx project.
import commands
from string import split
from time import sleep, strftime, localtime
from xml.dom.minidom import *
from Adafruit_I2C import Adafruit_I2C
from Adafruit_MCP230xx import Adafruit_MCP230XX
from Adafruit_CharLCDPlate import Adafruit_CharLCDPlate
from ListSelector import ListSelector
import smbus
configfile = 'lcdmenu.xml'
# set DEBUG=1 for print debug statements
DEBUG = 0
DISPLAY_ROWS = 2
DISPLAY_COLS = 16
# set busnum param to the correct value for your pi
# lcd = Adafruit_CharLCDPlate(busnum = 0)
lcd = Adafruit_CharLCDPlate()
# in case you add custom logic to lcd to check if it is connected (useful)
#if lcd.connected == 0:
# quit()
lcd.begin(DISPLAY_COLS, DISPLAY_ROWS)
lcd.backlight(lcd.OFF)
import subprocess
# commands
def StartSTFX():
p=subprocess.Popen(['/home/pi/stfx/stfx.sh'])
def StopSTFX():
commands.getoutput(" sudo kill -9 $(ps -ef | awk '/stfx[.]sh/{print $2}') ")
def BGtest():
p=subprocess.Popen(['/home/pi/stfx/lcdmenu-test.sh','--opt1','arg1'])
def DoQuit():
lcd.clear()
lcd.message('Are you sure?\nPress Sel for Y')
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
if lcd.buttonPressed(lcd.SELECT):
lcd.clear()
lcd.backlight(lcd.OFF)
quit()
sleep(0.25)
def DoShutdown():
lcd.clear()
lcd.message('Are you sure?\nPress Sel for Y')
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
if lcd.buttonPressed(lcd.SELECT):
lcd.clear()
lcd.backlight(lcd.OFF)
commands.getoutput("sudo shutdown -h now")
quit()
sleep(0.25)
def LcdOff():
lcd.backlight(lcd.OFF)
def LcdOn():
lcd.backlight(lcd.ON)
def ShowDateTime():
if DEBUG:
print('in ShowDateTime')
lcd.clear()
while not(lcd.buttonPressed(lcd.LEFT)):
sleep(0.25)
lcd.home()
lcd.message(strftime('%a %b %d %Y\n%I:%M:%S %p', localtime()))
def ShowIPAddress():
if DEBUG:
print('in ShowIPAddress')
lcd.clear()
lcd.message(commands.getoutput("/sbin/ifconfig").split("\n")[1].split()[1][5:])
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
sleep(0.25)
#only use the following if you find useful
def Use10Network():
"Allows you to switch to a different network for local connection"
lcd.clear()
lcd.message('Are you sure?\nPress Sel for Y')
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
if lcd.buttonPressed(lcd.SELECT):
# uncomment the following once you have a separate network defined
#commands.getoutput("sudo cp /etc/network/interfaces.hub.10 /etc/network/interfaces")
lcd.clear()
lcd.message('Please reboot')
sleep(1.5)
break
sleep(0.25)
#only use the following if you find useful
def UseDHCP():
"Allows you to switch to a network config that uses DHCP"
lcd.clear()
lcd.message('Are you sure?\nPress Sel for Y')
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
if lcd.buttonPressed(lcd.SELECT):
# uncomment the following once you get an original copy in place
#commands.getoutput("sudo cp /etc/network/interfaces.orig /etc/network/interfaces")
lcd.clear()
lcd.message('Please reboot')
sleep(1.5)
break
sleep(0.25)
# commands
class CommandToRun:
def __init__(self, myName, theCommand):
self.text = myName
self.commandToRun = theCommand
def Run(self):
self.clist = split(commands.getoutput(self.commandToRun), '\n')
if len(self.clist) > 0:
lcd.clear()
lcd.message(self.clist[0])
for i in range(1, len(self.clist)):
while 1:
if lcd.buttonPressed(lcd.DOWN):
break
sleep(0.25)
lcd.clear()
lcd.message(self.clist[i-1]+'\n'+self.clist[i])
sleep(0.5)
while 1:
if lcd.buttonPressed(lcd.LEFT):
break
class Widget:
def __init__(self, myName, myFunction):
self.text = myName
self.function = myFunction
class Folder:
def __init__(self, myName, myParent):
self.text = myName
self.items = []
self.parent = myParent
def HandleSettings(node):
global lcd
if node.getAttribute('lcdColor').lower() == 'red':
lcd.backlight(lcd.RED)
elif node.getAttribute('lcdColor').lower() == 'green':
lcd.backlight(lcd.GREEN)
elif node.getAttribute('lcdColor').lower() == 'blue':
lcd.backlight(lcd.BLUE)
elif node.getAttribute('lcdColor').lower() == 'yellow':
lcd.backlight(lcd.YELLOW)
elif node.getAttribute('lcdColor').lower() == 'teal':
lcd.backlight(lcd.TEAL)
elif node.getAttribute('lcdColor').lower() == 'violet':
lcd.backlight(lcd.VIOLET)
elif node.getAttribute('lcdColor').lower() == 'white':
lcd.backlight(lcd.ON)
if node.getAttribute('lcdBacklight').lower() == 'on':
lcd.backlight(lcd.ON)
elif node.getAttribute('lcdBacklight').lower() == 'off':
lcd.backlight(lcd.OFF)
def ProcessNode(currentNode, currentItem):
children = currentNode.childNodes
for child in children:
if isinstance(child, xml.dom.minidom.Element):
if child.tagName == 'settings':
HandleSettings(child)
elif child.tagName == 'folder':
thisFolder = Folder(child.getAttribute('text'), currentItem)
currentItem.items.append(thisFolder)
ProcessNode(child, thisFolder)
elif child.tagName == 'widget':
thisWidget = Widget(child.getAttribute('text'), child.getAttribute('function'))
currentItem.items.append(thisWidget)
elif child.tagName == 'run':
thisCommand = CommandToRun(child.getAttribute('text'), child.firstChild.data)
currentItem.items.append(thisCommand)
class Display:
def __init__(self, folder):
self.curFolder = folder
self.curTopItem = 0
self.curSelectedItem = 0
def display(self):
if self.curTopItem > len(self.curFolder.items) - DISPLAY_ROWS:
self.curTopItem = len(self.curFolder.items) - DISPLAY_ROWS
if self.curTopItem < 0:
self.curTopItem = 0
if DEBUG:
print('------------------')
str = ''
for row in range(self.curTopItem, self.curTopItem+DISPLAY_ROWS):
if row > self.curTopItem:
str += '\n'
if row < len(self.curFolder.items):
if row == self.curSelectedItem:
cmd = '-'+self.curFolder.items[row].text
if len(cmd) < 16:
for row in range(len(cmd), 16):
cmd += ' '
if DEBUG:
print('|'+cmd+'|')
str += cmd
else:
cmd = ' '+self.curFolder.items[row].text
if len(cmd) < 16:
for row in range(len(cmd), 16):
cmd += ' '
if DEBUG:
print('|'+cmd+'|')
str += cmd
if DEBUG:
print('------------------')
lcd.home()
lcd.message(str)
def update(self, command):
if DEBUG:
print('do',command)
if command == 'u':
self.up()
elif command == 'd':
self.down()
elif command == 'r':
self.right()
elif command == 'l':
self.left()
elif command == 's':
self.select()
def up(self):
if self.curSelectedItem == 0:
return
elif self.curSelectedItem > self.curTopItem:
self.curSelectedItem -= 1
else:
self.curTopItem -= 1
self.curSelectedItem -= 1
def down(self):
if self.curSelectedItem+1 == len(self.curFolder.items):
return
elif self.curSelectedItem < self.curTopItem+DISPLAY_ROWS-1:
self.curSelectedItem += 1
else:
self.curTopItem += 1
self.curSelectedItem += 1
def left(self):
if isinstance(self.curFolder.parent, Folder):
# find the current in the parent
itemno = 0
index = 0
for item in self.curFolder.parent.items:
if self.curFolder == item:
if DEBUG:
print('foundit')
index = itemno
else:
itemno += 1
if index < len(self.curFolder.parent.items):
self.curFolder = self.curFolder.parent
self.curTopItem = index
self.curSelectedItem = index
else:
self.curFolder = self.curFolder.parent
self.curTopItem = 0
self.curSelectedItem = 0
def right(self):
if isinstance(self.curFolder.items[self.curSelectedItem], Folder):
self.curFolder = self.curFolder.items[self.curSelectedItem]
self.curTopItem = 0
self.curSelectedItem = 0
elif isinstance(self.curFolder.items[self.curSelectedItem], Widget):
if DEBUG:
print('eval', self.curFolder.items[self.curSelectedItem].function)
eval(self.curFolder.items[self.curSelectedItem].function+'()')
elif isinstance(self.curFolder.items[self.curSelectedItem], CommandToRun):
self.curFolder.items[self.curSelectedItem].Run()
def select(self):
if DEBUG:
print('check widget')
if isinstance(self.curFolder.items[self.curSelectedItem], Widget):
if DEBUG:
print('eval', self.curFolder.items[self.curSelectedItem].function)
eval(self.curFolder.items[self.curSelectedItem].function+'()')
# now start things up
uiItems = Folder('root','')
dom = parse(configfile) # parse an XML file by name
top = dom.documentElement
ProcessNode(top, uiItems)
display = Display(uiItems)
display.display()
while 1:
if (lcd.buttonPressed(lcd.LEFT)):
display.update('l')
display.display()
if (lcd.buttonPressed(lcd.UP)):
display.update('u')
display.display()
if (lcd.buttonPressed(lcd.DOWN)):
display.update('d')
display.display()
if (lcd.buttonPressed(lcd.RIGHT)):
display.update('r')
display.display()
if (lcd.buttonPressed(lcd.SELECT)):
display.update('s')
display.display()
sleep(0.20)