Skip to content

Commit

Permalink
feat: LED Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
rutmanz committed Feb 5, 2024
1 parent f942fee commit c4b89ec
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/org/team1540/robot2024/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public final class Constants {
private static final Mode simMode = Mode.SIM; // Can also be Mode.REPLAY

public static final Mode currentMode = Robot.isReal() ? Mode.REAL : simMode;

public static final int LED_STRIP_PORT_PWM = 0;
public static final int LED_STRIP_LENGTH= 80;
public enum Mode {
/**
* Running on a real robot.
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/team1540/robot2024/Robot.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.team1540.robot2024;

import edu.wpi.first.wpilibj.DriverStation;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj2.command.Command;
import edu.wpi.first.wpilibj2.command.CommandScheduler;
import org.littletonrobotics.junction.LogFileUtil;
Expand All @@ -9,6 +10,9 @@
import org.littletonrobotics.junction.networktables.NT4Publisher;
import org.littletonrobotics.junction.wpilog.WPILOGReader;
import org.littletonrobotics.junction.wpilog.WPILOGWriter;
import org.team1540.robot2024.subsystems.led.LedPattern;
import org.team1540.robot2024.subsystems.led.LedPatternRainbow;
import org.team1540.robot2024.subsystems.led.Leds;
import org.team1540.robot2024.util.MechanismVisualiser;

/**
Expand Down Expand Up @@ -101,6 +105,8 @@ public void robotPeriodic() {
*/
@Override
public void disabledInit() {
robotContainer.leds.setPattern(Leds.Zone.ZONE1, LedPattern.solid(Color.kBlueViolet));
robotContainer.leds.setPattern(Leds.Zone.ZONE2, LedPattern.solid(Color.kPaleVioletRed));
}

/**
Expand All @@ -115,6 +121,7 @@ public void disabledPeriodic() {
*/
@Override
public void autonomousInit() {
robotContainer.leds.setPattern(Leds.Zone.ZONE1,LedPattern.alternating(Color.kBlueViolet, Color.kCrimson));
autonomousCommand = robotContainer.getAutonomousCommand();
// schedule the autonomous command (example)
if (autonomousCommand != null) {
Expand All @@ -138,6 +145,7 @@ public void teleopInit() {
// teleop starts running. If you want the autonomous to
// continue until interrupted by another command, remove
// this line or comment it out.
robotContainer.leds.setPattern(Leds.Zone.ZONE1, LedPattern.alternating(Color.kBlueViolet, Color.kGreen));
if (autonomousCommand != null) {
autonomousCommand.cancel();
}
Expand All @@ -155,6 +163,8 @@ public void teleopPeriodic() {
*/
@Override
public void testInit() {
robotContainer.leds.setPattern(Leds.Zone.ZONE1,new LedPatternRainbow(1));
robotContainer.leds.setPattern(Leds.Zone.ZONE2,new LedPatternRainbow(1));
// Cancels all running commands at the start of test mode.
CommandScheduler.getInstance().cancelAll();
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/team1540/robot2024/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.team1540.robot2024.subsystems.indexer.IndexerIO;
import org.team1540.robot2024.subsystems.indexer.IndexerIOSim;
import org.team1540.robot2024.subsystems.indexer.IndexerIOSparkMax;
import org.team1540.robot2024.subsystems.led.Leds;
import org.team1540.robot2024.subsystems.shooter.*;
import org.team1540.robot2024.subsystems.tramp.Tramp;
import org.team1540.robot2024.subsystems.tramp.TrampIO;
Expand Down Expand Up @@ -56,6 +57,7 @@ public class RobotContainer {
public final Elevator elevator;
public final Indexer indexer;
public final AprilTagVision aprilTagVision;
public final Leds leds = new Leds();

// Controller
public final CommandXboxController driver = new CommandXboxController(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.team1540.robot2024.subsystems.led;

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

import java.util.function.Consumer;

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

void apply(ZonedAddressableLEDBuffer buffer) {
this.applier.accept(buffer);
}

public static LedPattern solid(Color color) {
return new LedPattern((buffer) -> {
for (int i = 0; i<buffer.getLength();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);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.team1540.robot2024.subsystems.led;

import static java.lang.Math.round;

public class LedPatternRainbow extends LedPattern {
private final int speed;
int initialHue = 0;
public LedPatternRainbow(int speed) {
super(null, true);
this.speed = speed;
}

@Override
void apply(ZonedAddressableLEDBuffer buffer) {
for (int i = 0; i < buffer.getLength(); i++) {
int hue = (initialHue + (i * 180 / buffer.getLength())) % 180;
buffer.setHSV(i, hue, 255, 128);
}
initialHue += speed;
initialHue %= 180;
}

}
50 changes: 50 additions & 0 deletions src/main/java/org/team1540/robot2024/subsystems/led/Leds.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package org.team1540.robot2024.subsystems.led;

import edu.wpi.first.wpilibj.AddressableLED;
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj2.command.SubsystemBase;
import org.team1540.robot2024.Constants;


import static org.team1540.robot2024.Constants.LED_STRIP_PORT_PWM;

public class Leds extends SubsystemBase {

private final AddressableLEDBuffer ledBuffer = new AddressableLEDBuffer(Constants.LED_STRIP_LENGTH);
private final AddressableLED strip = new AddressableLED(LED_STRIP_PORT_PWM);
private final ZonedAddressableLEDBuffer[] buffers = new ZonedAddressableLEDBuffer[ZONE_COUNT];
private final LedPattern[] patterns = new LedPattern[ZONE_COUNT];

public Leds() {
strip.setLength(ledBuffer.getLength());
strip.setData(ledBuffer);
strip.start();

buffers[Zone.ZONE1.ordinal()] = new ZonedAddressableLEDBuffer(ledBuffer, 0, 40, false);
buffers[Zone.ZONE2.ordinal()] = new ZonedAddressableLEDBuffer(ledBuffer, 40, 80, false);
}

@Override
public void periodic() {
for (int i = 0; i < ZONE_COUNT;i++) {
if (patterns[i].isDynamic) {
patterns[i].apply(buffers[i]);
}
}
strip.setData(ledBuffer);
}

public void setPattern(Zone zone, LedPattern pattern) {
patterns[zone.ordinal()] = pattern;
if (!pattern.isDynamic) {
pattern.apply(buffers[zone.ordinal()]);
}
}


private static final int ZONE_COUNT=Zone.values().length;
public enum Zone {
ZONE1,
ZONE2;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package org.team1540.robot2024.subsystems.led;

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

class ZonedAddressableLEDBuffer {
private final AddressableLEDBuffer buffer;
public final boolean isInverted;
public final int start;
public final int length;

public ZonedAddressableLEDBuffer(AddressableLEDBuffer buffer, int start, int end, boolean isInverted) {
if (start > end) {
throw new IllegalArgumentException("start must be less than end");
}
this.buffer = buffer;
this.isInverted = isInverted;
this.start = start;
this.length = end - start;
}

public void setRGB(int index, int r, int g, int b) {
buffer.setRGB(this.getAbsoluteIndex(index), r, g, b);
}

public void setHSV(int index, int h, int s, int v) {
buffer.setHSV(this.getAbsoluteIndex(index), h, s, v);
}

public void setLED(int index, Color color) {
System.out.println(this.getAbsoluteIndex(index));
buffer.setLED(this.getAbsoluteIndex(index), color);
}

public void setLED(int index, Color8Bit color) {
buffer.setLED(this.getAbsoluteIndex(index), color);
}

public Color getLED(int index) {
return buffer.getLED(this.getAbsoluteIndex(index));
}

public Color8Bit getLED8Bit(int index) {
return buffer.getLED8Bit(this.getAbsoluteIndex(index));
}

public int getLength() {
return this.length;
}

private int getAbsoluteIndex(int index) {
if (index >= length) {
DriverStation.reportWarning("led index out of bounds", false);
return 0;
}
if (this.isInverted) {
return this.start + length -1 - index;
} else {
return this.start + index;
}
}
}

0 comments on commit c4b89ec

Please sign in to comment.