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