a circuit playground express wizard staff

blinkenring.py 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import urandom
  2. import neopixel
  3. class BlinkenRing:
  4. def __init__(self, pin, pixel_count, pixel_spacer):
  5. self.party_mode = False
  6. self.pixel_off = (0, 0, 0)
  7. self.offset = 0
  8. # Pin where the NeoPixel ring is located:
  9. self.pin = pin
  10. # Number of pixels on the ring:
  11. self.pixel_count = pixel_count
  12. # Space between pixels to light:
  13. self.pixel_spacer = pixel_spacer
  14. self.randomize_color()
  15. # https://github.com/adafruit/Adafruit_CircuitPython_NeoPixel
  16. self.pixels = neopixel.NeoPixel(pin, pixel_count, auto_write=False)
  17. self.pixels.brightness = 0.35
  18. def randomize_color(self):
  19. self.set_color((urandom.randrange(0, 127, 1),
  20. urandom.randrange(0, 127, 1),
  21. urandom.randrange(0, 127, 1)))
  22. def set_color(self, color):
  23. self.red = color[0]
  24. self.green = color[1]
  25. self.blue = color[2]
  26. def get_color(self):
  27. return (self.red, self.green, self.blue)
  28. def go_blank(self):
  29. self.pixels.fill((0, 0, 0))
  30. self.pixels.show()
  31. # Advance one frame of animation:
  32. def animate(self):
  33. if self.party_mode:
  34. self.randomize_color()
  35. # Kill previous trailing pixels:
  36. for target_pixel in range(self.offset - 1, self.pixel_count, self.pixel_spacer):
  37. self.pixels[target_pixel] = self.pixel_off
  38. # Light trailing pixels:
  39. for target_pixel in range(self.offset, self.pixel_count, self.pixel_spacer):
  40. self.pixels[target_pixel] = (int(self.red / 4), int(self.green / 4), int(self.blue / 4))
  41. # Increase pixel offset until it hits 6, then roll back to 0:
  42. self.offset += 1
  43. if self.offset == self.pixel_spacer:
  44. self.offset = 0;
  45. # Light new pixels:
  46. color = (self.red, self.green, self.blue)
  47. for target_pixel in range(self.offset, self.pixel_count, self.pixel_spacer):
  48. self.pixels[target_pixel] = color
  49. self.pixels.show()