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"
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
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
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);
100 FastLED.setMaxRefreshRate(pLed->getMaxRefreshRate(),true);
101 fl::EngineEvents::onStripAdded(pLed, nLedsOrOffset - nOffset);
102 return *pLed;
103}
104
105// This is bad code. But it produces the smallest binaries for reasons
106// beyond mortal comprehensions.
107// Instead of iterating through the link list for onBeginFrame(), beginShowLeds()
108// and endShowLeds(): store the pointers in an array and iterate through that.
109//
110// static uninitialized gControllersData produces the smallest binary on attiny85.
112
113void CFastLED::show(uint8_t scale) {
115 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
116 lastshow = micros();
117
118 // If we have a function for computing power, use it!
119 if(m_pPowerFunc) {
120 scale = (*m_pPowerFunc)(scale, m_nPowerData);
121 }
122
123
124 int length = 0;
126
127 while(pCur && length < MAX_CLED_CONTROLLERS) {
128 if (pCur->getEnabled()) {
129 gControllersData[length] = pCur->beginShowLeds(pCur->size());
130 } else {
131 gControllersData[length] = nullptr;
132 }
133 length++;
134 if (m_nFPS < 100) { pCur->setDither(0); }
135 pCur = pCur->next();
136 }
137
138 pCur = CLEDController::head();
139 for (length = 0; length < MAX_CLED_CONTROLLERS && pCur; length++) {
140 if (pCur->getEnabled()) {
141 pCur->showLedsInternal(scale);
142 }
143 pCur = pCur->next();
144
145 }
146
147 length = 0; // Reset length to 0 and iterate again.
148 pCur = CLEDController::head();
149 while(pCur && length < MAX_CLED_CONTROLLERS) {
150 if (pCur->getEnabled()) {
152 }
153 length++;
154 pCur = pCur->next();
155 }
156 countFPS();
157 onEndFrame();
159}
160
164
166 int x = 0;
168 while( pCur) {
169 ++x;
170 pCur = pCur->next();
171 }
172 return x;
173}
174
177 while(x-- && pCur) {
178 pCur = pCur->next();
179 }
180 if(pCur == NULL) {
181 return *(CLEDController::head());
182 } else {
183 return *pCur;
184 }
185}
186
187void CFastLED::showColor(const struct CRGB & color, uint8_t scale) {
188 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
189 lastshow = micros();
190
191 // If we have a function for computing power, use it!
192 if(m_pPowerFunc) {
193 scale = (*m_pPowerFunc)(scale, m_nPowerData);
194 }
195
196 int length = 0;
198 while(pCur && length < MAX_CLED_CONTROLLERS) {
199 if (pCur->getEnabled()) {
200 gControllersData[length] = pCur->beginShowLeds(pCur->size());
201 } else {
202 gControllersData[length] = nullptr;
203 }
204 length++;
205 pCur = pCur->next();
206 }
207
208 pCur = CLEDController::head();
209 while(pCur && length < MAX_CLED_CONTROLLERS) {
210 if(m_nFPS < 100) { pCur->setDither(0); }
211 if (pCur->getEnabled()) {
212 pCur->showColorInternal(color, scale);
213 }
214 pCur = pCur->next();
215 }
216
217 pCur = CLEDController::head();
218 length = 0; // Reset length to 0 and iterate again.
219 while(pCur && length < MAX_CLED_CONTROLLERS) {
220 if (pCur->getEnabled()) {
222 }
223 length++;
224 pCur = pCur->next();
225 }
226 countFPS();
227 onEndFrame();
228}
229
230void CFastLED::clear(bool writeData) {
231 if(writeData) {
232 showColor(CRGB(0,0,0), 0);
233 }
234 clearData();
235}
236
239 while(pCur) {
240 pCur->clearLedDataInternal();
241 pCur = pCur->next();
242 }
243}
244
245void CFastLED::delay(unsigned long ms) {
246 unsigned long start = millis();
247 do {
248#ifndef FASTLED_ACCURATE_CLOCK
249 // make sure to allow at least one ms to pass to ensure the clock moves
250 // forward
251 ::delay(1);
252#endif
253 show();
254 yield();
255 }
256 while((millis()-start) < ms);
257}
258
259void CFastLED::setTemperature(const struct CRGB & temp) {
261 while(pCur) {
262 pCur->setTemperature(temp);
263 pCur = pCur->next();
264 }
265}
266
267void CFastLED::setCorrection(const struct CRGB & correction) {
269 while(pCur) {
270 pCur->setCorrection(correction);
271 pCur = pCur->next();
272 }
273}
274
275void CFastLED::setDither(uint8_t ditherMode) {
277 while(pCur) {
278 pCur->setDither(ditherMode);
279 pCur = pCur->next();
280 }
281}
282
283//
284// template<int m, int n> void transpose8(unsigned char A[8], unsigned char B[8]) {
285// uint32_t x, y, t;
286//
287// // Load the array and pack it into x and y.
288// y = *(unsigned int*)(A);
289// x = *(unsigned int*)(A+4);
290//
291// // x = (A[0]<<24) | (A[m]<<16) | (A[2*m]<<8) | A[3*m];
292// // y = (A[4*m]<<24) | (A[5*m]<<16) | (A[6*m]<<8) | A[7*m];
293//
294 // // pre-transform x
295 // t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
296 // t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
297 //
298 // // pre-transform y
299 // t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
300 // t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
301 //
302 // // final transform
303 // t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
304 // y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
305 // x = t;
306//
307// B[7*n] = y; y >>= 8;
308// B[6*n] = y; y >>= 8;
309// B[5*n] = y; y >>= 8;
310// B[4*n] = y;
311//
312// B[3*n] = x; x >>= 8;
313// B[2*n] = x; x >>= 8;
314// B[n] = x; x >>= 8;
315// B[0] = x;
316// // B[0]=x>>24; B[n]=x>>16; B[2*n]=x>>8; B[3*n]=x>>0;
317// // B[4*n]=y>>24; B[5*n]=y>>16; B[6*n]=y>>8; B[7*n]=y>>0;
318// }
319//
320// void transposeLines(Lines & out, Lines & in) {
321// transpose8<1,2>(in.bytes, out.bytes);
322// transpose8<1,2>(in.bytes + 8, out.bytes + 1);
323// }
324
325
328extern int noise_min;
329
332extern int noise_max;
333
334void CFastLED::countFPS(int nFrames) {
335 static int br = 0;
336 static uint32_t lastframe = 0; // millis();
337
338 if(br++ >= nFrames) {
339 uint32_t now = millis();
340 now -= lastframe;
341 if(now == 0) {
342 now = 1; // prevent division by zero below
343 }
344 m_nFPS = (br * 1000) / now;
345 br = 0;
346 lastframe = millis();
347 }
348}
349
350void CFastLED::setMaxRefreshRate(uint16_t refresh, bool constrain) {
351 if(constrain) {
352 // if we're constraining, the new value of m_nMinMicros _must_ be higher than previously (because we're only
353 // allowed to slow things down if constraining)
354 if(refresh > 0) {
355 m_nMinMicros = ((1000000 / refresh) > m_nMinMicros) ? (1000000 / refresh) : m_nMinMicros;
356 }
357 } else if(refresh > 0) {
358 m_nMinMicros = 1000000 / refresh;
359 } else {
360 m_nMinMicros = 0;
361 }
362}
363
364
365uint8_t get_brightness() {
366 return FastLED.getBrightness();
367}
368
369
370
371#ifdef NEED_CXX_BITS
372namespace __cxxabiv1
373{
374 #if !defined(ESP8266) && !defined(ESP32)
375 extern "C" void __cxa_pure_virtual (void) {}
376 #endif
377
378 /* guard variables */
379
380 /* The ABI requires a 64-bit type. */
381 __extension__ typedef int __guard __attribute__((mode(__DI__)));
382
383 extern "C" int __cxa_guard_acquire (__guard *) __attribute__((weak));
384 extern "C" void __cxa_guard_release (__guard *) __attribute__((weak));
385 extern "C" void __cxa_guard_abort (__guard *) __attribute__((weak));
386
387 extern "C" int __cxa_guard_acquire (__guard *g)
388 {
389 return !*(char *)(g);
390 }
391
392 extern "C" void __cxa_guard_release (__guard *g)
393 {
394 *(char *)g = 1;
395 }
396
397 extern "C" void __cxa_guard_abort (__guard *)
398 {
399
400 }
401}
402#endif
403
int x
Definition Audio.ino:71
UISlider scale("Scale", 1.0f, 0.0f, 1.0f, 0.01f)
uint32_t _retry_cnt
Global frame retry counter, used for debugging ESP implementations.
Definition FastLED.cpp:70
#define MAX_CLED_CONTROLLERS
Definition FastLED.cpp:18
void * pSmartMatrix
Pointer to the matrix object when using the Smart Matrix Library.
Definition FastLED.cpp:56
FASTLED_NAMESPACE_BEGIN uint16_t cled_contoller_size()
Definition FastLED.cpp:48
int atexit(void(*)())
Has to be declared outside of any namespaces.
Definition FastLED.cpp:40
static void * gControllersData[MAX_CLED_CONTROLLERS]
Definition FastLED.cpp:111
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
uint8_t get_brightness()
Definition FastLED.cpp:365
static uint32_t lastshow
Definition FastLED.cpp:62
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.
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:58
central include file for FastLED, defines the CFastLED class/object
void onEndFrame()
Definition FastLED.cpp:161
int size()
Get the number of leds in the first controller.
Definition FastLED.cpp:84
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:350
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:259
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:334
CLEDController & operator[](int x)
Get a reference to a registered controller.
Definition FastLED.cpp:175
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:245
void showColor(const struct CRGB &color, uint8_t scale)
Set all leds on all controllers to the given color/scale.
Definition FastLED.cpp:187
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:275
uint32_t m_nPowerData
max power use parameter
Definition FastLED.h:375
void clearData()
Clear out the local data array.
Definition FastLED.cpp:237
void setCorrection(const struct CRGB &correction)
Set a global color correction.
Definition FastLED.cpp:267
int count()
Get how many controllers have been registered.
Definition FastLED.cpp:165
void clear(bool writeData=false)
Clear the leds, wiping the local array of data.
Definition FastLED.cpp:230
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:92
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.
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)
static CLEDController * m_pHead
pointer to the first LED controller in the linked list
Base definition for an LED controller.
static void onBeginFrame()
static void onStripAdded(CLEDController *strip, uint32_t num_leds)
static void onEndFrame()
static void onEndShowLeds()
UISlider length("Length", 1.0f, 0.0f, 1.0f, 0.01f)
#define FASTLED_NAMESPACE_END
Definition namespace.h:23
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:55