WledPipe Interface
Common interface for all WLED communication pipes.
Package
robowled.wledpipe.WledPipe
Overview
WledPipe is the base interface that all pipe implementations share. This allows you to write code that works with any transport mechanism (serial, network, or mock).
Implementations
| Class | Description |
|---|---|
| SerialPipe | USB/serial communication |
| NetworkPipe | TCP/IP network communication |
| DummyPipe | Mock pipe for testing and simulation |
Interface Methods
sendGson(JsonElement json)
Sends a Gson JsonElement as JSON text, followed by a newline.
void sendGson(JsonElement json) throws Exception;
Parameters:
| Parameter | Type | Description |
|---|---|---|
json |
JsonElement |
The Gson JSON element to send (JsonObject, JsonArray, etc.) |
Throws: Exception if sending fails
sendString(String str)
Sends a raw string.
void sendString(String str) throws Exception;
Parameters:
| Parameter | Type | Description |
|---|---|---|
str |
String |
The string to send |
Throws: Exception if sending fails
tryReadString()
Non-blocking read operation. Call periodically to check for incoming data.
String tryReadString() throws Exception;
Returns: A complete line (without newline), or null if no complete line is available
Throws: Exception if reading fails
tryReadGson()
Non-blocking read that parses the response as a Gson JsonElement.
JsonElement tryReadGson() throws Exception;
Returns: A parsed JsonElement, or null if no complete line is available
Throws: Exception if reading or parsing fails
isConnected()
Checks if the pipe is connected and ready for communication.
boolean isConnected();
Returns: true if connected, false otherwise
close()
Closes the pipe and releases any resources.
void close();
Using the Interface
The interface allows you to write flexible code that can work with different pipe implementations:
public class LedSubsystem extends SubsystemBase {
private final WledPipe wled;
// Accept any pipe implementation
public LedSubsystem(WledPipe wled) {
this.wled = wled;
}
public void setPreset(int preset) {
try {
wled.sendString("{\"ps\":" + preset + "}\n");
} catch (Exception e) {
System.err.println("Failed to set preset: " + e.getMessage());
}
}
public void setBrightness(int brightness) {
try {
wled.sendString("{\"bri\":" + brightness + "}\n");
} catch (Exception e) {
System.err.println("Failed: " + e.getMessage());
}
}
}
Production Usage
// With serial connection
LedSubsystem leds = new LedSubsystem(
new SerialPipe(SerialPort.Port.kUSB, 115200)
);
// With network connection
LedSubsystem leds = new LedSubsystem(
new NetworkPipe("wled-underglow.local", 21324)
);
Simulation/Testing Usage
// With dummy pipe for simulation
DummyPipe mockPipe = new DummyPipe();
LedSubsystem leds = new LedSubsystem(mockPipe);
// Verify accumulated state after commands
leds.setBrightness(200);
assertEquals(200, mockPipe.getStateValue("bri").getAsInt());
See Also
- SerialPipe - USB serial implementation
- NetworkPipe - Network implementation
- DummyPipe - Mock implementation for testing
- Simulation & Testing Guide