FastLED 3.6.0
Loading...
Searching...
No Matches
pixelset.h
Go to the documentation of this file.
1#ifndef __INC_PIXELSET_H
2#define __INC_PIXELSET_H
3
4#include "FastLED.h"
5
6#ifndef abs
7#include <stdlib.h>
8#endif
9
12
13
17
22template<class PIXEL_TYPE>
24public:
25 const int8_t dir;
26 const int len;
28 PIXEL_TYPE * const leds;
29 PIXEL_TYPE * const end_pos;
30
31public:
33 inline CPixelView(const CPixelView & other) : dir(other.dir), len(other.len), leds(other.leds), end_pos(other.end_pos) {}
34
39 inline CPixelView(PIXEL_TYPE *_leds, int _len) : dir(_len < 0 ? -1 : 1), len(_len), leds(_leds), end_pos(_leds + _len) {}
40
46 inline CPixelView(PIXEL_TYPE *_leds, int _start, int _end) : dir(((_end-_start)<0) ? -1 : 1), len((_end - _start) + dir), leds(_leds + _start), end_pos(_leds + _start + len) {}
47
50 int size() { return abs(len); }
51
54 bool reversed() { return len < 0; }
55
57 bool operator==(const CPixelView & rhs) const { return leds == rhs.leds && len == rhs.len && dir == rhs.dir; }
58
60 bool operator!=(const CPixelView & rhs) const { return leds != rhs.leds || len != rhs.len || dir != rhs.dir; }
61
63 inline PIXEL_TYPE & operator[](int x) const { if(dir & 0x80) { return leds[-x]; } else { return leds[x]; } }
64
70 inline CPixelView operator()(int start, int end) { return CPixelView(leds, start, end); }
71
72 // Access an inclusive subset of the LEDs in this set, starting from the first.
73 // @param end the last element for the new subset
74 // @todo Not sure i want this? inline CPixelView operator()(int end) { return CPixelView(leds, 0, end); }
75
77 inline CPixelView operator-() { return CPixelView(leds, len - dir, 0); }
78
80 inline operator PIXEL_TYPE* () const { return leds; }
81
84 inline CPixelView & operator=(const PIXEL_TYPE & color) {
85 for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) = color; }
86 return *this;
87 }
88
91 void dump() const {
98 }
99
103 inline CPixelView & operator=(const CPixelView & rhs) {
104 for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) {
105 (*pixel) = (*rhspixel);
106 }
107 return *this;
108 }
109
112
114 inline CPixelView & addToRGB(uint8_t inc) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) += inc; } return *this; }
116 inline CPixelView & operator+=(CPixelView & rhs) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { (*pixel) += (*rhspixel); } return *this; }
117
119 inline CPixelView & subFromRGB(uint8_t inc) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) -= inc; } return *this; }
121 inline CPixelView & operator-=(CPixelView & rhs) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { (*pixel) -= (*rhspixel); } return *this; }
122
124 inline CPixelView & operator++() { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel)++; } return *this; }
126 inline CPixelView & operator++(int DUMMY_ARG) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel)++; } return *this; }
127
129 inline CPixelView & operator--() { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel)--; } return *this; }
131 inline CPixelView & operator--(int DUMMY_ARG) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel)--; } return *this; }
132
134 inline CPixelView & operator/=(uint8_t d) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) /= d; } return *this; }
136 inline CPixelView & operator>>=(uint8_t d) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) >>= d; } return *this; }
138 inline CPixelView & operator*=(uint8_t d) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) *= d; } return *this; }
139
141 inline CPixelView & nscale8_video(uint8_t scaledown) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel).nscale8_video(scaledown); } return *this;}
143 inline CPixelView & operator%=(uint8_t scaledown) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel).nscale8_video(scaledown); } return *this; }
145 inline CPixelView & fadeLightBy(uint8_t fadefactor) { return nscale8_video(255 - fadefactor); }
146
148 inline CPixelView & nscale8(uint8_t scaledown) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel).nscale8(scaledown); } return *this; }
150 inline CPixelView & nscale8(PIXEL_TYPE & scaledown) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel).nscale8(scaledown); } return *this; }
152 inline CPixelView & nscale8(CPixelView & rhs) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { (*pixel).nscale8((*rhspixel)); } return *this; }
153
155 inline CPixelView & fadeToBlackBy(uint8_t fade) { return nscale8(255 - fade); }
156
160 inline CPixelView & operator|=(const PIXEL_TYPE & rhs) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) |= rhs; } return *this; }
163 inline CPixelView & operator|=(const CPixelView & rhs) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { (*pixel) |= (*rhspixel); } return *this; }
166 inline CPixelView & operator|=(uint8_t d) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) |= d; } return *this; }
167
171 inline CPixelView & operator&=(const PIXEL_TYPE & rhs) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) &= rhs; } return *this; }
174 inline CPixelView & operator&=(const CPixelView & rhs) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { (*pixel) &= (*rhspixel); } return *this; }
177 inline CPixelView & operator&=(uint8_t d) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { (*pixel) &= d; } return *this; }
178
180
181
183 inline operator bool() { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { if((*pixel)) return true; } return false; }
184
185
188
191 inline CPixelView & fill_solid(const PIXEL_TYPE & color) { *this = color; return *this; }
193 inline CPixelView & fill_solid(const CHSV & color) { if(dir>0) { *this = color; return *this; } }
194
199 inline CPixelView & fill_rainbow(uint8_t initialhue, uint8_t deltahue=5) {
200 if(dir >= 0) {
201 ::fill_rainbow(leds,len,initialhue,deltahue);
202 } else {
203 ::fill_rainbow(leds+len+1,-len,initialhue,deltahue);
204 }
205 return *this;
206 }
207
213 inline CPixelView & fill_gradient(const CHSV & startcolor, const CHSV & endcolor, TGradientDirectionCode directionCode = SHORTEST_HUES) {
214 if(dir >= 0) {
215 ::fill_gradient(leds,len,startcolor, endcolor, directionCode);
216 } else {
217 ::fill_gradient(leds + len + 1, (-len), endcolor, startcolor, directionCode);
218 }
219 return *this;
220 }
221
228 inline CPixelView & fill_gradient(const CHSV & c1, const CHSV & c2, const CHSV & c3, TGradientDirectionCode directionCode = SHORTEST_HUES) {
229 if(dir >= 0) {
230 ::fill_gradient(leds, len, c1, c2, c3, directionCode);
231 } else {
232 ::fill_gradient(leds + len + 1, -len, c3, c2, c1, directionCode);
233 }
234 return *this;
235 }
236
244 inline CPixelView & fill_gradient(const CHSV & c1, const CHSV & c2, const CHSV & c3, const CHSV & c4, TGradientDirectionCode directionCode = SHORTEST_HUES) {
245 if(dir >= 0) {
246 ::fill_gradient(leds, len, c1, c2, c3, c4, directionCode);
247 } else {
248 ::fill_gradient(leds + len + 1, -len, c4, c3, c2, c1, directionCode);
249 }
250 return *this;
251 }
252
258 inline CPixelView & fill_gradient_RGB(const PIXEL_TYPE & startcolor, const PIXEL_TYPE & endcolor, TGradientDirectionCode directionCode = SHORTEST_HUES) {
259 if(dir >= 0) {
260 ::fill_gradient_RGB(leds,len,startcolor, endcolor);
261 } else {
262 ::fill_gradient_RGB(leds + len + 1, (-len), endcolor, startcolor);
263 }
264 return *this;
265 }
266
272 inline CPixelView & fill_gradient_RGB(const PIXEL_TYPE & c1, const PIXEL_TYPE & c2, const PIXEL_TYPE & c3) {
273 if(dir >= 0) {
274 ::fill_gradient_RGB(leds, len, c1, c2, c3);
275 } else {
276 ::fill_gradient_RGB(leds + len + 1, -len, c3, c2, c1);
277 }
278 return *this;
279 }
280
287 inline CPixelView & fill_gradient_RGB(const PIXEL_TYPE & c1, const PIXEL_TYPE & c2, const PIXEL_TYPE & c3, const PIXEL_TYPE & c4) {
288 if(dir >= 0) {
289 ::fill_gradient_RGB(leds, len, c1, c2, c3, c4);
290 } else {
291 ::fill_gradient_RGB(leds + len + 1, -len, c4, c3, c2, c1);
292 }
293 return *this;
294 }
295
300 inline CPixelView & nblend(const PIXEL_TYPE & overlay, fract8 amountOfOverlay) { for(iterator pixel = begin(), _end = end(); pixel != _end; ++pixel) { ::nblend((*pixel), overlay, amountOfOverlay); } return *this; }
301
306 inline CPixelView & nblend(const CPixelView & rhs, fract8 amountOfOverlay) { for(iterator pixel = begin(), rhspixel = rhs.begin(), _end = end(), rhs_end = rhs.end(); (pixel != _end) && (rhspixel != rhs_end); ++pixel, ++rhspixel) { ::nblend((*pixel), (*rhspixel), amountOfOverlay); } return *this; }
307
312 inline CPixelView & blur1d(fract8 blur_amount) {
313 if(dir >= 0) {
314 ::blur1d(leds, len, blur_amount);
315 } else {
316 ::blur1d(leds + len + 1, -len, blur_amount);
317 }
318 return *this;
319 }
320
324 inline CPixelView & napplyGamma_video(float gamma) {
325 if(dir >= 0) {
327 } else {
328 ::napplyGamma_video(leds + len + 1, -len, gamma);
329 }
330 return *this;
331 }
332
338 inline CPixelView & napplyGamma_video(float gammaR, float gammaG, float gammaB) {
339 if(dir >= 0) {
340 ::napplyGamma_video(leds, len, gammaR, gammaG, gammaB);
341 } else {
342 ::napplyGamma_video(leds + len + 1, -len, gammaR, gammaG, gammaB);
343 }
344 return *this;
345 }
346
348
349
352
356 template <class T>
358 T * leds;
359 const int8_t dir;
360
361 public:
363 __attribute__((always_inline)) inline pixelset_iterator_base(const pixelset_iterator_base & rhs) : leds(rhs.leds), dir(rhs.dir) {}
364
369 __attribute__((always_inline)) inline pixelset_iterator_base(T * _leds, const char _dir) : leds(_leds), dir(_dir) {}
370
371 __attribute__((always_inline)) inline pixelset_iterator_base& operator++() { leds += dir; return *this; }
372 __attribute__((always_inline)) inline pixelset_iterator_base operator++(int) { pixelset_iterator_base tmp(*this); leds += dir; return tmp; }
373
374 __attribute__((always_inline)) inline bool operator==(pixelset_iterator_base & other) const { return leds == other.leds; /* && set==other.set; */ }
375 __attribute__((always_inline)) inline bool operator!=(pixelset_iterator_base & other) const { return leds != other.leds; /* || set != other.set; */ }
376
377 __attribute__((always_inline)) inline PIXEL_TYPE& operator*() const { return *leds; }
378 };
379
382
383 iterator begin() { return iterator(leds, dir); }
384 iterator end() { return iterator(end_pos, dir); }
385
386 iterator begin() const { return iterator(leds, dir); }
387 iterator end() const { return iterator(end_pos, dir); }
388
391
393};
394
397
399__attribute__((always_inline))
400inline CRGB *operator+(const CRGBSet & pixels, int offset) { return (CRGB*)pixels + offset; }
401
402
405template<int SIZE>
406class CRGBArray : public CPixelView<CRGB> {
407 CRGB rawleds[SIZE];
408
409public:
410 CRGBArray() : CPixelView<CRGB>(rawleds, SIZE) {}
411 using CPixelView::operator=;
412};
413
415
416
417#endif
central include file for FastLED, defines the CFastLED class/object
Iterator helper class for CPixelView.
Definition pixelset.h:357
pixelset_iterator_base & operator++()
Increment LED pointer in data direction.
Definition pixelset.h:371
bool operator!=(pixelset_iterator_base &other) const
Check if iterator is not at the same position.
Definition pixelset.h:375
bool operator==(pixelset_iterator_base &other) const
Check if iterator is at the same position.
Definition pixelset.h:374
pixelset_iterator_base(T *_leds, const char _dir)
Base constructor.
Definition pixelset.h:369
PIXEL_TYPE & operator*() const
Dereference operator, to get underlying pointer to the LEDs.
Definition pixelset.h:377
pixelset_iterator_base(const pixelset_iterator_base &rhs)
Copy constructor.
Definition pixelset.h:363
pixelset_iterator_base operator++(int)
Increment LED pointer in data direction.
Definition pixelset.h:372
Represents a set of LED objects.
Definition pixelset.h:23
CPixelView(PIXEL_TYPE *_leds, int _len)
PixelSet constructor for a pixel set starting at the given PIXEL_TYPE* and going for _len leds.
Definition pixelset.h:39
CPixelView & nscale8(PIXEL_TYPE &scaledown)
Scale every LED by the given scale.
Definition pixelset.h:150
CPixelView & operator&=(const CPixelView &rhs)
Apply the PIXEL_TYPE &= operator to every pixel in this set with every pixel in the passed in set.
Definition pixelset.h:174
CPixelView & fill_gradient(const CHSV &c1, const CHSV &c2, const CHSV &c3, TGradientDirectionCode directionCode=SHORTEST_HUES)
Fill all of the LEDs with a smooth HSV gradient between three HSV colors.
Definition pixelset.h:228
CPixelView & operator-=(CPixelView &rhs)
Subtract every pixel in the other set from this set.
Definition pixelset.h:121
CPixelView & fill_gradient_RGB(const PIXEL_TYPE &c1, const PIXEL_TYPE &c2, const PIXEL_TYPE &c3, const PIXEL_TYPE &c4)
Fill all of the LEDs with a smooth RGB gradient between four RGB colors.
Definition pixelset.h:287
CPixelView & fill_rainbow(uint8_t initialhue, uint8_t deltahue=5)
Fill all of the LEDs with a rainbow of colors.
Definition pixelset.h:199
CPixelView & addToRGB(uint8_t inc)
Add the passed in value to all channels for all of the pixels in this set.
Definition pixelset.h:114
PIXEL_TYPE *const leds
pointer to the LED data
Definition pixelset.h:28
CPixelView & nscale8_video(uint8_t scaledown)
Scale every LED by the given scale.
Definition pixelset.h:141
CPixelView & operator--()
Decrement every pixel value in this set.
Definition pixelset.h:129
const_iterator cbegin() const
Makes a const iterator instance for the start of the LED set, const qualified.
Definition pixelset.h:389
const_iterator cend() const
Makes a const iterator instance for the end of the LED set, const qualified.
Definition pixelset.h:390
CPixelView & operator*=(uint8_t d)
Multiply every LED in this set by the given value.
Definition pixelset.h:138
CPixelView & operator|=(const PIXEL_TYPE &rhs)
Apply the PIXEL_TYPE |= operator to every pixel in this set with the given PIXEL_TYPE value.
Definition pixelset.h:160
bool reversed()
Whether or not this set goes backwards.
Definition pixelset.h:54
CPixelView & operator--(int DUMMY_ARG)
Decrement every pixel value in this set.
Definition pixelset.h:131
CPixelView & fill_solid(const CHSV &color)
Fill all of the LEDs with a solid color.
Definition pixelset.h:193
CPixelView & operator=(const CPixelView &rhs)
Copy the contents of the passed-in set to our set.
Definition pixelset.h:103
CPixelView & operator++(int DUMMY_ARG)
Increment every pixel value in this set.
Definition pixelset.h:126
iterator end()
Makes an iterator instance for the end of the LED set.
Definition pixelset.h:384
CPixelView & operator+=(CPixelView &rhs)
Add every pixel in the other set to this set.
Definition pixelset.h:116
CPixelView & operator=(const PIXEL_TYPE &color)
Assign the passed in color to all elements in this set.
Definition pixelset.h:84
const int8_t dir
direction of the LED data, either 1 or -1. Determines how the pointer is incremented.
Definition pixelset.h:25
CPixelView & operator++()
Increment every pixel value in this set.
Definition pixelset.h:124
CPixelView & fill_gradient_RGB(const PIXEL_TYPE &c1, const PIXEL_TYPE &c2, const PIXEL_TYPE &c3)
Fill all of the LEDs with a smooth RGB gradient between three RGB colors.
Definition pixelset.h:272
void dump() const
Print debug data to serial, disabled for release.
Definition pixelset.h:91
CPixelView & blur1d(fract8 blur_amount)
One-dimensional blur filter.
Definition pixelset.h:312
CPixelView & nblend(const PIXEL_TYPE &overlay, fract8 amountOfOverlay)
Destructively modifies all LEDs, blending in a given fraction of an overlay color.
Definition pixelset.h:300
CPixelView & operator&=(const PIXEL_TYPE &rhs)
Apply the PIXEL_TYPE &= operator to every pixel in this set with the given PIXEL_TYPE value.
Definition pixelset.h:171
CPixelView & fill_gradient(const CHSV &startcolor, const CHSV &endcolor, TGradientDirectionCode directionCode=SHORTEST_HUES)
Fill all of the LEDs with a smooth HSV gradient between two HSV colors.
Definition pixelset.h:213
CPixelView & operator%=(uint8_t scaledown)
Scale down every LED by the given scale.
Definition pixelset.h:143
pixelset_iterator_base< PIXEL_TYPE > iterator
Iterator helper type for this class.
Definition pixelset.h:380
CPixelView & operator/=(uint8_t d)
Divide every LED by the given value.
Definition pixelset.h:134
CPixelView & nscale8(uint8_t scaledown)
Scale every LED by the given scale.
Definition pixelset.h:148
CPixelView & fill_gradient_RGB(const PIXEL_TYPE &startcolor, const PIXEL_TYPE &endcolor, TGradientDirectionCode directionCode=SHORTEST_HUES)
Fill all of the LEDs with a smooth RGB gradient between two RGB colors.
Definition pixelset.h:258
CPixelView & operator>>=(uint8_t d)
Shift every LED in this set right by the given number of bits.
Definition pixelset.h:136
CPixelView operator()(int start, int end)
Access an inclusive subset of the LEDs in this set.
Definition pixelset.h:70
bool operator==(const CPixelView &rhs) const
Do these sets point to the same thing? Note that this is different from the contents of the set being...
Definition pixelset.h:57
CPixelView & napplyGamma_video(float gamma)
Destructively applies a gamma adjustment to all LEDs.
Definition pixelset.h:324
CPixelView & fadeToBlackBy(uint8_t fade)
Fade every LED down by the given scale.
Definition pixelset.h:155
CPixelView & fill_gradient(const CHSV &c1, const CHSV &c2, const CHSV &c3, const CHSV &c4, TGradientDirectionCode directionCode=SHORTEST_HUES)
Fill all of the LEDs with a smooth HSV gradient between four HSV colors.
Definition pixelset.h:244
CPixelView & operator|=(uint8_t d)
Apply the PIXEL_TYPE |= operator to every pixel in this set.
Definition pixelset.h:166
PIXEL_TYPE *const end_pos
pointer to the end position of the LED data
Definition pixelset.h:29
PIXEL_TYPE & operator[](int x) const
Access a single element in this set, just like an array operator.
Definition pixelset.h:63
CPixelView & subFromRGB(uint8_t inc)
Subtract the passed in value from all channels for all of the pixels in this set.
Definition pixelset.h:119
CPixelView & fadeLightBy(uint8_t fadefactor)
Fade every LED down by the given scale.
Definition pixelset.h:145
CPixelView & operator|=(const CPixelView &rhs)
Apply the PIXEL_TYPE |= operator to every pixel in this set with every pixel in the passed in set.
Definition pixelset.h:163
pixelset_iterator_base< const PIXEL_TYPE > const_iterator
Const iterator helper type for this class.
Definition pixelset.h:381
CPixelView & nscale8(CPixelView &rhs)
Scale every LED in this set by every led in the other set.
Definition pixelset.h:152
CPixelView operator-()
Return the reverse ordering of this set.
Definition pixelset.h:77
CPixelView & fill_solid(const PIXEL_TYPE &color)
Fill all of the LEDs with a solid color.
Definition pixelset.h:191
CPixelView(PIXEL_TYPE *_leds, int _start, int _end)
PixelSet constructor for the given set of LEDs, with start and end boundaries.
Definition pixelset.h:46
CPixelView & operator&=(uint8_t d)
Apply the PIXEL_TYPE &= operator to every pixel in this set with the passed in value.
Definition pixelset.h:177
bool operator!=(const CPixelView &rhs) const
Do these sets point to different things? Note that this is different from the contents of the set bei...
Definition pixelset.h:60
CPixelView & nblend(const CPixelView &rhs, fract8 amountOfOverlay)
Destructively blend another set of LEDs into this one.
Definition pixelset.h:306
iterator begin() const
Makes an iterator instance for the start of the LED set, const qualified.
Definition pixelset.h:386
iterator begin()
Makes an iterator instance for the start of the LED set.
Definition pixelset.h:383
iterator end() const
Makes an iterator instance for the end of the LED set, const qualified.
Definition pixelset.h:387
const int len
length of the LED data, in PIXEL_TYPE units.
Definition pixelset.h:26
CPixelView & napplyGamma_video(float gammaR, float gammaG, float gammaB)
Destructively applies a gamma adjustment to all LEDs.
Definition pixelset.h:338
CPixelView(const CPixelView &other)
PixelSet copy constructor.
Definition pixelset.h:33
int size()
Get the size of this set.
Definition pixelset.h:50
A version of CPixelView<CRGB> with an included array of CRGB LEDs.
Definition pixelset.h:406
TGradientDirectionCode
Hue direction for calculating fill gradients.
Definition colorutils.h:69
@ SHORTEST_HUES
Hue goes whichever way is shortest.
Definition colorutils.h:72
uint8_t fract8
ANSI: unsigned short _Fract.
Definition lib8tion.h:402
CPixelView< CRGB > CRGBSet
CPixelView for CRGB arrays.
Definition pixelset.h:396
Representation of an HSV pixel (hue, saturation, value (aka brightness)).
Definition pixeltypes.h:27
Representation of an RGB pixel (Red, Green, Blue)
Definition pixeltypes.h:120