Files
led-driver/test/patterns/chase.py
jimmy b7d2f52fc3 Refactor patterns to use preset-based API and fix initialization order
- Fix initialization order: initialize self.presets before calling self.select()
- Separate add() and edit() methods: add() creates new presets, edit() updates existing ones
- Update all test files to use add() instead of edit() for creating presets
- Add comprehensive auto/manual mode test
- Remove tool.py (moved to led-tool project)
2026-01-25 23:23:14 +13:00

120 lines
2.8 KiB
Python

#!/usr/bin/env python3
import utime
from machine import WDT
from settings import Settings
from patterns import Patterns
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 = Patterns(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.add("chase1", {
"pattern": "chase",
"brightness": 255,
"delay": 200,
"n1": 5,
"n2": 5,
"n3": 1,
"n4": 1,
"colors": [(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.add("chase2", {
"pattern": "chase",
"n1": 3,
"n2": 3,
"n3": 2,
"n4": -1,
"delay": 150,
"colors": [(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.add("chase3", {
"pattern": "chase",
"n1": 10,
"n2": 5,
"n3": 3,
"n4": 3,
"delay": 200,
"colors": [(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.add("chase4", {
"pattern": "chase",
"n1": 4,
"n2": 4,
"n3": 5,
"n4": 5,
"delay": 100,
"colors": [(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.add("chase5", {
"pattern": "chase",
"n1": 6,
"n2": 4,
"n3": -2,
"n4": -2,
"delay": 200,
"colors": [(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.add("chase6", {
"pattern": "chase",
"n1": 5,
"n2": 5,
"n3": 3,
"n4": -2,
"delay": 250,
"colors": [(255, 0, 0), (0, 255, 0)]
})
p.select("chase6")
run_for(p, wdt, 4000)
# Cleanup
print("Test complete, turning off")
p.add("cleanup_off", {"pattern": "off"})
p.select("cleanup_off")
run_for(p, wdt, 100)
if __name__ == "__main__":
main()