forked from SleepUnit/OpenStickFirmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleds.py
67 lines (55 loc) · 1.55 KB
/
leds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import time
import board
import neopixel
RED = (255, 0, 0)
YELLOW = (255, 150, 0)
GREEN = (0, 255, 0)
CYAN = (0, 255, 255)
BLUE = (0, 0, 255)
PURPLE = (180, 0, 255)
WHEEL_FRAMES = 64
WHEEL_TOTAL_TIME = 1.4
WHEEL_FRAME_TIME = (WHEEL_TOTAL_TIME) / WHEEL_FRAMES
class Leds:
def __init__(self, pixels, pixel_group):
self.pixels = pixels
self.pixel_group = pixel_group
self.reset()
def reset(self):
self.current_frame = 0
self.last_frame_time = None
self.animation = None
self.color((0, 0, 0))
def start(self, animation):
if self.current_frame < 1:
self.current_frame = 0
self.last_frame_time = None
self.animation = animation
else:
self.current_frame = 0
def color(self, color):
for pixel in self.pixel_group:
self.pixels[pixel] = color
def animate(self):
if self.animation == "Wheel":
self.wheel()
def wheel(self):
if self.current_frame > WHEEL_FRAMES:
self.reset()
return
if self.last_frame_time == None or time.monotonic() - self.last_frame_time > WHEEL_FRAME_TIME:
self.color(self.wheel_position())
self.last_frame_time = time.monotonic()
self.current_frame = self.current_frame + 1
def wheel_position(self):
conversion = 255 / WHEEL_FRAMES
pos = self.current_frame * conversion
if pos < 0 or pos > 255:
return (0, 0, 0)
if pos < 85:
return (255 - pos * 3, pos * 3, 0)
if pos < 170:
pos -= 85
return (0, 255 - pos * 3, pos * 3)
pos -= 170
return (pos * 3, 0, 255 - pos * 3)