generated from Greater-Rochester-Robotics/GRRBase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc0d982
commit 80cf8dc
Showing
4 changed files
with
147 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
package org.team340.robot.subsystems; | ||
|
||
import edu.wpi.first.wpilibj.AddressableLED; | ||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj.DriverStation; | ||
import edu.wpi.first.wpilibj.Timer; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import java.util.function.Supplier; | ||
import org.team340.lib.util.Alliance; | ||
import org.team340.lib.util.GRRSubsystem; | ||
import org.team340.lib.util.Math2; | ||
import org.team340.robot.Constants.RobotMap; | ||
|
||
public class Lights extends GRRSubsystem { | ||
|
||
private static final int kLength = 56; | ||
|
||
private final AddressableLED lights; | ||
private final AddressableLEDBuffer buffer; | ||
private final Timer timer; | ||
|
||
public Lights() { | ||
lights = new AddressableLED(RobotMap.kLights); | ||
buffer = new AddressableLEDBuffer(kLength); | ||
timer = new Timer(); | ||
|
||
lights.setLength(buffer.getLength()); | ||
apply(); | ||
lights.start(); | ||
timer.start(); | ||
} | ||
|
||
/** | ||
* Applies the buffer to the LED strip. | ||
*/ | ||
private void apply() { | ||
lights.setData(buffer); | ||
} | ||
|
||
/** | ||
* Modifies the buffer to be a single color. | ||
* @param r Red value from {@code 0} to {@code 255}. | ||
* @param g Green value from {@code 0} to {@code 255}. | ||
* @param b Blue value from {@code 0} to {@code 255}. | ||
*/ | ||
private void set(int r, int g, int b) { | ||
for (int i = 0; i < buffer.getLength(); i++) { | ||
buffer.setRGB(i, r, g, b); | ||
} | ||
} | ||
|
||
/** | ||
* Modifies the buffer with values mirrored across the center of the LED strip. | ||
* @param i The index of the buffer to modify. | ||
* @param r Red value from {@code 0} to {@code 255}. | ||
* @param g Green value from {@code 0} to {@code 255}. | ||
* @param b Blue value from {@code 0} to {@code 255}. | ||
*/ | ||
private void setMirrored(int i, int r, int g, int b) { | ||
buffer.setRGB(i, r, g, b); | ||
buffer.setRGB(buffer.getLength() - i - 1, r, g, b); | ||
} | ||
|
||
/** | ||
* Runs the lights. | ||
*/ | ||
public Command run(Supplier<Boolean> hasNote) { | ||
int[] state = new int[buffer.getLength()]; | ||
for (int i = 0; i < state.length; i++) { | ||
state[i] = 0; | ||
} | ||
|
||
return commandBuilder() | ||
.onExecute(() -> { | ||
if (DriverStation.isTeleopEnabled()) { | ||
if (hasNote.get()) { | ||
double time = timer.get(); | ||
int v = time % 1.0 > 0.5 ? 255 : 0; | ||
for (int i = 0; i < buffer.getLength() / 2; i++) { | ||
setMirrored(i, 0, v, 0); | ||
} | ||
} else { | ||
int v = (int) (((Math.cos(timer.get() * 8.5) + 1.0) * 87.5) + 25.0); | ||
if (Alliance.isBlue()) { | ||
set(0, 0, v); | ||
} else { | ||
set(v, 0, 0); | ||
} | ||
} | ||
} else if (DriverStation.isAutonomousEnabled()) { | ||
for (int i = 0; i < buffer.getLength() / 2; i++) { | ||
state[i] = (int) Math.max( | ||
0.0, | ||
state[i] - Math2.random((0.5 + (i / (buffer.getLength() * 0.25))) * 70.0) + 4.0 | ||
); | ||
} | ||
for (int i = (buffer.getLength() / 2) - 1; i >= 2; i--) { | ||
state[i] = (state[i - 1] + state[i - 2] + state[i - 2]) / 3; | ||
} | ||
if (Math.random() < 0.5) { | ||
int i = (int) Math2.random(8.0); | ||
state[i] = (int) (state[i] + Math2.random(160.0, 255.0)); | ||
} | ||
for (int i = 0; i < buffer.getLength() / 2; i++) { | ||
int heat = (int) ((state[i] / 255.0) * 191.0); | ||
int ramp = (heat & 63) << 2; | ||
if (heat > 180) { | ||
setMirrored(i, 255, 255, ramp); | ||
} else if (heat > 60) { | ||
setMirrored(i, 255, ramp, 0); | ||
} else { | ||
setMirrored(i, ramp, 0, 0); | ||
} | ||
} | ||
} else if (DriverStation.isDisabled()) { | ||
if (Alliance.isBlue()) set(0, 0, 150); | ||
else set(150, 0, 0); | ||
} else { | ||
set(0, 0, 0); | ||
} | ||
|
||
apply(); | ||
}) | ||
.onEnd(() -> { | ||
set(0, 0, 0); | ||
apply(); | ||
}) | ||
.ignoringDisable(true) | ||
.withName("lights.defaultCommand()"); | ||
} | ||
} |