Mercurial > code > home > repos > micro
diff ub-timer/main.py @ 24:872d8155ed0c default tip
start ub-timer
author | drewp@bigasterisk.com |
---|---|
date | Tue, 24 Dec 2024 21:36:53 -0800 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ub-timer/main.py Tue Dec 24 21:36:53 2024 -0800 @@ -0,0 +1,101 @@ +''' +pin 11 = ws2812 ring +pin 39 = sw 4 +pin 37 = sw 3 +pin 35 = sw 2 +pin 33 = sw 1 +pin 40 = audio trigger 0 +pin 38 = audio trigger 1 +pin 36 = audio trigger 2 +pin 34 = audio trigger 3 + +pdm run ampy -p /dev/ttyACM0 put ub-timer/ ./ && pdm run ampy -p /dev/ttyACM0 reset --hard +minicom -b 115200 -D /dev/ttyACM0 +''' + +import time +from machine import Pin +from neopixel import NeoPixel +import random + +from colorsys_local import hsv_to_rgb + + +def rand_color(): + value = .1 + sat = 1 + hue = random.random() + return tuple(int(x * 255) for x in hsv_to_rgb(hue, sat, value)) + + +pin = Pin(11, Pin.OUT) +switch_pin = [ + Pin(33, Pin.IN, Pin.PULL_UP), + Pin(35, Pin.IN, Pin.PULL_UP), + Pin(37, Pin.IN, Pin.PULL_UP), + Pin(39, Pin.IN, Pin.PULL_UP), +] +audio_trigger = [ + Pin(40, Pin.OUT, value=1), + Pin(38, Pin.OUT, value=1), + Pin(36, Pin.OUT, value=1), + Pin(34, Pin.OUT, value=1), +] +song_duration = [ + 121.47, + 2.16, + 1, + 1, +] +num_leds = 12 +np = NeoPixel(pin, num_leds) +audio_start_ms = 1 +audio_end_ms = 0 +last_sw_pressed = [0, 0, 0, 0] + + +def startAudio(index, now): + global audio_start_ms, audio_end_ms + audio_trigger[index].value(0) + audio_start_ms = now + audio_end_ms = now + song_duration[index] * 1000 + for fr in range(num_leds): + np[fr] = rand_color() + np.write() + time.sleep(.02) + audio_trigger[index].value(1) + + +def rand_fill(start): + for fr in range(num_leds): + if fr > start: + np[fr] = rand_color() + else: + np[fr] = (0, 0, 0) + np.write() + + +def scan_buttons(now): + for index in range(4): + pressed = not switch_pin[index]() + if pressed != last_sw_pressed[index]: + last_sw_pressed[index] = pressed + if pressed: + startAudio(index, now) + + +def update_leds(now): + frac = (now - audio_start_ms) / (audio_end_ms - audio_start_ms) + if 0 < frac < 1: + cur = max(0, min(11, int(frac * num_leds))) + rand_fill(cur) + else: + np.fill((0, 0, 0)) + np.write() + + +while True: + now = time.ticks_ms() + update_leds(now) + scan_buttons(now) + time.sleep(.05)