This repository has been archived by the owner on Feb 3, 2025. It is now read-only.
generated from Frc5572/Java-Template
-
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
7a776e7
commit 382cb5b
Showing
6 changed files
with
262 additions
and
1 deletion.
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,65 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.LEDs; | ||
|
||
/** Command to flash LEDs on and off once per second. */ | ||
public class FlashingLEDColor extends Command { | ||
private LEDs leds; | ||
private int ledLength; | ||
private int flashingDelay = 0; | ||
private Color color; | ||
private Color altColor; | ||
|
||
/** | ||
* Command to flash the LED strip between 2 colors | ||
* | ||
* @param leds LED Subsystem | ||
* @param color The first color | ||
* @param altColor The second color | ||
*/ | ||
public FlashingLEDColor(LEDs leds, Color color, Color altColor) { | ||
this.leds = leds; | ||
this.color = color; | ||
this.altColor = altColor; | ||
ledLength = leds.getLength(); | ||
addRequirements(leds); | ||
} | ||
|
||
/** | ||
* Command to flash the LED strip between a color and black | ||
* | ||
* @param leds LED Subsystem | ||
* @param color The color | ||
*/ | ||
public FlashingLEDColor(LEDs leds, Color color) { | ||
this(leds, color, Color.kBlack); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (flashingDelay < 10) { | ||
for (var i = 0; i < ledLength; i++) { | ||
leds.setColor(i, color); | ||
} | ||
} else { | ||
for (var i = 0; i < ledLength; i++) { | ||
leds.setColor(i, altColor); | ||
} | ||
} | ||
leds.setData(); | ||
flashingDelay++; | ||
flashingDelay %= 20; | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean runsWhenDisabled() { | ||
return true; | ||
} | ||
} |
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,67 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.LEDs; | ||
|
||
/** | ||
* Command to move LEDs back and forth like a Cylon eye | ||
*/ | ||
public class MovingColorLEDs extends Command { | ||
private final LEDs leds; | ||
private int ledLength; | ||
private Color color; | ||
private int count; | ||
private boolean inverted = false; | ||
private int movingColorDelay = 0; | ||
private int movingLED = 0; | ||
private boolean movingDirection = true; | ||
|
||
/** | ||
* Command to move LEDs back and forth like a Cylon eye | ||
* | ||
* @param leds LED subsystem | ||
* @param color Color to which to set the LEDs | ||
* @param count The number of LEDs in the moving dot. Best is an odd number. | ||
* @param inverted Whether to invert the Color that moves. | ||
*/ | ||
public MovingColorLEDs(LEDs leds, Color color, int count, boolean inverted) { | ||
this.leds = leds; | ||
this.color = color; | ||
this.count = count + 2; | ||
this.inverted = inverted; | ||
ledLength = leds.getLength(); | ||
addRequirements(leds); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
Color theColor = inverted ? Color.kBlack : color; | ||
Color secondColor = inverted ? color : Color.kBlack; | ||
if (movingColorDelay == 0) { | ||
for (var i = 0; i < ledLength; i++) { | ||
if (Math.abs(i - movingLED) < count) { | ||
leds.setColor(i, theColor); | ||
} else { | ||
leds.setColor(i, secondColor); | ||
} | ||
} | ||
if (movingDirection) { | ||
movingLED++; | ||
} else { | ||
movingLED--; | ||
} | ||
if (movingLED >= ledLength - 1 || movingLED <= 0) { | ||
movingDirection = !movingDirection; | ||
} | ||
leds.setData(); | ||
} | ||
movingColorDelay += 1; | ||
movingColorDelay %= 1; | ||
} | ||
|
||
@Override | ||
public boolean runsWhenDisabled() { | ||
return true; | ||
} | ||
} |
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,110 @@ | ||
package frc.robot.subsystems; | ||
|
||
import edu.wpi.first.wpilibj.AddressableLED; | ||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
/** | ||
* This is the class header for the LEDs Subsystem | ||
*/ | ||
public class LEDs extends SubsystemBase { | ||
private AddressableLEDBuffer controLedBuffer; | ||
@SuppressWarnings("IOCheck") | ||
private AddressableLED addressableLED; | ||
|
||
/** | ||
* constructs a LED Subsystem | ||
* | ||
* @param length length of the addressable LEDS | ||
* @param port port ID for PWM | ||
*/ | ||
public LEDs(int length, int port) { | ||
controLedBuffer = new AddressableLEDBuffer(length); | ||
addressableLED = new AddressableLED(port); | ||
|
||
addressableLED.setLength(controLedBuffer.getLength()); | ||
addressableLED.setData(controLedBuffer); | ||
addressableLED.start(); | ||
} | ||
|
||
/** | ||
* Get LED strip length | ||
* | ||
* @return number of LEDs | ||
*/ | ||
public int getLength() { | ||
return controLedBuffer.getLength(); | ||
} | ||
|
||
/** | ||
* Set individual LED color via HSV | ||
* | ||
* @param index the LED index to set | ||
* @param h the h value [0-180) | ||
* @param s the s value [0-255] | ||
* @param v the v value [0-255] | ||
*/ | ||
public void setHSV(int index, int h, int s, int v) { | ||
controLedBuffer.setHSV(index, h, s, v); | ||
} | ||
|
||
/** | ||
* Sets the LED output data. | ||
*/ | ||
public void setData() { | ||
addressableLED.setData(controLedBuffer); | ||
} | ||
|
||
/** | ||
* Set individual LED color via RGB | ||
* | ||
* @param index the LED index to set | ||
* @param r the r value [0-255] | ||
* @param g the g value [0-255] | ||
* @param b the b value [0-255] | ||
*/ | ||
public void setRGB(int index, int r, int g, int b) { | ||
controLedBuffer.setRGB(index, r, g, b); | ||
} | ||
|
||
/** | ||
* Sets RGB Color of the entire LED strip | ||
* | ||
* @param r - [0 - 255] | ||
* @param g - [0 - 255] | ||
* @param b - [0 - 255] | ||
*/ | ||
public void setRGB(int r, int g, int b) { | ||
for (var i = 0; i < getLength(); i++) { | ||
setRGB(i, r, g, b); | ||
} | ||
setData(); | ||
} | ||
|
||
/** | ||
* Set individual LED color via Color | ||
* | ||
* @param index the LED index to set | ||
* @param color The color of the LED | ||
*/ | ||
public void setColor(int index, Color color) { | ||
controLedBuffer.setLED(index, color); | ||
} | ||
|
||
/** | ||
* Sets the Color of the entire LED strip | ||
* | ||
* @param color color set for the LEDs | ||
*/ | ||
public void setColor(Color color) { | ||
for (var i = 0; i < getLength(); i++) { | ||
setColor(i, color); | ||
} | ||
setData(); | ||
} | ||
|
||
public Color getColor(int index) { | ||
return controLedBuffer.getLED(index); | ||
} | ||
} |