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