From 294fd5786fbcb65334dd9beaae6727064180e3b2 Mon Sep 17 00:00:00 2001 From: elma Date: Wed, 13 Aug 2025 21:02:39 +0300 Subject: [PATCH 1/6] initial commit for library, led example --- src/main/java/frc/kelrotlib/leds/Led.java | 20 +++++++++++++++++++ src/main/java/frc/robot/RobotContainer.java | 1 + .../frc/robot/subsystems/LedSubsystem.java | 20 +++++++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 src/main/java/frc/kelrotlib/leds/Led.java create mode 100644 src/main/java/frc/robot/subsystems/LedSubsystem.java diff --git a/src/main/java/frc/kelrotlib/leds/Led.java b/src/main/java/frc/kelrotlib/leds/Led.java new file mode 100644 index 0000000..cb47f2b --- /dev/null +++ b/src/main/java/frc/kelrotlib/leds/Led.java @@ -0,0 +1,20 @@ +package frc.kelrotlib.leds; + +import edu.wpi.first.wpilibj2.command.SubsystemBase; + +public class Led extends SubsystemBase { + /** Creates a new ExampleSubsystem. */ + public Led() { + + } + + @Override + public void periodic() { + // This method will be called once per scheduler run + } + + @Override + public void simulationPeriodic() { + // This method will be called once per scheduler run during simulation + } +} diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 5374114..20bd630 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -5,6 +5,7 @@ package frc.robot; import edu.wpi.first.wpilibj2.command.Command; +import frc.kelrotlib.leds.Led; public class RobotContainer { public RobotContainer() { diff --git a/src/main/java/frc/robot/subsystems/LedSubsystem.java b/src/main/java/frc/robot/subsystems/LedSubsystem.java new file mode 100644 index 0000000..b036163 --- /dev/null +++ b/src/main/java/frc/robot/subsystems/LedSubsystem.java @@ -0,0 +1,20 @@ +package frc.robot.subsystems; + +import frc.kelrotlib.leds.Led; + +public class LedSubsystem extends Led { + /** Creates a new ExampleSubsystem. */ + public LedSubsystem() { + + } + + @Override + public void periodic() { + // This method will be called once per scheduler run + } + + @Override + public void simulationPeriodic() { + // This method will be called once per scheduler run during simulation + } +} From b4e8899fc5f125290edf5578397c4b340f062d74 Mon Sep 17 00:00:00 2001 From: elma Date: Wed, 13 Aug 2025 21:35:12 +0300 Subject: [PATCH 2/6] led example finished, needs review --- gradlew | 0 src/main/java/frc/kelrotlib/leds/Led.java | 104 +++++++++++++++--- src/main/java/frc/robot/Constants.java | 9 ++ src/main/java/frc/robot/RobotContainer.java | 7 +- .../frc/robot/subsystems/LedSubsystem.java | 11 +- 5 files changed, 104 insertions(+), 27 deletions(-) mode change 100644 => 100755 gradlew diff --git a/gradlew b/gradlew old mode 100644 new mode 100755 diff --git a/src/main/java/frc/kelrotlib/leds/Led.java b/src/main/java/frc/kelrotlib/leds/Led.java index cb47f2b..e3e197f 100644 --- a/src/main/java/frc/kelrotlib/leds/Led.java +++ b/src/main/java/frc/kelrotlib/leds/Led.java @@ -1,20 +1,94 @@ package frc.kelrotlib.leds; +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; +import edu.wpi.first.wpilibj.LEDPattern; +import edu.wpi.first.wpilibj.util.Color; +import edu.wpi.first.wpilibj2.command.Command; import edu.wpi.first.wpilibj2.command.SubsystemBase; +import frc.robot.Constants.LedConstants; + +public class Led extends SubsystemBase{ //a Java inheritance example + private AddressableLED m_led; + private AddressableLEDBuffer m_buffer; + private HashMap m_groupList; + private HashMap m_patternList; + private LEDPattern k_defaultPattern = LEDPattern.solid(Color.kBlack); + + public Led() { + 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(); //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(); //For this version of grouping we need to store previous patterns for each group + + 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 + 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 + } + + public void setSolidColor(Color color, Integer[] groupIDs){ + 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 + } + runPattern(); + } + + public void setBlinkColor(Color color, double interval, Integer[] groupIDs){ + 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); + } + runPattern(); + } + + public void rainbow(Integer[] groupIDs){ + 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); + } + runPattern(); + } + + public void turnOff(){ + runPattern(k_defaultPattern); + } + + public 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 + } + }); + } + + public Command runPattern(LEDPattern pattern) { //might get removed later. + return run(() -> { + pattern.applyTo(m_buffer); + }); + } -public class Led extends SubsystemBase { - /** Creates a new ExampleSubsystem. */ - public Led() { - - } - - @Override - public void periodic() { - // This method will be called once per scheduler run - } - - @Override - public void simulationPeriodic() { - // This method will be called once per scheduler run during simulation - } + @Override + public void periodic() { //update the data every 20ms + m_led.setData(m_buffer); + } + + @Override + public void simulationPeriodic() { + } } diff --git a/src/main/java/frc/robot/Constants.java b/src/main/java/frc/robot/Constants.java index 43947fc..eef6ae4 100644 --- a/src/main/java/frc/robot/Constants.java +++ b/src/main/java/frc/robot/Constants.java @@ -4,6 +4,15 @@ package frc.robot; +import static edu.wpi.first.units.Units.Meters; + +import edu.wpi.first.units.measure.Distance; + public final class Constants { + 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 + } } diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 20bd630..95567f9 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -4,13 +4,16 @@ package frc.robot; +import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; -import frc.kelrotlib.leds.Led; +import frc.robot.subsystems.LedSubsystem; public class RobotContainer { + private final LedSubsystem leds = new LedSubsystem(); + public RobotContainer() { - configureBindings(); + leds.setSolidColor(Color.kFirstBlue, new Integer[] {1}); } private void configureBindings() { diff --git a/src/main/java/frc/robot/subsystems/LedSubsystem.java b/src/main/java/frc/robot/subsystems/LedSubsystem.java index b036163..8c14452 100644 --- a/src/main/java/frc/robot/subsystems/LedSubsystem.java +++ b/src/main/java/frc/robot/subsystems/LedSubsystem.java @@ -5,16 +5,7 @@ public class LedSubsystem extends Led { /** Creates a new ExampleSubsystem. */ public LedSubsystem() { - + createGroup(0,29, 1); } - @Override - public void periodic() { - // This method will be called once per scheduler run - } - - @Override - public void simulationPeriodic() { - // This method will be called once per scheduler run during simulation - } } From d828bb3aa81d79a4d24d1c73f2ac8ddcf51f9cb7 Mon Sep 17 00:00:00 2001 From: elma Date: Wed, 13 Aug 2025 21:49:11 +0300 Subject: [PATCH 3/6] added second group --- src/main/java/frc/robot/subsystems/LedSubsystem.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/frc/robot/subsystems/LedSubsystem.java b/src/main/java/frc/robot/subsystems/LedSubsystem.java index 8c14452..4ac9a0d 100644 --- a/src/main/java/frc/robot/subsystems/LedSubsystem.java +++ b/src/main/java/frc/robot/subsystems/LedSubsystem.java @@ -6,6 +6,7 @@ public class LedSubsystem extends Led { /** Creates a new ExampleSubsystem. */ public LedSubsystem() { createGroup(0,29, 1); + createGroup(30,59,2); } } From a066ba8fd4ef9e838b83ba057a79948660314a91 Mon Sep 17 00:00:00 2001 From: recepsirin0 <159413980+recepsirin0@users.noreply.github.com> Date: Thu, 14 Aug 2025 15:39:21 +0300 Subject: [PATCH 4/6] Update Led.java --- src/main/java/frc/kelrotlib/leds/Led.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/frc/kelrotlib/leds/Led.java b/src/main/java/frc/kelrotlib/leds/Led.java index e3e197f..f73686b 100644 --- a/src/main/java/frc/kelrotlib/leds/Led.java +++ b/src/main/java/frc/kelrotlib/leds/Led.java @@ -14,7 +14,7 @@ import edu.wpi.first.wpilibj2.command.SubsystemBase; import frc.robot.Constants.LedConstants; -public class Led extends SubsystemBase{ //a Java inheritance example +public class Led extends SubsystemBase { //a Java inheritance example private AddressableLED m_led; private AddressableLEDBuffer m_buffer; private HashMap m_groupList; From b3e0ccb92bea953fb387566e8503e9fcae1232ca Mon Sep 17 00:00:00 2001 From: elma Date: Fri, 15 Aug 2025 14:53:48 +0300 Subject: [PATCH 5/6] deleted Led example, added actual example files --- .../frc/kelrotlib/leds/ExampleLibrary.java | 25 +++++ src/main/java/frc/kelrotlib/leds/Led.java | 94 ------------------- src/main/java/frc/robot/RobotContainer.java | 5 - .../subsystems/ExampleLibrarySubsystem.java | 10 ++ .../frc/robot/subsystems/LedSubsystem.java | 12 --- 5 files changed, 35 insertions(+), 111 deletions(-) create mode 100644 src/main/java/frc/kelrotlib/leds/ExampleLibrary.java delete mode 100644 src/main/java/frc/kelrotlib/leds/Led.java create mode 100644 src/main/java/frc/robot/subsystems/ExampleLibrarySubsystem.java delete mode 100644 src/main/java/frc/robot/subsystems/LedSubsystem.java diff --git a/src/main/java/frc/kelrotlib/leds/ExampleLibrary.java b/src/main/java/frc/kelrotlib/leds/ExampleLibrary.java new file mode 100644 index 0000000..d2b8530 --- /dev/null +++ b/src/main/java/frc/kelrotlib/leds/ExampleLibrary.java @@ -0,0 +1,25 @@ +package frc.kelrotlib.leds; + +import edu.wpi.first.wpilibj2.command.SubsystemBase; + +public class ExampleLibrary extends SubsystemBase { + /** Creates a new ExampleSubsystem. */ + public ExampleLibrary() { + + } + + public void exampleMethod() { + // Example method that could be used to control LEDs + // This is where you would implement your LED control logic + } + + @Override + public void periodic() { + // This method will be called once per scheduler run + } + + @Override + public void simulationPeriodic() { + // This method will be called once per scheduler run during simulation + } +} diff --git a/src/main/java/frc/kelrotlib/leds/Led.java b/src/main/java/frc/kelrotlib/leds/Led.java deleted file mode 100644 index f73686b..0000000 --- a/src/main/java/frc/kelrotlib/leds/Led.java +++ /dev/null @@ -1,94 +0,0 @@ -package frc.kelrotlib.leds; - -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; -import edu.wpi.first.wpilibj.LEDPattern; -import edu.wpi.first.wpilibj.util.Color; -import edu.wpi.first.wpilibj2.command.Command; -import edu.wpi.first.wpilibj2.command.SubsystemBase; -import frc.robot.Constants.LedConstants; - -public class Led extends SubsystemBase { //a Java inheritance example - private AddressableLED m_led; - private AddressableLEDBuffer m_buffer; - private HashMap m_groupList; - private HashMap m_patternList; - private LEDPattern k_defaultPattern = LEDPattern.solid(Color.kBlack); - - public Led() { - 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(); //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(); //For this version of grouping we need to store previous patterns for each group - - 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 - 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 - } - - public void setSolidColor(Color color, Integer[] groupIDs){ - 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 - } - runPattern(); - } - - public void setBlinkColor(Color color, double interval, Integer[] groupIDs){ - 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); - } - runPattern(); - } - - public void rainbow(Integer[] groupIDs){ - 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); - } - runPattern(); - } - - public void turnOff(){ - runPattern(k_defaultPattern); - } - - public 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 - } - }); - } - - public Command runPattern(LEDPattern pattern) { //might get removed later. - return run(() -> { - pattern.applyTo(m_buffer); - }); - } - - @Override - public void periodic() { //update the data every 20ms - m_led.setData(m_buffer); - } - - @Override - public void simulationPeriodic() { - } -} diff --git a/src/main/java/frc/robot/RobotContainer.java b/src/main/java/frc/robot/RobotContainer.java index 95567f9..f22fc1b 100644 --- a/src/main/java/frc/robot/RobotContainer.java +++ b/src/main/java/frc/robot/RobotContainer.java @@ -4,16 +4,11 @@ package frc.robot; -import edu.wpi.first.wpilibj.util.Color; import edu.wpi.first.wpilibj2.command.Command; -import frc.robot.subsystems.LedSubsystem; public class RobotContainer { - private final LedSubsystem leds = new LedSubsystem(); - public RobotContainer() { configureBindings(); - leds.setSolidColor(Color.kFirstBlue, new Integer[] {1}); } private void configureBindings() { diff --git a/src/main/java/frc/robot/subsystems/ExampleLibrarySubsystem.java b/src/main/java/frc/robot/subsystems/ExampleLibrarySubsystem.java new file mode 100644 index 0000000..7052752 --- /dev/null +++ b/src/main/java/frc/robot/subsystems/ExampleLibrarySubsystem.java @@ -0,0 +1,10 @@ +package frc.robot.subsystems; + +import frc.kelrotlib.leds.ExampleLibrary; + +public class ExampleLibrarySubsystem extends ExampleLibrary { + + public ExampleLibrarySubsystem() { + + } +} diff --git a/src/main/java/frc/robot/subsystems/LedSubsystem.java b/src/main/java/frc/robot/subsystems/LedSubsystem.java deleted file mode 100644 index 4ac9a0d..0000000 --- a/src/main/java/frc/robot/subsystems/LedSubsystem.java +++ /dev/null @@ -1,12 +0,0 @@ -package frc.robot.subsystems; - -import frc.kelrotlib.leds.Led; - -public class LedSubsystem extends Led { - /** Creates a new ExampleSubsystem. */ - public LedSubsystem() { - createGroup(0,29, 1); - createGroup(30,59,2); - } - -} From acbda4c5f8494ffbf88f205fb54a3dc618002130 Mon Sep 17 00:00:00 2001 From: elma Date: Fri, 15 Aug 2025 17:11:54 +0300 Subject: [PATCH 6/6] corrected file path --- .../java/frc/kelrotlib/{leds => example}/ExampleLibrary.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename src/main/java/frc/kelrotlib/{leds => example}/ExampleLibrary.java (94%) diff --git a/src/main/java/frc/kelrotlib/leds/ExampleLibrary.java b/src/main/java/frc/kelrotlib/example/ExampleLibrary.java similarity index 94% rename from src/main/java/frc/kelrotlib/leds/ExampleLibrary.java rename to src/main/java/frc/kelrotlib/example/ExampleLibrary.java index d2b8530..644a158 100644 --- a/src/main/java/frc/kelrotlib/leds/ExampleLibrary.java +++ b/src/main/java/frc/kelrotlib/example/ExampleLibrary.java @@ -1,4 +1,4 @@ -package frc.kelrotlib.leds; +package frc.kelrotlib.example; import edu.wpi.first.wpilibj2.command.SubsystemBase;