Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 25 additions & 22 deletions src/main/java/frc/kelrotlib/leds/Led.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import static edu.wpi.first.units.Units.MetersPerSecond;
import static edu.wpi.first.units.Units.Seconds;

import java.util.HashMap;

import edu.wpi.first.wpilibj.AddressableLED;
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.AddressableLEDBufferView;
Expand All @@ -17,50 +15,55 @@
public class Led extends SubsystemBase{ //a Java inheritance example
private AddressableLED m_led;
private AddressableLEDBuffer m_buffer;
private HashMap<Integer, AddressableLEDBufferView> m_groupList;
private HashMap<Integer, LEDPattern> m_patternList;
private LEDPattern k_defaultPattern = LEDPattern.solid(Color.kBlack);
private AddressableLEDBufferView[] m_groupList;
private LEDPattern[] m_patternList;

private LEDPattern k_defaultPattern;
private final int[][] k_ledGroups;

public Led() {
public Led(int[][] ledGroups) {
m_led = new AddressableLED(LedConstants.kLedPort); //every variable you might change later (ports, length etc.) should be added to Constants.java*
m_buffer = new AddressableLEDBuffer(LedConstants.kLedLength); // * this way every port and important variable is in one place.
m_led.setLength(LedConstants.kLedLength); //setting the length takes a lot of load so do it only one time when possible
m_led.start();

m_groupList = new HashMap<Integer, AddressableLEDBufferView>(); //I decided to use Integers for the keys as it is much more straight-forward, in future uses the keys can be String's for more complicated group identification needs.
m_patternList = new HashMap<Integer, LEDPattern>(); //For this version of grouping we need to store previous patterns for each group
k_defaultPattern = LEDPattern.solid(Color.kBlack);
k_ledGroups = ledGroups;
Comment thread
mekathegapple marked this conversation as resolved.
Outdated
for (int[] i : k_ledGroups) { //create groups based on the ledGroups array
createGroup(i[0], i[1]);
}

setDefaultCommand(runPattern(k_defaultPattern).withName("Off")); //set all leds to off/black on start
}

public void createGroup(int startingIndex, int endingIndex, Integer groupID) { //infinite amount of groups creatable, without complicating the code
public void createGroup(int startingIndex, int endingIndex) {
AddressableLEDBufferView group = m_buffer.createView(startingIndex, endingIndex); //create new group(view)
m_groupList.put(groupID, group);
m_patternList.put(groupID, k_defaultPattern); //initially set the defaultPattern, so the runPattern command doesn't break
m_groupList[m_groupList.length] = group; //add the group to the groupList
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a NullPointerException, because the array is never initialized with a size and cannot grow dynamically. Replace the arrays with ArrayList so you can add groups safely. same thing for the patternlist.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I guess I thought about Python arrays while writing it, instead of ArrayList's I've initialized the arrays with the required size in the constructor class. Also coming in the next commit.

m_patternList[m_patternList.length] = k_defaultPattern; //add the default pattern to the patternList
}

public void setSolidColor(Color color, Integer[] groupIDs){
public void setSolidColor(Color color, int[] groupIndexes){
LEDPattern solidColorPattern = LEDPattern.solid(color);
for (int i = 0; i < groupIDs.length; i++) { //update the latest pattern for every LED group given
m_patternList.replace(groupIDs[i], solidColorPattern); //I love hashmap it is just so cool
for (int i = 0; i < groupIndexes.length; i++) { //update the latest pattern for every LED group given
m_patternList[groupIndexes[i]] = solidColorPattern; //replace the pattern for the groupID with the new solid color pattern
}
runPattern();
}

public void setBlinkColor(Color color, double interval, Integer[] groupIDs){
public void setBlinkColor(Color color, double interval, int[] groupIndexes){
LEDPattern base = LEDPattern.solid(color);
LEDPattern blinkPattern = base.blink(Seconds.of(interval)); //synchronised blink, wpilib also has support for asynchronised blink
for (int i = 0; i < groupIDs.length; i++) {
m_patternList.replace(groupIDs[i], blinkPattern);
for (int i = 0; i < groupIndexes.length; i++) {
m_patternList[groupIndexes[i]] = blinkPattern;
}
runPattern();
}

public void rainbow(Integer[] groupIDs){
public void rainbow(int[] groupIndexes){
LEDPattern rainbow = LEDPattern.rainbow(255, 128); //all hues at maximum saturation and *half* brightness
LEDPattern scrollingRainbow = rainbow.scrollAtAbsoluteSpeed(MetersPerSecond.of(1), LedConstants.kLedSpacing); //moves/scrolls the effect at a speed of 1 meter per second
for (int i = 0; i < groupIDs.length; i++) {
m_patternList.replace(groupIDs[i], scrollingRainbow);
for (int i = 0; i < groupIndexes.length; i++) {
m_patternList[groupIndexes[i]] = scrollingRainbow;
}
runPattern();
}
Expand All @@ -71,8 +74,8 @@ public void turnOff(){

private Command runPattern(){ //A command is used as it doesn't allow actions to run simultaneously, for this usage it is crucial, because we need to stop the previous patterns and start the new ones.
return run(() -> {
for (Integer i : m_groupList.keySet()) { //parsing through every key in the groupList HashMap
m_patternList.get(i).applyTo(m_groupList.get(i)); //getting every setted pattern and applying it to the ID'd LED Group
for (int i = 0; i < m_groupList.length; i++) {
m_patternList[i].applyTo(m_groupList[i]); //apply the pattern to the group
}
});
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/frc/robot/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ public static class LedConstants {
public static final int kLedPort = 6; //PWM port on RoborIO
public static final int kLedLength = 60; //led count
public static final Distance kLedSpacing = Meters.of(1/ 60.0); // density of 120 LEDs per meter
public static final int[][] kledGroups = {
new int[] {0,1}, //group 0
new int[] {2,3}, // group 1
new int[] {4,5} // group 2
};

}

}
8 changes: 3 additions & 5 deletions src/main/java/frc/robot/RobotContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

package frc.robot;

import frc.robot.Constants.LedConstants;
import frc.robot.subsystems.LedSubsystem;
import edu.wpi.first.wpilibj.util.Color;
import edu.wpi.first.wpilibj2.command.Command;
public class RobotContainer {
private final LedSubsystem m_led = new LedSubsystem();
private final LedSubsystem m_led = new LedSubsystem(LedConstants.kledGroups);

public RobotContainer() {
m_led.createGroup(0, 29, 1); //indexing: in a 60 led strip, first LED is index 0 and the last led is index 59
m_led.createGroup(30, 59, 2);

m_led.setSolidColor(Color.kFirstBlue, new Integer[] {1,2}); //The library Color includes many color presets, but it is also possible to use RGB values to create "Color"s
m_led.setSolidColor(Color.kFirstBlue, new int[] {1,2}); //The library Color includes many color presets, but it is also possible to use RGB values to create "Color"s

configureBindings();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/frc/robot/subsystems/LedSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ public class LedSubsystem extends Led {
// Inherits all methods and properties from the KelrotLib Led class
// You can add additional methods or override existing ones if needed

public LedSubsystem() {
super(); // Call the constructor of the parent class
// Additional initialization if necessary
public LedSubsystem(int[][] ledGroups) {
super(ledGroups);
}
}