Repository navigation
Expand file tree
/
Copy pathLaserClock.py
More file actions
1241 lines (1128 loc) · 42.1 KB
/
Copy pathLaserClock.py
File metadata and controls
1241 lines (1128 loc) · 42.1 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
# =====================================================================================
# LASER CLOCK — HH:MM forged one dot at a time
#
# The time exists as a shape on a black field and stays invisible until a laser
# reaches it. One beam stays on at full green (0, 255, 0) and sweeps every dot that
# needs to change: left to right, top to bottom, across all of those digits.
# Each dot is blasted into view and throws sparks that arc, fall, and bounce.
# The dot cools from white-hot to red.
# The nozzle at the source fades into and out of existence.
# The weekday is on its own line. The month and day share the line under it.
# Each weld starts from a random corner of the panel.
# A blue two-pixel mark travels the outer edge, one sixtieth per second,
# fading in and out as it moves. At :00 it sits in the center of the top edge.
# A few seconds before the minute, the beam goes out. A digit that is about
# to change falls backwards on its own, slams on the ledge above that caption,
# and shatters into dark particles. Then the beam welds the new one.
#
# A "dot" is one cell of a 5×7 clock font, drawn as a 2×2 block so the strike
# reads as a single hot pixel on a 64×32 panel.
#
# Idle rotation: clock slot "laserclock" (5 min), between the other clocks.
# The whole picture fades in when the clock starts and fades out when it stops.
#
# Run: python3 LaserClock.py
# LEDpanel / action "laserclock" / ?laserclock
# =====================================================================================
from __future__ import annotations
import math
import random
import time
from datetime import datetime, timedelta
import LEDarcade as LED
try:
import pygame
HAS_PYGAME = True
except Exception:
HAS_PYGAME = False
TARGET_FPS = 30
# 5×7 glyphs. Bit 4 is the leftmost column.
DIGIT_ROWS = (
(0b01110, 0b10001, 0b10011, 0b10101, 0b11001, 0b10001, 0b01110), # 0
(0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110), # 1
(0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b11111), # 2
(0b11110, 0b00001, 0b00001, 0b01110, 0b00001, 0b00001, 0b11110), # 3
(0b10001, 0b10001, 0b10001, 0b11111, 0b00001, 0b00001, 0b00001), # 4
(0b11111, 0b10000, 0b10000, 0b11110, 0b00001, 0b00001, 0b11110), # 5
(0b01110, 0b10000, 0b10000, 0b11110, 0b10001, 0b10001, 0b01110), # 6
(0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000), # 7
(0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b01110), # 8
(0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00001, 0b01110), # 9
)
GLYPH_W = 5
GLYPH_H = 7
COLON_ROWS = (2, 4) # which glyph rows get a colon dot
# 3×5 captions. Bit 2 is the leftmost column. Widest names are 9 letters.
TINY_W = 3
TINY_H = 5
TINY_GAP = 1
DATE_GAP = 1
DATE_BLOCK = TINY_H * 2 + DATE_GAP
DATE_RGB = (255, 150, 0)
SECOND_RGB = (0, 0, 255)
WEEKDAYS = (
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY",
"FRIDAY", "SATURDAY", "SUNDAY",
)
MONTHS = (
"JANUARY", "FEBRUARY", "MARCH", "APRIL", "MAY", "JUNE",
"JULY", "AUGUST", "SEPTEMBER", "OCTOBER", "NOVEMBER", "DECEMBER",
)
TINY_FONT = {
"A": (0b111, 0b101, 0b111, 0b101, 0b101),
"B": (0b110, 0b101, 0b110, 0b101, 0b110),
"C": (0b111, 0b100, 0b100, 0b100, 0b111),
"D": (0b110, 0b101, 0b101, 0b101, 0b110),
"E": (0b111, 0b100, 0b110, 0b100, 0b111),
"F": (0b111, 0b100, 0b110, 0b100, 0b100),
"G": (0b111, 0b100, 0b101, 0b101, 0b111),
"H": (0b101, 0b101, 0b111, 0b101, 0b101),
"I": (0b010, 0b010, 0b010, 0b010, 0b010),
"J": (0b001, 0b001, 0b001, 0b101, 0b111),
"K": (0b101, 0b110, 0b100, 0b110, 0b101),
"L": (0b100, 0b100, 0b100, 0b100, 0b111),
"M": (0b101, 0b111, 0b101, 0b101, 0b101),
"N": (0b101, 0b110, 0b101, 0b101, 0b101),
"O": (0b111, 0b101, 0b101, 0b101, 0b111),
"P": (0b111, 0b101, 0b111, 0b100, 0b100),
"Q": (0b111, 0b101, 0b101, 0b111, 0b001),
"R": (0b110, 0b101, 0b110, 0b101, 0b101),
"S": (0b111, 0b100, 0b111, 0b001, 0b111),
"T": (0b111, 0b010, 0b010, 0b010, 0b010),
"U": (0b101, 0b101, 0b101, 0b101, 0b111),
"V": (0b101, 0b101, 0b101, 0b101, 0b010),
"W": (0b101, 0b101, 0b101, 0b111, 0b101),
"X": (0b101, 0b101, 0b010, 0b101, 0b101),
"Y": (0b101, 0b101, 0b010, 0b010, 0b010),
"Z": (0b111, 0b001, 0b010, 0b100, 0b111),
"0": (0b111, 0b101, 0b101, 0b101, 0b111),
"1": (0b010, 0b110, 0b010, 0b010, 0b111),
"2": (0b111, 0b001, 0b111, 0b100, 0b111),
"3": (0b111, 0b001, 0b111, 0b001, 0b111),
"4": (0b101, 0b101, 0b111, 0b001, 0b001),
"5": (0b111, 0b100, 0b111, 0b001, 0b111),
"6": (0b111, 0b100, 0b111, 0b101, 0b111),
"7": (0b111, 0b001, 0b010, 0b010, 0b010),
"8": (0b111, 0b101, 0b111, 0b101, 0b111),
"9": (0b111, 0b101, 0b111, 0b001, 0b111),
}
# Largest cell size that still leaves a black margin for the beam to travel.
DOT_ZOOM_CANDIDATES = (2, 1)
MARGIN = 4
INTRO_PAUSE = 0.30 # black field before the beam comes on
FADE_SECONDS = 0.50 # whole picture in at the start, out at the stop
HOP_STEPS = 4 # frames to sweep the head from one dot to the next
FALL_TIME = 0.78 # tip backwards onto the floor
SLAM_TIME = 0.12 # flat contact, still dark
SHATTER_TIME = 0.95 # dark particles, then the weld may start
BREAK_TIME = FALL_TIME + SLAM_TIME + SHATTER_TIME
LEAD_SECONDS = 4.0 # start that break this long before the minute
PERSPECTIVE = 0.055 # how hard depth shrinks a tipping digit
SHARD_GRAVITY = 38.0
NOZZLE_PERIOD = 2.2 # fade in, then out, then gone, then repeat
NOZZLE_OUT = 0.55 # leftover fade after the beam shuts off
NOZZLE_RGB = (255, 244, 180)
BEAM_RGB = (0, 255, 0)
# White-hot → yellow → orange → settled red. Age is in seconds.
# The drop to red takes about 8s, so a digit is still cooling after it is finished.
HEAT_STOPS = (
(0.00, 255, 255, 255),
(0.60, 255, 248, 220),
(1.60, 255, 190, 60),
(3.50, 255, 80, 0),
(6.00, 220, 20, 0),
(8.00, 210, 16, 0),
)
SPARK_GRAVITY = 92.0 # px/s^2, +y is down. Strong enough to land on a 32px panel.
SPARK_BOUNCE = 0.52 # vertical speed kept after hitting the floor
SPARK_FRICTION = 0.78 # horizontal speed kept on that hit
FLASH_TIME = 0.05
SLOT_NAMES = ("H1", "H2", ":", "M1", "M2")
def _stop(StopEvent):
try:
return StopEvent is not None and StopEvent.is_set()
except Exception:
return False
def _perimeter(vw, vh):
"""Clockwise loop of the outer edge. Corners are included once."""
if vw <= 1 or vh <= 1:
return [(x, y) for y in range(max(vh, 0)) for x in range(max(vw, 0))]
path = []
for x in range(vw):
path.append((x, 0))
for y in range(1, vh):
path.append((vw - 1, y))
for x in range(vw - 2, -1, -1):
path.append((x, vh - 1))
for y in range(vh - 2, 0, -1):
path.append((0, y))
return path
def _second_u(clock):
"""0 at the start of a minute, 1 at the next. Smooth within each second."""
if clock is None:
clock = datetime.now()
return (clock.second + clock.microsecond / 1000000.0) / 60.0
def _outside(x, y, bounds):
x0, y0, x1, y1 = bounds
return x < x0 or x > x1 or y < y0 or y > y1
def _line(x0, y0, x1, y1):
"""Bresenham, including both ends."""
points = []
dx = abs(x1 - x0)
dy = abs(y1 - y0)
sx = 1 if x0 < x1 else -1
sy = 1 if y0 < y1 else -1
err = dx - dy
x, y = x0, y0
while True:
points.append((x, y))
if x == x1 and y == y1:
break
e2 = err * 2
if e2 > -dy:
err -= dy
x += sx
if e2 < dx:
err += dx
y += sy
return points
def _heat_rgb(age, now, x, y):
if age < 0:
age = 0.0
stops = HEAT_STOPS
rgb = stops[-1][1:]
for i in range(len(stops) - 1):
t0, r0, g0, b0 = stops[i]
t1, r1, g1, b1 = stops[i + 1]
if age <= t1:
u = 0.0 if t1 == t0 else (age - t0) / (t1 - t0)
rgb = (
int(r0 + (r1 - r0) * u),
int(g0 + (g1 - g0) * u),
int(b0 + (b1 - b0) * u),
)
break
else:
# Ember breathes after the metal has settled. Fade it in so the
# color does not jump on the frame cooling finishes.
wobble = 0.88 + 0.12 * math.sin(now * 2.3 + x * 1.7 + y * 0.9)
k = (age - stops[-1][0]) / 0.40
if k > 1.0:
k = 1.0
scale = 1.0 + (wobble - 1.0) * k
rgb = (
int(rgb[0] * scale),
int(rgb[1] * scale),
int(rgb[2] * scale),
)
return rgb
def _set(buf, x, y, rgb, vw, vh):
if x < 0 or y < 0 or x >= vw or y >= vh:
return
buf[y][x] = rgb
def _max(buf, x, y, rgb, vw, vh):
if x < 0 or y < 0 or x >= vw or y >= vh:
return
r, g, b = buf[y][x]
buf[y][x] = (r if r > rgb[0] else rgb[0], g if g > rgb[1] else rgb[1], b if b > rgb[2] else rgb[2])
def _blend(buf, x, y, rgb, alpha, vw, vh):
"""Fade rgb in over whatever is already there. alpha 0 is gone."""
if alpha <= 0.0 or x < 0 or y < 0 or x >= vw or y >= vh:
return
if alpha > 1.0:
alpha = 1.0
r, g, b = buf[y][x]
buf[y][x] = (
int(r + (rgb[0] - r) * alpha),
int(g + (rgb[1] - g) * alpha),
int(b + (rgb[2] - b) * alpha),
)
def _tiny_width(text):
n = len(text)
if n <= 0:
return 0
return n * TINY_W + (n - 1) * TINY_GAP
def _caption(hhmm, clock):
"""Weekday and month for the minute being shown, including a midnight roll."""
if clock is None:
clock = datetime.now()
base = clock.replace(second=0, microsecond=0)
try:
shown = base.replace(hour=int(hhmm[0:2]), minute=int(hhmm[2:4]))
except (ValueError, IndexError):
shown = clock
if shown < base:
shown = shown + timedelta(days=1)
month = MONTHS[shown.month - 1] + " " + str(shown.day)
return (WEEKDAYS[shown.weekday()], month)
def layout_for(vw, vh):
"""Place HH : MM above the weekday and the month-day line."""
zoom = 1
pair_gap = 1
colon_gap = 2
colon_w = 1
dw = GLYPH_W
dh = GLYPH_H
# Top margin, a gap, and the caption. Zoom has to fit above that.
room_h = vh - DATE_BLOCK - 3
if room_h < GLYPH_H:
room_h = vh
for z in DOT_ZOOM_CANDIDATES:
dw_z = GLYPH_W * z
dh_z = GLYPH_H * z
pair_z = max(1, z)
colon_gap_z = z + 1
colon_w_z = z
total_w = dw_z * 4 + pair_z * 2 + colon_gap_z * 2 + colon_w_z
total_h = dh_z
if total_w <= vw - 2 * MARGIN and total_h <= room_h:
zoom = z
pair_gap = pair_z
colon_gap = colon_gap_z
colon_w = colon_w_z
dw = dw_z
dh = dh_z
break
total_w = dw * 4 + pair_gap * 2 + colon_gap * 2 + colon_w
x = max(0, (vw - total_w) // 2)
date_top = max(0, vh - DATE_BLOCK - 1)
# The open row above the caption is the floor. The slam stays on it.
floor = max(0, date_top - 1)
y = 1 if vh > dh + 2 else 0
y += 2
if y + dh > date_top:
y = max(0, date_top - dh)
origins = [None] * 5
origins[0] = (x, y)
x += dw + pair_gap
origins[1] = (x, y)
x += dw + colon_gap
origins[2] = (x, y)
x += colon_w + colon_gap
origins[3] = (x, y)
x += dw + pair_gap
origins[4] = (x, y)
x1 = origins[4][0] + dw - 1
y1 = y + dh - 1
return {
"zoom": zoom,
"origins": origins,
"bounds": (origins[0][0], y, x1, y1),
"dw": dw,
"dh": dh,
"date_top": date_top,
"floor": floor,
}
def _paint_tiny(buf, text, y, rgb, vw, vh):
"""Center one 3×5 line. Returns the pixels it drew."""
gap = TINY_GAP
width = _tiny_width(text)
if width > vw and text:
gap = 0
width = len(text) * TINY_W
x = max(0, (vw - width) // 2)
drawn = []
for ch in text:
rows = TINY_FONT.get(ch)
if rows is None:
x += TINY_W + gap
continue
for row, bits in enumerate(rows):
for col in range(TINY_W):
if bits & (1 << (TINY_W - 1 - col)):
px, py = x + col, y + row
_set(buf, px, py, rgb, vw, vh)
drawn.append((px, py))
x += TINY_W + gap
return drawn
def _cell_pixels(ox, oy, col, row, zoom):
pixels = []
base_x = ox + col * zoom
base_y = oy + row * zoom
for dy in range(zoom):
for dx in range(zoom):
pixels.append((base_x + dx, base_y + dy))
return tuple(pixels)
def dots_for(layout, slot, ch):
"""Lit cells of one slot. Each entry is the LEDs that ignite together."""
zoom = layout["zoom"]
ox, oy = layout["origins"][slot]
dots = []
if ch == ":":
for row in COLON_ROWS:
dots.append(_cell_pixels(ox, oy, 0, row, zoom))
return dots
rows = DIGIT_ROWS[int(ch)]
for row, bits in enumerate(rows):
for col in range(GLYPH_W):
if bits & (1 << (GLYPH_W - 1 - col)):
dots.append(_cell_pixels(ox, oy, col, row, zoom))
return dots
def _aim_of(pixels):
xs = [p[0] for p in pixels]
ys = [p[1] for p in pixels]
return ((min(xs) + max(xs)) // 2, (min(ys) + max(ys)) // 2)
def _target_chars(hhmm):
return [hhmm[0], hhmm[1], ":", hhmm[2], hhmm[3]]
def _dot_key(pixels):
"""Top-to-bottom, then left-to-right. First LED of a cell is its top-left."""
return (pixels[0][1], pixels[0][0])
def _lerp(a, b, u):
return (
int(round(a[0] + (b[0] - a[0]) * u)),
int(round(a[1] + (b[1] - a[1]) * u)),
)
class World:
"""Black field. One continuous beam scans every dot that must change."""
def __init__(self, vw, vh):
self.vw = int(vw)
self.vh = int(vh)
self.layout = layout_for(self.vw, self.vh)
self.bounds = self.layout["bounds"]
self.floor = self.layout["floor"]
self.caption = ("", "")
self.perimeter = _perimeter(self.vw, self.vh)
self.second_u = 0.0
self.live = [] # {slot, pixels, born}
self.breaks = [] # a digit tipping back, then shards
self.shards = [] # dark debris, separate from weld sparks
self.sparks = []
self.flash = None # {pixels, until}
self.beam = None # continuous ray: origin stays, head sweeps
self.origin = None
self.head = None
self.queue = [] # dots still to strike, raster order
self.goal = [None] * 5 # char a slot is currently being built as
self.phase = "pause"
self.phase_until = None
self.started = False
self.shown = [None] * 5
self.prev = [None] * 5
self.announced = None
self.seen_complete = False
self.last_now = None
self.lead_hhmm = None # upcoming minute we already started
self.nozzle_t0 = None
self.nozzle_out_t = None
self.nozzle_out_a = 0.0
self.nozzle_pixels = []
def update(self, now, hhmm, clock=None):
self.second_u = _second_u(clock)
if not self.started:
self.started = True
self.last_now = now
self.phase = "pause"
self.phase_until = now + INTRO_PAUSE
self.caption = _caption(hhmm, clock)
return
dt = now - self.last_now
self.last_now = now
if dt < 0.0:
dt = 0.0
elif dt > 0.1:
dt = 0.1
self._move_sparks(dt)
self._advance_breaks(now, dt)
if self.nozzle_out_t is not None and (now - self.nozzle_out_t) >= NOZZLE_OUT:
self.nozzle_out_t = None
hhmm = self._target_hhmm(hhmm, clock)
self.caption = _caption(hhmm, clock)
target = _target_chars(hhmm)
if self.phase == "beam":
self._update_beam(now, target)
elif self.phase == "fade":
if now >= self.phase_until:
self._poll(now, target)
elif self.phase == "pause":
if now >= self.phase_until:
self._poll(now, target)
else:
self._poll(now, target)
def _move_sparks(self, dt):
floor = float(self.floor)
alive = []
for s in self.sparks:
s["age"] += dt
if s["age"] >= s["life"]:
continue
s["vy"] += SPARK_GRAVITY * dt
s["x"] += s["vx"] * dt
s["y"] += s["vy"] * dt
# Land and hop. A weak hit settles instead of buzzing on the floor.
if s["y"] >= floor and s["vy"] > 0.0:
s["y"] = floor
rebound = s["vy"] * SPARK_BOUNCE
s["vx"] *= SPARK_FRICTION
if rebound < 14.0:
s["vy"] = 0.0
else:
s["vy"] = -rebound
s["bounces"] = s.get("bounces", 0) + 1
alive.append(s)
if len(alive) > 80:
alive = alive[-80:]
self.sparks = alive
def _poll(self, now, target):
need = [i for i, ch in enumerate(target) if self.shown[i] != ch]
if not need:
self.phase = "hold"
self._drop_beam(now)
text = "".join(target)
if text != self.announced:
print("[LaserClock] forged {}".format(text))
self.announced = text
self.seen_complete = True
return
if self._break_slots(need, now):
self._hide_laser()
self.phase = "fade"
self.phase_until = self._break_until(now)
return
self._begin_pass(need, target, now)
def _slot_clearing(self, index, now):
for b in self.breaks:
if b["slot"] == index and now < b["until"]:
return True
return False
def _break_until(self, now):
until = now
for b in self.breaks:
if b["until"] > until:
until = b["until"]
if until <= now:
return now + BREAK_TIME
return until
def _break_slots(self, slots, now):
"""Tip live digits backwards. Returns True if any slot left the clock."""
slotset = set(slots)
grouped = {}
kept = []
for dot in self.live:
slot = dot["slot"]
if slot not in slotset:
kept.append(dot)
continue
grouped.setdefault(slot, []).append(dot)
self.live = kept
self.queue = [d for d in self.queue if d["slot"] not in slotset]
already = {b["slot"] for b in self.breaks if now < b["until"]}
ground = float(self.floor)
had = False
for i in slots:
if self.shown[i] is not None:
self.prev[i] = self.shown[i]
self.shown[i] = None
self.goal[i] = None
dots = grouped.get(i)
if not dots or i in already:
continue
had = True
xs = []
ys = []
for dot in dots:
for x, y in dot["pixels"]:
xs.append(x)
ys.append(y)
pivot_x = (min(xs) + max(xs)) / 2.0
bottom = float(max(ys))
floor = ground if ground > bottom else bottom
pieces = []
max_ly = 1.0
half = 1
for dot in dots:
px, py = dot["pixels"][0]
color = _heat_rgb(now - dot["born"], now, px, py)
for x, y in dot["pixels"]:
lx = x - pivot_x
ly = bottom - y
if ly > max_ly:
max_ly = ly
if abs(lx) > half:
half = abs(lx)
pieces.append({"lx": lx, "ly": ly, "color": color})
self.breaks.append({
"slot": i,
"t0": now,
"until": now + BREAK_TIME,
"pieces": pieces,
"pivot_x": pivot_x,
"bottom": bottom,
"ground": floor,
"span": max_ly,
"half": int(half) + 1,
"shattered": False,
})
return had
def _advance_breaks(self, now, dt):
slam_at = FALL_TIME + SLAM_TIME
alive = []
for b in self.breaks:
if (now - b["t0"]) >= slam_at and not b["shattered"]:
self._spawn_shards(b)
b["shattered"] = True
if now < b["until"]:
alive.append(b)
self.breaks = alive
self._move_shards(dt)
def _spawn_shards(self, b):
"""Dim debris from the floor line. Not the bright weld sparks."""
room = 120 - len(self.shards)
if room <= 0:
return
pieces = b["pieces"]
step = 1
if len(pieces) > room:
step = (len(pieces) + room - 1) // room
theta = math.pi * 0.5
pivot_x = b["pivot_x"]
ground = b["ground"]
for i, p in enumerate(pieces):
if i % step:
continue
sx, _sy = self._project(p["lx"], p["ly"], pivot_x, ground, theta)
spread = sx - pivot_x
if spread > 0.3:
direction = 1.0
elif spread < -0.3:
direction = -1.0
else:
direction = random.choice((-1.0, 1.0))
vx = direction * random.uniform(8.0, 22.0) + random.uniform(-4.0, 4.0)
vy = -random.uniform(14.0, 34.0)
life = random.uniform(SHATTER_TIME * 0.75, SHATTER_TIME)
self.shards.append({
"x": sx,
"y": ground - 0.2,
"vx": vx,
"vy": vy,
"age": 0.0,
"life": life,
"r": random.randint(34, 70),
"g": random.randint(2, 10),
})
def _move_shards(self, dt):
alive = []
for s in self.shards:
s["age"] += dt
if s["age"] >= s["life"]:
continue
s["vy"] += SHARD_GRAVITY * dt
s["x"] += s["vx"] * dt
s["y"] += s["vy"] * dt
if s["y"] > self.vh + 1:
continue
alive.append(s)
if len(alive) > 140:
alive = alive[-140:]
self.shards = alive
def _project(self, lx, ly, pivot_x, pivot_y, theta):
"""Tip ly up off the hinge. theta 0 is upright, pi/2 is flat on the floor."""
z = ly * math.sin(theta)
height = ly * math.cos(theta)
denom = 1.0 + z * PERSPECTIVE
return (pivot_x + lx / denom, pivot_y - height / denom)
def _break_pose(self, b, now):
age = now - b["t0"]
if age < 0.0:
age = 0.0
ground = b["ground"]
bottom = b["bottom"]
if age < FALL_TIME:
u = age / FALL_TIME
ease = u * u
theta = (math.pi * 0.5) * ease
pivot_y = bottom + (ground - bottom) * ease
shade = 0.16 + 0.84 * math.cos(theta)
slam = 0.0
elif age < FALL_TIME + SLAM_TIME:
theta = math.pi * 0.5
local = (age - FALL_TIME) / SLAM_TIME
# Bite one pixel into the floor, then settle on it.
pivot_y = ground + (1.0 if local < 0.45 else 0.0)
slam = 1.0 - local
shade = 0.16 + 0.40 * slam
else:
theta = math.pi * 0.5
pivot_y = ground
shade = 0.0
slam = 0.0
cap = float(self.layout["date_top"] - 1)
if pivot_y > cap:
pivot_y = cap
return theta, pivot_y, shade, slam
def _begin_pass(self, slots, target, now):
"""One sweep, in reading order, over every slot that changed."""
self.queue = []
self.goal = [None] * 5
self.origin = self._pass_origin()
self.head = self.origin
self._arm_nozzle(now)
for i in slots:
self._enqueue_slot(i, target[i])
names = " ".join(SLOT_NAMES[i] for i in slots)
if self.seen_complete:
print("[LaserClock] reweld {} ({} dots)".format(names, len(self.queue)))
else:
print("[LaserClock] welding {} dots across {} (left to right, top to bottom)".format(
len(self.queue), names))
self._launch_next(now, target)
def _enqueue_slot(self, index, ch):
self.goal[index] = ch
self.shown[index] = None
for pixels in dots_for(self.layout, index, ch):
self.queue.append({"slot": index, "ch": ch, "pixels": pixels})
self.queue.sort(key=lambda d: _dot_key(d["pixels"]))
def _reconcile(self, now, target):
"""Drop stale dots and pick up digits that changed during the sweep."""
self.queue = [
d for d in self.queue
if target[d["slot"]] == d["ch"] and self.goal[d["slot"]] == d["ch"]
]
stale = []
for i, ch in enumerate(target):
if self.goal[i] not in (None, ch) or self.shown[i] not in (None, ch):
stale.append(i)
if stale:
self._break_slots(stale, now)
current = self.beam["dot"] if self.beam else None
for i, ch in enumerate(target):
if self.shown[i] == ch:
continue
if self._slot_clearing(i, now):
continue
if self.goal[i] == ch and any(d["slot"] == i for d in self.queue):
continue
if (
current is not None
and current["slot"] == i
and current["ch"] == ch
and self.goal[i] == ch
):
continue
self._enqueue_slot(i, ch)
def _launch_next(self, now, target):
self._reconcile(now, target)
if self.breaks and any(now < b["until"] for b in self.breaks):
# A digit is decaying. Leave the queue; the weld resumes after.
self._hide_laser()
self.phase = "fade"
self.phase_until = self._break_until(now)
return
if not self.queue:
pending = [
i for i, ch in enumerate(target)
if self.shown[i] != ch
]
if pending and any(self._slot_clearing(i, now) for i in pending):
self._hide_laser()
self.phase = "fade"
self.phase_until = self._break_until(now)
return
self._drop_beam(now)
self.phase = "hold"
return
item = self.queue.pop(0)
aim = _aim_of(item["pixels"])
frm = self.head if self.head is not None else self.origin
self.beam = {
"origin": self.origin,
"frm": frm,
"to": aim,
"step": 0,
"dot": item,
}
self.phase = "beam"
def _hide_laser(self):
"""Digit decay plays alone. Beam and nozzle are gone, not faded."""
self.beam = None
self.nozzle_t0 = None
self.nozzle_out_t = None
self.nozzle_out_a = 0.0
self.nozzle_pixels = []
def _arm_nozzle(self, now):
"""Start a fade-in. Leave an in-progress cycle alone."""
self.nozzle_out_t = None
if self.nozzle_t0 is None:
self.nozzle_t0 = now
def _drop_beam(self, now):
"""Beam off. The nozzle finishes fading out instead of popping off."""
if self.beam is not None and self.nozzle_t0 is not None:
self.nozzle_out_a = self._nozzle_alpha(now)
self.nozzle_out_t = now
self.nozzle_t0 = None
self.beam = None
def _nozzle_alpha(self, now):
"""0 → 1 → 0 across one period. Fully gone at both ends."""
if self.nozzle_t0 is None:
return 0.0
u = (now - self.nozzle_t0) / NOZZLE_PERIOD
u = u - math.floor(u)
return math.sin(math.pi * u)
def _target_hhmm(self, hhmm, clock):
"""Use the next minute a few seconds early, and stick with it."""
if self.lead_hhmm is not None:
if hhmm == self.lead_hhmm:
self.lead_hhmm = None
else:
return self.lead_hhmm
if (
clock is None
or self.phase != "hold"
or not self.seen_complete
or self.beam is not None
):
return hhmm
sec = clock.second + clock.microsecond / 1000000.0
if sec < 60.0 - LEAD_SECONDS:
return hhmm
nxt = (clock.replace(second=0, microsecond=0) + timedelta(minutes=1)).strftime("%H%M")
if nxt == hhmm:
return hhmm
self.lead_hhmm = nxt
left = 60.0 - sec
print("[LaserClock] {} → {} in {:.0f}s, old digits fall and shatter".format(
hhmm[:2] + ":" + hhmm[2:], nxt[:2] + ":" + nxt[2:], left
))
return nxt
def _pass_origin(self):
"""One screen corner for this weld. It stays put until the next pass."""
vw, vh = self.vw, self.vh
corners = (
(0, 0),
(vw - 1, 0),
(0, vh - 1),
(vw - 1, vh - 1),
)
for ox, oy in random.sample(corners, len(corners)):
if _outside(ox, oy, self.bounds):
return (ox, oy)
return corners[0]
def _update_beam(self, now, target):
if self.beam is None:
self.phase = "hold"
return
self.beam["step"] += 1
if self.beam["step"] < HOP_STEPS:
return
item = self.beam["dot"]
if item is not None and target[item["slot"]] == item["ch"] and self.goal[item["slot"]] == item["ch"]:
self._ignite(item, now)
if item is not None:
self.head = self.beam["to"]
self._launch_next(now, target)
def _ignite(self, item, now):
self.live.append({
"slot": item["slot"],
"pixels": item["pixels"],
"born": now,
})
aim = _aim_of(item["pixels"])
self._spawn_sparks(aim[0], aim[1])
self._spawn_flash(item["pixels"], now)
if not any(d["slot"] == item["slot"] for d in self.queue):
self.shown[item["slot"]] = item["ch"]
self.prev[item["slot"]] = item["ch"]
self.goal[item["slot"]] = None
def _spawn_sparks(self, x, y):
count = random.randint(10, 14)
for i in range(count):
ang = random.uniform(0.0, math.tau)
if i < 4:
# These last long enough to reach the floor and hop.
speed = random.uniform(12.0, 22.0)
life = random.uniform(1.05, 1.45)
else:
speed = random.uniform(10.0, 18.0)
life = random.uniform(0.30, 0.62)
vx = math.cos(ang) * speed
vy = math.sin(ang) * speed - random.uniform(6.0, 14.0)
# Already in flight on the impact frame, clear of the white dot.
kick = 0.05
self.sparks.append({
"x": float(x) + 0.5 + vx * kick,
"y": float(y) + 0.5 + vy * kick,
"vx": vx,
"vy": vy,
"age": 0.0,
"life": life,
"bounces": 0,
})
def _spawn_flash(self, dot, now):
blocked = set(dot)
halo = []
for x, y in dot:
for ox, oy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
p = (x + ox, y + oy)
if p not in blocked:
halo.append(p)
self.flash = {"pixels": halo, "until": now + FLASH_TIME}
def _beam_pixels(self):
"""Full ray from the fixed origin to the moving head. Stays on."""
beam = self.beam
if beam is None:
return []
steps = HOP_STEPS
if steps <= 1:
u = 1.0
else:
u = beam["step"] / float(steps - 1)
if u < 0.0:
u = 0.0
elif u > 1.0:
u = 1.0
ox, oy = beam["origin"]
hx, hy = _lerp(beam["frm"], beam["to"], u)
full = _line(ox, oy, hx, hy)
dot = beam.get("dot")
if dot is not None and u < 1.0:
blocked = set(dot["pixels"])
while full and full[-1] in blocked:
full.pop()
if not full:
full = [(ox, oy)]
return full
def render(self, now):
vw, vh = self.vw, self.vh
buf = [[(0, 0, 0) for _ in range(vw)] for _ in range(vh)]
for dot in self.live:
px, py = dot["pixels"][0]
age = now - dot["born"]
rgb = _heat_rgb(age, now, px, py)
for x, y in dot["pixels"]:
_set(buf, x, y, rgb, vw, vh)
# Short halo while the dot is still white-hot. Only onto empty LEDs.
if age < 0.28:
spill = int(90 * (1.0 - age / 0.28))
glow = (spill, spill // 2, 0)
blocked = set(dot["pixels"])
for x, y in dot["pixels"]:
for ox, oy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = x + ox, y + oy
if (nx, ny) in blocked:
continue
if nx < 0 or ny < 0 or nx >= vw or ny >= vh:
continue
if buf[ny][nx] == (0, 0, 0):
_max(buf, nx, ny, glow, vw, vh)
self._paint_breaks(buf, now, vw, vh)
self._paint_shards(buf, vw, vh)
if self.flash is not None and now < self.flash["until"]:
for x, y in self.flash["pixels"]:
_max(buf, x, y, (255, 255, 255), vw, vh)
for s in self.sparks:
u = s["age"] / s["life"] if s["life"] else 1.0