Skip to content

Commit

Permalink
Fix level write mode for LEDs which don't support white and color upd…
Browse files Browse the repository at this point in the history
…ate at once. (openhab#15846)

Signed-off-by: Madeorsk <[email protected]>
  • Loading branch information
Madeorsk authored Sep 14, 2024
1 parent ce12f70 commit cae6539
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ public enum Driver {
FADING
}

public enum LevelWriteMode {
ALL((byte) 0x00),
COLORS((byte) 0xF0),
WHITES((byte) 0x0F);

public final byte byteValue;

private LevelWriteMode(byte byteValue) {
this.byteValue = byteValue;
}
}

public static final Integer DEFAULT_PORT = 5577;

protected static final int DEFAULT_SOCKET_TIMEOUT = 5000;
Expand Down Expand Up @@ -199,11 +211,15 @@ protected void sendRaw(byte[] data, DataOutputStream outputStream) throws IOExce
}

protected byte[] getBytesForColor(byte r, byte g, byte b, byte w, byte w2) {
return getBytesForColor(r, g, b, w, w2, LevelWriteMode.ALL);
}

protected byte[] getBytesForColor(byte r, byte g, byte b, byte w, byte w2, LevelWriteMode writeMode) {
byte[] bytes;
if (protocol == Protocol.LD382 || protocol == Protocol.LD382A) {
bytes = new byte[] { 0x31, r, g, b, w, 0x00 };
bytes = new byte[] { 0x31, r, g, b, w, writeMode.byteValue };
} else if (protocol == Protocol.LD686) {
bytes = new byte[] { 0x31, r, g, b, w, w2, 0x00 };
bytes = new byte[] { 0x31, r, g, b, w, w2, writeMode.byteValue };
} else {
throw new UnsupportedOperationException("Protocol " + protocol + " not yet implemented");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,55 +70,55 @@ public synchronized void setColor(HSBType color) throws IOException {
logger.debug("Setting color to {}", color);

LEDStateDTO ledState = getLEDStateDTO().withColor(color).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.COLORS);
}

@Override
public synchronized void setBrightness(PercentType brightness) throws IOException {
logger.debug("Setting brightness to {}", brightness);

LEDStateDTO ledState = getLEDStateDTO().withBrightness(brightness).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.COLORS);
}

@Override
public synchronized void incBrightness(int step) throws IOException {
logger.debug("Changing brightness by {}", step);

LEDStateDTO ledState = getLEDStateDTO().withIncrementedBrightness(step).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.COLORS);
}

@Override
public synchronized void setWhite(PercentType white) throws IOException {
logger.debug("Setting (warm) white LED to {}", white);

LEDStateDTO ledState = getLEDStateDTO().withWhite(white).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.WHITES);
}

@Override
public synchronized void incWhite(int step) throws IOException {
logger.debug("Changing white by {}", step);

LEDStateDTO ledState = getLEDStateDTO().withIncrementedWhite(step).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.WHITES);
}

@Override
public void setWhite2(PercentType white2) throws IOException {
logger.debug("Setting (warm) white 2 LED to {}", white2);

LEDStateDTO ledState = getLEDStateDTO().withWhite2(white2).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.WHITES);
}

@Override
public void incWhite2(int step) throws IOException {
logger.debug("Changing white by {}", step);

LEDStateDTO ledState = getLEDStateDTO().withIncrementedWhite2(step).withoutProgram();
sendLEDData(ledState);
sendLEDData(ledState, LevelWriteMode.WHITES);
}

@Override
Expand Down Expand Up @@ -156,6 +156,10 @@ public synchronized void incProgramSpeed(int step) throws IOException {
}

private synchronized void sendLEDData(final LEDStateDTO ledState) {
this.sendLEDData(ledState, LevelWriteMode.ALL);
}

private synchronized void sendLEDData(final LEDStateDTO ledState, LevelWriteMode writeMode) {
cachedLedStatus = ledState;
if (!ledUpdateFuture.isDone()) {
ledUpdateFuture.cancel(true);
Expand All @@ -171,7 +175,7 @@ private synchronized void sendLEDData(final LEDStateDTO ledState) {
byte w = (byte) (((int) (ledState.getWhite().doubleValue() * 255 / 100)) & 0xFF);
byte w2 = (byte) (((int) (ledState.getWhite2().doubleValue() * 255 / 100)) & 0xFF);

bytes = getBytesForColor(r, g, b, w, w2);
bytes = getBytesForColor(r, g, b, w, w2, writeMode);
} else {
// program selected
byte p = (byte) (program & 0xFF);
Expand Down

0 comments on commit cae6539

Please sign in to comment.