44 lines
1.4 KiB
Python
44 lines
1.4 KiB
Python
"""CircuitPython NeoPixel LED utility.
|
|
|
|
Uses the neopixel library and time.monotonic() to run breathing and rainbow animations.
|
|
"""
|
|
|
|
import math
|
|
import time
|
|
import neopixel
|
|
|
|
class BoardLED:
|
|
def __init__(self, pin):
|
|
# Initialize 1 NeoPixel on the specified pin
|
|
self.np = neopixel.NeoPixel(pin, 1, brightness=1.0, auto_write=False)
|
|
self.base_color = (0, 0, 0)
|
|
self.off()
|
|
|
|
def set_color(self, r, g, b):
|
|
self.base_color = (r, g, b)
|
|
self.np[0] = (r, g, b)
|
|
self.np.show()
|
|
|
|
def off(self):
|
|
self.set_color(0, 0, 0)
|
|
|
|
def update_breathing(self, speed_factor=1.0):
|
|
if self.base_color == (0, 0, 0):
|
|
return
|
|
t = time.monotonic() * speed_factor
|
|
# Sine wave from 0.05 to 1.0
|
|
factor = 0.525 + 0.475 * math.sin(t * math.pi)
|
|
br = int(self.base_color[0] * factor)
|
|
bg = int(self.base_color[1] * factor)
|
|
bb = int(self.base_color[2] * factor)
|
|
self.np[0] = (br, bg, bb)
|
|
self.np.show()
|
|
|
|
def update_rainbow(self, speed_factor=0.2):
|
|
t = time.monotonic() * speed_factor
|
|
r = int(127.5 * (1.0 + math.sin(t * 2.0 * math.pi)))
|
|
g = int(127.5 * (1.0 + math.sin(t * 2.0 * math.pi + 2.0 * math.pi / 3.0)))
|
|
b = int(127.5 * (1.0 + math.sin(t * 2.0 * math.pi + 4.0 * math.pi / 3.0)))
|
|
self.np[0] = (r, g, b)
|
|
self.np.show()
|