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 enum class LegacyChannelFunction : uint8_t {
75 None,
76 RGBW,
77 RGB,
78 Dimmer
79 };
80
88 explicit RGBCCTBase(RGBCCTBase *parent = nullptr);
89 virtual ~RGBCCTBase() = default;
90
91 void purgeConfig() override;
92 Supla::ApplyConfigResult applyChannelConfig(TSD_ChannelConfig *result,
93 bool local = false) override;
94 void fillChannelConfig(void *channelConfig,
95 int *size,
96 uint8_t configType) override;
97
98 virtual void setRGBCCTValueOnDevice(uint32_t red,
99 uint32_t green,
100 uint32_t blue,
101 uint32_t colorBrightness,
102 uint32_t white1Brightness,
103 uint32_t white2Brightness) = 0;
104
105 virtual void setRGBW(int red,
106 int green,
107 int blue,
108 int colorBrightness,
109 int whiteBrightness,
110 bool toggle = false,
111 bool instant = false);
112
113 virtual void setRGBCCT(int red,
114 int green,
115 int blue,
116 int colorBrightness,
117 int whiteBrightness,
118 int whiteTemperature,
119 bool toggle = false,
120 bool instant = false);
121
122 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
123 virtual void turnOn();
124 virtual void turnOff();
125 virtual void toggle();
126 bool isOn();
127 bool isOnW();
128 bool isOnRGB();
129 void handleAction(int event, int action) override;
130 void setStep(int step);
131 void setDefaultDimmedBrightness(int dimmedBrightness);
132 void setFadeEffectTime(int timeMs);
133 void setMinIterationBrightness(uint8_t minBright);
134 void setMinMaxIterationDelay(uint16_t delayMs);
135
136 void onInit() override;
137 void iterateAlways() override;
138 void onFastTimer() override;
139 void onLoadState() override;
140 void onSaveState() override;
141 bool isStateStorageMigrationNeeded() const override;
142 void onLoadConfig(SuplaDeviceClass *) override;
143
149 void convertStorageFromLegacyChannel(LegacyChannelFunction channelFunction);
150
157
158 void attach(Supla::Control::Button *);
159
160 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
161 // value for specific channel type (i.e. to prefill durationMS field when
162 // required)
164
165 virtual RGBCCTBase &setDefaultStateOn();
166 virtual RGBCCTBase &setDefaultStateOff();
167 virtual RGBCCTBase &setDefaultStateRestore();
168 // Set mapping between interface setting of brightness and actual value
169 // set on device. Values should be between 0 and 1023 (min, max).
170 // I.e. if limit is set to (100, 800), then values from Supla in range
171 // 0-100% are mapped to PWM values in range 100 and 800.
172 virtual RGBCCTBase &setBrightnessLimits(int min, int max);
173 // Set mapping between interface setting of color brightness and actual value
174 // set on device. Values should be between 0 and 1023 (min, max).
175 virtual RGBCCTBase &setColorBrightnessLimits(int min, int max);
176
177 void setBrightnessAdjuster(BrightnessAdjuster *adjuster);
178 int getCurrentDimmerBrightness() const;
179 int getCurrentRGBBrightness() const;
180 void setMaxHwValue(int newMaxHwValue);
181
187 bool hasParent() const;
188
194 int getAncestorCount() const;
195
196 protected:
206 int getMissingGpioCount() const;
207
208 void enableChannel();
209 void disableChannel();
210
211 uint8_t addWithLimit(int value, int addition, int limit = 255);
212 virtual void iterateDimmerRGBW(int rgbStep, int wStep);
213 // Set mapping between interface setting of brightness and actual value
214 // set on device.
215 // Input value is in range 0-100.
216 // Returns value in range 0-1023 adjusted by selected function.
217 int adjustBrightness(int value);
218
219 int getStep(int step, int target, int current, int distance) const;
220 bool calculateAndUpdate(int targetValue,
221 uint16_t *hwValue,
222 int distance,
223 uint32_t *lastChangeMs) const;
224
225 bool valueChanged = true;
226 uint8_t buttonStep = 10; // 10
227 uint8_t curRed = 0; // 0 - 255
228 uint8_t curGreen = 255; // 0 - 255
229 uint8_t curBlue = 0; // 0 - 255
230 uint8_t curColorBrightness = 0; // 0 - 100
231 uint8_t curWhiteBrightness = 0; // 0 - 100
232 uint8_t curWhiteTemperature = 0; // 0 - 100
233 uint8_t lastColorBrightness = 100; // 0 - 100
234 uint8_t lastWhiteBrightness = 100; // 0 - 100
235 uint8_t defaultDimmedBrightness = 20; // 20
236 bool dimIterationDirection = false;
237 bool resetDisance = false;
238 bool instant = false;
239 bool enabled = true;
240 bool initDone = false;
241 bool skipLegacyMigration = false;
242 int8_t stateOnInit = RGBW_STATE_ON_INIT_RESTORE;
243 uint8_t minIterationBrightness = 1;
244 LegacyChannelFunction legacyChannelFunction = LegacyChannelFunction::None;
245
246 enum ButtonControlType buttonControlType = BUTTON_FOR_RGBW;
247 enum AutoIterateMode autoIterateMode = AutoIterateMode::OFF;
248
249 uint16_t maxHwValue = 1023;
250 uint16_t hwRed = 0; // 0 - maxHwValue
251 uint16_t hwGreen = 0; // 0 - maxHwValue
252 uint16_t hwBlue = 0; // 0 - maxHwValue
253 uint16_t hwColorBrightness = 0; // 0 - maxHwValue
254 uint16_t hwBrightness = 0; // 0 - maxHwValue
255 uint16_t hwWhiteTemperature = 0; // 0 - maxHwValue
256 uint16_t hwWhite1Brightness = 0; // 0 - maxHwValue
257 uint16_t hwWhite2Brightness = 0; // 0 - maxHwValue
258 uint16_t minBrightness = 1;
259 uint16_t maxBrightness = 1023;
260 uint16_t minColorBrightness = 1;
261 uint16_t maxColorBrightness = 1023;
262 uint16_t redDistance = 0;
263 uint16_t greenDistance = 0;
264 uint16_t blueDistance = 0;
265 uint16_t colorBrightnessDistance = 0;
266 uint16_t brightnessDistance = 0;
267 uint16_t whiteTemperatureDistance = 0;
268
269 uint16_t minMaxIterationDelay = 750;
270 uint16_t fadeEffect = 500;
271
272 uint32_t lastTick = 0;
273 uint32_t lastChangeRedMs = 0;
274 uint32_t lastChangeGreenMs = 0;
275 uint32_t lastChangeBlueMs = 0;
276 uint32_t lastChangeColorBrightnessMs = 0;
277 uint32_t lastChangeBrightnessMs = 0;
278 uint32_t lastChangeWhiteTemperatureMs = 0;
279 uint32_t lastMsgReceivedMs = 0;
280 uint32_t lastIterateDimmerTimestamp = 0;
281 uint32_t iterationDelayTimestamp = 0;
282 uint32_t lastAutoIterateStartTimestamp = 0;
283
284 float warmWhiteGain = 1.0;
285 float coldWhiteGain = 1.0;
286
287 BrightnessAdjuster *brightnessAdjuster = nullptr;
288 Supla::Control::Button *attachedButton = nullptr;
289 RGBCCTBase *parent = nullptr;
290};
291
292}; // namespace Control
293}; // namespace Supla
294
295#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:709
RGBCCTBase(RGBCCTBase *parent=nullptr)
Constructor.
Definition rgb_cct_base.cpp:67
void setSkipLegacyMigration()
Disables storage conversion from legacy channel function to new.
Definition rgb_cct_base.cpp:1376
int getAncestorCount() const
Returns number of ancestor instances (by parent)
Definition rgb_cct_base.cpp:1364
void purgeConfig() override
Removes all configration data related to the element from Storage::Config.
Definition rgb_cct_base.cpp:1286
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition rgb_cct_base.cpp:182
void convertStorageFromLegacyChannel(LegacyChannelFunction channelFunction)
Enables storage conversion from legacy channel function to new.
Definition rgb_cct_base.cpp:1309
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition rgb_cct_base.cpp:1084
int getMissingGpioCount() const
Returns number of GPIO pins that are required to control this channel.
Definition rgb_cct_base.cpp:1314
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition rgb_cct_base.cpp:164
bool hasParent() const
Checks if this instance has parent.
Definition rgb_cct_base.cpp:1360
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition rgb_cct_base.cpp:1018
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition rgb_cct_base.cpp:1202
bool isStateStorageMigrationNeeded() const override
Method called after onInit() to check if state storage migration is needed.
Definition rgb_cct_base.cpp:1371
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition rgb_cct_base.cpp:866
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition rgb_cct_base.cpp:1251
Definition proto.h:3075
Definition proto.h:1200