generated from Choate-Robotics/7407-DriveCode-Template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathleds.py
185 lines (157 loc) · 5.37 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from wpilib import AddressableLED, PowerDistribution
import math
class ALeds():
'''Addressable LEDS from PWM RIO'''
m_led: AddressableLED
# active_mode: dict
# speed: int
# brightness: float
# last_active_mode: dict
# last_speed: int
# last_brightness: float
# m_ledBuffer: AddressableLED.LEDData
class Type():
def KStatic(r, g, b):
return {
'type': 1,
'color': {
'r': r,
'g': g,
'b': b
}
}
def KRainbow():
return {
'type': 2
}
def KTrack(r1, g1, b1, r2, g2, b2):
return {
'type': 3,
'color': {
'r1': r1,
'g1': g1,
'b1': b1,
'r2': r2,
'g2': g2,
'b2': b2
}
}
def KBlink(r,g,b):
return {
'type': 4,
'color': {
'r': r,
'g': g,
'b': b
}
}
def __init__(self, id: int, size: int):
self.size = size
self.id = id
self.speed = 5
self.track_index = 0
self.blink_index = 0
self.active_mode = None
self.last_active_mode = None
self.last_brightness = None
self.last_speed = None
self.brightness = 1
def init(self):
self.m_rainbowFirstPixelHue = 0
self.m_led = AddressableLED(self.id)
self.m_led.setLength(self.size)
self.m_ledBuffer = self.m_led.LEDData
self.array = []
for i in range(self.size):
self.array.append(self.m_led.LEDData())
self.m_led.setData(self.array)
self.m_led.start()
def enable(self):
self.m_led.start()
def disable(self):
self.m_led.stop()
def run(self):
pass
def storeCurrent(self):
self.last_active_mode = self.active_mode
self.last_speed = self.speed
self.last_brightness = self.brightness
def setLED(self, type, brightness: float = 1.0, speed: int = 5):
self.storeCurrent()
self.active_mode = type
self.speed = speed
self.brightness = brightness
def getLED(self):
if self.active_mode == None:
return {
'type': 0,
'color': {
'r': 0,
'g': 0,
'b': 0
}
}
else:
return self.active_mode
def setLast(self):
self.active_mode = self.last_active_mode
self.speed = self.last_speed
self.brightness = self.last_brightness
def cycle(self):
'''
cycles through LED array
this should be called periodically
'''
match self.active_mode['type']:
case 1:
color = self.active_mode['color']
self._setStatic(color['r'], color['g'], color['b'])
case 2:
self._setRainbow()
case 3:
color = self.active_mode['color']
self._setTrack(color['r1'], color['g1'], color['b1'], color['r2'], color['g2'], color['b2'])
case 4:
color = self.active_mode['color']
self._setBlink(color['r'], color['g'], color['b'])
def _setStatic(self, red: int, green: int, blue: int):
for i in range(self.size):
self.array[i].setRGB(red, green, blue)
self.m_led.setData(self.array)
def _setRainbow(self):
for i in range(len(self.array)):
# Calculate the hue - hue is easier for rainbows because the color
# shape is a circle so only one value needs to precess
hue = math.floor((self.m_rainbowFirstPixelHue + (i * 180 / len(self.array))) % 180)
# Set the value
self.array[i].setHSV(hue, 255, 128)
# Increase by to make the rainbow "move"
self.m_rainbowFirstPixelHue += self.speed
# Check bounds
self.m_rainbowFirstPixelHue %= 180
self.m_led.setData(self.array)
def _setTrack(self, r1, g1, b1, r2, g2, b2):
for i in range(len(self.array)):
self.array[i].setRGB(r1, g1, b1)
for i in range(self.track_index, len(self.array), 4):
self.array[i].setRGB(r2, g2, b2)
self.track_index += 1
if self.track_index > len(self.array):
self.track_index = 0
self.m_led.setData(self.array)
def _setBlink(self, r,g,b):
if self.blink_index / 10 <= .5:
for i in range(len(self.array)):
self.array[i].setRGB(r, g, b)
else:
for i in range(len(self.array)):
self.array[i].setRGB(0,0,0)
self.blink_index += 1
if self.blink_index > 10:
self.blink_index = 0
class SLEDS:
'''Switchable LEDS from Switchable PDH'''
def on(self):
PowerDistribution.setSwitchableChannel(True)
def off(self):
PowerDistribution.setSwitchableChannel(False)