Skip to content

Commit 7383de3

Browse files
committed
Make LED subsystem a singleton
1 parent b089fba commit 7383de3

File tree

3 files changed

+85
-50
lines changed

3 files changed

+85
-50
lines changed

src/main/java/org/lasarobotics/led/LEDStrip.java

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,17 @@ public static Hardware initializeHardware(ID id) {
140140
return ledStripHardware;
141141
}
142142

143-
Runnable runAnimation() {
144-
return () -> {
145-
m_sectionLEDPatterns.entrySet().stream().forEach(entry -> {
146-
for (var section : entry.getKey()) entry.getValue().applyTo(SECTION_MAP.get(section));
147-
});
148-
m_leds.setData(m_ledBuffer);
149-
};
143+
void runAnimation() {
144+
m_sectionLEDPatterns.entrySet().stream().forEach(entry -> {
145+
for (var section : entry.getKey()) entry.getValue().applyTo(SECTION_MAP.get(section));
146+
});
147+
m_leds.setData(m_ledBuffer);
150148
}
151149

152150
/**
153151
* Prepare for LED override
154152
*/
155-
protected void startOverride() {
153+
void startOverride() {
156154
// Save LED patterns
157155
m_tempLEDPatterns.clear();
158156
m_tempLEDPatterns.putAll(m_sectionLEDPatterns);
@@ -161,24 +159,16 @@ protected void startOverride() {
161159
/**
162160
* Restore LED patterns after override
163161
*/
164-
protected void endOverride() {
162+
void endOverride() {
165163
m_sectionLEDPatterns.clear();
166164
m_sectionLEDPatterns.putAll(m_tempLEDPatterns);
167165
}
168166

169-
/**
170-
* Get latest LED buffer
171-
* @return Addressable LED buffer
172-
*/
173-
public AddressableLEDBuffer getBuffer() {
174-
return m_ledBuffer;
175-
}
176-
177167
/**
178168
* Set pattern and color of LED strip
179169
* @param pattern Desired pattern
180170
*/
181-
public void set(LEDPattern pattern) {
171+
void set(LEDPattern pattern) {
182172
set(pattern, Section.FULL);
183173
}
184174

@@ -187,7 +177,7 @@ public void set(LEDPattern pattern) {
187177
* @param pattern Desired pattern
188178
* @param sections LED strip sections to set
189179
*/
190-
public void set(LEDPattern pattern, Section... sections) {
180+
void set(LEDPattern pattern, Section... sections) {
191181
// Remove all conflicting scheduled LED patterns
192182
m_sectionLEDPatterns.entrySet().removeIf(
193183
(entry) -> !Collections.disjoint(Arrays.asList(entry.getKey()), Arrays.asList(sections))
@@ -201,19 +191,27 @@ public void set(LEDPattern pattern, Section... sections) {
201191
Logger.recordOutput(String.join("/", m_leds.getName(), section.name()), pattern.toString());
202192
}
203193

194+
/**
195+
* Turn off LED strip sections
196+
* @param sections LED strip sections
197+
*/
198+
void off(Section... sections) {
199+
set(LEDPattern.kOff, sections);
200+
}
201+
204202
/**
205203
* Turn off LED strip
206204
*/
207-
public void off() {
205+
void off() {
208206
set(LEDPattern.kOff, Section.FULL);
209207
}
210208

211209
/**
212-
* Turn off LED strip sections
213-
* @param sections LED strip sections
210+
* Get latest LED buffer
211+
* @return Addressable LED buffer
214212
*/
215-
public void off(Section... sections) {
216-
set(LEDPattern.kOff, sections);
213+
public AddressableLEDBuffer getBuffer() {
214+
return m_ledBuffer;
217215
}
218216

219217
@Override

src/main/java/org/lasarobotics/led/LEDSubsystem.java

Lines changed: 51 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,84 @@
1111

1212
/** LED Subsystem */
1313
public class LEDSubsystem extends SubsystemBase implements AutoCloseable {
14+
private static LEDSubsystem s_instance;
1415
private static LEDPattern m_overridePattern = LEDPattern.solid(LEDStrip.TEAM_COLOR);
15-
16-
private LEDStrip m_ledStrip;
16+
private static LEDStrip s_ledStrip;
1717

1818
/** Creates a new LEDSubsystem. */
19-
public LEDSubsystem(LEDStrip ledStrip) {
20-
this.m_ledStrip = ledStrip;
21-
setDefaultCommand(run(m_ledStrip.runAnimation()));
19+
private LEDSubsystem() {
20+
setDefaultCommand(run(() -> {
21+
if (s_ledStrip != null) s_ledStrip.runAnimation();
22+
}));
23+
}
24+
25+
public static LEDSubsystem getInstance() {
26+
if (s_instance == null) s_instance = new LEDSubsystem();
27+
return s_instance;
28+
}
29+
30+
public void setLEDStrip(LEDStrip ledStrip) {
31+
s_ledStrip = ledStrip;
2232
}
2333

2434
@Override
2535
public void periodic() {
2636
// This method will be called once per scheduler run
2737
}
2838

29-
/**
30-
* Add LED strips
31-
* @param ledStrips LED strips to add
32-
*/
33-
public void add(LEDStrip ledStrips) {
34-
m_ledStrip = ledStrips;
35-
}
36-
3739
/**
3840
* Start override of all LEDs
3941
* @param pattern Pattern to override with
4042
*/
4143
public void startOverride(LEDPattern pattern) {
4244
m_overridePattern = pattern;
43-
m_ledStrip.startOverride();
44-
m_ledStrip.set(m_overridePattern, Section.FULL);
45+
s_ledStrip.startOverride();
46+
s_ledStrip.set(m_overridePattern, Section.FULL);
4547
}
4648

4749
/**
4850
* End override of LEDs, resume previous patterns
4951
*/
5052
public void endOverride() {
5153
m_overridePattern = LEDPattern.solid(LEDStrip.TEAM_COLOR);
52-
m_ledStrip.endOverride();
54+
s_ledStrip.endOverride();
55+
}
56+
57+
/**
58+
* Set pattern and color of LED strip sections
59+
* @param pattern Desired pattern
60+
* @param sections LED strip sections to set
61+
*/
62+
public void set(LEDPattern pattern, Section... sections) {
63+
s_ledStrip.set(pattern, sections);
64+
}
65+
66+
/**
67+
* Set pattern and color of LED strip
68+
* @param pattern Desired pattern
69+
*/
70+
public void set(LEDPattern pattern) {
71+
s_ledStrip.set(pattern, Section.FULL);
72+
}
73+
74+
/**
75+
* Turn off LED strip sections
76+
* @param sections LED strip sections
77+
*/
78+
public void off(Section... sections) {
79+
s_ledStrip.set(LEDPattern.kOff, sections);
80+
}
81+
82+
/**
83+
* Turn off LED strip
84+
*/
85+
public void off() {
86+
s_ledStrip.set(LEDPattern.kOff, Section.FULL);
5387
}
5488

5589
@Override
5690
public void close() {
57-
m_ledStrip.close();
91+
s_ledStrip.close();
5892
}
5993
}
6094

src/test/java/org/lasarobotics/led/LEDSubsystemTest.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,10 @@ public void setup() {
4747
m_ledStrip1 = new LEDStrip(new LEDStrip.Hardware(m_leds1));
4848

4949
// Create LEDSubsystem object
50-
m_ledSubsystem = new LEDSubsystem(m_ledStrip1);
50+
m_ledSubsystem = LEDSubsystem.getInstance();
51+
52+
// Set LED strip for subsystem
53+
m_ledSubsystem.setLEDStrip(m_ledStrip1);
5154
}
5255

5356
@AfterEach
@@ -61,7 +64,7 @@ public void close() {
6164
@DisplayName("Test if robot can set LED strip to single static solid color")
6265
public void solidFull() {
6366
// Set LED pattern
64-
m_ledStrip1.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), LEDStrip.Section.FULL);
67+
m_ledSubsystem.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), LEDStrip.Section.FULL);
6568

6669
// Run LED subsystem loop
6770
m_ledSubsystem.getDefaultCommand().execute();
@@ -79,8 +82,8 @@ public void solidFull() {
7982
@DisplayName("Test if robot can set LED strip start section independently")
8083
public void startSection() {
8184
// Set LED pattern
82-
m_ledStrip1.set(LEDPattern.solid(Color.kRed), Section.START);
83-
m_ledStrip1.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.MIDDLE, Section.END);
85+
m_ledSubsystem.set(LEDPattern.solid(Color.kRed), Section.START);
86+
m_ledSubsystem.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.MIDDLE, Section.END);
8487

8588
// Run LED subsystem loop
8689
m_ledSubsystem.getDefaultCommand().execute();
@@ -97,8 +100,8 @@ public void startSection() {
97100
@DisplayName("Test if robot can set LED strip middle section independently")
98101
public void middleSection() {
99102
// Set LED pattern
100-
m_ledStrip1.set(LEDPattern.solid(Color.kRed), Section.MIDDLE);
101-
m_ledStrip1.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.START, Section.END);
103+
m_ledSubsystem.set(LEDPattern.solid(Color.kRed), Section.MIDDLE);
104+
m_ledSubsystem.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.START, Section.END);
102105

103106
// Run LED subsystem loop
104107
m_ledSubsystem.getDefaultCommand().execute();
@@ -120,8 +123,8 @@ public void middleSection() {
120123
@DisplayName("Test if robot can set LED strip end section independently")
121124
public void endSection() {
122125
// Set LED pattern
123-
m_ledStrip1.set(LEDPattern.solid(Color.kRed), Section.END);
124-
m_ledStrip1.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.START, Section.MIDDLE);
126+
m_ledSubsystem.set(LEDPattern.solid(Color.kRed), Section.END);
127+
m_ledSubsystem.set(LEDPattern.solid(LEDStrip.TEAM_COLOR), Section.START, Section.MIDDLE);
125128

126129
// Run LED subsystem loop
127130
m_ledSubsystem.getDefaultCommand().execute();
@@ -138,7 +141,7 @@ public void endSection() {
138141
@DisplayName("Test if robot can override subsystem LED control")
139142
public void ledOverride() {
140143
// Set LED pattern
141-
m_ledStrip1.set(LEDPattern.solid(Color.kBlue), Section.FULL);
144+
m_ledSubsystem.set(LEDPattern.solid(Color.kBlue), Section.FULL);
142145

143146
// Request LED override
144147
m_ledSubsystem.startOverride(LEDPattern.solid(LEDStrip.TEAM_COLOR));

0 commit comments

Comments
 (0)