2 * Copyright (c) 2010-2021 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;
20 import org.openhab.core.library.types.DecimalType;
21 import org.openhab.core.library.types.HSBType;
22 import org.openhab.core.library.types.OnOffType;
23 import org.openhab.core.library.types.PercentType;
24 import org.openhab.core.library.types.StringType;
27 * The {@link LEDStateDTO} class holds the data and the settings for a LED device (i.e. the selected colors, the running
30 * @author Osman Basha - Initial contribution
31 * @author Stefan Endrullis - Initial contribution
32 * @author Ries van Twisk - Prevent flashes during classic driver color + white updates
34 public class LEDStateDTO {
35 private HSBType hsbType;
36 private OnOffType power;
37 private PercentType white;
38 private PercentType white2;
39 private StringType program;
40 private PercentType programSpeed;
42 public LEDStateDTO(OnOffType power, DecimalType hue, PercentType saturation, PercentType brightness,
43 PercentType white, PercentType white2, StringType program, PercentType programSpeed) {
44 this.hsbType = new HSBType(hue, saturation, brightness);
48 this.program = program;
49 this.programSpeed = programSpeed;
52 public LEDStateDTO(OnOffType power, HSBType hsb, PercentType white, PercentType white2, StringType program,
53 PercentType programSpeed) {
58 this.program = program;
59 this.programSpeed = programSpeed;
62 public PercentType getWhite() {
66 public PercentType getWhite2() {
70 public StringType getProgram() {
74 public PercentType getProgramSpeed() {
79 public String toString() {
80 return power + "," + hsbType.getHue() + "," + hsbType.getSaturation() + "," + hsbType.getBrightness() + ","
81 + getWhite() + "," + getWhite2() + " [" + getProgram() + "," + getProgramSpeed() + "]";
84 public static LEDStateDTO valueOf(int state, int program, int programSpeed, int red, int green, int blue, int white,
86 OnOffType power = (state & 0x01) != 0 ? OnOffType.ON : OnOffType.OFF;
88 float[] hsv = new float[3];
89 Color.RGBtoHSB(red, green, blue, hsv);
90 DecimalType h = new DecimalType(new BigDecimal(hsv[0]).multiply(new BigDecimal(360.0)));
91 PercentType s = new PercentType(new BigDecimal(hsv[1]).multiply(new BigDecimal(100.0)));
92 PercentType b = new PercentType(new BigDecimal(hsv[2]).multiply(new BigDecimal(100.0)));
93 HSBType hsbType = new HSBType(h, s, b);
95 PercentType w = new PercentType(new BigDecimal(white).divide(new BigDecimal(255.0), 3, BigDecimal.ROUND_HALF_UP)
96 .multiply(new BigDecimal(100.0)));
97 PercentType w2 = new PercentType(new BigDecimal(white2)
98 .divide(new BigDecimal(255.0), 3, BigDecimal.ROUND_HALF_UP).multiply(new BigDecimal(100.0)));
100 // Range: 0x00 .. 0x1F. Speed is inversed
101 BigDecimal ps = new BigDecimal(programSpeed).divide(new BigDecimal(0x1f), 2, BigDecimal.ROUND_HALF_UP)
102 .multiply(new BigDecimal(100.0));
103 PercentType e = new PercentType(new BigDecimal(100.0).subtract(ps));
105 StringType p = new StringType(Integer.toString(program));
107 return new LEDStateDTO(power, hsbType, w, w2, p, e);
110 public LEDStateDTO withColor(HSBType color) {
111 return new LEDStateDTO(power, color, this.getWhite(), this.getWhite2(), this.getProgram(),
112 this.getProgramSpeed());
115 public LEDStateDTO withBrightness(PercentType brightness) {
116 return new LEDStateDTO(power, hsbType.getHue(), hsbType.getSaturation(), brightness, this.getWhite(),
117 this.getWhite2(), this.getProgram(), this.getProgramSpeed());
120 public LEDStateDTO withIncrementedBrightness(int step) {
121 int brightness = hsbType.getBrightness().intValue();
122 brightness = max(min(brightness + step, 0), 100);
124 return withBrightness(new PercentType(brightness));
127 public LEDStateDTO withWhite(PercentType white) {
128 return new LEDStateDTO(power, hsbType, white, this.getWhite2(), this.getProgram(), this.getProgramSpeed());
131 public LEDStateDTO withIncrementedWhite(int step) {
132 int white = this.getWhite().intValue();
133 white = max(min(white + step, 0), 100);
135 return withWhite(new PercentType(white));
138 public LEDStateDTO withWhite2(PercentType white2) {
139 return new LEDStateDTO(power, hsbType, this.getWhite(), white2, this.getProgram(), this.getProgramSpeed());
142 public LEDStateDTO withIncrementedWhite2(int step) {
143 int white = this.getWhite().intValue();
144 white = max(min(white + step, 0), 100);
146 return withWhite(new PercentType(white));
149 public LEDStateDTO withProgram(StringType program) {
150 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), program, this.getProgramSpeed());
153 public LEDStateDTO withoutProgram() {
154 return withProgram(new StringType(String.valueOf(0x61)));
157 public LEDStateDTO withProgramSpeed(PercentType programSpeed) {
158 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), programSpeed);
161 public LEDStateDTO withIncrementedProgramSpeed(int step) {
162 int programSpeed = this.getProgramSpeed().intValue();
163 programSpeed = max(min(programSpeed + step, 0), 100);
165 return withProgramSpeed(new PercentType(programSpeed));
168 public LEDStateDTO withPower(OnOffType power) {
169 return new LEDStateDTO(power, hsbType, this.getWhite(), this.getWhite2(), this.getProgram(), getProgramSpeed());
172 public int getRGB() {
173 return hsbType.getRGB();
176 public HSBType getHSB() {
180 public OnOffType getPower() {