supla-device
Loading...
Searching...
No Matches
rgb_cct_base.h
1/*
2 Copyright (C) AC SOFTWARE SP. Z O.O.
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17*/
18
19#ifndef SRC_SUPLA_CONTROL_RGB_CCT_BASE_H_
20#define SRC_SUPLA_CONTROL_RGB_CCT_BASE_H_
21
22#include <stdint.h>
23
24#include "../action_handler.h"
25#include "../actions.h"
26#include "../channel_element.h"
27
28#define RGBW_STATE_ON_INIT_RESTORE -1
29#define RGBW_STATE_ON_INIT_OFF 0
30#define RGBW_STATE_ON_INIT_ON 1
31
32namespace Supla {
33namespace Control {
34
36 public:
37 virtual ~BrightnessAdjuster() = default;
38 virtual int adjustBrightness(int input) = 0;
39 virtual void setMaxHwValue(int maxHwValue) = 0;
40};
41
42class GeometricBrightnessAdjuster : public BrightnessAdjuster {
43 public:
44 explicit GeometricBrightnessAdjuster(double power = 1.505,
45 int offset = 0,
46 int maxHwValue = 1023);
47 void setMaxHwValue(int maxHwValue) override;
48 int adjustBrightness(int input) override;
49
50 private:
51 double power = 1.505;
52 int offset = 0;
53 int maxHwValue = 1023;
54};
55
56class Button;
57
58class RGBCCTBase : public ChannelElement, public ActionHandler {
59 public:
60 enum ButtonControlType : uint8_t {
61 BUTTON_FOR_RGBW,
62 BUTTON_FOR_RGB,
63 BUTTON_FOR_W,
64 BUTTON_NOT_USED
65 };
66
67 enum class AutoIterateMode : uint8_t {
68 OFF,
69 DIMMER,
70 RGB,
71 ALL
72 };
73
74 RGBCCTBase();
75
76 void purgeConfig() override;
77 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
78 bool local = false) override;
79 void fillChannelConfig(void *channelConfig,
80 int *size,
81 uint8_t configType) override;
82
83 virtual void setRGBCCTValueOnDevice(uint32_t red,
84 uint32_t green,
85 uint32_t blue,
86 uint32_t colorBrightness,
87 uint32_t white1Brightness,
88 uint32_t white2Brightness) = 0;
89
90 virtual void setRGBW(int red,
91 int green,
92 int blue,
93 int colorBrightness,
94 int whiteBrightness,
95 bool toggle = false,
96 bool instant = false);
97
98 virtual void setRGBCCT(int red,
99 int green,
100 int blue,
101 int colorBrightness,
102 int whiteBrightness,
103 int whiteTemperature,
104 bool toggle = false,
105 bool instant = false);
106
107 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
108 virtual void turnOn();
109 virtual void turnOff();
110 virtual void toggle();
111 bool isOn();
112 bool isOnW();
113 bool isOnRGB();
114 void handleAction(int event, int action) override;
115 void setStep(int step);
116 void setDefaultDimmedBrightness(int dimmedBrightness);
117 void setFadeEffectTime(int timeMs);
118 void setMinIterationBrightness(uint8_t minBright);
119 void setMinMaxIterationDelay(uint16_t delayMs);
120
121 void onInit() override;
122 void iterateAlways() override;
123 void onFastTimer() override;
124 void onLoadState() override;
125 void onSaveState() override;
126 void onLoadConfig(SuplaDeviceClass *) override;
127
128 void attach(Supla::Control::Button *);
129
130 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
131 // value for specific channel type (i.e. to prefill durationMS field when
132 // required)
134
135 virtual RGBCCTBase &setDefaultStateOn();
136 virtual RGBCCTBase &setDefaultStateOff();
137 virtual RGBCCTBase &setDefaultStateRestore();
138 // Set mapping between interface setting of brightness and actual value
139 // set on device. Values should be between 0 and 1023 (min, max).
140 // I.e. if limit is set to (100, 800), then values from Supla in range
141 // 0-100% are mapped to PWM values in range 100 and 800.
142 virtual RGBCCTBase &setBrightnessLimits(int min, int max);
143 // Set mapping between interface setting of color brightness and actual value
144 // set on device. Values should be between 0 and 1023 (min, max).
145 virtual RGBCCTBase &setColorBrightnessLimits(int min, int max);
146
147 void setBrightnessAdjuster(BrightnessAdjuster *adjuster);
148 int getCurrentDimmerBrightness() const;
149 int getCurrentRGBBrightness() const;
150 void setMaxHwValue(int newMaxHwValue);
151
152 protected:
153 uint8_t addWithLimit(int value, int addition, int limit = 255);
154 virtual void iterateDimmerRGBW(int rgbStep, int wStep);
155 // Set mapping between interface setting of brightness and actual value
156 // set on device.
157 // Input value is in range 0-100.
158 // Returns value in range 0-1023 adjusted by selected function.
159 int adjustBrightness(int value);
160
161 int getStep(int step, int target, int current, int distance) const;
162 bool calculateAndUpdate(int targetValue,
163 uint16_t *hwValue,
164 int distance,
165 uint32_t *lastChangeMs) const;
166
167 bool valueChanged = true;
168 uint8_t buttonStep = 10; // 10
169 uint8_t curRed = 0; // 0 - 255
170 uint8_t curGreen = 255; // 0 - 255
171 uint8_t curBlue = 0; // 0 - 255
172 uint8_t curColorBrightness = 0; // 0 - 100
173 uint8_t curWhiteBrightness = 0; // 0 - 100
174 uint8_t curWhiteTemperature = 0; // 0 - 100
175 uint8_t lastColorBrightness = 100; // 0 - 100
176 uint8_t lastWhiteBrightness = 100; // 0 - 100
177 uint8_t defaultDimmedBrightness = 20; // 20
178 bool dimIterationDirection = false;
179 bool resetDisance = false;
180 bool instant = false;
181 int8_t stateOnInit = RGBW_STATE_ON_INIT_RESTORE;
182 uint8_t minIterationBrightness = 1;
183
184 enum ButtonControlType buttonControlType = BUTTON_FOR_RGBW;
185 enum AutoIterateMode autoIterateMode = AutoIterateMode::OFF;
186
187 uint16_t maxHwValue = 1023;
188 uint16_t hwRed = 0; // 0 - maxHwValue
189 uint16_t hwGreen = 0; // 0 - maxHwValue
190 uint16_t hwBlue = 0; // 0 - maxHwValue
191 uint16_t hwColorBrightness = 0; // 0 - maxHwValue
192 uint16_t hwBrightness = 0; // 0 - maxHwValue
193 uint16_t hwWhiteTemperature = 0; // 0 - maxHwValue
194 uint16_t hwWhite1Brightness = 0; // 0 - maxHwValue
195 uint16_t hwWhite2Brightness = 0; // 0 - maxHwValue
196 uint16_t minBrightness = 1;
197 uint16_t maxBrightness = 1023;
198 uint16_t minColorBrightness = 1;
199 uint16_t maxColorBrightness = 1023;
200 uint16_t redDistance = 0;
201 uint16_t greenDistance = 0;
202 uint16_t blueDistance = 0;
203 uint16_t colorBrightnessDistance = 0;
204 uint16_t brightnessDistance = 0;
205 uint16_t whiteTemperatureDistance = 0;
206
207 uint16_t minMaxIterationDelay = 750;
208 uint16_t fadeEffect = 500;
209
210 uint32_t lastTick = 0;
211 uint32_t lastChangeRedMs = 0;
212 uint32_t lastChangeGreenMs = 0;
213 uint32_t lastChangeBlueMs = 0;
214 uint32_t lastChangeColorBrightnessMs = 0;
215 uint32_t lastChangeBrightnessMs = 0;
216 uint32_t lastChangeWhiteTemperatureMs = 0;
217 uint32_t lastMsgReceivedMs = 0;
218 uint32_t lastIterateDimmerTimestamp = 0;
219 uint32_t iterationDelayTimestamp = 0;
220 uint32_t lastAutoIterateStartTimestamp = 0;
221
222 float warmWhiteGain = 1.0;
223 float coldWhiteGain = 1.0;
224
225 BrightnessAdjuster *brightnessAdjuster = nullptr;
226 Supla::Control::Button *attachedButton = nullptr;
227};
228
229}; // namespace Control
230}; // namespace Supla
231
232#endif // SRC_SUPLA_CONTROL_RGB_CCT_BASE_H_
Definition SuplaDevice.h:153
Definition action_handler.h:21
Definition rgb_cct_base.h:35
Definition button.h:34
void onFastTimer() override
Method called on fast timer interupt.
Definition rgb_cct_base.cpp:701
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition rgb_cct_base.cpp:1160
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition rgb_cct_base.cpp:174
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition rgb_cct_base.cpp:1024
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition rgb_cct_base.cpp:161
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition rgb_cct_base.cpp:1001
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition rgb_cct_base.cpp:1101
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition rgb_cct_base.cpp:852
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition rgb_cct_base.cpp:1126
Definition proto.h:3071
Definition proto.h:1199