2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.wifiled.internal.handler;
15 import static java.lang.Math.*;
17 import java.awt.Color;
18 import java.math.BigDecimal;
19 import java.math.RoundingMode;
21 import org.openhab.core.library.types.DecimalType;
22 import org.openhab.core.library.types.HSBType;
23 import org.openhab.core.library.types.OnOffType;
24 import org.openhab.core.library.types.PercentType;
25 import org.openhab.core.library.types.StringType;
28 * The {@link LEDStateDTO} class holds the data and the settings for a LED device (i.e. the selected colors, the running
31 * @author Osman Basha - Initial contribution
32 * @author Stefan Endrullis - Initial contribution
33 * @author Ries van Twisk - Prevent flashes during classic driver color + white updates
35 public class LEDStateDTO {
36 private HSBType hsbType;
37 private OnOffType power;
38 private PercentType white;
39 private PercentType white2;
40 private StringType program;
41 private PercentType programSpeed;
43 public LEDStateDTO(OnOffType power, DecimalType hue, PercentType saturation, PercentType brightness,
44 PercentType white, PercentType white2, StringType program, PercentType programSpeed) {
45 this.hsbType = new HSBType(hue, saturation, brightness);
49 this.program = program;
50 this.programSpeed = programSpeed;
53 public LEDStateDTO(OnOffType power, HSBType hsb, PercentType white, PercentType white2, StringType program,
54 PercentType programSpeed) {
59 this.program = program;
60 this.programSpeed = programSpeed;
63 public PercentType getWhite() {
67 public PercentType getWhite2() {
71 public StringType getProgram() {
75 public PercentType getProgramSpeed() {
80 public String toString() {
81 return power + "," + hsbType.getHue() + "," + hsbType.getSaturation() + "," + hsbType.getBrightness() + ","
82 + getWhite() + "," + getWhite2() + " [" + getProgram() + "," + getProgramSpeed() + "]";
85 public static LEDStateDTO valueOf(int state, int program, int programSpeed, int red, int green, int blue, int white,
87 OnOffType power = OnOffType.from((state & 0x01) != 0);
89 float[] hsv = new float[3];
90 Color.RGBtoHSB(red, green, blue, hsv);
91 DecimalType h = new DecimalType(new BigDecimal(hsv[0]).multiply(new BigDecimal(360.0)));
92 PercentType s = new PercentType(new BigDecimal(hsv[1]).multiply(new BigDecimal(100.0)));
93 PercentType b = new PercentType(new BigDecimal(hsv[2]).multiply(new BigDecimal(100.0)));
94 HSBType hsbType = new HSBType(h, s, b);
96 PercentType w = new PercentType(new BigDecimal(white).divide(new BigDecimal(255.0), 3, RoundingMode.HALF_UP)
97 .multiply(new BigDecimal(100.0)));
98 PercentType w2 = new PercentType(new BigDecimal(white2).divide(new BigDecimal(255.0), 3, RoundingMode.HALF_UP)
99 .multiply(new BigDecimal(100.0)));
101 // Range: 0x00 .. 0x1F. Speed is inversed
102 BigDecimal ps = new BigDecimal(programSpeed).divide(new BigDecimal(0x1f), 2, RoundingMode.HALF_UP)
103 .multiply(new BigDecimal(100.0));
104 PercentType e = new PercentType(new BigDecimal(100.0).subtract(ps));
106 StringType p = new StringType(Integer.toString(program));
108 return new LEDStateDTO(power, hsbType, w, w2, p, e);
111 public LEDStateDTO withColor(HSBType color) {
112 return new LEDStateDTO(power, color, this.getWhite(), this.getWhite2(), this.getProgram(),
113 this.getProgramSpeed());
116 public LEDStateDTO withBrightness(PercentType brightness) {
117 return new LEDStateDTO(power, hsbType.getHue(), hsbType.getSaturation(), brightness, this.getWhite(),
118 this.getWhite2(), this.getProgram(), this.getProgramSpeed());
121 public LEDStateDTO withIncrementedBrightness(int step) {
122 int brightness = hsbType.getBrightness().intValue();
123 brightness = max(min(brightness + step, 0), 100);
125 return withBrightness(new PercentType(brightness));
128 public LEDStateDTO withWhite(PercentType white) {
129 return new LEDStateDTO(power, hsbType, white, this.getWhite2(), this.getProgram(), this.getProgramSpeed());
132 public LEDStateDTO withIncrementedWhite(int step) {
133 int white = this.getWhite().intValue();
134 white = max(min(white + step, 0), 100);
136 return withWhite(new PercentType(white));
139 public LEDStateDTO withWhite2(PercentType white2) {
140 return new LEDStateDTO(power, hsbType, this.getWhite(), white2, this.getProgram(), this.getProgramSpeed());
143 public LEDStateDTO withIncrementedWhite2(int step) {
144 int white = this.getWhite().intValue();
145 white = max(min(white + step, 0), 100);
147 return withWhite(new PercentType(white));
150 public LEDStateDTO withProgram(StringType program) {
151 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), program, this.getProgramSpeed());
154 public LEDStateDTO withoutProgram() {
155 return withProgram(new StringType(String.valueOf(0x61)));
158 public LEDStateDTO withProgramSpeed(PercentType programSpeed) {
159 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), programSpeed);
162 public LEDStateDTO withIncrementedProgramSpeed(int step) {
163 int programSpeed = this.getProgramSpeed().intValue();
164 programSpeed = max(min(programSpeed + step, 0), 100);
166 return withProgramSpeed(new PercentType(programSpeed));
169 public LEDStateDTO withPower(OnOffType power) {
170 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), getProgramSpeed());
173 public int getRGB() {
174 return hsbType.getRGB();
177 public HSBType getHSB() {
181 public OnOffType getPower() {