Files
led-driver/test/patterns/chase.py
jimmy 43957adb28 Rename patterns module to presets
Rename the driver module and update imports so tests and main entry use the new presets naming, while moving Preset to its own file.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-07 11:40:04 +13:00

162 lines
4.0 KiB
Python

#!/usr/bin/env python3
import utime
from machine import WDT
from settings import Settings
from presets import Presets
def run_for(p, wdt, ms):
"""Helper: run current pattern for given ms using tick()."""
start = utime.ticks_ms()
while utime.ticks_diff(utime.ticks_ms(), start) < ms:
wdt.feed()
p.tick()
utime.sleep_ms(10)
def main():
s = Settings()
pin = s.get("led_pin", 10)
num = s.get("num_leds", 30)
p = Presets(pin=pin, num_leds=num)
wdt = WDT(timeout=10000)
# Test 1: Basic chase (n1=5, n2=5, n3=1, n4=1)
print("Test 1: Basic chase (n1=5, n2=5, n3=1, n4=1)")
p.edit("chase1", {
"p": "chase",
"b": 255,
"d": 200,
"n1": 5,
"n2": 5,
"n3": 1,
"n4": 1,
"c": [(255, 0, 0), (0, 255, 0)],
})
p.select("chase1")
run_for(p, wdt, 3000)
# Test 2: Forward and backward (n3=2, n4=-1)
print("Test 2: Forward and backward (n3=2, n4=-1)")
p.edit("chase2", {
"p": "chase",
"n1": 3,
"n2": 3,
"n3": 2,
"n4": -1,
"d": 150,
"c": [(0, 0, 255), (255, 255, 0)],
})
p.select("chase2")
run_for(p, wdt, 3000)
# Test 3: Large segments (n1=10, n2=5)
print("Test 3: Large segments (n1=10, n2=5, n3=3, n4=3)")
p.edit("chase3", {
"p": "chase",
"n1": 10,
"n2": 5,
"n3": 3,
"n4": 3,
"d": 200,
"c": [(255, 128, 0), (128, 0, 255)],
})
p.select("chase3")
run_for(p, wdt, 3000)
# Test 4: Fast movement (n3=5, n4=5)
print("Test 4: Fast movement (n3=5, n4=5)")
p.edit("chase4", {
"p": "chase",
"n1": 4,
"n2": 4,
"n3": 5,
"n4": 5,
"d": 100,
"c": [(255, 0, 255), (0, 255, 255)],
})
p.select("chase4")
run_for(p, wdt, 2000)
# Test 5: Backward movement (n3=-2, n4=-2)
print("Test 5: Backward movement (n3=-2, n4=-2)")
p.edit("chase5", {
"p": "chase",
"n1": 6,
"n2": 4,
"n3": -2,
"n4": -2,
"d": 200,
"c": [(255, 255, 255), (0, 0, 0)],
})
p.select("chase5")
run_for(p, wdt, 3000)
# Test 6: Alternating forward/backward (n3=3, n4=-2)
print("Test 6: Alternating forward/backward (n3=3, n4=-2)")
p.edit("chase6", {
"p": "chase",
"n1": 5,
"n2": 5,
"n3": 3,
"n4": -2,
"d": 250,
"c": [(255, 0, 0), (0, 255, 0)],
})
p.select("chase6")
run_for(p, wdt, 4000)
# Test 7: Manual mode - advance one step per beat
print("Test 7: Manual mode chase (auto=False, n3=2, n4=1)")
p.edit("chase_manual", {
"p": "chase",
"n1": 4,
"n2": 4,
"n3": 2,
"n4": 1,
"d": 200,
"c": [(255, 255, 0), (0, 255, 255)],
"a": False,
})
p.step = 0 # Reset step counter
print(" Advancing pattern with 10 beats (select + tick)...")
for i in range(10):
p.select("chase_manual") # Simulate beat - restarts generator
p.tick() # Advance one step
utime.sleep_ms(500) # Pause to see the pattern
wdt.feed()
print(f" Beat {i+1}: step={p.step}")
# Test 8: Verify step increments correctly in manual mode
print("Test 8: Verify step increments (auto=False)")
p.edit("chase_manual2", {
"p": "chase",
"n1": 3,
"n2": 3,
"n3": 1,
"n4": 1,
"a": False,
})
p.step = 0
initial_step = p.step
p.select("chase_manual2")
p.tick()
final_step = p.step
print(f" Step updated from {initial_step} to {final_step} (expected: 1)")
if final_step == 1:
print(" ✓ Step increment working correctly")
else:
print(f" ✗ Step increment mismatch! Expected 1, got {final_step}")
# Cleanup
print("Test complete, turning off")
p.edit("cleanup_off", {"p": "off"})
p.select("cleanup_off")
run_for(p, wdt, 100)
if __name__ == "__main__":
main()