|
1 |
| -### filament-led-cycling v1.0 |
| 1 | +### filament-led-cycling v1.1 |
2 | 2 | ### Basic test patterns using pwm for six channel constant current LED driver circuit
|
3 | 3 |
|
4 | 4 | ### Tested on Cytron Maker Nano RP2040 with CircuitPython 9.0.5
|
|
32 | 32 | ### TODO - auto-adjust using ambient light sensor
|
33 | 33 | ### TODO - add current estimate in code based
|
34 | 34 | ### 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 |
35 | 36 |
|
36 | 37 | import random
|
37 | 38 | import time
|
@@ -279,6 +280,46 @@ def larson_scanner(time_ns,
|
279 | 280 | ## audio_out.play(scanner_sample) ### this sounds terrible
|
280 | 281 |
|
281 | 282 |
|
| 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 | + |
282 | 323 | def staggered_on(time_ns,
|
283 | 324 | state ### pylint: disable=unused-argument
|
284 | 325 | ):
|
@@ -349,7 +390,7 @@ def calc_temperature(vbes, temp_coef):
|
349 | 390 |
|
350 | 391 |
|
351 | 392 | mode = 0
|
352 |
| -MODE_COUNT = 5 |
| 393 | +MODE_COUNT = 6 |
353 | 394 |
|
354 | 395 | ### Loop is currently 8-10ms
|
355 | 396 | min_loop_pause_ns = 5_000_000 ### 5ms
|
@@ -422,6 +463,8 @@ def calc_temperature(vbes, temp_coef):
|
422 | 463 | elif mode == 3:
|
423 | 464 | larson_scanner(time_diff_ns, anim_state)
|
424 | 465 | elif mode == 4:
|
| 466 | + some_notes(time_diff_ns, anim_state) |
| 467 | + elif mode == 5: |
425 | 468 | staggered_on(time_diff_ns, anim_state)
|
426 | 469 |
|
427 | 470 | last_loop_ns = now_ns
|
|
0 commit comments