FastLED 3.9.7
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();
110 if (m_nFPS < 100) { pCur->setDither(0); }
111 pCur->showLedsInternal(scale);
112 pCur = pCur->next();
113 }
114
115 length = 0; // Reset length to 0 and iterate again.
116 pCur = CLEDController::head();
117 while(pCur && length < MAX_CLED_CONTROLLERS) {
118 pCur->endShowLeds(gControllersData[length++]);
119 pCur = pCur->next();
120 }
121 countFPS();
122 fl::EngineEvents::onEndShowLeds();
123 fl::EngineEvents::onEndFrame();
124}
125
127 int x = 0;
129 while( pCur) {
130 ++x;
131 pCur = pCur->next();
132 }
133 return x;
134}
135
138 while(x-- && pCur) {
139 pCur = pCur->next();
140 }
141 if(pCur == NULL) {
142 return *(CLEDController::head());
143 } else {
144 return *pCur;
145 }
146}
147
148void CFastLED::showColor(const struct CRGB & color, uint8_t scale) {
149 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
150 lastshow = micros();
151
152 // If we have a function for computing power, use it!
153 if(m_pPowerFunc) {
154 scale = (*m_pPowerFunc)(scale, m_nPowerData);
155 }
156
157 int length = 0;
159 while(pCur && length < MAX_CLED_CONTROLLERS) {
160 gControllersData[length++] = pCur->beginShowLeds();
161 if(m_nFPS < 100) { pCur->setDither(0); }
162 pCur->showColorInternal(color, scale);
163 pCur = pCur->next();
164 }
165
166 pCur = CLEDController::head();
167 length = 0; // Reset length to 0 and iterate again.
168 while(pCur && length < MAX_CLED_CONTROLLERS) {
169 pCur->endShowLeds(gControllersData[length++]);
170 pCur = pCur->next();
171 }
172 countFPS();
173}
174
175void CFastLED::clear(bool writeData) {
176 if(writeData) {
177 showColor(CRGB(0,0,0), 0);
178 }
179 clearData();
180}
181
184 while(pCur) {
185 pCur->clearLedDataInternal();
186 pCur = pCur->next();
187 }
188}
189
190void CFastLED::delay(unsigned long ms) {
191 unsigned long start = millis();
192 do {
193#ifndef FASTLED_ACCURATE_CLOCK
194 // make sure to allow at least one ms to pass to ensure the clock moves
195 // forward
196 ::delay(1);
197#endif
198 show();
199 yield();
200 }
201 while((millis()-start) < ms);
202}
203
204void CFastLED::setTemperature(const struct CRGB & temp) {
206 while(pCur) {
207 pCur->setTemperature(temp);
208 pCur = pCur->next();
209 }
210}
211
212void CFastLED::setCorrection(const struct CRGB & correction) {
214 while(pCur) {
215 pCur->setCorrection(correction);
216 pCur = pCur->next();
217 }
218}
219
220void CFastLED::setDither(uint8_t ditherMode) {
222 while(pCur) {
223 pCur->setDither(ditherMode);
224 pCur = pCur->next();
225 }
226}
227
228//
229// template<int m, int n> void transpose8(unsigned char A[8], unsigned char B[8]) {
230// uint32_t x, y, t;
231//
232// // Load the array and pack it into x and y.
233// y = *(unsigned int*)(A);
234// x = *(unsigned int*)(A+4);
235//
236// // x = (A[0]<<24) | (A[m]<<16) | (A[2*m]<<8) | A[3*m];
237// // y = (A[4*m]<<24) | (A[5*m]<<16) | (A[6*m]<<8) | A[7*m];
238//
239 // // pre-transform x
240 // t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
241 // t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
242 //
243 // // pre-transform y
244 // t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
245 // t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
246 //
247 // // final transform
248 // t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
249 // y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
250 // x = t;
251//
252// B[7*n] = y; y >>= 8;
253// B[6*n] = y; y >>= 8;
254// B[5*n] = y; y >>= 8;
255// B[4*n] = y;
256//
257// B[3*n] = x; x >>= 8;
258// B[2*n] = x; x >>= 8;
259// B[n] = x; x >>= 8;
260// B[0] = x;
261// // B[0]=x>>24; B[n]=x>>16; B[2*n]=x>>8; B[3*n]=x>>0;
262// // B[4*n]=y>>24; B[5*n]=y>>16; B[6*n]=y>>8; B[7*n]=y>>0;
263// }
264//
265// void transposeLines(Lines & out, Lines & in) {
266// transpose8<1,2>(in.bytes, out.bytes);
267// transpose8<1,2>(in.bytes + 8, out.bytes + 1);
268// }
269
270
273extern int noise_min;
274
277extern int noise_max;
278
279void CFastLED::countFPS(int nFrames) {
280 static int br = 0;
281 static uint32_t lastframe = 0; // millis();
282
283 if(br++ >= nFrames) {
284 uint32_t now = millis();
285 now -= lastframe;
286 if(now == 0) {
287 now = 1; // prevent division by zero below
288 }
289 m_nFPS = (br * 1000) / now;
290 br = 0;
291 lastframe = millis();
292 }
293}
294
295void CFastLED::setMaxRefreshRate(uint16_t refresh, bool constrain) {
296 if(constrain) {
297 // if we're constraining, the new value of m_nMinMicros _must_ be higher than previously (because we're only
298 // allowed to slow things down if constraining)
299 if(refresh > 0) {
300 m_nMinMicros = ((1000000 / refresh) > m_nMinMicros) ? (1000000 / refresh) : m_nMinMicros;
301 }
302 } else if(refresh > 0) {
303 m_nMinMicros = 1000000 / refresh;
304 } else {
305 m_nMinMicros = 0;
306 }
307}
308
309
310uint8_t get_brightness() {
311 return FastLED.getBrightness();
312}
313
314
315
316#ifdef NEED_CXX_BITS
317namespace __cxxabiv1
318{
319 #if !defined(ESP8266) && !defined(ESP32)
320 extern "C" void __cxa_pure_virtual (void) {}
321 #endif
322
323 /* guard variables */
324
325 /* The ABI requires a 64-bit type. */
326 __extension__ typedef int __guard __attribute__((mode(__DI__)));
327
328 extern "C" int __cxa_guard_acquire (__guard *) __attribute__((weak));
329 extern "C" void __cxa_guard_release (__guard *) __attribute__((weak));
330 extern "C" void __cxa_guard_abort (__guard *) __attribute__((weak));
331
332 extern "C" int __cxa_guard_acquire (__guard *g)
333 {
334 return !*(char *)(g);
335 }
336
337 extern "C" void __cxa_guard_release (__guard *g)
338 {
339 *(char *)g = 1;
340 }
341
342 extern "C" void __cxa_guard_abort (__guard *)
343 {
344
345 }
346}
347#endif
348
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:354
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:295
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:204
void show()
Update all our controllers with the current led colors.
Definition FastLED.h:743
void countFPS(int nFrames=25)
For debugging, this will keep track of time between calls to countFPS().
Definition FastLED.cpp:279
CLEDController & operator[](int x)
Get a reference to a registered controller.
Definition FastLED.cpp:136
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:190
void showColor(const struct CRGB &color, uint8_t scale)
Set all leds on all controllers to the given color/scale.
Definition FastLED.cpp:148
void setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode.
Definition FastLED.cpp:220
uint8_t getBrightness()
Get the current global brightness setting.
Definition FastLED.h:727
void clearData()
Clear out the local data array.
Definition FastLED.cpp:182
void setCorrection(const struct CRGB &correction)
Set a global color correction.
Definition FastLED.cpp:212
int count()
Get how many controllers have been registered.
Definition FastLED.cpp:126
void clear(bool writeData=false)
Clear the leds, wiping the local array of data.
Definition FastLED.cpp:175
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.
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