Skip to content

Commit 85f5ed4

Browse files
author
Kevin J Walters
committed
Adding a new animation mode with a short tune.
1 parent 3bf4d44 commit 85f5ed4

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

maker-nano-rp2040/filament-led-cycling.py

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
### filament-led-cycling v1.0
1+
### filament-led-cycling v1.1
22
### Basic test patterns using pwm for six channel constant current LED driver circuit
33

44
### Tested on Cytron Maker Nano RP2040 with CircuitPython 9.0.5
@@ -32,6 +32,7 @@
3232
### TODO - auto-adjust using ambient light sensor
3333
### TODO - add current estimate in code based
3434
### on RP2040 base usage plus LED brightness levels, speaker use and RGB pixels
35+
### TODO - add one_by_one mode for checking current on each channel
3536

3637
import random
3738
import time
@@ -279,6 +280,46 @@ def larson_scanner(time_ns,
279280
## audio_out.play(scanner_sample) ### this sounds terrible
280281

281282

283+
tune1 = [( 0, 1.0, [(0, 0.1), (1, 0.1), (2, 0.1), (3, 0.1), (4, 0.1)]),
284+
(69, 0.25, [(4, 0.7)]), ### tone,
285+
(71, 0.25, [(3, 0.7)]), ### up a full tone,
286+
(67, 0.25, [(2, 0.7)]), ### down a major third,
287+
(55, 0.25, [(0, 0.7)]), ### now drop an octave,
288+
(62, 1.25, [(1, 0.7)]), ### up a perfect fifth.
289+
( 0, 0.75, [(0, 0.4), (1, 0.4), (2, 0.4), (3, 0.4), (4, 0.4)]) ### (rest, wait for arrival)
290+
]
291+
tune1_len = sum([note[1] for note in tune1])
292+
def some_notes(time_ns,
293+
state ### pylint: disable=unused-argument
294+
):
295+
"""This turns them all off for 2 seconds, then adds one every 2 seconds.
296+
This is intended to facilitate current measurement from slow meters.
297+
"""
298+
if len(state) == 0:
299+
state["start_ns"] = time_ns
300+
state["barlength"] = 60 / 90 * 4
301+
state["notegap"] = 0.05
302+
state["length_ns"] = round(tune1_len * state["barlength"] * 1e9)
303+
state["next_note"] = 0
304+
305+
reltime_ns = (time_ns - state["start_ns"]) % state["length_ns"]
306+
307+
note_start = 0.0
308+
for idx, (midi_note, length, pwm_changes) in enumerate(tune1):
309+
note_len = length * state["barlength"]
310+
note_end = note_start + note_len
311+
if idx == state["next_note"] and note_start <= reltime_ns / 1e9 < note_end:
312+
state["next_note"] = (idx + 1) % len(tune1)
313+
for pwm_idx, brightness in pwm_changes:
314+
pwms[pwm_idx].duty_cycle = brightness_to_dc(brightness)
315+
if midi_note > 0:
316+
simpleio.tone(SPEAKER_PIN,
317+
midi_to_freq(midi_note),
318+
note_len - state["notegap"])
319+
break
320+
note_start = note_end
321+
322+
282323
def staggered_on(time_ns,
283324
state ### pylint: disable=unused-argument
284325
):
@@ -349,7 +390,7 @@ def calc_temperature(vbes, temp_coef):
349390

350391

351392
mode = 0
352-
MODE_COUNT = 5
393+
MODE_COUNT = 6
353394

354395
### Loop is currently 8-10ms
355396
min_loop_pause_ns = 5_000_000 ### 5ms
@@ -422,6 +463,8 @@ def calc_temperature(vbes, temp_coef):
422463
elif mode == 3:
423464
larson_scanner(time_diff_ns, anim_state)
424465
elif mode == 4:
466+
some_notes(time_diff_ns, anim_state)
467+
elif mode == 5:
425468
staggered_on(time_diff_ns, anim_state)
426469

427470
last_loop_ns = now_ns

0 commit comments

Comments
 (0)