supla-device
Loading...
Searching...
No Matches
rgbw_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_RGBW_BASE_H_
20#define SRC_SUPLA_CONTROL_RGBW_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 RGBWBase : 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 RGBWBase();
75
76 virtual void setRGBWValueOnDevice(uint32_t red,
77 uint32_t green,
78 uint32_t blue,
79 uint32_t colorBrightness,
80 uint32_t brightness) = 0;
81
82 virtual void setRGBW(int red,
83 int green,
84 int blue,
85 int colorBrightness,
86 int brightness,
87 bool toggle = false,
88 bool instant = false);
89
90 int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override;
91 virtual void turnOn();
92 virtual void turnOff();
93 virtual void toggle();
94 bool isOn();
95 bool isOnW();
96 bool isOnRGB();
97 void handleAction(int event, int action) override;
98 void setStep(int step);
99 void setDefaultDimmedBrightness(int dimmedBrightness);
100 void setFadeEffectTime(int timeMs);
101 void setMinIterationBrightness(uint8_t minBright);
102 void setMinMaxIterationDelay(uint16_t delayMs);
103
104 void onInit() override;
105 void iterateAlways() override;
106 void onFastTimer() override;
107 void onLoadState() override;
108 void onSaveState() override;
109 void onLoadConfig(SuplaDeviceClass *) override;
110
111 void attach(Supla::Control::Button *);
112
113 // Method is used by external integrations to prepare TSD_SuplaChannelNewValue
114 // value for specific channel type (i.e. to prefill durationMS field when
115 // required)
117
118 virtual RGBWBase &setDefaultStateOn();
119 virtual RGBWBase &setDefaultStateOff();
120 virtual RGBWBase &setDefaultStateRestore();
121 // Set mapping between interface setting of brightness and actual value
122 // set on device. Values should be between 0 and 1023 (min, max).
123 // I.e. if limit is set to (100, 800), then values from Supla in range
124 // 0-100% are mapped to PWM values in range 100 and 800.
125 virtual RGBWBase &setBrightnessLimits(int min, int max);
126 // Set mapping between interface setting of color brightness and actual value
127 // set on device. Values should be between 0 and 1023 (min, max).
128 virtual RGBWBase &setColorBrightnessLimits(int min, int max);
129
130 void setBrightnessAdjuster(BrightnessAdjuster *adjuster);
131 int getCurrentDimmerBrightness() const;
132 int getCurrentRGBBrightness() const;
133 void setMaxHwValue(int newMaxHwValue);
134
135 protected:
136 uint8_t addWithLimit(int value, int addition, int limit = 255);
137 virtual void iterateDimmerRGBW(int rgbStep, int wStep);
138 // Set mapping between interface setting of brightness and actual value
139 // set on device.
140 // Input value is in range 0-100.
141 // Returns value in range 0-1023 adjusted by selected function.
142 int adjustBrightness(int value);
143
144 int getStep(int step, int target, int current, int distance) const;
145 bool calculateAndUpdate(int targetValue,
146 uint16_t *hwValue,
147 int distance,
148 uint32_t *lastChangeMs) const;
149
150 bool valueChanged = true;
151 uint8_t buttonStep = 10; // 10
152 uint8_t curRed = 0; // 0 - 255
153 uint8_t curGreen = 255; // 0 - 255
154 uint8_t curBlue = 0; // 0 - 255
155 uint8_t curColorBrightness = 0; // 0 - 100
156 uint8_t curBrightness = 0; // 0 - 100
157 uint8_t lastColorBrightness = 100; // 0 - 100
158 uint8_t lastBrightness = 100; // 0 - 100
159 uint8_t defaultDimmedBrightness = 20; // 20
160 bool dimIterationDirection = false;
161 bool resetDisance = false;
162 bool instant = false;
163 int8_t stateOnInit = RGBW_STATE_ON_INIT_RESTORE;
164 uint8_t minIterationBrightness = 1;
165
166 enum ButtonControlType buttonControlType = BUTTON_FOR_RGBW;
167 enum AutoIterateMode autoIterateMode = AutoIterateMode::OFF;
168
169 uint16_t maxHwValue = 1023;
170 uint16_t hwRed = 0; // 0 - maxHwValue
171 uint16_t hwGreen = 0; // 0 - maxHwValue
172 uint16_t hwBlue = 0; // 0 - maxHwValue
173 uint16_t hwColorBrightness = 0; // 0 - maxHwValue
174 uint16_t hwBrightness = 0; // 0 - maxHwValue
175 uint16_t minBrightness = 1;
176 uint16_t maxBrightness = 1023;
177 uint16_t minColorBrightness = 1;
178 uint16_t maxColorBrightness = 1023;
179 uint16_t redDistance = 0;
180 uint16_t greenDistance = 0;
181 uint16_t blueDistance = 0;
182 uint16_t colorBrightnessDistance = 0;
183 uint16_t brightnessDistance = 0;
184
185 uint16_t minMaxIterationDelay = 750;
186 uint16_t fadeEffect = 500;
187
188 uint32_t lastTick = 0;
189 uint32_t lastChangeRedMs = 0;
190 uint32_t lastChangeGreenMs = 0;
191 uint32_t lastChangeBlueMs = 0;
192 uint32_t lastChangeColorBrightnessMs = 0;
193 uint32_t lastChangeBrightnessMs = 0;
194 uint32_t lastMsgReceivedMs = 0;
195 uint32_t lastIterateDimmerTimestamp = 0;
196 uint32_t iterationDelayTimestamp = 0;
197 uint32_t lastAutoIterateStartTimestamp = 0;
198
199 BrightnessAdjuster *brightnessAdjuster = nullptr;
200 Supla::Control::Button *attachedButton = nullptr;
201};
202
203}; // namespace Control
204}; // namespace Supla
205
206#endif // SRC_SUPLA_CONTROL_RGBW_BASE_H_
Definition SuplaDevice.h:93
Definition action_handler.h:21
Definition rgbw_base.h:35
Definition button.h:30
void fillSuplaChannelNewValue(TSD_SuplaChannelNewValue *value) override
Fills TSD_SuplaChannelNewValue based on current state.
Definition rgbw_base.cpp:1012
void onLoadConfig(SuplaDeviceClass *) override
First method called on element in SuplaDevice.begin().
Definition rgbw_base.cpp:990
void iterateAlways() override
Method called on each SuplaDevice iteration.
Definition rgbw_base.cpp:139
int32_t handleNewValueFromServer(TSD_SuplaChannelNewValue *newValue) override
Handles "new value" request from server.
Definition rgbw_base.cpp:148
void onFastTimer() override
Method called on fast timer interupt.
Definition rgbw_base.cpp:630
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition rgbw_base.cpp:748
void onSaveState() override
Method called periodically during SuplaDevice iteration.
Definition rgbw_base.cpp:890
void onLoadState() override
Second method called on element in SuplaDevice.begin().
Definition rgbw_base.cpp:913
Definition proto.h:1167