Skip to content

Commit a015b05

Browse files
committed
Update ledData to avoid high memory usage
1 parent abd2d97 commit a015b05

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

addressableled/robot.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,51 @@
1-
from wpilib import run, TimedRobot, AddressableLED
1+
import wpilib
22

33
kLEDBuffer = 60
44

5-
class MyRobot(TimedRobot):
5+
6+
class MyRobot(wpilib.TimedRobot):
67
def robotInit(self):
78
# PWM Port 9
89
# Must be a PWM header, not MXP or DIO
9-
self.m_led = AddressableLED(9)
10-
10+
self.led = wpilib.AddressableLED(9)
11+
1112
# LED Data
12-
self.m_ledData = []
13+
self.ledData = [wpilib.AddressableLED.LEDData() for i in range(kLEDBuffer)]
1314

1415
# Store what the last hue of the first pixel is
15-
self.m_rainbowFirstPixelHue = 0
16+
self.rainbowFirstPixelHue = 0
1617

1718
# Default to a length of 60, start empty output
1819
# Length is expensive to set, so only set it once, then just update data
19-
self.m_led.setLength(kLEDBuffer)
20-
21-
self.m_led.start()
20+
self.led.setLength(kLEDBuffer)
21+
22+
# Set the data
23+
self.led.setData(self.ledData)
24+
self.led.start()
2225

2326
def robotPeriodic(self):
2427
# Fill the buffer with a rainbow
2528
self.rainbow()
26-
29+
2730
# Set the LEDs
28-
if len(self.m_ledData) <= kLEDBuffer:
29-
self.m_led.setData(self.m_ledData)
31+
self.led.setData(self.ledData)
3032

3133
def rainbow(self):
3234
# For every pixel
3335
for i in range(kLEDBuffer):
3436
# Calculate the hue - hue is easier for rainbows because the color
3537
# shape is a circle so only one value needs to precess
36-
hue = (self.m_rainbowFirstPixelHue + (i * 180 / kLEDBuffer)) % 180
37-
38-
self.m_ledData.append(AddressableLED.LEDData())
39-
38+
hue = (self.rainbowFirstPixelHue + (i * 180 / kLEDBuffer)) % 180
39+
4040
# Set the value
41-
self.m_ledData[i].setHSV(int(hue), 255, 128)
41+
self.ledData[i].setHSV(int(hue), 255, 128)
4242

4343
# Increase by to make the rainbow "move"
44-
self.m_rainbowFirstPixelHue += 3
44+
self.rainbowFirstPixelHue += 3
4545

4646
# Check bounds
47-
self.m_rainbowFirstPixelHue %= 100
47+
self.rainbowFirstPixelHue %= 100
48+
4849

49-
if __name__ == '__main__':
50-
run(MyRobot)
50+
if __name__ == "__main__":
51+
wpilib.run(MyRobot)

0 commit comments

Comments
 (0)