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