-
Notifications
You must be signed in to change notification settings - Fork 1
Merge AddressableLED subsystem #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 12 commits
6421de4
fa8e40a
8b27e76
b26a7b9
b51bcd3
e827996
fad22db
74fb53f
0cc78fe
40c06ef
7f0147c
13ba9fe
6cf0918
fd39f56
e9c44d4
d3c6e7b
9efff02
650ee6b
79bca46
0b9126f
ebcae4c
cead75c
e6fedd3
8fcde4c
b5dda59
d3ffd09
21a0484
f2ed688
ed4e9b5
ea88276
b545f59
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package frc.robot.commands; | ||
|
||
import edu.wpi.first.wpilibj.LEDPattern; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.addressableled.AddressableLEDSubsystem; | ||
|
||
public class SetAddressableLEDPattern extends Command { | ||
private final AddressableLEDSubsystem ledSystem; | ||
|
||
/** | ||
* @apiNote If this is -1, that means that this command is targeting the whole strip | ||
*/ | ||
private final int section; | ||
|
||
private final LEDPattern pattern; | ||
|
||
/** | ||
* @param led Addressable LED subsystem to use | ||
* @param pattern Pattern to apply when command run | ||
* @param section Section of LED strip to apply pattern to (index into | ||
* AddressableLEDConstants.SECTIONS) | ||
*/ | ||
public SetAddressableLEDPattern( | ||
AddressableLEDSubsystem ledSystem, LEDPattern pattern, int section) { | ||
this.section = section; | ||
this.pattern = pattern; | ||
this.ledSystem = ledSystem; | ||
addRequirements(ledSystem); | ||
} | ||
|
||
/** | ||
* @param led Addressable LED subsystem to use | ||
* @param pattern Pattern to apply when command run | ||
*/ | ||
public SetAddressableLEDPattern(AddressableLEDSubsystem ledSystem, LEDPattern pattern) { | ||
section = -1; | ||
this.pattern = pattern; | ||
this.ledSystem = ledSystem; | ||
addRequirements(ledSystem); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (section < 0) { | ||
ledSystem.applyPattern(pattern); | ||
} else { | ||
ledSystem.applySectionedPattern(pattern, section); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package frc.robot.commands.test; | ||
|
||
import static edu.wpi.first.units.Units.MetersPerSecond; | ||
import static edu.wpi.first.units.Units.Seconds; | ||
|
||
import edu.wpi.first.wpilibj.LEDPattern; | ||
import edu.wpi.first.wpilibj.util.Color; | ||
import edu.wpi.first.wpilibj2.command.Command; | ||
import frc.robot.subsystems.addressableled.AddressableLEDConstants; | ||
import frc.robot.subsystems.addressableled.AddressableLEDSubsystem; | ||
|
||
public class AddressableLEDTestCommand extends Command { | ||
private final AddressableLEDSubsystem ledSystem; | ||
private LEDPattern currentPattern; | ||
private int ticker = 0; | ||
private int testCount = 0; | ||
|
||
public AddressableLEDTestCommand(AddressableLEDSubsystem ledSystem) { | ||
this.ledSystem = ledSystem; | ||
addRequirements(ledSystem); | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
ticker = 0; | ||
testCount = 0; | ||
currentPattern = | ||
LEDPattern.rainbow(255, 128) | ||
.scrollAtAbsoluteSpeed(MetersPerSecond.of(1), AddressableLEDConstants.LED_DENSITY); | ||
} | ||
|
||
@Override | ||
public void execute() { | ||
if (ticker < 500) { | ||
ticker++; | ||
} else { | ||
ticker = 0; | ||
ledSystem.applySectionedPattern(LEDPattern.kOff, testCount); | ||
testCount++; | ||
if (testCount == AddressableLEDConstants.SECTIONS.length) { | ||
currentPattern = LEDPattern.solid(Color.kLimeGreen).breathe(Seconds.of(2)); | ||
ledSystem.applyPattern(currentPattern); | ||
} else { | ||
ledSystem.applySectionedPattern(currentPattern, testCount); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isFinished() { | ||
return testCount < (AddressableLEDConstants.SECTIONS.length + 1); | ||
} | ||
|
||
@Override | ||
public void end(boolean interrupted) { | ||
RobotLeopard86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ledSystem.applyPattern(LEDPattern.kOff); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package frc.robot.subsystems.addressableled; | ||
|
||
RobotLeopard86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
import static edu.wpi.first.units.Units.Meters; | ||
|
||
import edu.wpi.first.units.measure.Distance; | ||
|
||
public class AddressableLEDConstants { | ||
/** | ||
* @param low The lower bound of the range | ||
* @param high The upper bound of the range | ||
*/ | ||
public record Range(int low, int high) {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does range do? It's not immediately obvious, so there should be a javadoc here There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like Aceius said, it's still not clear what the purpose of Range is. The parameters are obvious, so you can remove the javadoc comments for those, but you need to make it clear when and where Range is used. Someone should not have to read through to code and use context to understand the purpose of this |
||
|
||
// TODO: Implement real values | ||
public static final int LED_COUNT = 64; | ||
public static final Distance LED_DENSITY = Meters.of(1.0 / LED_COUNT); | ||
public static final int LED_STRIP_PORT = 0; | ||
|
||
/** | ||
* @apiNote The lower bound of the range represents the lowest index LED for this section, and the | ||
* upper bound represents the highest index LED | ||
*/ | ||
public static final Range SECTIONS[] = {new Range(0, LED_COUNT)}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package frc.robot.subsystems.addressableled; | ||
RobotLeopard86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import edu.wpi.first.wpilibj.AddressableLED; | ||
import edu.wpi.first.wpilibj.AddressableLEDBuffer; | ||
import edu.wpi.first.wpilibj.AddressableLEDBufferView; | ||
import edu.wpi.first.wpilibj.LEDPattern; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
public class AddressableLEDSubsystem extends SubsystemBase { | ||
private final AddressableLED led; | ||
private final AddressableLEDBuffer ledBuffer; | ||
private AddressableLEDBufferView ledViews[]; | ||
private LEDPattern[] currentPatterns; | ||
|
||
public AddressableLEDSubsystem() { | ||
// Create strip and buffer | ||
led = new AddressableLED(AddressableLEDConstants.LED_STRIP_PORT); | ||
ledBuffer = new AddressableLEDBuffer(AddressableLEDConstants.LED_COUNT); | ||
led.setLength(AddressableLEDConstants.LED_COUNT); | ||
|
||
// Create current pattern trackers | ||
currentPatterns = new LEDPattern[AddressableLEDConstants.SECTIONS.length]; | ||
|
||
// Create section views | ||
ledViews = new AddressableLEDBufferView[AddressableLEDConstants.SECTIONS.length]; | ||
for (int i = 0; i < ledViews.length; i++) { | ||
RobotLeopard86 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ledViews[i] = | ||
new AddressableLEDBufferView( | ||
ledBuffer, | ||
AddressableLEDConstants.SECTIONS[i].low(), | ||
AddressableLEDConstants.SECTIONS[i].high()); | ||
} | ||
} | ||
|
||
// Periodically update the LED strip | ||
@Override | ||
public void periodic() { | ||
for (int i = 0; i < ledViews.length; i++) { | ||
currentPatterns[i].applyTo(ledViews[i]); | ||
} | ||
led.setData(ledBuffer); | ||
} | ||
|
||
// Apply a color pattern to a section of the LED strip | ||
public void applySectionedPattern(LEDPattern pattern, int section) { | ||
if (section < 0 || section >= ledViews.length) return; | ||
pattern.applyTo(ledViews[section]); | ||
currentPatterns[section] = pattern; | ||
} | ||
|
||
// Apply a color pattern to a section of the LED strip | ||
public void applyPattern(LEDPattern pattern) { | ||
pattern.applyTo(ledBuffer); | ||
for (int i = 0; i < currentPatterns.length; i++) { | ||
currentPatterns[i] = pattern; | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.