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