Files
led-hoop/pico/test/test_roll.py
2026-03-05 20:25:57 +13:00

119 lines
3.1 KiB
Python

"""
On-device test for the Roll pattern using mpremote.
Usage (from pico/ dir or project root with adjusted paths):
mpremote connect <device> cp src/*.py :
mpremote connect <device> cp src/patterns/*.py :patterns
mpremote connect <device> cp lib/*.py :
mpremote connect <device> cp test/test_roll.py :
mpremote connect <device> run test_roll.py
This script:
- Instantiates Presets
- Creates a few in-memory roll presets with different parameters
- Runs each one for a short time so you can visually compare behaviour
"""
import utime
from presets import Presets, Preset
def make_roll_preset(name, color1, color2, n1=0, n2=0, n3=0, n4=0, delay_ms=50, brightness=255):
"""Helper to build a Preset dict for the roll pattern."""
data = {
"p": "roll",
"c": [color1, color2],
"b": brightness,
"d": delay_ms,
"n1": n1,
"n2": n2,
"n3": n3,
"n4": n4,
"a": True, # animated
}
return name, Preset(data)
def run_preset(presets, name, preset_obj, duration_ms):
"""Run a given roll preset for duration_ms using the existing tick loop."""
presets.presets[name] = preset_obj
presets.select(name)
start = utime.ticks_ms()
while utime.ticks_diff(utime.ticks_ms(), start) < duration_ms:
presets.tick()
def main():
presets = Presets()
presets.load()
print("Starting roll pattern test...")
ref_len = presets.strip_length(0)
# Use some margins based on strip length to show different bands.
quarter = ref_len // 4
# Define a few different roll presets:
roll_presets = []
# 1. Full-strip, white -> off, clockwise, medium speed
roll_presets.append(
make_roll_preset(
"roll_full_cw",
color1=(255, 255, 255),
color2=(0, 0, 0),
n1=0,
n2=0,
n3=2, # 2 rotations then stop
n4=0, # clockwise
delay_ms=40,
brightness=255,
)
)
# 2. Inner band only, red -> blue, clockwise, slower
roll_presets.append(
make_roll_preset(
"roll_inner_band",
color1=(255, 0, 0),
color2=(0, 0, 255),
n1=quarter, # start margin
n2=quarter, # end margin
n3=2,
n4=0,
delay_ms=60,
brightness=255,
)
)
# 3. Full-strip, green -> off, counter-clockwise, faster
roll_presets.append(
make_roll_preset(
"roll_full_ccw",
color1=(0, 255, 0),
color2=(0, 0, 0),
n1=0,
n2=0,
n3=2,
n4=1, # counter-clockwise
delay_ms=30,
brightness=255,
)
)
# Run each roll preset for about 5 seconds
for name, preset_obj in roll_presets:
print("Running roll preset:", name)
run_preset(presets, name, preset_obj, duration_ms=5000)
print("Roll pattern test finished. Turning off LEDs.")
presets.select("off")
presets.tick()
if __name__ == "__main__":
main()