supla-device
Loading...
Searching...
No Matches
addressable_leds.h
1/*
2 Copyright (C) malarz
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// Dependencies:
20// https://github.com/adafruit/Adafruit_NeoPixel
21
22#ifndef SRC_SUPLA_CONTROL_ADDRESSABLE_LEDS_H_
23#define SRC_SUPLA_CONTROL_ADDRESSABLE_LEDS_H_
24
25#include <Adafruit_NeoPixel.h>
26#include <supla/log_wrapper.h>
27#include <supla/control/virtual_relay.h>
28#include <supla/control/rgb_base.h>
29
30namespace Supla {
31namespace Control {
32
33enum AddressableLEDsEffect : uint8_t {
34 STILL,
35 SWAP,
36 FLOW,
37 RAINBOWWHEEL,
38 RAINBOW,
39};
40
47
49 public:
59 AddressableLEDs(uint16_t number, int16_t pin,
60 neoPixelType type = NEO_GRB + NEO_KHZ800) {
61 numberOfLeds = number;
62 pixels = new Adafruit_NeoPixel(numberOfLeds, pin, type);
63 }
64
78 void setEffect(AddressableLEDsEffect neweffect, uint16_t newStepTime,
79 uint8_t turnAllLEDsTime = 0) {
80 if (effect != neweffect) {
81 effect = neweffect;
82 counter = 0;
83 }
84
85 if (turnOnTime != turnAllLEDsTime) {
86 // switching OFF all LEDs
87 for (int i = 0; i < numberOfLeds; i++) {
88 pixels->setPixelColor(i, 0);
89 }
90 pixels->show();
91 lightedLeds = 0;
92 }
93
94 stepTime = newStepTime;
95 turnOnTime = turnAllLEDsTime;
96 }
97
98 void onInit() override {
99 pixels->begin();
100 }
101
102 AddressableLEDsEffect getEffect() {
103 return effect;
104 }
105 uint16_t getStepTime() {
106 return stepTime;
107 }
108 uint16_t getTurnOnTime() {
109 return turnOnTime;
110 }
111
112 bool isOn() {
113 return on;
114 }
115 void turnOn() {
116 on = true;
117 }
118 void turnOff() {
119 on = false;
120 }
121
122 void setColor(uint32_t color) {
123 RGBcolor = color;
124 }
125 void setColor(uint8_t red, uint8_t green, uint8_t blue) {
126 RGBcolor = pixels->Color(red, green, blue);
127 }
128 void setBrightness(uint8_t brightness) {
129 pixels->setBrightness(brightness);
130 }
131
133 // LEDs switching ON
134 if (isOn() && lightedLeds < numberOfLeds
135 && ((millis() - lastLEDTime >= (1000 * turnOnTime / numberOfLeds))
136 || (millis() < lastLEDTime))) {
137 lastLEDTime = millis();
138 SUPLA_LOG_DEBUG("RGB strip: switching on LED %d", lightedLeds);
139 lightedLeds++;
140 }
141
142 if (lightedLeds > 0 && ((millis()-lastTime >= stepTime)
143 || (millis() < lastTime))) {
144 lastTime = millis();
145 counter++;
146 // show effect
147 switch (effect) {
148 case STILL:
149 iterate_Still();
150 break;
151 case SWAP:
152 iterate_Swap();
153 break;
154 case FLOW:
155 iterate_Flow();
156 break;
157 case RAINBOWWHEEL:
158 iterate_RainbowWheel();
159 break;
160 case RAINBOW:
161 iterate_Rainbow();
162 break;
163 }
164 }
165
166 // LEDs switching OFF
167 if (!isOn() && lightedLeds > 0
168 && ((millis() - lastLEDTime >= (1000 * turnOnTime / numberOfLeds))
169 || (millis() < lastLEDTime))) {
170 lastLEDTime = millis();
171 lightedLeds--;
172 SUPLA_LOG_DEBUG("RGB strip: switching off LED %d", lightedLeds);
173 pixels->setPixelColor(lightedLeds, 0);
174 pixels->show();
175 }
176 }
177
178 protected:
179 Adafruit_NeoPixel *pixels;
180 bool on = false;
181 uint16_t numberOfLeds;
182 uint16_t lightedLeds = 0;
183 uint32_t RGBcolor = 0x004400; // color of SUPLA :-)
184 uint32_t LastColor = 0;
185
186 uint8_t turnOnTime = 0;
187 uint32_t lastLEDTime = 0;
188
189 uint16_t stepTime = 1000;
190 uint32_t lastTime = 0;
191
192 AddressableLEDsEffect effect = STILL;
193 uint32_t counter = 0;
194
195 void iterate_Still() {
196 for (int i = 0; i < lightedLeds; i++) {
197 pixels->setPixelColor(i, RGBcolor);
198 }
199 pixels->show();
200 }
201
202 void iterate_Swap() {
203 for (int i = 0; i < lightedLeds; i++) {
204 if ((i+counter)%2) {
205 pixels->setPixelColor(i, RGBcolor);
206 } else {
207 pixels->setPixelColor(i, 0);
208 }
209 }
210 pixels->show();
211 }
212
213 void iterate_Flow() {
214 for (int i = 0; i < numberOfLeds; i++) {
215 if ((i+counter)%4) {
216 pixels->setPixelColor(i, RGBcolor);
217 } else {
218 pixels->setPixelColor(i, 0);
219 }
220 }
221 pixels->show();
222 }
223
224 void iterate_RainbowWheel() {
225 for (int i = 0; i < lightedLeds; i++) {
226 if (counter > 255) {
227 counter = 0;
228 }
229 pixels->setPixelColor(i, RainbowWheel((i*1+counter) & 255));
230 }
231 pixels->show();
232 }
233
234 void iterate_Rainbow() {
235 for (int i = 0; i < lightedLeds; i++) {
236 if (counter*256 > 5*65536) {
237 counter = 0;
238 }
239 int pixelHue = 256*counter + (i * 65536L / numberOfLeds);
240 pixels->setPixelColor(i, pixels->gamma32(pixels->ColorHSV(pixelHue)));
241 }
242 pixels->show();
243 }
244
245 uint32_t RainbowWheel(byte WheelPos) {
246 if (WheelPos < 85) {
247 return pixels->Color(WheelPos * 3, 255 - WheelPos * 3, 0);
248 } else if (WheelPos < 170) {
249 WheelPos -= 85;
250 return pixels->Color(255 - WheelPos * 3, 0, WheelPos * 3);
251 } else {
252 WheelPos -= 170;
253 return pixels->Color(0, WheelPos * 3, 255 - WheelPos * 3);
254 }
255 }
256};
257
258} // namespace Control
259} // namespace Supla
260
261#endif // SRC_SUPLA_CONTROL_ADDRESSABLE_LEDS_H_
void iterateAlways()
Method called on each SuplaDevice iteration.
Definition addressable_leds.h:132
void onInit() override
Third method called on element in SuplaDevice.begin()
Definition addressable_leds.h:98
AddressableLEDs(uint16_t number, int16_t pin, neoPixelType type=NEO_GRB+NEO_KHZ800)
Constructor.
Definition addressable_leds.h:59
void setEffect(AddressableLEDsEffect neweffect, uint16_t newStepTime, uint8_t turnAllLEDsTime=0)
Setting effect on strip/ring.
Definition addressable_leds.h:78
Base class for all elements of SuplaDevice.
Definition element.h:37