FastLED 3.9.13
Loading...
Searching...
No Matches
___pixeltypes.h
1#include <stdint.h>
2#include <string.h>
3
4#ifdef USE_FASTLED
5#include "FastLED.h"
6#endif
7// #include "Arduino.h"
8
9#define _OUT_OF_BOUND -12
10
11namespace fl {
12
13#ifdef COLOR_RGBW
14
15struct Pixel {
16 union {
17 uint8_t raw[4];
18 struct {
19 uint8_t red;
20 uint8_t green;
21 uint8_t blue;
22 uint8_t white;
23 };
24 };
25
26 inline Pixel(uint8_t r, uint8_t g, uint8_t b, uint8_t w)
27 __attribute__((always_inline))
28 : red(r), green(g), blue(b), white(w) {
29 // brigthness =0xE0 |(br&31);
30 }
31
32 inline Pixel(uint8_t r, uint8_t g, uint8_t b) __attribute__((always_inline))
33 : red(r), green(g), blue(b) {
34 white = MIN(red, green);
35 white = MIN(white, blue);
36 red = red - white;
37 green = green - white;
38 blue = blue - white;
39 }
40
41 inline Pixel() __attribute__((always_inline)) {}
42
43#ifdef USE_FASTLED
44 inline Pixel &operator=(const CRGB &rhs) __attribute__((always_inline)) {
45
46 red = rhs.r;
47 green = rhs.g;
48 blue = rhs.b;
49 white = MIN(red, green);
50 white = MIN(white, blue);
51 red = red - white;
52 green = green - white;
53 blue = blue - white;
54 return *this;
55 }
56#endif
57
58 inline Pixel(const Pixel &rhs) __attribute__((always_inline)) {
59 // brigthness=rhs.brigthness;
60 red = rhs.red;
61 green = rhs.green;
62 blue = rhs.blue;
63 white = rhs.white;
64 }
65 inline Pixel &operator=(const uint32_t colorcode)
66 __attribute__((always_inline)) {
67 // rgb colorg;
68 red = (colorcode >> 24) & 0xFF;
69 green = (colorcode >> 16) & 0xFF;
70 blue = (colorcode >> 8) & 0xFF;
71 white = colorcode & 0xFF;
72 return *this;
73 }
74};
75#else
76
77struct Pixel {
78 union {
79 uint8_t raw[3];
80 struct {
81 uint8_t red;
82 uint8_t green;
83 uint8_t blue;
84 };
85 };
86
87 inline Pixel(uint8_t r, uint8_t g, uint8_t b) __attribute__((always_inline))
88 : red(r), green(g), blue(b) {
89 // brigthness =0xE0 |(br&31);
90 }
91
92 inline Pixel() __attribute__((always_inline)) {}
93
94#ifdef USE_FASTLED
95 inline Pixel &operator=(const CRGB &rhs) __attribute__((always_inline)) {
96 red = rhs.r;
97 green = rhs.g;
98 blue = rhs.b;
99 return *this;
100 }
101 inline Pixel &operator=(CRGB &rhs) __attribute__((always_inline)) {
102 red = rhs.r;
103 green = rhs.g;
104 blue = rhs.b;
105 return *this;
106 }
107 inline Pixel(const CRGB &rhs) __attribute__((always_inline)) {
108 red = rhs.r;
109 green = rhs.g;
110 blue = rhs.b;
111 }
112#endif
113
114 inline Pixel(const Pixel &rhs) __attribute__((always_inline)) {
115 // brigthness=rhs.brigthness;
116 red = rhs.red;
117 green = rhs.green;
118 blue = rhs.blue;
119 }
120 inline Pixel &operator=(const uint32_t colorcode)
121 __attribute__((always_inline)) {
122 // rgb colorg;
123 red = (colorcode >> 16) & 0xFF;
124 green = (colorcode >> 8) & 0xFF;
125 blue = (colorcode >> 0) & 0xFF;
126 return *this;
127 }
128 inline __attribute__((always_inline)) bool operator==(const Pixel &rhs) {
129 return (red == rhs.red) && (green == rhs.green) && (blue == rhs.blue);
130 }
131 inline __attribute__((always_inline)) bool operator!=(const Pixel &rhs) {
132 return !((red == rhs.red) && (green == rhs.green) &&
133 (blue == rhs.blue));
134 }
135};
136
137#endif
138
139enum class leddirection { FORWARD, BACKWARD, MAP };
140
141class Pixels {
142 public:
143 inline Pixels() __attribute__((always_inline)) {}
144 inline Pixels(const Pixels &rhs) __attribute__((always_inline)) {
145 _size = rhs._size;
146 _direction = rhs._direction;
147 _num_strips = rhs._num_strips;
148 for (int i = 0; i < _num_strips; i++) {
149 _sizes[i] = rhs._sizes[i];
150 }
151 ledpointer = rhs.ledpointer;
152 mapFunction = rhs.mapFunction;
153
154 // parent=rhs.parent;
155 }
156 Pixels(int size, Pixel *ledpoi) {
157 Pixels(size, ledpoi, leddirection::FORWARD);
158 }
159
160 Pixels(int size, Pixel *ledpoi, leddirection direction) {
161 __Pixels(size, ledpoi, direction, this);
162 }
163
164 void __Pixels(int size, Pixel *ledpoi, leddirection direction,
165 Pixels *pib) {
166 pib->_size = size;
167 pib->ledpointer = ledpoi;
168 pib->_num_strips = 0;
169 pib->_direction = direction;
170 // pib->nb_child=0;
171 }
172
173 Pixels(int num_led_per_strip, int num_strips) {
174 int sizes[16];
175 for (int i = 0; i < num_strips; i++) {
176 sizes[i] = num_led_per_strip;
177 }
178 __Pixels(sizes, num_strips, leddirection::FORWARD, this);
179 }
180
181 Pixels(int *sizes, int num_strips) {
182 __Pixels(sizes, num_strips, leddirection::FORWARD, this);
183 }
184
185 Pixels(int *sizes, int num_strips, leddirection direction) {
186 __Pixels(sizes, num_strips, direction, this);
187 }
188 void __Pixels(int *sizes, int num_strips, leddirection direction,
189 Pixels *pib) {
190 int size = 0;
191 for (int i = 0; i < num_strips; i++) {
192 size += sizes[i];
193 pib->_sizes[i] = sizes[i];
194 }
195
196 pib->_num_strips = num_strips;
197
198 ledpointer = (Pixel *)calloc(size, sizeof(Pixel));
199 if (ledpointer == NULL) {
200 pib->_size = 0;
201 } else {
202 pib->_size = size;
203 }
204 pib->_direction = direction;
205 }
206 Pixel &operator[](int i) {
207 switch (_direction) {
208
209 case (leddirection::FORWARD):
210
211 return *(ledpointer + i % _size);
212 break;
213
214 case (leddirection::BACKWARD):
215
216 return *(ledpointer + (_size - i % (_size)-1));
217 break;
218
219 case (leddirection::MAP):
220 if (mapFunction) {
221 int offset = mapFunction(i, arguments);
222 // printf("%d %d\n",i,offset);
223 if (offset == _OUT_OF_BOUND) {
224 return offPixel;
225 } else
226 return *(ledpointer + (mapFunction(i, arguments) % _size));
227 }
228
229 else
230 return *(ledpointer);
231 break;
232 default:
233 return *(ledpointer);
234 break;
235 }
236 }
237
238 void copy(Pixels ori) { copy(ori, leddirection::FORWARD); }
239
240 void copy(Pixels ori, leddirection dir) {
241 leddirection ledd = _direction;
242 if (_direction == leddirection::MAP)
243 ledd = leddirection::FORWARD;
244 for (int i = 0; i < ori._size; i++) {
245 if (ledd == dir) {
246 (*this)[i] = ori[i];
247 } else {
248 (*this)[i] = ori[ori._size - i % (ori._size) - 1];
249 }
250 }
251 }
252
253 Pixels getStrip(int num_strip, leddirection direction) {
254 if (_num_strips == 0 or _num_strips < num_strip) {
255
256 int d[0];
257 return Pixels(d, 1, direction);
258 } else {
259 uint32_t off = 0;
260 for (int i = 0; i < num_strip % _num_strips; i++) {
261 off += _sizes[i];
262 }
263
264 return Pixels(_sizes[num_strip], ledpointer + off, direction);
265 }
266 }
267
268 Pixels getStrip(int num_strip) {
269 return getStrip(num_strip, leddirection::FORWARD);
270 }
271
272 int *getLengths() { return _sizes; }
273
274 int getNumStrip() { return _num_strips; }
275 uint8_t *getPixels() { return (uint8_t *)ledpointer; }
276 void clear() {
277 // memset(ledpointer,0,_size*sizeof(Pixel));
278 }
279
280 Pixels createSubset(int start, int length) {
281 return createSubset(start, length, leddirection::FORWARD);
282 }
283
284 Pixels createSubset(int start, leddirection direction) {
285 if (start < 0)
286 start = 0;
287 return Pixels(_size, ledpointer + start, direction);
288 }
289
290 Pixels createSubset(int start, int length, leddirection direction) {
291 if (start < 0)
292 start = 0;
293 if (length <= 0)
294 length = 1;
295 return Pixels(length, ledpointer + start, direction);
296 }
297 /*
298 Pixels getParent()
299 {
300 return *parent;
301 }
302
303 Pixels * getChild(int i)
304 {
305
306 return children[i%nb_child];
307 }
308 */
309 inline void setMapFunction(int (*fptr)(int i, void *args), void *args,
310 int size) {
311 mapFunction = fptr;
312 if (arguments == NULL)
313 arguments = (void *)malloc(sizeof(size));
314 memcpy(arguments, args, size);
315 }
316
317 private:
318 Pixel *ledpointer;
319 size_t _size = 0;
320 int _sizes[16];
321 int _num_strips = 0;
322 leddirection _direction;
323 // int nb_child;
324 // Pixels *parent;
325 void *arguments;
326 // Pixels **children;
327 int (*mapFunction)(int i, void *args);
328 /*
329 * this is the pixel to retuen when out of bound
330 */
331 Pixel offPixel;
332};
333
334} // namespace fl
central include file for FastLED, defines the CFastLED class/object
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
uint8_t r
Red channel value.
Definition crgb.h:58
uint8_t g
Green channel value.
Definition crgb.h:62
uint8_t red
Red channel value.
Definition crgb.h:59
uint8_t blue
Blue channel value.
Definition crgb.h:67
uint8_t b
Blue channel value.
Definition crgb.h:66
uint8_t green
Green channel value.
Definition crgb.h:63