-
Notifications
You must be signed in to change notification settings - Fork 2
Led Grouping v4 #21
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: led-subsystem
Are you sure you want to change the base?
Led Grouping v4 #21
Changes from 1 commit
6b35dfd
a0a858c
43f7c27
0a5b2d4
c47d1c2
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 |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
| 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 | ||
|
Member
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. 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.
Collaborator
Author
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. 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(); | ||
| } | ||
|
|
@@ -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 | ||
| } | ||
| }); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.