FastLED 3.6.0
Loading...
Searching...
No Matches
FastLED.cpp
Go to the documentation of this file.
1#define FASTLED_INTERNAL
2#include "FastLED.h"
3
6
7#if defined(__SAM3X8E__)
8volatile uint32_t fuckit;
9#endif
10
11FASTLED_NAMESPACE_BEGIN
12
15void *pSmartMatrix = NULL;
16
18
21static uint32_t lastshow = 0;
22
25uint32_t _frame_cnt=0;
26
29uint32_t _retry_cnt=0;
30
31// uint32_t CRGB::Squant = ((uint32_t)((__TIME__[4]-'0') * 28))<<16 | ((__TIME__[6]-'0')*50)<<8 | ((__TIME__[7]-'0')*28);
32
33CFastLED::CFastLED() {
34 // clear out the array of led controllers
35 // m_nControllers = 0;
36 m_Scale = 255;
37 m_nFPS = 0;
38 m_pPowerFunc = NULL;
39 m_nPowerData = 0xFFFFFFFF;
40}
41
43 struct CRGB *data,
44 int nLedsOrOffset, int nLedsIfOffset) {
45 int nOffset = (nLedsIfOffset > 0) ? nLedsOrOffset : 0;
46 int nLeds = (nLedsIfOffset > 0) ? nLedsIfOffset : nLedsOrOffset;
47
48 pLed->init();
49 pLed->setLeds(data + nOffset, nLeds);
51 return *pLed;
52}
53
54void CFastLED::show(uint8_t scale) {
55 // guard against showing too rapidly
56 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
57 lastshow = micros();
58
59 // If we have a function for computing power, use it!
60 if(m_pPowerFunc) {
61 scale = (*m_pPowerFunc)(scale, m_nPowerData);
62 }
63
65 while(pCur) {
66 uint8_t d = pCur->getDither();
67 if(m_nFPS < 100) { pCur->setDither(0); }
68 pCur->showLeds(scale);
69 pCur->setDither(d);
70 pCur = pCur->next();
71 }
72 countFPS();
73}
74
76 int x = 0;
78 while( pCur) {
79 ++x;
80 pCur = pCur->next();
81 }
82 return x;
83}
84
87 while(x-- && pCur) {
88 pCur = pCur->next();
89 }
90 if(pCur == NULL) {
91 return *(CLEDController::head());
92 } else {
93 return *pCur;
94 }
95}
96
97void CFastLED::showColor(const struct CRGB & color, uint8_t scale) {
98 while(m_nMinMicros && ((micros()-lastshow) < m_nMinMicros));
99 lastshow = micros();
100
101 // If we have a function for computing power, use it!
102 if(m_pPowerFunc) {
103 scale = (*m_pPowerFunc)(scale, m_nPowerData);
104 }
105
107 while(pCur) {
108 uint8_t d = pCur->getDither();
109 if(m_nFPS < 100) { pCur->setDither(0); }
110 pCur->showColor(color, scale);
111 pCur->setDither(d);
112 pCur = pCur->next();
113 }
114 countFPS();
115}
116
117void CFastLED::clear(bool writeData) {
118 if(writeData) {
119 showColor(CRGB(0,0,0), 0);
120 }
121 clearData();
122}
123
126 while(pCur) {
127 pCur->clearLedData();
128 pCur = pCur->next();
129 }
130}
131
132void CFastLED::delay(unsigned long ms) {
133 unsigned long start = millis();
134 do {
135#ifndef FASTLED_ACCURATE_CLOCK
136 // make sure to allow at least one ms to pass to ensure the clock moves
137 // forward
138 ::delay(1);
139#endif
140 show();
141 yield();
142 }
143 while((millis()-start) < ms);
144}
145
146void CFastLED::setTemperature(const struct CRGB & temp) {
148 while(pCur) {
149 pCur->setTemperature(temp);
150 pCur = pCur->next();
151 }
152}
153
154void CFastLED::setCorrection(const struct CRGB & correction) {
156 while(pCur) {
157 pCur->setCorrection(correction);
158 pCur = pCur->next();
159 }
160}
161
162void CFastLED::setDither(uint8_t ditherMode) {
164 while(pCur) {
165 pCur->setDither(ditherMode);
166 pCur = pCur->next();
167 }
168}
169
170//
171// template<int m, int n> void transpose8(unsigned char A[8], unsigned char B[8]) {
172// uint32_t x, y, t;
173//
174// // Load the array and pack it into x and y.
175// y = *(unsigned int*)(A);
176// x = *(unsigned int*)(A+4);
177//
178// // x = (A[0]<<24) | (A[m]<<16) | (A[2*m]<<8) | A[3*m];
179// // y = (A[4*m]<<24) | (A[5*m]<<16) | (A[6*m]<<8) | A[7*m];
180//
181 // // pre-transform x
182 // t = (x ^ (x >> 7)) & 0x00AA00AA; x = x ^ t ^ (t << 7);
183 // t = (x ^ (x >>14)) & 0x0000CCCC; x = x ^ t ^ (t <<14);
184 //
185 // // pre-transform y
186 // t = (y ^ (y >> 7)) & 0x00AA00AA; y = y ^ t ^ (t << 7);
187 // t = (y ^ (y >>14)) & 0x0000CCCC; y = y ^ t ^ (t <<14);
188 //
189 // // final transform
190 // t = (x & 0xF0F0F0F0) | ((y >> 4) & 0x0F0F0F0F);
191 // y = ((x << 4) & 0xF0F0F0F0) | (y & 0x0F0F0F0F);
192 // x = t;
193//
194// B[7*n] = y; y >>= 8;
195// B[6*n] = y; y >>= 8;
196// B[5*n] = y; y >>= 8;
197// B[4*n] = y;
198//
199// B[3*n] = x; x >>= 8;
200// B[2*n] = x; x >>= 8;
201// B[n] = x; x >>= 8;
202// B[0] = x;
203// // B[0]=x>>24; B[n]=x>>16; B[2*n]=x>>8; B[3*n]=x>>0;
204// // B[4*n]=y>>24; B[5*n]=y>>16; B[6*n]=y>>8; B[7*n]=y>>0;
205// }
206//
207// void transposeLines(Lines & out, Lines & in) {
208// transpose8<1,2>(in.bytes, out.bytes);
209// transpose8<1,2>(in.bytes + 8, out.bytes + 1);
210// }
211
212
215extern int noise_min;
216
219extern int noise_max;
220
221void CFastLED::countFPS(int nFrames) {
222 static int br = 0;
223 static uint32_t lastframe = 0; // millis();
224
225 if(br++ >= nFrames) {
226 uint32_t now = millis();
227 now -= lastframe;
228 if(now == 0) {
229 now = 1; // prevent division by zero below
230 }
231 m_nFPS = (br * 1000) / now;
232 br = 0;
233 lastframe = millis();
234 }
235}
236
237void CFastLED::setMaxRefreshRate(uint16_t refresh, bool constrain) {
238 if(constrain) {
239 // if we're constraining, the new value of m_nMinMicros _must_ be higher than previously (because we're only
240 // allowed to slow things down if constraining)
241 if(refresh > 0) {
242 m_nMinMicros = ((1000000 / refresh) > m_nMinMicros) ? (1000000 / refresh) : m_nMinMicros;
243 }
244 } else if(refresh > 0) {
245 m_nMinMicros = 1000000 / refresh;
246 } else {
247 m_nMinMicros = 0;
248 }
249}
250
254extern "C" int atexit(void (* /*func*/ )()) { return 0; }
255
256#ifdef FASTLED_NEEDS_YIELD
257extern "C" void yield(void) { }
258#endif
259
260#ifdef NEED_CXX_BITS
261namespace __cxxabiv1
262{
263 #if !defined(ESP8266) && !defined(ESP32)
264 extern "C" void __cxa_pure_virtual (void) {}
265 #endif
266
267 /* guard variables */
268
269 /* The ABI requires a 64-bit type. */
270 __extension__ typedef int __guard __attribute__((mode(__DI__)));
271
272 extern "C" int __cxa_guard_acquire (__guard *) __attribute__((weak));
273 extern "C" void __cxa_guard_release (__guard *) __attribute__((weak));
274 extern "C" void __cxa_guard_abort (__guard *) __attribute__((weak));
275
276 extern "C" int __cxa_guard_acquire (__guard *g)
277 {
278 return !*(char *)(g);
279 }
280
281 extern "C" void __cxa_guard_release (__guard *g)
282 {
283 *(char *)g = 1;
284 }
285
286 extern "C" void __cxa_guard_abort (__guard *)
287 {
288
289 }
290}
291#endif
292
293FASTLED_NAMESPACE_END
uint32_t _retry_cnt
Global frame retry counter, used for debugging ESP implementations.
Definition FastLED.cpp:29
void * pSmartMatrix
Pointer to the matrix object when using the Smart Matrix Library.
Definition FastLED.cpp:15
int atexit(void(*)())
Called at program exit when run in a desktop environment.
Definition FastLED.cpp:254
CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:17
int noise_max
Unused value.
uint32_t _frame_cnt
Global frame counter, used for debugging ESP implementations.
Definition FastLED.cpp:25
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:17
High level controller interface for FastLED.
Definition FastLED.h:221
void setMaxRefreshRate(uint16_t refresh, bool constrain=false)
Set the maximum refresh rate.
Definition FastLED.cpp:237
void setTemperature(const struct CRGB &temp)
Set a global color temperature.
Definition FastLED.cpp:146
void show()
Update all our controllers with the current led colors.
Definition FastLED.h:573
void countFPS(int nFrames=25)
For debugging, this will keep track of time between calls to countFPS().
Definition FastLED.cpp:221
CLEDController & operator[](int x)
Get a reference to a registered controller.
Definition FastLED.cpp:85
void delay(unsigned long ms)
Delay for the given number of milliseconds.
Definition FastLED.cpp:132
void showColor(const struct CRGB &color, uint8_t scale)
Set all leds on all controllers to the given color/scale.
Definition FastLED.cpp:97
void setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode.
Definition FastLED.cpp:162
void clearData()
Clear out the local data array.
Definition FastLED.cpp:124
void setCorrection(const struct CRGB &correction)
Set a global color correction.
Definition FastLED.cpp:154
int count()
Get how many controllers have been registered.
Definition FastLED.cpp:75
void clear(bool writeData=false)
Clear the leds, wiping the local array of data.
Definition FastLED.cpp:117
static CLEDController & addLeds(CLEDController *pLed, struct CRGB *data, int nLedsOrOffset, int nLedsIfOffset=0)
Add a CLEDController instance to the world.
Definition FastLED.cpp:42
Base definition for an LED controller.
Definition controller.h:61
virtual void showColor(const struct CRGB &data, int nLeds, CRGB scale)=0
Set all the LEDs to a given color.
CLEDController * next()
Get the next controller in the linked list after this one.
Definition controller.h:145
CLEDController & setDither(uint8_t ditherMode=BINARY_DITHER)
Set the dithering mode for this controller to use.
Definition controller.h:183
virtual uint16_t getMaxRefreshRate() const
Gets the maximum possible refresh rate of the strip.
Definition controller.h:249
uint8_t getDither()
Get the dithering option currently set for this controller.
Definition controller.h:187
CLEDController & setLeds(CRGB *data, int nLeds)
Set the default array of LEDs to be used by this controller.
Definition controller.h:150
void clearLedData()
Zero out the LED data managed by this controller.
Definition controller.h:157
CLEDController & setCorrection(CRGB correction)
The color corrction to use for this controller, expressed as a CRGB object.
Definition controller.h:192
static CLEDController * head()
Get the first LED controller in the linked list of controllers.
Definition controller.h:141
CLEDController & setTemperature(CRGB temperature)
Set the color temperature, aka white point, for this controller.
Definition controller.h:204
void showLeds(uint8_t brightness=255)
Write the data to the LEDs managed by this controller.
Definition controller.h:126
static CLEDController * m_pTail
pointer to the last LED controller in the linked list
Definition controller.h:71
virtual void init()=0
Initialize the LED controller.
static CLEDController * m_pHead
pointer to the first LED controller in the linked list
Definition controller.h:70
Representation of an RGB pixel (Red, Green, Blue)
Definition pixeltypes.h:120