-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.py
More file actions
186 lines (151 loc) · 6.04 KB
/
code.py
File metadata and controls
186 lines (151 loc) · 6.04 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
import gc
import time
import board
import asyncio
import adafruit_neopxl8
from the_sign import Sign
from the_sign.animations.knight_rider import KnightRider
from the_sign.animations.confetti import Confetti
from the_sign.animations.rainbow_shimmer import RainbowShimmer
from the_sign.animations.shimmer import Shimmer
from the_sign.animations.rainbow_cycle import RainbowRingCycle
from the_sign.animations.innovation_norge import InnovationNorge
from the_sign.animations.sparkle import Sparkle
from the_sign.animations.static import SolidColor
from the_sign.animations.snake import Snake
from the_sign.color import Colors
from micropython import const
NUM_PIXELS = const(37)
MS_PER_NS = const(1_000_000)
S_PER_NS = const(1_000 * MS_PER_NS)
class App:
def __init__(self, frame_rate: int = 25, max_brightness: float = 1.0):
self.start = None
self.frame_rate = frame_rate
self.max_brightness = max_brightness
self.animation_length = 60
self.pixels = adafruit_neopxl8.NeoPxl8(
data0=board.NEOPIXEL0,
n=NUM_PIXELS,
num_strands=1,
bpp=4,
brightness=self.max_brightness,
auto_write=False,
pixel_order=adafruit_neopxl8.GRBW,
)
self.sign = Sign(self.pixels)
self.sign.clear()
self.current_animation = -1
self.next_animation_at = 0
# shimmer_intelecy = Shimmer(
# c1=Colors.INTELECY,
# c2=Colors.INTELECY.with_brightness(0.4),
# duration=self.animation_length,
# frame_rate=self.frame_rate,
# )
shimmer_intelecy = SolidColor(
color=Colors.INTELECY,
frame_rate=self.frame_rate,
)
shimmer_white = Shimmer(
c1=Colors.WHITE,
c2=Colors.WHITE.with_brightness(0.25),
duration=self.animation_length,
frame_rate=self.frame_rate,
)
self.animations = [
Snake(frame_rate=self.frame_rate),
shimmer_white,
Confetti(spark_duration_frames=10, frame_rate=self.frame_rate),
shimmer_intelecy,
RainbowShimmer(
duration=5,
frame_rate=self.frame_rate,
),
shimmer_white,
KnightRider(
color=Colors.RED,
duration=2,
frame_rate=self.frame_rate,
),
shimmer_intelecy,
RainbowRingCycle(duration=8, frame_rate=self.frame_rate),
shimmer_white,
Sparkle(
base_color=Colors.INTELECY,
# flash_color=Colors.YELLOW,
frame_rate=self.frame_rate,
),
shimmer_white,
Shimmer(
c1=Colors.YELLOW,
c2=Colors.RED,
speed=3,
duration=self.animation_length,
frame_rate=self.frame_rate,
),
]
async def draw_frame(self):
now = time.monotonic()
if now > self.next_animation_at:
self.next_animation_at = now + self.animation_length
self.current_animation = (self.current_animation + 1) % len(self.animations)
self.animations[self.current_animation].setup(self.sign)
gc.collect()
total_memory = gc.mem_alloc() + gc.mem_free()
free_memory = gc.mem_free()
print(f"{self.current_animation}")
print(f"Total memory: {total_memory} bytes")
print(f"Free memory: {free_memory} bytes")
print(f"Used memory: {gc.mem_alloc()} bytes")
self.start = time.monotonic_ns()
self.animations[self.current_animation].exec(self.sign, self.frame_number)
self.sign.show()
@property
def elapsed(self) -> float:
return (time.monotonic_ns() - self.start) / 1_000_000_000
@property
def frame_number(self) -> int:
return int(self.frame_rate * self.elapsed)
async def animate(self):
await call_at_rate(self.frame_rate, self.draw_frame)
# async def next_animation(self):
# print(f"next animation {self.current_animation}")
# self.current_animation = (self.current_animation + 1) % len(self.animations)
# self.animations[self.current_animation].setup(self.sign)
async def run(self, run_for: float = 1.0):
print("starting...")
self.start = time.monotonic_ns()
await asyncio.gather(
call_at_rate("draw", self.frame_rate, self.draw_frame),
# call_at_rate("next", 1 / 10, self.next_animation),
)
async def call_at_rate(lbl: str, frame_rate: float, target_func, *args, **kwargs):
"""
Call the target function at the specified framerate.
Args:
frame_rate (float): The desired frequency in Hz (e.g., 30 for 30 calls per second).
target_func (coroutine): The async function to call.
*args: Positional arguments to pass to the target function.
**kwargs: Keyword arguments to pass to the target function.
"""
interval = int(const(1000) // frame_rate)
next_call_time = time.monotonic_ns() // MS_PER_NS
while True:
# print(f"{lbl} {time.monotonic_ns() // MS_PER_NS} calling target func...")
await target_func(*args, **kwargs)
# print(f"{lbl} {time.monotonic_ns() // MS_PER_NS} done calling target func")
next_call_time += interval
sleep_time = next_call_time - (time.monotonic_ns() // MS_PER_NS)
if sleep_time < 0:
print(
f"{lbl} {time.monotonic_ns() // MS_PER_NS} target_func took too long to run. over={-1 * sleep_time} ms interval={interval}"
)
next_call_time = time.monotonic_ns() // MS_PER_NS
# else:
# print(sleep_time)
# print(f"{lbl} {time.monotonic_ns() // MS_PER_NS} sleeping for {sleep_time} ms")
sleep_time = max(0, sleep_time)
await asyncio.sleep_ms(sleep_time)
if board.board_id == "adafruit_feather_rp2040_scorpio":
asyncio.run(App(frame_rate=2 * (1_000 // NUM_PIXELS)).run())