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