forked from MTN-RowinAndruscavage/STEM4T-Display-ESP32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
menu.py
64 lines (52 loc) · 1.54 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
import utime, os, gc
import st7789
import tft_typeset
import button2
from machine import Pin, SPI
def btn1handler(pin):
ms.incr()
TD.clear()
TD.typesetlist(ms.listoptions(), font=tft_typeset.font2)
def btn2handler(pin):
TD.clear(TMOMAGENTA)
TD.typeset(ms.getselection(), font=tft_typeset.font2)
cleanupAndLaunch(ms.getselection())
btn1 = button2.Button2(0)
btn2 = button2.Button2(35)
btn1.setTapHandler(btn1handler)
btn2.setTapHandler(btn2handler)
class MenuSelector:
"""
List of python scripts to run
"""
def __init__(self, prefilter='_'):
self.selection=0
self.options=[o for o in os.listdir() if o.startswith(prefilter)]
def incr(self):
self.selection=(self.selection+1) % len(self.options)
def decr(self):
self.selection=(self.selection-1) % len(self.options)
def listoptions(self):
return self.options[self.selection:]
def getselection(self):
return self.options[self.selection]
ms = MenuSelector()
def cleanupAndLaunch(scriptfile):
print('Script: %s' % scriptfile)
print('mem_free %.0fkB' % (gc.mem_free() / 1024))
# # Destroy button handler objects
# btn1 = None
# btn2 = None
# btn1handler = None
# btn2handler = None
# ms = None
gc.collect()
exec(open(scriptfile).read())
def main():
TD.clear()
TD.typesetlist(ms.listoptions(), font=tft_typeset.font2)
while True:
btn1.loop()
btn2.loop()
utime.sleep(0.1)
main()