FastLED 3.9.13
Loading...
Searching...
No Matches
FastLED.cpp
Go to the documentation of this file.
1#define FASTLED_INTERNAL
2#include "FastLED.h"
3#include "fl/singleton.h"
4#include "fl/engine_events.h"
5
8
9#ifndef MAX_CLED_CONTROLLERS
10#ifdef __AVR__
11// if mega or leonardo, allow more controllers
12#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega32U4__)
13#define MAX_CLED_CONTROLLERS 16
14#else
15#define MAX_CLED_CONTROLLERS 8
16#endif
17#else
18#define MAX_CLED_CONTROLLERS 64
19#endif // __AVR__
20#endif // MAX_CLED_CONTROLLERS
21
22#if defined(__SAM3X8E__)
23volatile uint32_t fuckit;
24#endif
25
26#ifndef FASTLED_DEFINE_WEAK_YEILD_FUNCTION
27#if defined(__AVR_ATtiny13__)
28// Arduino.h also defines this as a weak function on this platform.
29#define FASTLED_DEFINE_WEAK_YEILD_FUNCTION 0
30#else
31#define FASTLED_DEFINE_WEAK_YEILD_FUNCTION 1
32#endif
33#endif
34
39extern "C" __attribute__((weak)) int atexit(void (* /*func*/ )()) { return 0; }
40
41#if FASTLED_DEFINE_WEAK_YEILD_FUNCTION
42extern "C" __attribute__((weak)) void yield(void) { }
43#endif
44
46
47uint16_t cled_contoller_size() {
48 return sizeof(CLEDController);
49}
50
51uint8_t get_brightness();
52
55void *pSmartMatrix = NULL;
56
58
61static uint32_t lastshow = 0;
62
65uint32_t _frame_cnt=0;
66
69uint32_t _retry_cnt=0;
70
71// uint32_t CRGB::Squant = ((uint32_t)((__TIME__[4]-'0') * 28))<<16 | ((__TIME__[6]-'0')*50)<<8 | ((__TIME__[7]-'0')*28);
72
73CFastLED::CFastLED() {
74 // clear out the array of led controllers
75 // m_nControllers = 0;
76 m_Scale = 255;
77 m_nFPS = 0;
78 m_pPowerFunc = NULL;
79 m_nPowerData = 0xFFFFFFFF;
80 m_nMinMicros = 0;
81}
82
84 return (*this)[0].size();
85}
86
88 return (*this)[0].leds();
89}
90
92 struct CRGB *data,
93 int nLedsOrOffset, int nLedsIfOffset) {
94 int nOffset = (nLedsIfOffset > 0) ? nLedsOrOffset : 0;
95 int nLeds = (nLedsIfOffset > 0) ? nLedsIfOffset : nLedsOrOffset;
96
97 pLed->init();
98 pLed->setLeds(data + nOffset, nLeds);
100 fl::EngineEvents::onStripAdded(pLed, nLedsOrOffset - nOffset);
101 return *pLed;
102}
103
104static void* gControllersData[MAX_CLED_CONTROLLERS];
105
106void CFastLED::show(uint8_t scale) {
107 fl::EngineEvents::onBeginFrame();
108 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
109 lastshow = micros();
110
111 // If we have a function for computing power, use it!
112 if(m_pPowerFunc) {
113 scale = (*m_pPowerFunc)(scale, m_nPowerData);
114 }
115
116 // static uninitialized gControllersData produces the smallest binary on attiny85.
117 int length = 0;
119
120 while(pCur && length < MAX_CLED_CONTROLLERS) {
121 if (pCur->getEnabled()) {
122 gControllersData[length] = pCur->beginShowLeds(pCur->size());
123 } else {
124 gControllersData[length] = nullptr;
125 }
126 length++;
127 if (m_nFPS < 100) { pCur->setDither(0); }
128 pCur = pCur->next();
129 }
130
131 pCur = CLEDController::head();
132 for (length = 0; length < MAX_CLED_CONTROLLERS && pCur; length++) {
133 if (pCur->getEnabled()) {
134 pCur->showLedsInternal(scale);
135 }
136 pCur = pCur->next();
137
138 }
139
140 length = 0; // Reset length to 0 and iterate again.
141 pCur = CLEDController::head();
142 while(pCur && length < MAX_CLED_CONTROLLERS) {
143 if (pCur->getEnabled()) {
144 pCur->endShowLeds(gControllersData[length]);
145 }
146 length++;
147 pCur = pCur->next();
148 }
149 countFPS();
150 fl::EngineEvents::onEndShowLeds();
151 fl::EngineEvents::onEndFrame();
152}
153
155 int x = 0;
157 while( pCur) {
158 ++x;
159 pCur = pCur->next();
160 }
161 return x;
162}
163
166 while(x-- && pCur) {
167 pCur = pCur->next();
168 }
169 if(pCur == NULL) {
170 return *(CLEDController::head());
171 } else {
172 return *pCur;
173 }
174}
175
176void CFastLED::showColor(const struct CRGB & color, uint8_t scale) {
177 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
178 lastshow = micros();
179
180 // If we have a function for computing power, use it!
181 if(m_pPowerFunc) {
182 scale = (*m_pPowerFunc)(scale, m_nPowerData);
183 }
184
185 int length = 0;
187 while(pCur && length < MAX_CLED_CONTROLLERS) {
188 if (pCur->getEnabled()) {
189 gControllersData[length] = pCur->beginShowLeds(pCur->size());
190 } else {
191 gControllersData[length] = nullptr;
192 }
193 length++;
194 pCur = pCur->next();
195 }
196
197 pCur = CLEDController::head();
198 while(pCur && length < MAX_CLED_CONTROLLERS) {
199 if(m_nFPS < 100) { pCur->setDither(0); }
200 if (pCur->getEnabled()) {
201 pCur->showColorInternal(color, scale);
202 }
203 pCur = pCur->next();
204 }
205
206 pCur = CLEDController::head();
207 length = 0; // Reset length to 0 and iterate again.
208 while(pCur && length < MAX_CLED_CONTROLLERS) {
209 if (pCur->getEnabled()) {
210 pCur->endShowLeds(gControllersData[length]);
211 }
212 length++;
213 pCur = pCur->next();
214 }
215 countFPS();
216}
217
218void CFastLED::clear(bool writeData) {
219 if(writeData) {
220 showColor(CRGB(0,0,0), 0);
221 }
222 clearData();
223}
224
227 while(pCur) {
228 pCur->clearLedDataInternal();
229 pCur = pCur->next();
230 }
231}
232
233void CFastLED::delay(unsigned long ms) {
234 unsigned long start = millis();
235 do {
236#ifndef FASTLED_ACCURATE_CLOCK
237 // make sure to allow at least one ms to pass to ensure the clock moves
238 // forward
239 ::delay(1);
240#endif
241 show();
242 yield();
243 }
244 while((millis()-start) < ms);
245}
246
247void CFastLED::setTemperature(const struct CRGB & temp) {
249 while(pCur) {
250 pCur->setTemperature(temp);
251 pCur = pCur->next();
252 }
253}
254
255void CFastLED::setCorrection(const struct CRGB & correction) {
257 while(pCur) {
258 pCur->setCorrection(correction);
259 pCur = pCur->next();
260 }
261}
262
263void CFastLED::setDither(uint8_t ditherMode) {
265 while(pCur) {
266 pCur->setDither(ditherMode);
267 pCur = pCur->next();
268 }
269}
270
271//
272// template<int m, int n> void transpose8(unsigned char A[8], unsigned char B[8]) {
273// uint32_t x, y, t;
274//
275// // Load the array and pack it into x and y.
276// y = *(unsigned int*)(A);
277// x = *(unsigned int*)(A+4);
278//
279// // x = (A[0]<<24) | (A[m]<<16) | (A[2*m]<<8) | A[3*m];
280// // y = (A[4*m]<<24) | (A[5*m]<<16) | (A[6*m]<<8) | A[7*m];
281//
282 // // pre-transform x
283 // t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
284 // t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
285 //
286 // // pre-transform y
287 // t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
288 // t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
289 //
290 // // final transform
291 // t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
292 // y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
293 // x = t;
294//
295// B[7*n] = y; y >>= 8;
296// B[6*n] = y; y >>= 8;
297// B[5*n] = y; y >>= 8;
298// B[4*n] = y;
299//
300// B[3*n] = x; x >>= 8;
301// B[2*n] = x; x >>= 8;
302// B[n] = x; x >>= 8;
303// B[0] = x;
304// // B[0]=x>>24; B[n]=x>>16; B[2*n]=x>>8; B[3*n]=x>>0;
305// // B[4*n]=y>>24; B[5*n]=y>>16; B[6*n]=y>>8; B[7*n]=y>>0;
306// }
307//
308// void transposeLines(Lines & out, Lines & in) {
309// transpose8<1,2>(in.bytes, out.bytes);
310// transpose8<1,2>(in.bytes + 8, out.bytes + 1);
311// }
312
313
316extern int noise_min;
317
320extern int noise_max;
321
322void CFastLED::countFPS(int nFrames) {
323 static int br = 0;
324 static uint32_t lastframe = 0; // millis();
325
326 if(br++ >= nFrames) {
327 uint32_t now = millis();
328 now -= lastframe;
329 if(now == 0) {
330 now = 1; // prevent division by zero below
331 }
332 m_nFPS = (br * 1000) / now;
333 br = 0;
334 lastframe = millis();
335 }
336}
337
338void CFastLED::setMaxRefreshRate(uint16_t refresh, bool constrain) {
339 if(constrain) {
340 // if we're constraining, the new value of m_nMinMicros _must_ be higher than previously (because we're only
341 // allowed to slow things down if constraining)
342 if(refresh > 0) {
343 m_nMinMicros = ((1000000 / refresh) > m_nMinMicros) ? (1000000 / refresh) : m_nMinMicros;
344 }
345 } else if(refresh > 0) {
346 m_nMinMicros = 1000000 / refresh;
347 } else {
348 m_nMinMicros = 0;
349 }
350}
351
352
353uint8_t get_brightness() {
354 return FastLED.getBrightness();
355}
356
357
358
359#ifdef NEED_CXX_BITS
360namespace __cxxabiv1
361{
362 #if !defined(ESP8266) && !defined(ESP32)
363 extern "C" void __cxa_pure_virtual (void) {}
364 #endif
365
366 /* guard variables */
367
368 /* The ABI requires a 64-bit type. */
369 __extension__ typedef int __guard __attribute__((mode(__DI__)));
370
371 extern "C" int __cxa_guard_acquire (__guard *) __attribute__((weak));
372 extern "C" void __cxa_guard_release (__guard *) __attribute__((weak));
373 extern "C" void __cxa_guard_abort (__guard *) __attribute__((weak));
374
375 extern "C" int __cxa_guard_acquire (__guard *g)
376 {
377 return !*(char *)(g);
378 }
379
380 extern "C" void __cxa_guard_release (__guard *g)
381 {
382 *(char *)g = 1;
383 }
384
385 extern "C" void __cxa_guard_abort (__guard *)
386 {
387
388 }
389}
390#endif
391
uint32_t _retry_cnt
Global frame retry counter, used for debugging ESP implementations.
Definition FastLED.cpp:69
void * pSmartMatrix
Pointer to the matrix object when using the Smart Matrix Library.
Definition FastLED.cpp:55
int atexit(void(*)())
Has to be declared outside of any namespaces.
Definition FastLED.cpp:39
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:57
int noise_max
Unused value.
uint32_t _frame_cnt
Global frame counter, used for debugging ESP implementations.
Definition FastLED.cpp:65
int noise_min
Unused value.
central include file for FastLED, defines the CFastLED class/object
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:57
High level controller interface for FastLED.
Definition FastLED.h:353
int size()
Get the number of leds in the first controller.
Definition FastLED.cpp:83
void setMaxRefreshRate(uint16_t refresh, bool constrain=false)
Set the maximum refresh rate.
Definition FastLED.cpp:338
CRGB * leds()
Get a pointer to led data for the first controller.
Definition FastLED.cpp:87
void setTemperature(const struct CRGB &temp)
Set a global color temperature.
Definition FastLED.cpp:247
void show()
Update all our controllers with the current led colors.
Definition FastLED.h:745
void countFPS(int nFrames=25)
For debugging, this will keep track of time between calls to countFPS().
Definition FastLED.cpp:322
CLEDController & operator[](int x)
Get a reference to a registered controller.
Definition FastLED.cpp:164
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:233
void showColor(const struct CRGB &color, uint8_t scale)
Set all leds on all controllers to the given color/scale.
Definition FastLED.cpp:176
void setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode.
Definition FastLED.cpp:263
uint8_t getBrightness()
Get the current global brightness setting.
Definition FastLED.h:729
void clearData()
Clear out the local data array.
Definition FastLED.cpp:225
void setCorrection(const struct CRGB &correction)
Set a global color correction.
Definition FastLED.cpp:255
int count()
Get how many controllers have been registered.
Definition FastLED.cpp:154
void clear(bool writeData=false)
Clear the leds, wiping the local array of data.
Definition FastLED.cpp:218
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:91
Base definition for an LED controller.
CLEDController * next()
Get the next controller in the linked list after this one.
CLEDController & setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode for this controller to use.
virtual uint16_t getMaxRefreshRate() const
Gets the maximum possible refresh rate of the strip.
virtual int size()
How many LEDs does this controller manage?
CLEDController & setLeds(CRGB *data, int nLeds)
Set the default array of LEDs to be used by this controller.
CLEDController & setCorrection(CRGB correction)
The color corrction to use for this controller, expressed as a CRGB object.
static CLEDController * head()
Get the first LED controller in the linked list of controllers.
CLEDController & setTemperature(CRGB temperature)
Set the color temperature, aka white point, for this controller.
static CLEDController * m_pTail
pointer to the last LED controller in the linked list
void showColorInternal(const struct CRGB &data, int nLeds, uint8_t brightness)
void showLedsInternal(uint8_t brightness)
Write the data to the LEDs managed by this controller.
void clearLedDataInternal(int nLeds=-1)
Zero out the LED data managed by this controller.
virtual void init()=0
Initialize the LED controller.
static CLEDController * m_pHead
pointer to the first LED controller in the linked list
#define FASTLED_NAMESPACE_END
End of the FastLED namespace.
Definition namespace.h:16
#define FASTLED_NAMESPACE_BEGIN
Start of the FastLED namespace.
Definition namespace.h:14
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54