Skip to content

Commit 9168f2a

Browse files
Strflightmight09Nummun14levyishai
authored
Fix LEDs (#50)
* Fix for CANdle, which uses 0-255 (int) instead of 0-1 (double) * Started with the records * Quick fix + continuation * Implemented records. Still couldn't find a great alternative to setting the default command for every LED strip on each default command * fixed some minor jd and efficiency things that annoyed me * these should be protected * extra new line * AHHHHHHHHHHHHHH Made all animations the same data type * added length 0 survivability * Fix * More correct way to do this * best way to do this --------- Co-authored-by: Nummun14 <[email protected]> Co-authored-by: Nummun14 <[email protected]> Co-authored-by: Yishai Levy <[email protected]>
1 parent 95dcb92 commit 9168f2a

File tree

5 files changed

+242
-141
lines changed

5 files changed

+242
-141
lines changed

src/main/java/org/trigon/hardware/misc/leds/AddressableLEDStrip.java

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static void initiateAddressableLED(int port, int totalAmountOfLEDs) {
4040
}
4141

4242
/**
43-
* Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the setAddressableLED and setAddressableLEDBuffer methods must be called.
43+
* Constructs a new AddressableLEDStrip. Before any commands are sent to the LED strip, the {@link AddressableLEDStrip#initiateAddressableLED(int, int)} method must be called.
4444
*
4545
* @param inverted whether the LED strip is inverted
4646
* @param numberOfLEDs the amount of LEDs in the strip
@@ -58,12 +58,12 @@ public void periodic() {
5858
}
5959

6060
@Override
61-
void clearLEDColors() {
62-
staticColor(Color.kBlack);
61+
protected void clearLEDColors() {
62+
setStaticColor(Color.kBlack);
6363
}
6464

6565
@Override
66-
void blink(Color firstColor, double speed) {
66+
protected void blink(Color color, double speed) {
6767
final double correctedSpeed = 1 - speed;
6868
final double currentTime = Timer.getTimestamp();
6969

@@ -73,19 +73,19 @@ void blink(Color firstColor, double speed) {
7373
}
7474

7575
if (isLEDAnimationChanged) {
76-
staticColor(firstColor);
76+
setStaticColor(color);
7777
return;
7878
}
7979
clearLEDColors();
8080
}
8181

8282
@Override
83-
void staticColor(Color color) {
84-
setLEDColors(color, 0, numberOfLEDs - 1);
83+
protected void staticColor(Color color) {
84+
setStaticColor(color);
8585
}
8686

8787
@Override
88-
void breathe(Color color, int breathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) {
88+
protected void breathe(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) {
8989
clearLEDColors();
9090
final boolean correctedInverted = this.inverted != inverted;
9191
final double moveLEDTimeSeconds = 1 - speed;
@@ -99,12 +99,12 @@ void breathe(Color color, int breathingLEDs, double speed, boolean inverted, Lar
9999
lastBreatheLED++;
100100
}
101101

102-
checkIfBreathingHasHitEnd(breathingLEDs, correctedInverted, bounceMode);
103-
setBreathingLEDs(color, breathingLEDs, bounceMode);
102+
checkIfBreathingHasHitEnd(numberOfBreathingLEDs, correctedInverted, bounceMode);
103+
setBreathingLEDs(color, numberOfBreathingLEDs, bounceMode);
104104
}
105105

106106
@Override
107-
void colorFlow(Color color, double speed, boolean inverted) {
107+
protected void colorFlow(Color color, double speed, boolean inverted) {
108108
clearLEDColors();
109109
final boolean correctedInverted = this.inverted != inverted;
110110
final double moveLEDTimeSeconds = 1 - speed;
@@ -123,13 +123,13 @@ void colorFlow(Color color, double speed, boolean inverted) {
123123
}
124124

125125
@Override
126-
void alternateColor(Color firstColor, Color secondColor) {
126+
protected void alternateColor(Color firstColor, Color secondColor) {
127127
for (int i = 0; i < numberOfLEDs; i++)
128128
LED_BUFFER.setLED(i + indexOffset, i % 2 == 0 ? firstColor : secondColor);
129129
}
130130

131131
@Override
132-
void rainbow(double brightness, double speed, boolean inverted) {
132+
protected void rainbow(double brightness, double speed, boolean inverted) {
133133
final boolean correctedInverted = this.inverted != inverted;
134134
final int adjustedBrightness = (int) (brightness * 255);
135135
final int hueIncrement = (int) (speed * 8);
@@ -150,9 +150,9 @@ void rainbow(double brightness, double speed, boolean inverted) {
150150
}
151151

152152
@Override
153-
void sectionColor(Supplier<Color>[] colors) {
153+
protected void sectionColor(Supplier<Color>[] colors) {
154154
final int amountOfSections = colors.length;
155-
final int ledsPerSection = (int) Math.floor(numberOfLEDs / amountOfSections);
155+
final int ledsPerSection = (int) Math.floor((double) numberOfLEDs / amountOfSections);
156156

157157
for (int i = 0; i < amountOfSections; i++)
158158
setLEDColors(
@@ -163,14 +163,18 @@ void sectionColor(Supplier<Color>[] colors) {
163163
}
164164

165165
@Override
166-
void resetLEDSettings() {
166+
protected void resetLEDSettings() {
167167
lastBreatheLED = indexOffset;
168168
lastLEDAnimationChangeTime = Timer.getTimestamp();
169169
rainbowFirstPixelHue = 0;
170170
isLEDAnimationChanged = false;
171171
amountOfColorFlowLEDs = 0;
172172
}
173173

174+
private void setStaticColor(Color color) {
175+
setLEDColors(color, 0, numberOfLEDs - 1);
176+
}
177+
174178
private void checkIfBreathingHasHitEnd(int amountOfBreathingLEDs, boolean inverted, LarsonAnimation.BounceMode bounceMode) {
175179
final int bounceModeAddition = switch (bounceMode) {
176180
case Back -> amountOfBreathingLEDs;

src/main/java/org/trigon/hardware/misc/leds/CANdleLEDStrip.java

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class CANdleLEDStrip extends LEDStrip {
2121
* @param candle the CANdle instance to be used
2222
*/
2323
public static void setCANdle(CANdle candle) {
24-
if (CANDLE == null)
24+
if (CANDLE == null && !RobotHardwareStats.isSimulation())
2525
CANDLE = candle;
2626
}
2727

@@ -37,7 +37,7 @@ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) {
3737
}
3838

3939
/**
40-
* Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the setCANdle method must be called.
40+
* Constructs a new CANdleLEDStrip. Before any commands are sent to the LED strip, the {@link CANdleLEDStrip#setCANdle(CANdle)} method must be called.
4141
*
4242
* @param inverted whether the LED strip is inverted
4343
* @param numberOfLEDs the amount of LEDs in the strip
@@ -50,17 +50,17 @@ public static void setTotalAmountOfLEDs(int totalAmountOfLEDs) {
5050
}
5151

5252
@Override
53-
void clearLEDColors() {
53+
protected void clearLEDColors() {
5454
CANDLE.clearAnimation(animationSlot);
5555
}
5656

5757
@Override
58-
void blink(Color firstColor, double speed) {
58+
protected void blink(Color color, double speed) {
5959
CANDLE.animate(
6060
new SingleFadeAnimation(
61-
(int) firstColor.red,
62-
(int) firstColor.green,
63-
(int) firstColor.blue,
61+
(int) (color.red * 255),
62+
(int) (color.green * 255),
63+
(int) (color.blue * 255),
6464
0,
6565
speed,
6666
this.numberOfLEDs,
@@ -71,49 +71,56 @@ void blink(Color firstColor, double speed) {
7171
}
7272

7373
@Override
74-
void staticColor(Color color) {
75-
CANDLE.setLEDs((int) color.red, (int) color.green, (int) color.blue, 0, indexOffset, numberOfLEDs);
74+
protected void staticColor(Color color) {
75+
CANDLE.setLEDs(
76+
((int) color.red * 255),
77+
((int) color.green * 255),
78+
((int) color.blue * 255),
79+
0,
80+
indexOffset,
81+
numberOfLEDs
82+
);
7683
}
7784

7885
@Override
79-
void breathe(Color color, int amountOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) {
86+
protected void breathe(Color color, int numberOfBreathingLEDs, double speed, boolean inverted, LarsonAnimation.BounceMode bounceMode) {
8087
CANDLE.animate(
8188
new LarsonAnimation(
82-
(int) color.red,
83-
(int) color.green,
84-
(int) color.blue,
89+
(int) (color.red * 255),
90+
(int) (color.green * 255),
91+
(int) (color.blue * 255),
8592
0,
8693
speed,
8794
this.numberOfLEDs,
8895
bounceMode,
89-
amountOfBreathingLEDs,
96+
numberOfBreathingLEDs,
9097
indexOffset
9198
),
9299
animationSlot
93100
);
94101
}
95102

96103
@Override
97-
void alternateColor(Color firstColor, Color secondColor) {
104+
protected void alternateColor(Color firstColor, Color secondColor) {
98105
for (int i = 0; i < numberOfLEDs; i++)
99106
CANDLE.setLEDs(
100-
(int) (isEven(i) ? firstColor.red : secondColor.red),
101-
(int) (isEven(i) ? firstColor.green : secondColor.green),
102-
(int) (isEven(i) ? firstColor.blue : secondColor.blue),
107+
(int) ((isEven(i) ? firstColor.red : secondColor.red) * 255),
108+
(int) ((isEven(i) ? firstColor.green : secondColor.green) * 255),
109+
(int) ((isEven(i) ? firstColor.blue : secondColor.blue) * 255),
103110
0,
104111
i + indexOffset,
105112
1
106113
);
107114
}
108115

109116
@Override
110-
void colorFlow(Color color, double speed, boolean inverted) {
117+
protected void colorFlow(Color color, double speed, boolean inverted) {
111118
final boolean correctedInverted = this.inverted != inverted;
112119
CANDLE.animate(
113120
new ColorFlowAnimation(
114-
(int) color.red,
115-
(int) color.green,
116-
(int) color.blue,
121+
(int) (color.red * 255),
122+
(int) (color.green * 255),
123+
(int) (color.blue * 255),
117124
0,
118125
speed,
119126
this.numberOfLEDs,
@@ -125,7 +132,7 @@ void colorFlow(Color color, double speed, boolean inverted) {
125132
}
126133

127134
@Override
128-
void rainbow(double brightness, double speed, boolean inverted) {
135+
protected void rainbow(double brightness, double speed, boolean inverted) {
129136
final boolean correctedInverted = this.inverted != inverted;
130137
CANDLE.animate(
131138
new RainbowAnimation(
@@ -140,17 +147,17 @@ void rainbow(double brightness, double speed, boolean inverted) {
140147
}
141148

142149
@Override
143-
void sectionColor(Supplier<Color>[] colors) {
144-
final int ledsPerSection = (int) Math.floor(numberOfLEDs / colors.length);
150+
protected void sectionColor(Supplier<Color>[] colors) {
151+
final int ledsPerSection = (int) Math.floor((double) numberOfLEDs / colors.length);
145152
setSectionColor(colors.length, ledsPerSection, colors);
146153
}
147154

148155
private void setSectionColor(int amountOfSections, int ledsPerSection, Supplier<Color>[] colors) {
149156
for (int i = 0; i < amountOfSections; i++) {
150157
CANDLE.setLEDs(
151-
(int) (inverted ? colors[amountOfSections - i - 1].get().red : colors[i].get().red),
152-
(int) (inverted ? colors[amountOfSections - i - 1].get().green : colors[i].get().green),
153-
(int) (inverted ? colors[amountOfSections - i - 1].get().blue : colors[i].get().blue),
158+
(int) ((inverted ? colors[amountOfSections - i - 1].get().red : colors[i].get().red) * 255),
159+
(int) ((inverted ? colors[amountOfSections - i - 1].get().green : colors[i].get().green) * 255),
160+
(int) ((inverted ? colors[amountOfSections - i - 1].get().blue : colors[i].get().blue) * 255),
154161
0,
155162
ledsPerSection * i + indexOffset,
156163
i == amountOfSections - 1 ? numberOfLEDs - 1 : ledsPerSection * (i + 1) - 1

0 commit comments

Comments
 (0)