Skip to content

Commit

Permalink
refactor: clean up modifiers, loops
Browse files Browse the repository at this point in the history
  • Loading branch information
rutmanz committed Feb 5, 2024
1 parent c4b89ec commit da7b227
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
28 changes: 13 additions & 15 deletions src/main/java/org/team1540/robot2024/subsystems/led/LedPattern.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package org.team1540.robot2024.subsystems.led;

import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.util.Color;

import java.util.function.Consumer;
import java.util.function.BiConsumer;

public class LedPattern {
private final Consumer<ZonedAddressableLEDBuffer> applier;
private final BiConsumer<ZonedAddressableLEDBuffer, Integer> applier;
public final boolean isDynamic;
LedPattern(Consumer<ZonedAddressableLEDBuffer> applier, boolean isDynamic) {
private LedPattern(BiConsumer<ZonedAddressableLEDBuffer, Integer> applier, boolean isDynamic) {
this.applier = applier;
this.isDynamic = isDynamic;
}
protected LedPattern(boolean isDynamic) {
this.applier = null;
this.isDynamic = isDynamic;
}

void apply(ZonedAddressableLEDBuffer buffer) {
this.applier.accept(buffer);
assert this.applier != null;
for (int i = 0; i < buffer.getLength(); i++) {
this.applier.accept(buffer, i);
}
}

public static LedPattern solid(Color color) {
return new LedPattern((buffer) -> {
for (int i = 0; i<buffer.getLength();i++) {
buffer.setLED(i, color);
}
}, false);
return new LedPattern((buffer, i) -> buffer.setLED(i, color), false);
}
public static LedPattern alternating(Color a, Color b) {
return new LedPattern((buffer) -> {
for (int i = 0; i<buffer.getLength();i++) {
buffer.setLED(i, i % 2 == 0 ? a : b);
}
}, false);
return new LedPattern((buffer, i) -> buffer.setLED(i, i % 2 == 0 ? a : b), false);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package org.team1540.robot2024.subsystems.led;

import static java.lang.Math.round;

public class LedPatternRainbow extends LedPattern {
private final int speed;
int initialHue = 0;
private int initialHue = 0;

public LedPatternRainbow(int speed) {
super(null, true);
super(true);
this.speed = speed;
}

Expand Down

0 comments on commit da7b227

Please sign in to comment.