-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
185 lines (176 loc) · 6.48 KB
/
app.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
from machine import reset
from network import WLAN, STA_IF
from time import sleep, localtime, time
from lcd import LCD
from font import cntr_st
from gc import collect, mem_free
from env import HOSTNAME, SSID, WIFI_PASSWORD, SCREENS
from _thread import start_new_thread
from ntptime import settime
from screens import *
class App:
def __init__(self) -> None:
if (len(SCREENS) == 0):
print("No screens configured. Check env.py.")
self.scr_n = -1
else: self.scr_n = 0
self.lcd = LCD()
self.last_cng = 0
self.must_draw = False
def __connect(self) -> int:
wlan = WLAN(STA_IF)
wlan.active(True)
wlan.config(hostname=HOSTNAME)
wlan.connect(SSID, WIFI_PASSWORD)
while wlan.isconnected() == False:
print('Waiting for connection...')
sleep(1)
self.ip = wlan.ifconfig()[0]
print(f'Connected on {self.ip}')
return self.ip
def __boot(self) -> None:
collect()
print("Booting")
self.lcd.fill(0x0000)
cntr_st(self.lcd, self.lcd.width, "Booting...", 120, 2, 150, 150, 150)
self.lcd.show()
self.__connect()
try:
settime()
print("Local time after synchronization: %s" %str(localtime()))
except:
pass
def __resetButtonStatuses(self) -> None:
if (self.lcd.keyA["v"]): self.lcd.keyA["v"] = False
if (self.lcd.keyB["v"]): self.lcd.keyB["v"] = False
if (self.lcd.keyX["v"]): self.lcd.keyX["v"] = False
if (self.lcd.keyY["v"]): self.lcd.keyY["v"] = False
if (self.lcd.left["v"]): self.lcd.left["v"] = False
if (self.lcd.right["v"]): self.lcd.right["v"] = False
if (self.lcd.up["v"]): self.lcd.up["v"] = False
if (self.lcd.down["v"]): self.lcd.down["v"] = False
if (self.lcd.ctrl["v"]): self.lcd.ctrl["v"] = False
def __manageButtons(self) -> None:
while (True):
a = False
if (self.lcd.keyA["p"].value() == 0):
self.lcd.keyA["v"] = True
a = True
if (self.lcd.keyB["p"].value() == 0):
self.lcd.keyB["v"] = True
a = True
if (self.lcd.keyX["p"].value() == 0):
self.lcd.keyX["v"] = True
a = True
if (self.lcd.keyY["p"].value() == 0):
self.lcd.keyY["v"] = True
a = True
if (self.lcd.right["p"].value() == 0):
self.lcd.right["v"] = True
a = True
if (self.lcd.left["p"].value() == 0):
self.lcd.left["v"] = True
a = True
if (self.lcd.up["p"].value() == 0):
self.lcd.up["v"] = True
a = True
if (self.lcd.down["p"].value() == 0):
self.lcd.down["v"] = True
a = True
if (self.lcd.ctrl["p"].value() == 0):
self.lcd.ctrl["v"] = True
a = True
if (a):
sleep(0.2)
def __changeScreen(self) -> bool:
c = False
if (self.lcd.right["v"]):
c = True
if (self.scr_n == len(SCREENS) - 1):
self.scr_n = 0
else:
self.scr_n += 1
elif (self.lcd.left["v"]):
c = True
if (self.scr_n == 0):
self.scr_n = len(SCREENS) - 1
else:
self.scr_n -= 1
self.lcd.left["v"] = False
self.lcd.right["v"] = False
if (c): self.last_cng = time()
return c
def handleButtons(self, o: bool = True) -> bool:
up = self.lcd.up["v"]
down = self.lcd.down["v"]
left = self.lcd.left["v"]
right = self.lcd.right["v"]
ctrl = self.lcd.ctrl["v"]
keyA = self.lcd.keyA["v"]
keyB = self.lcd.keyB["v"]
keyX = self.lcd.keyX["v"]
keyY = self.lcd.keyY["v"]
self.__resetButtonStatuses()
a = up or down or left or right or keyA or keyB or keyX or keyY or ctrl
if (a): self.last_cng = time()
if (o):
c = self.s.handleButtons(up, down, left, right, keyA, keyB, keyX, keyY, ctrl)
return c
return a
def __manageScreen(self) -> None:
started = False
active = True
self.last_cng = time()
while (True):
collect()
if (localtime()[3] == 0):
try:
settime()
except:
pass
if (active and time() - self.last_cng > 15):
LCD.setDuty(0)
active = False
elif (active):
if (self.lcd.ctrl["v"]):
LCD.setDuty(0)
active = False
self.lcd.ctrl["v"] = False
continue
changed = not started or self.__changeScreen()
# if the screen has changed, redraw the whole screen
if (changed):
self.last_cng = time()
if (not started): started = True
self.__resetButtonStatuses()
collect()
if (SCREENS[self.scr_n]["type"] == 0):
self.s = LightsScreen(SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entities"])
elif (SCREENS[self.scr_n]["type"] == 1):
self.s = MediaScreen(SCREENS[self.scr_n]["name"], SCREENS[self.scr_n]["entity"])
else:
self.s = UnknownScreen("Unknown")
self.must_draw = self.s.display(self.lcd)
# otherwise minimise the number of pixels being changed
else:
b = self.handleButtons()
if (b):
self.last_cng = time()
continue
if (self.must_draw):
self.must_draw = self.s.display(self.lcd)
else:
self.must_draw = self.s.update(self.lcd)
else:
sleep(0.2)
if (self.handleButtons(False)):
LCD.setDuty()
active = True
def run(self) -> None:
if (self.scr_n == None): return
self.__boot()
try:
start_new_thread(self.__manageButtons, ())
self.__manageScreen()
except KeyboardInterrupt:
reset()