Featureful Python controller code for WS2811/WS2812/NeoPixels
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

34 lines
1.2KB

  1. import time
  2. from gradients import Gradient
  3. class Pattern:
  4. def __init__(self, gradient, cycle_length, cycle_time_s=None, reverse=False, march=False):
  5. self.gradient = gradient
  6. self.cycle_length = cycle_length
  7. self.cycle_time_s = cycle_time_s or (cycle_length / 2 if march else cycle_length)
  8. self.reverse = reverse
  9. self.march = march
  10. self.last = None
  11. self.offset = 0
  12. def step(self, length):
  13. now = time.time()
  14. duration = 0 if self.last is None else now - self.last
  15. self.last = now
  16. offset_delta = self.cycle_length * duration / self.cycle_time_s
  17. self.offset = (self.offset + (offset_delta if self.reverse else -offset_delta)) % self.cycle_length
  18. offset = self.offset if not self.march else int(self.offset)
  19. return (self.gradient.at(((i + offset) / self.cycle_length + 1) % 1) for i in range(0, length))
  20. def from_dict(dict):
  21. return Pattern(
  22. Gradient.from_dict(dict['gradient']),
  23. dict['cycle_length'],
  24. cycle_time_s = dict.get('cycle_time_s'),
  25. reverse = not not dict.get('reverse', False),
  26. march = not not dict.get('march', False)
  27. )