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