-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1404 lines (1255 loc) · 52.6 KB
/
Copy pathmain.py
File metadata and controls
1404 lines (1255 loc) · 52.6 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import gc
import random
import time
import micropython
from ir_remote_mapper import IRActionReader
from lib.switches import (
CMD_UP, CMD_UP_L, CMD_DOWN, CMD_DOWN_L, CMD_LEFT, CMD_LEFT_L,
CMD_RIGHT, CMD_RIGHT_L, CMD_CONFIRM, CMD_CONFIRM_L, CMD_L1, CMD_L1_L,
CMD_L2, CMD_L2_L
)
_BUTTON_TO_ACTION = {
CMD_CONFIRM: "NEXT_PARAM",
CMD_CONFIRM_L: "TOGGLE_EDIT_MODE",
CMD_UP: "PARAM_UP",
CMD_UP_L: "PARAM_UP_LONG",
CMD_DOWN: "PARAM_DOWN",
CMD_DOWN_L: "PARAM_DOWN_LONG",
CMD_LEFT: "PREV_EFFECT",
CMD_LEFT_L: "PREV_FAVORITE",
CMD_RIGHT: "NEXT_EFFECT",
CMD_RIGHT_L: "NEXT_FAVORITE",
CMD_L1: "LED1_MODE",
CMD_L1_L: "LED1_POWER",
CMD_L2: "LED2_MODE",
CMD_L2_L: "LED2_POWER",
}
# ---- SETTINGS ----
TARGET_FPS = 25
FRAME_MIN_TIME = 1000 // TARGET_FPS
OVERLAY_FRAME_MIN_TIME = 50
UI_MESSAGE_MS = 10000
TOGGLE_COOLDOWN_MS = 600
AUTO_INTERVAL_MS = 5000
AUTO_INTERVAL_MIN_MS = 3000
AUTO_INTERVAL_MAX_MS = 60000
BRIGHTNESS_STEP = 16
PARAM_LONG_STEPS = 3
IR_CONFIG_READY = True
IR_MAP_FILE = "/ir_map.conf"
LED1_MODES = ("TIME", "EFFECTS", "OFF")
LED2_MODES = ("TIME", "EFFECTS", "OFF")
_TOGGLE_ACTIONS = ("AUTO_TOGGLE", "LED1_POWER", "LED2_POWER")
def ticks_diff(now, last):
if hasattr(time, "ticks_diff"):
return time.ticks_diff(now, last)
return now - last
def effect_category(effect):
category = effect.get("category")
if category:
return category
name = effect.get("name", "")
if ("BARS" in name or "SPLIT" in name or "CLASSIC" in name or
"WATERFALL" in name or "ENERGY" in name or "MATRIX" in name or
"SPECTRUM" in name):
return "bars"
if "GRAVITY" in name or "SPRING" in name or "PENDULUM" in name or "BOUNCE" in name:
return "gravity"
if "RAINBOW" in name or "PLASMA" in name or "RADIAL" in name or "PULSE" in name or "GRAD" in name:
return "color"
if "STAR" in name or "SPARK" in name or "FIRE" in name or "RAIN" in name:
return "spark"
return "other"
class LampMenu:
def __init__(self, catalog, modes, initial_effect=0, initial_mode=0, on_change_callback=None):
self.catalog = catalog
self.modes = modes
self.effect_index = initial_effect % len(catalog)
self.param_index = 0
self.mode_index = initial_mode % len(modes)
self.last_on_mode_index = self.mode_index
self.brightness = 255
self.params = {}
self.edit_mode = "scenario"
if hasattr(time, "ticks_ms"):
self.last_interaction_ms = time.ticks_ms()
else:
self.last_interaction_ms = int(time.time() * 1000)
self.load_params()
self.brightness_per_effect = {}
self.favorites = {}
self.on_change_callback = on_change_callback
def current_effect(self):
return self.catalog[self.effect_index]
def mode_name(self):
return self.modes[self.mode_index]
def load_params(self):
self.params.clear()
import random
for meta in self.current_effect().get("params", ()):
name, val = meta[0], meta[1]
if val == "RM":
h = random.randint(0, 255)
from helpers import EffectsHelpers
r, g, b = EffectsHelpers._hsv_to_rgb(h, 255, 255)
val = [r, g, b]
if name in ("color_p", "color_s", "color_t", "color_q"):
hue_key = "hue_" + name[-1]
self.params[hue_key] = h
self.params[name] = val
self.param_index = 0
sc = self.current_effect().get("scenario", {})
speed_val = 1.0
for k in ("speed", "scroll_speed", "rotation_speed", "fall_speed"):
if k in sc:
speed_val = float(sc[k])
break
else:
if "delay" in sc:
delay = float(sc["delay"])
speed_val = 1000.0 / delay if delay > 0 else 1.0
self.params["speed"] = speed_val
self.edit_mode = "scenario"
if hasattr(time, "ticks_ms"):
self.last_interaction_ms = time.ticks_ms()
else:
self.last_interaction_ms = int(time.time() * 1000)
def next_effect(self):
self.brightness_per_effect[self.effect_index] = self.brightness
self.effect_index = (self.effect_index + 1) % len(self.catalog)
self.load_params()
self.brightness = self.brightness_per_effect.get(self.effect_index, 255)
def prev_effect(self):
self.brightness_per_effect[self.effect_index] = self.brightness
self.effect_index = (self.effect_index - 1) % len(self.catalog)
self.load_params()
self.brightness = self.brightness_per_effect.get(self.effect_index, 255)
def next_param(self):
params = self.current_effect().get("params", ())
if not params:
return False
self.param_index = (self.param_index + 1) % len(params)
return True
def prev_param(self):
params = self.current_effect().get("params", ())
if not params:
return False
self.param_index = (self.param_index - 1) % len(params)
return True
def adjust_param(self, direction):
params = self.current_effect().get("params", ())
if not params:
return False
meta = params[self.param_index]
name, default, step, minimum, maximum = meta
value = self.params.get(name, default) + (step if direction > 0 else -step)
if value < minimum:
value = minimum
elif value > maximum:
value = maximum
self.params[name] = value
if self.effect_index in self.favorites:
self.favorites[self.effect_index] = self.params.copy()
if self.on_change_callback:
self.on_change_callback()
return True
def randomize_params(self):
import random
changed = False
for meta in self.current_effect().get("params", ()):
name = meta[0]
if len(meta) >= 3 and meta[2] == "color":
h = random.randint(0, 255)
from helpers import EffectsHelpers
r, g, b = EffectsHelpers._hsv_to_rgb(h, 255, 255)
self.params[name] = [r, g, b]
if name in ("color_p", "color_s", "color_t", "color_q"):
hue_key = "hue_" + name[-1]
self.params[hue_key] = h
changed = True
else:
name, _default, _step, minimum, maximum = meta
if isinstance(minimum, float) or isinstance(maximum, float):
self.params[name] = round(random.uniform(minimum, maximum), 2)
else:
self.params[name] = random.randint(int(minimum), int(maximum))
changed = True
if changed and self.effect_index in self.favorites:
self.favorites[self.effect_index] = self.params.copy()
if self.on_change_callback:
self.on_change_callback()
return changed
def next_mode(self):
old = self.mode_name()
self.mode_index = (self.mode_index + 1) % len(self.modes)
if self.mode_name() != "OFF":
self.last_on_mode_index = self.mode_index
elif old != "OFF":
self.last_on_mode_index = self.modes.index(old)
def set_mode(self, name):
if name not in self.modes:
return False
self.mode_index = self.modes.index(name)
if name != "OFF":
self.last_on_mode_index = self.mode_index
return True
def toggle_power(self):
if self.mode_name() == "OFF":
self.mode_index = self.last_on_mode_index
else:
self.last_on_mode_index = self.mode_index
self.set_mode("OFF")
def adjust_brightness(self, direction, step=BRIGHTNESS_STEP):
self.brightness += step if direction > 0 else -step
if self.brightness < 0:
self.brightness = 0
elif self.brightness > 255:
self.brightness = 255
return True
def adjust_named_param(self, name, direction):
params = self.current_effect().get("params", ())
for i in range(len(params)):
if params[i][0] == name:
self.param_index = i
return self.adjust_param(direction)
return False
def select_category(self, category):
count = len(self.catalog)
start = self.effect_index
for offset in range(1, count + 1):
idx = (start + offset) % count
if effect_category(self.catalog[idx]) == category:
self.effect_index = idx
self.load_params()
return True
return False
def get_sibling_scenario_indices(self):
current_mode = self.current_effect().get("scenario", {}).get("mode", "")
if not current_mode:
return [self.effect_index]
indices = []
for i, eff in enumerate(self.catalog):
if eff.get("scenario", {}).get("mode", "") == current_mode:
indices.append(i)
return indices
def adjust_scenario(self, direction):
siblings = self.get_sibling_scenario_indices()
if len(siblings) <= 1:
return False
try:
curr_pos = siblings.index(self.effect_index)
except ValueError:
return False
next_pos = (curr_pos + direction) % len(siblings)
next_effect_index = siblings[next_pos]
self.brightness_per_effect[self.effect_index] = self.brightness
self.effect_index = next_effect_index
self.load_params()
self.brightness = self.brightness_per_effect.get(self.effect_index, 255)
return True
def param_label(self):
if self.edit_mode == "scenario":
siblings = self.get_sibling_scenario_indices()
if len(siblings) > 1:
try:
curr_pos = siblings.index(self.effect_index)
except ValueError:
curr_pos = 0
return "S:%d/%d" % (curr_pos + 1, len(siblings))
return "S:SINGLE"
else:
params = self.current_effect().get("params", ())
if not params:
return "P:NONE"
name = params[self.param_index][0]
return "P:%s=%s" % (name, self.params.get(name, ""))
def toggle_favorite(self):
if self.effect_index in self.favorites:
del self.favorites[self.effect_index]
else:
self.favorites[self.effect_index] = self.params.copy()
if self.on_change_callback:
self.on_change_callback()
def next_favorite(self):
favs = sorted(self.favorites)
if not favs:
return
idx = favs.index(self.effect_index) if self.effect_index in favs else -1
self.brightness_per_effect[self.effect_index] = self.brightness
self.effect_index = favs[(idx + 1) % len(favs)]
self.load_params()
saved = self.favorites.get(self.effect_index)
if saved:
self.params.update(saved)
self.brightness = self.brightness_per_effect.get(self.effect_index, 255)
def prev_favorite(self):
favs = sorted(self.favorites)
if not favs:
return
idx = favs.index(self.effect_index) if self.effect_index in favs else 0
self.brightness_per_effect[self.effect_index] = self.brightness
self.effect_index = favs[(idx - 1) % len(favs)]
self.load_params()
saved = self.favorites.get(self.effect_index)
if saved:
self.params.update(saved)
self.brightness = self.brightness_per_effect.get(self.effect_index, 255)
class AppState:
def __init__(self, unified_catalog, c):
self.catalog = unified_catalog
self.c = c
clock_idx = 0
for idx, eff in enumerate(unified_catalog):
if eff.get("scenario", {}).get("mode", "") == "analog_clock" or "ANALOG" in eff.get("name", ""):
clock_idx = idx
break
self.clock_idx = clock_idx
horiz_idx = 0
for idx, eff in enumerate(unified_catalog):
if "BARS HORIZ" in eff.get("name", ""):
horiz_idx = idx
break
else:
for idx, eff in enumerate(unified_catalog):
if eff.get("category") == "bars":
horiz_idx = idx
break
self.led1 = LampMenu(unified_catalog, LED1_MODES, initial_effect=clock_idx, initial_mode=0, on_change_callback=self.save_favorites)
self.led2 = LampMenu(unified_catalog, LED2_MODES, initial_effect=horiz_idx, initial_mode=0, on_change_callback=self.save_favorites)
fav1 = c.settings.get("favorites_led1", {})
for k, v in fav1.items():
self.led1.favorites[int(k)] = v
fav2 = c.settings.get("favorites_led2", {})
for k, v in fav2.items():
self.led2.favorites[int(k)] = v
self.focus_led = 2
self.led1_last_non_clock_idx = horiz_idx
self.auto_enabled = False
self.auto_interval_ms = AUTO_INTERVAL_MS
self.last_auto_ms = 0
self.ui_message = ""
self.ui_until = 0
self.time_sub = "CUSTOM" # TIME_ONLY, CUSTOM, TIME_CUSTOM
self._toggle_last_ms = {}
self.param_hold_start_time = 0
self.param_hold_direction = 0
self.auto_category = None
self.beat_sync_enabled = False
self.beat_detected = False
self.s2 = True
self.status_was_active = False
def save_favorites(self):
fav1 = {str(k): v for k, v in self.led1.favorites.items()}
fav2 = {str(k): v for k, v in self.led2.favorites.items()}
self.c.settings["favorites_led1"] = fav1
self.c.settings["favorites_led2"] = fav2
self.c.save_settings()
def focused(self):
return self.led1 if self.focus_led == 1 else self.led2
def focus_next(self):
self.focus_led = 1 if self.focus_led == 2 else 2
def handle_action(self, action):
if not action:
return None
if not "CLOCK" in self.led1.current_effect().get("name", ""):
self.led1_last_non_clock_idx = self.led1.effect_index
now_ms = time.ticks_ms()
if action in _TOGGLE_ACTIONS:
last = self._toggle_last_ms.get(action, 0)
if ticks_diff(now_ms, last) < TOGGLE_COOLDOWN_MS:
return None
self._toggle_last_ms[action] = now_ms
menu = self.focused()
if action == "LED1_POWER":
self.led1.toggle_power()
self.focus_led = 1
return "mode"
if action == "LED2_POWER":
self.led2.toggle_power()
self.focus_led = 2
return "mode"
if action == "AUTO_TOGGLE":
self.auto_enabled = not self.auto_enabled
return "auto"
if action == "TOGGLE_BEAT_SYNC":
self.beat_sync_enabled = not self.beat_sync_enabled
return "beat_sync"
if action.startswith("AUTO_CATEGORY_"):
self.auto_category = action[14:].lower()
return "auto"
if action == "TOGGLE_FAVORITE":
menu.toggle_favorite()
return "favorite"
if action == "NEXT_FAVORITE":
menu.next_favorite()
return "effect"
if action == "PREV_FAVORITE":
menu.prev_favorite()
return "effect"
if action == "FOCUS_NEXT":
self.focus_next()
return "focus"
if action == "FOCUS_LED1":
self.focus_led = 1
return "focus"
if action == "FOCUS_LED2":
self.focus_led = 2
return "focus"
if action == "NEXT_EFFECT":
menu.next_effect()
return "effect"
if action == "PREV_EFFECT":
menu.prev_effect()
return "effect"
if action == "NEXT_PARAM":
if menu.edit_mode == "scenario":
menu.edit_mode = "parameter"
menu.param_index = 0
menu.last_interaction_ms = now_ms
return "param_select"
else:
if menu.next_param():
menu.last_interaction_ms = now_ms
return "param_select"
else:
menu.edit_mode = "scenario"
menu.last_interaction_ms = now_ms
return "effect"
if action == "PREV_PARAM":
if menu.edit_mode == "scenario":
menu.edit_mode = "parameter"
params = menu.current_effect().get("params", ())
menu.param_index = len(params) - 1 if params else 0
menu.last_interaction_ms = now_ms
return "param_select"
else:
if menu.prev_param():
menu.last_interaction_ms = now_ms
return "param_select"
else:
menu.edit_mode = "scenario"
menu.last_interaction_ms = now_ms
return "effect"
if action in ("TOGGLE_EDIT_MODE", "NEXT_PARAM_LONG"):
if menu.edit_mode == "scenario":
menu.edit_mode = "parameter"
menu.param_index = 0
menu.last_interaction_ms = now_ms
return "param_select"
else:
menu.edit_mode = "scenario"
menu.last_interaction_ms = now_ms
return "effect"
if action in ("PARAM_UP", "PARAM_UP_LONG"):
steps = PARAM_LONG_STEPS if action == "PARAM_UP_LONG" else 1
changed = False
menu.last_interaction_ms = now_ms
if menu.edit_mode == "scenario":
for _ in range(steps):
if menu.adjust_scenario(1):
changed = True
return "effect" if changed else None
else:
for _ in range(steps):
if menu.adjust_param(1):
changed = True
return "param" if changed else None
if action in ("PARAM_DOWN", "PARAM_DOWN_LONG"):
steps = PARAM_LONG_STEPS if action == "PARAM_DOWN_LONG" else 1
changed = False
menu.last_interaction_ms = now_ms
if menu.edit_mode == "scenario":
for _ in range(steps):
if menu.adjust_scenario(-1):
changed = True
return "effect" if changed else None
else:
for _ in range(steps):
if menu.adjust_param(-1):
changed = True
return "param" if changed else None
if action == "RANDOMIZE":
return "param" if menu.randomize_params() else None
if action == "BRIGHTNESS_UP":
menu.adjust_brightness(1)
return "brightness"
if action == "BRIGHTNESS_DOWN":
menu.adjust_brightness(-1)
return "brightness"
if action == "SPEED_UP":
current_speed = menu.params.get("speed", 1.0)
menu.params["speed"] = min(20.0, current_speed + 0.2)
return "speed"
if action == "SPEED_DOWN":
current_speed = menu.params.get("speed", 1.0)
menu.params["speed"] = max(0.1, current_speed - 0.2)
return "speed"
if action == "AUTO_INTERVAL_UP":
self.auto_interval_ms = min(AUTO_INTERVAL_MAX_MS, self.auto_interval_ms + 5000)
return "auto"
if action == "AUTO_INTERVAL_DOWN":
self.auto_interval_ms = max(AUTO_INTERVAL_MIN_MS, self.auto_interval_ms - 5000)
return "auto"
if action.startswith("CATEGORY_"):
category = action[9:].lower()
return "effect" if menu.select_category(category) else None
if action == "NEXT_MODE":
menu.next_mode()
return "mode"
if action == "LED2_TIME":
if self.focus_led == 1:
current_mode = self.led1.current_effect().get("scenario", {}).get("mode", "")
if self.led1.mode_name() == "TIME" and current_mode == "analog_clock":
self.led1.adjust_scenario(1)
else:
self.led1.set_mode("TIME")
for idx, eff in enumerate(self.catalog):
if eff.get("scenario", {}).get("mode", "") == "analog_clock":
self.led1.brightness_per_effect[self.led1.effect_index] = self.led1.brightness
self.led1.effect_index = idx
self.led1.load_params()
self.led1.brightness = self.led1.brightness_per_effect.get(idx, 255)
break
return "mode"
else:
if self.led2.mode_name() == "TIME":
subs = ("TIME_ONLY", "CUSTOM", "TIME_CUSTOM")
i = subs.index(self.time_sub) if self.time_sub in subs else 2
self.time_sub = subs[(i + 1) % 3]
else:
self.led2.set_mode("TIME")
self.time_sub = "TIME_ONLY"
self.focus_led = 2
return "mode"
if action == "LED2_TIME_FX":
self.led2.set_mode("TIME")
self.time_sub = "TIME_CUSTOM"
self.focus_led = 2
return "mode"
if action == "LED2_FX":
if self.focus_led == 1:
current_mode = self.led1.current_effect().get("scenario", {}).get("mode", "")
if current_mode == "analog_clock":
self.led1.brightness_per_effect[self.led1.effect_index] = self.led1.brightness
self.led1.effect_index = self.led1_last_non_clock_idx
self.led1.load_params()
self.led1.brightness = self.led1.brightness_per_effect.get(self.led1_last_non_clock_idx, 255)
self.led1.set_mode("EFFECTS")
return "mode"
else:
self.led2.set_mode("EFFECTS")
self.focus_led = 2
return "mode"
if action == "LED1_MODE":
self.led1.next_mode()
self.focus_led = 1
return "mode"
if action == "LED2_MODE":
self.led2.next_mode()
self.focus_led = 2
return "mode"
return None
def auto_step(self, now):
timeout_occurred = False
for menu in (self.led1, self.led2):
if menu.edit_mode == "parameter":
if ticks_diff(now, menu.last_interaction_ms) > 10000:
menu.edit_mode = "scenario"
menu.last_interaction_ms = now
timeout_occurred = True
print("[Menu] Parameter edit timeout. Reverted to scenario mode.")
if timeout_occurred:
return "param_timeout"
if not self.auto_enabled:
return None
if self.beat_sync_enabled:
if not self.beat_detected:
return None
self.beat_detected = False
elif ticks_diff(now, self.last_auto_ms) < self.auto_interval_ms:
return None
self.last_auto_ms = now
if self.auto_category:
menu = self.focused()
count = len(menu.catalog)
start = menu.effect_index
for offset in range(1, count + 1):
idx = (start + offset) % count
if effect_category(menu.catalog[idx]) == self.auto_category:
menu.brightness_per_effect[menu.effect_index] = menu.brightness
menu.effect_index = idx
menu.load_params()
menu.brightness = menu.brightness_per_effect.get(menu.effect_index, 255)
break
else:
self.focused().next_effect()
return "effect"
def set_ui_message(self, msg, now, duration=1500):
if not self.c.settings.get("status_messages_enabled", True):
self.ui_message = ""
return
self.ui_message = msg
self.ui_until = now + duration
def current_ui_message(self, now):
if self.ui_message and ticks_diff(self.ui_until, now) > 0:
return self.ui_message
return ""
def led2_frame_due(mode_name, now, last_tick):
if mode_name != "TIME":
return True
return ticks_diff(now, last_tick) >= OVERLAY_FRAME_MIN_TIME
def ir_action(reader):
if reader:
return reader.read()
return None
MODE_DEFAULTS = {
"center_split": {
"max_height": 36, "center_offset": -7, "show_peaks": True,
"enable_ghosting": True, "ghosting_factor": 0.7, "enable_peak_flash": True
},
"bars": {
"bar_size": 8, "spacing": 2, "start_row": 20, "reverse_bands": True,
"show_peaks": False, "enable_ghosting": True, "ghosting_factor": 0.6,
"enable_symmetric": False, "enable_peak_flash": True, "center_offset": -7
},
"classic": {
"show_peaks": True, "enable_ghosting": True, "ghosting_factor": 0.74,
"enable_peak_flash": True, "peak_flash_threshold": 180, "gain": 1.0
},
"analog_clock": {
"show_marks": True, "target_brightness": 255, "auto_brightness": True
},
"sparkles": {
"sparkle_count": 4, "enable_ghosting": True, "ghosting_factor": 0.68
},
"spiral_audio": {
"rotation_speed": 4.0, "arms": 4, "enable_ghosting": True, "ghosting_factor": 0.6
},
"orbital_dots": {
"n_dots": 4, "ring": 0, "direction": 1, "speed": 2.0,
"ghosting": 0.7, "trail": 4, "audio_reactive": True, "pulse": True
},
"attractor": {
"mass": 100, "particles": 82, "size": 1, "friction": 0,
"color_by_age": False, "move_attractor": False, "swallow": False, "ghosting": 0.7
},
"dna": {
"scroll_speed": 8.0, "cycles": 3, "rung_spacing": 0,
"enable_ghosting": True, "ghosting_factor": 0.72, "audio_reactive": True
},
"fire": {
"cooling": 175, "sparking": 80, "audio_reactive": False, "speed": 2
},
"fireworks": {
"sparks_per_burst": 14, "gravity": 3, "min_interval": 400, "audio_reactive": True
},
"motion_patterns": {
"speed": 1.0, "direction": 1, "segment_len": 15, "spacing": 15,
"bg_brightness": 0, "audio_reactive": True, "palette_shift_speed": 0.5
},
"plasma_audio": {
"speed": 3.0, "intensity": 1.5
},
"wave_audio": {
"wave_height": 40, "start_row": 0
},
"rain": {
"start_row": 0, "fall_speed": 1.0
},
"fast_bars": {
"scale": 8
},
"fire_ice": {
"cooling": 175, "sparking": 80, "speed": 2
},
"colored_snake": {
"num_snakes": 3, "min_len": 5, "max_len": 30, "delay": 30
},
"static_bars": {
"rainbow": False, "target_brightness": 255, "auto_brightness": False, "color_interval_ms": 0
},
"mixer": {
"sections": 2, "color_mode": 0, "multiplier_mode": 0, "effect_mode": 1,
"beginning_offset": 0, "delay_in": 30, "delay_intermediate": 30, "delay_out": 30, "wait_time": 1000, "random_cycle": False,
"color_p": [255, 255, 255], "color_s": [255, 165, 0], "color_t": [128, 0, 128]
},
"rotating": {
"hue_offset": 0.0
},
"spectrum": {
"hue_offset": 0.0
},
"gravity_orbiters": {
"gravity": 1.3, "friction": 1.2, "n_orbiters": 3
},
"gravity_cascade": {
"gravity": 0.15, "wind": 0.5, "bounce": 0.7
},
"planet_orbit": {
"gravity": 2.0, "speed": 1.0, "planet_count": 3
},
"black_hole": {
"gravity": 2.5, "swallow_radius": 2.0, "particle_count": 80
},
"bg_fire": {
"intensity": 150, "cooling": 35, "sparking": 120, "delay": 15
},
"gravity_fountain": {
"gravity": 0.15, "bounce": 0.6, "wind": 0.01, "color_mode": 0, "decay_rate": 0.025,
"max_particles": 40, "enable_ghosting": False, "ghosting_factor": 0.7, "audio_reactive": True
}
}
for m in ("scan", "diagonal", "radar", "shapes", "wave", "plasma", "spiral", "radial", "noise", "rain_v", "rain_h", "spinner", "edge_walker", "crosshair"):
MODE_DEFAULTS[m] = {
"vertical": True, "horizontal": False, "direction": 1,
"ghosting": 0.7, "random_values": False, "angle": 0.0,
"speed": 1.0, "audio_reactive": True
}
def make_param_metadata(name, val):
if val is None:
return None
if isinstance(val, bool):
return (name, 1 if val else 0, 1, 0, 1)
lname = name.lower()
if (lname.startswith("color_") or lname == "color" or lname.endswith("_color") or lname in ("color_p", "color_s", "color_t", "color_q")) and lname not in ("color_mode", "color_interval_ms"):
return (name, val, "color")
if isinstance(val, (list, tuple, str)):
return None
if lname == "height" or (lname.startswith("height_") and lname[7:].isdigit()):
return (name, int(val), 2, -137, 137)
if lname == "pos" or (lname.startswith("pos_") and lname[4:].isdigit()):
return (name, int(val), 2, 0, 137)
if lname in ("hue_p", "hue_s", "hue_t", "hue_q"):
return (name, int(val), 5, -1, 255)
if "brightness" in lname:
return (name, int(val), 1, 0, 255)
if lname == "speed" or lname.endswith("_speed"):
step = 0.1 if isinstance(val, float) else 1
return (name, val, step, 0.1, 10.0)
if lname in ("cooling", "sparking", "intensity"):
return (name, int(val), 2, 0, 300)
if lname in ("max_height", "wave_height", "bar_size", "braid_length", "min_len", "max_len", "segment_len"):
return (name, int(val), 1, 1, 150)
if lname == "delay" or lname.startswith("delay_") or lname.endswith("_delay"):
return (name, int(val), 1, 0, 1000)
if lname == "color_interval_ms" or lname.endswith("_ms"):
return (name, int(val), 1, 0, 10000)
if lname == "wait_time" or lname == "time" or (lname.endswith("_time") and lname != "time_sub"):
return (name, int(val), 1, 0, 10000)
if lname == "direction":
return (name, int(val), 2, -1, 1)
if lname in ("ghosting", "ghosting_factor", "rnd_fac", "friction", "angle", "palette_shift_speed", "hue_offset"):
step = 5.0 if "angle" in lname else (10.0 if "hue" in lname else 0.05)
return (name, float(val), step, 0.0, 360.0 if "hue" in lname or "angle" in lname else 1.0)
if lname in ("arms", "n_dots", "num_snakes", "sections", "cols"):
return (name, int(val), 1, 1, 36)
if lname in ("spacing", "cycles", "size"):
return (name, int(val), 1, 0 if lname == "spacing" else 1, 50)
if lname in ("mass", "particles", "max_particles"):
return (name, int(val), 10 if lname == "mass" else 5, 5, 1000)
if lname == "decay_rate":
return (name, float(val), 0.005, 0.001, 0.2)
if lname == "color_mode":
return (name, int(val), 1, -1, 4)
if lname == "center_offset":
return (name, int(val), 1, -69, 69)
if lname == "gravity":
return (name, float(val), 0.1, 0.1, 10.0)
if lname == "wind":
return (name, float(val), 0.05, -2.0, 2.0)
if lname == "bounce":
return (name, float(val), 0.05, 0.0, 1.0)
if lname == "swallow_radius":
return (name, float(val), 0.1, 0.5, 10.0)
if lname in ("n_orbiters", "planet_count"):
return (name, int(val), 1, 1, 10)
if lname == "particle_count":
return (name, int(val), 1, 5, 200)
if isinstance(val, float):
return (name, val, 0.1, val - 5.0, val + 5.0)
if isinstance(val, int):
return (name, val, 1, val - 100, val + 100)
return None
def combine_params(scenario, adjusted_params):
res = scenario.copy()
mode = scenario.get("mode", "")
defaults = MODE_DEFAULTS.get(mode, {})
if "speed" in adjusted_params:
spd = adjusted_params["speed"]
for k in ("scroll_speed", "rotation_speed", "fall_speed"):
if k in scenario or k in defaults:
res[k] = spd
break
else:
if "delay" in scenario or "delay" in defaults:
res["delay"] = int(1000 / spd) if spd > 0 else 1000
elif "speed" in scenario or "speed" in defaults:
res["speed"] = spd
for k, v in adjusted_params.items():
if k == "speed":
continue
res[k] = v
for k, v in res.items():
default_val = defaults.get(k, scenario.get(k))
if isinstance(default_val, bool):
res[k] = bool(v)
return {k: v for k, v in res.items() if v is not None}
def rgb_to_hue(rgb):
if not isinstance(rgb, (list, tuple)) or len(rgb) < 3:
return 0
r, g, b = rgb[0], rgb[1], rgb[2]
mx = r if r > g else g
mx = mx if mx > b else b
mn = r if r < g else g
mn = mn if mn < b else b
df = mx - mn
h = 0
if df != 0:
if mx == r:
h = (60 * (g - b)) // df
elif mx == g:
h = 120 + (60 * (b - r)) // df
elif mx == b:
h = 240 + (60 * (r - g)) // df
if h < 0:
h += 360
return (h * 255) // 360
def make_catalog():
from effects import matrix_service
import json
try:
with open("scenarios.json", "r", encoding="utf-8") as f:
scenarios = json.load(f)
except Exception as e:
print("Failed to load scenarios.json, fallback to static catalog:", e)
scenarios = []
catalog = []
for sc in scenarios:
desc = sc.get("desc", sc.get("mode", "UNKNOWN")).upper()
params_list = []
mode = sc.get("mode", "")
sc_params = MODE_DEFAULTS.get(mode, {}).copy()
for k, v in sc.items():
if k in ("mode", "desc"):
continue
sc_params[k] = v
if mode == "static_bars":
if "height" not in sc_params:
h_list = sc_params.get("heights")
sc_params["height"] = h_list[0] if isinstance(h_list, (list, tuple)) and len(h_list) > 0 else -138
if "pos" not in sc_params:
p_list = sc_params.get("start_positions")
sc_params["pos"] = p_list[0] if isinstance(p_list, (list, tuple)) and len(p_list) > 0 else 137
if "hue_p" not in sc_params:
c_p = sc_params.get("color_p")
sc_params["hue_p"] = rgb_to_hue(c_p) if isinstance(c_p, (list, tuple)) else -1
if "hue_s" not in sc_params:
c_s = sc_params.get("color_s")
sc_params["hue_s"] = rgb_to_hue(c_s) if isinstance(c_s, (list, tuple)) else -1
if "hue_t" not in sc_params:
c_t = sc_params.get("color_t")
sc_params["hue_t"] = rgb_to_hue(c_t) if isinstance(c_t, (list, tuple)) else -1
if "hue_q" not in sc_params:
c_q = sc_params.get("color_q")
sc_params["hue_q"] = rgb_to_hue(c_q) if isinstance(c_q, (list, tuple)) else -1
p_list = sc_params.get("start_positions")
h_list = sc_params.get("heights")
for i in range(4):
pos_key = f"pos_{i}"
if pos_key not in sc_params:
sc_params[pos_key] = p_list[i] if isinstance(p_list, (list, tuple)) and len(p_list) > i else 137
height_key = f"height_{i}"
if height_key not in sc_params:
sc_params[height_key] = h_list[i] if isinstance(h_list, (list, tuple)) and len(h_list) > i else -138
for k, v in sc_params.items():
if k == "brightness":
continue
meta = make_param_metadata(k, v)
if meta is not None:
params_list.append(meta)
category = "other"
if mode in (
"bars", "center_split", "classic", "fast_bars", "energy_bars",
"spectrum", "spectrum1", "spectrum_matrix", "waterfall", "wave_audio",
"static_bars", "radial_audio", "blocks", "vibrant_lights"
) or "bars" in mode or "spectrum" in mode:
category = "bars"
elif mode in (
"spring_balls", "pendulum_audio", "planet_orbit", "black_hole",
"attractor", "gravity_bounce", "gravity_cascade", "gravity_orbiters",
"gravity_well", "sandclock"
) or "gravity" in mode:
category = "gravity"
elif mode in (
"rainbow_effect", "rotating", "plasma", "dna", "gradient_energy",
"motion_patterns", "pulse"
) or "color" in mode or "rainbow" in mode or "plasma" in mode:
category = "color"
elif mode in (
"sparkles", "sparks", "stars", "comet", "rain", "rain_h", "rain_v",
"fire", "bg_fire", "fire_ice", "fireworks", "storm", "storm2", "noise",
"beat_flash", "beat_impact", "bpm_pulse", "flux_onset"
) or "spark" in mode or "fire" in mode or "rain" in mode or "storm" in mode or "flash" in mode:
category = "spark"
catalog.append({
"name": desc,
"category": category,
"mode": mode,
"params": tuple(params_list),
"scenario": sc,
"func": lambda b, p, sc=sc: matrix_service([b], **combine_params(sc, p))
})
if not catalog:
catalog = [
{"name": "ANALOG CLOCK", "category": "other",
"params": (("show_marks", 1, 1, 0, 1), ("target_brightness", 63, 3, 0, 255)),
"func": lambda b, p: b.render_analog_clock(show_marks=bool(p.get("show_marks", True)), h_width=1.2, m_width=1.0, s_width=0.8, target_brightness=p.get("target_brightness", 63), auto_brightness=True)},
{"name": "BARS HORIZ", "category": "bars",
"params": (("bar_size", 8, 2, 4, 20), ("spacing", 2, 1, 0, 10), ("show_peaks", 1, 1, 0, 1), ("reverse_bands", 1, 1, 0, 1), ("enable_symmetric", 0, 1, 0, 1), ("enable_peak_flash", 1, 1, 0, 1), ("center_offset", -10, 1, -50, 50)),
"func": lambda b, p: b.render_bars(orientation="h", bar_size=p.get("bar_size", 8), spacing=p.get("spacing", 2), show_peaks=bool(p.get("show_peaks", True)), reverse_bands=bool(p.get("reverse_bands", True)), enable_symmetric=bool(p.get("enable_symmetric", False)), enable_peak_flash=bool(p.get("enable_peak_flash", True)), center_offset=p.get("center_offset", -10))}
]
return tuple(catalog)
def configure_display(c):
c.led2.init_display_system(rows=138, cols=4)
ds = c.led2.display_system
ds.set_zones([("time", 1, 45), ("s", 49, 137), ("status", 1, 137), ("s2", 1, 137)])
ds.set_zone_colors("time", fg=(c.palette[1230], c.palette[1231], c.palette[1232]))
ds.enable_colon_blink("time", period_ms=500)