-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
305 lines (275 loc) · 10 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
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
import time
import app
from app_components import Menu, Notification, clear_background
from typing import Literal
from app_components.tokens import clear_background, set_color
import settings
from .effects import Effects
from .palettes import Palettes
main_menu_items = [
"Power",
"Speed",
"Brightness",
"Effects",
"Palette",
"Main Slot",
"Mirror Slot",
"About",
]
power_menu_items = ["On", "Off"]
from events.input import Buttons, BUTTON_TYPES
from tildagonos import tildagonos
class EEHNeoPixelLogo(app.App):
def __init__(self):
# Menu setup
self.menu = None
self.current_menu = None
self.set_menu("main")
self.show_palette = False
# Notification setup
self.notification = None
# Button setup
self.button_states = Buttons(self)
# Main app setup
self.power = True
self.effects = Effects()
self.palettes = Palettes()
def set_slot(self, slot):
slot = int(slot)
settings.set("eeh.slot", slot)
settings.save()
self.effects.clear_leds()
self.effects.init_chain()
def set_slot2(self, slot):
if slot != "None":
slot = int(slot)
settings.set("eeh.slot2", slot)
settings.save()
self.effects.clear_leds()
self.effects.init_chain()
def back_handler(self):
# If in the topmost menu, minimize, otherwise move one menu up.
if self.current_menu == "main":
self.minimise()
else:
if self.current_menu == "Palette":
self.show_palette = True
else:
self.show_palette = False
self.set_menu("main")
self.effects.set_speed(None, 1)
self.effects.set_effect(None, 1)
self.effects.set_palette(None, 1)
def select_handler(self, item, idx):
# If Power or Preset item selected enter that menu
if item in main_menu_items:
self.set_menu(item)
else:
if self.current_menu == "Power":
if item == "On":
self.notification = Notification("Power On")
self.power = True
self.set_menu("main")
elif item == "Off":
self.notification = Notification("Power Off")
self.power = False
self.effects.set_led_all((0, 0, 0))
self.set_menu("main")
elif self.current_menu == "Speed":
if item in self.effects.get_speeds():
self.notification = Notification("Speed=" + item)
self.effects.set_speed(item)
self.set_menu("main")
elif self.current_menu == "Brightness":
if item in self.effects.get_brightnesses():
self.notification = Notification("Brightness=" + item)
self.effects.set_brightness(item)
self.set_menu("main")
elif self.current_menu == "Effects":
if item in self.effects.get_effect_list():
self.notification = Notification(item)
self.effects.set_effect(item)
self.set_menu("main")
elif self.current_menu == "Palette":
if item in self.palettes.get_palette_list():
self.notification = Notification(item)
self.effects.set_palette(item)
self.set_menu("main")
elif self.current_menu == "Main Slot":
if item in ["1", "2", "3", "4", "5", "6"]:
self.notification = Notification("Slot=" + item)
self.set_slot(item)
self.set_menu("main")
elif self.current_menu == "Mirror Slot":
if item in ["None", "1", "2", "3", "4", "5", "6"]:
self.notification = Notification("Slot=" + item)
self.set_slot2(item)
self.set_menu("main")
else:
self.notification = Notification(self.current_menu + "." + item + '"!')
def change_handler(self, item):
if self.current_menu == "main":
if item == "Palette":
self.show_palette = True
else:
self.show_palette = False
elif self.current_menu == "Speed":
if item in self.effects.get_speeds():
self.effects.set_speed(item, 1)
elif self.current_menu == "Brightness":
if item in self.effects.get_brightnesses():
self.effects.set_brightness(item, 1)
elif self.current_menu == "Effects":
if item in self.effects.get_effect_list():
self.effects.set_effect(item, 1)
elif self.current_menu == "Palette":
self.show_palette = True
if item in self.palettes.get_palette_list():
self.effects.set_palette(item, 1)
def set_menu(
self,
menu_name: Literal[
"main",
"Power",
"Speed",
"Brightness",
"Effects",
"Palette",
"Main Slot",
"Mirror Slot",
"About",
],
):
if self.menu:
self.menu._cleanup()
if self.current_menu:
previous_menu = self.current_menu
else:
previous_menu = "Power"
self.current_menu = menu_name
if menu_name == "main":
self.menu = Menu(
self,
main_menu_items,
select_handler=self.select_handler,
change_handler=self.change_handler,
back_handler=self.back_handler,
position=(
[
"Power",
"Speed",
"Brightness",
"Effects",
"Palette",
"Main Slot",
"Mirror Slot",
"About",
]
).index(previous_menu),
)
elif menu_name == "Power":
self.menu = Menu(
self,
power_menu_items,
select_handler=self.select_handler,
back_handler=self.back_handler,
position=0 if self.power else 1,
)
elif menu_name == "Speed":
self.menu = Menu(
self,
self.effects.get_speeds(),
select_handler=self.select_handler,
change_handler=self.change_handler,
back_handler=self.back_handler,
position=self.effects.get_speeds().index(str(self.effects.get_speed())),
)
elif menu_name == "Brightness":
self.menu = Menu(
self,
self.effects.get_brightnesses(),
select_handler=self.select_handler,
change_handler=self.change_handler,
back_handler=self.back_handler,
position=self.effects.get_brightnesses().index(
str(self.effects.get_brightness())
),
)
elif menu_name == "Effects":
self.menu = Menu(
self,
self.effects.get_effect_list(),
select_handler=self.select_handler,
change_handler=self.change_handler,
back_handler=self.back_handler,
position=self.effects.get_effect_list().index(
self.effects.get_effect()
),
)
elif menu_name == "Palette":
self.show_palette = True
self.menu = Menu(
self,
self.palettes.get_palette_list(),
select_handler=self.select_handler,
change_handler=self.change_handler,
back_handler=self.back_handler,
position=self.palettes.get_palette_list().index(
self.effects.get_palette()
),
)
elif menu_name == "Main Slot":
self.menu = Menu(
self,
["1", "2", "3", "4", "5", "6"],
select_handler=self.select_handler,
back_handler=self.back_handler,
position=settings.get("eeh.slot", 1) - 1,
)
elif menu_name == "Mirror Slot":
pos = settings.get("eeh.slot2", "None")
if pos == "None":
pos = 0
else:
pos = pos + 1
self.menu = Menu(
self,
["None", "1", "2", "3", "4", "5", "6"],
select_handler=self.select_handler,
back_handler=self.back_handler,
position=pos,
)
elif menu_name == "About":
self.menu = Menu(
self,
[
"Version: 0.2.1",
"",
"Essex Hackspace",
"NeoPixel Hexpansion",
"eehack.space",
"",
"Next available in",
"2026!",
],
back_handler=self.back_handler,
)
def draw(self, ctx):
clear_background(ctx)
if self.show_palette:
shape_colour = self.palettes.get_palette(self.effects.get_palette())
ctx.linear_gradient(-100, -35, 100, -35)
for key, value in shape_colour.items():
ctx.add_stop(key / 256, value, 1)
ctx.rectangle(-100, -35, 200, 10).fill()
self.menu.draw(ctx)
if self.notification:
self.notification.draw(ctx)
def update(self, delta):
self.menu.update(delta)
if self.notification:
self.notification.update(delta)
def background_update(self, delta):
if self.power == True:
self.effects.cycle()
__app_export__ = EEHNeoPixelLogo