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