RSeries astromech firmware
LEDPixelEngine.h
Go to the documentation of this file.
1 #ifndef LEDPIXELENGINE_H
2 #define LEDPIXELENGINE_H
3 
4 #ifndef USE_LEDLIB
5  #ifdef ESP32
6  #define USE_LEDLIB 0 //0 for FastLED, 1 for Adafruit_NeoPixel, 2 for NeoPixelBus
7  #else
8  #define USE_LEDLIB 1 //0 for FastLED, 1 for Adafruit_NeoPixel, 2 for NeoPixelBus
9  #endif
10 #endif
11 
12 #include "ReelTwo.h"
13 #if USE_LEDLIB == 0
14  #include <FastLED.h>
15  #include <Adafruit_NeoPixel.h>
16 #elif USE_LEDLIB == 1
17  #include <Adafruit_NeoPixel.h>
18  #include "core/NeoPixel_FastLED.h"
19 #else
20  #error Not supported
21 #endif
22 
23 #if USE_LEDLIB == 0
24 /*
25  * Project FastLED NeoPixel Library
26  * @author David Madison
27  * @link github.com/dmadison/FastLED_NeoPixel
28  * @license MIT - Copyright (c) 2021 David Madison
29  *
30  * Permission is hereby granted, free of charge, to any person obtaining a copy
31  * of this software and associated documentation files (the "Software"), to deal
32  * in the Software without restriction, including without limitation the rights
33  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
34  * copies of the Software, and to permit persons to whom the Software is
35  * furnished to do so, subject to the following conditions:
36  *
37  * The above copyright notice and this permission notice shall be included in
38  * all copies or substantial portions of the Software.
39  *
40  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
41  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
42  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
43  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
44  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
45  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
46  * THE SOFTWARE.
47  *
48  */
49 
70 class FastLED_NeoPixel_Variant {
71 public:
78  FastLED_NeoPixel_Variant(CRGB* ledPtr, uint16_t nLeds) :
79  fLED(ledPtr),
80  fMaxLEDs(nLeds),
81  fNumLEDs(nLeds)
82  {
83  fEndTime = micros();
84  }
85 
110  void begin(CLEDController& ctrl)
111  {
112  fController = &ctrl;
113  }
114 
116  void show()
117  {
118  if (fController != nullptr)
119  {
120  fController->show(fLED, fNumLEDs, fBrightness);
121  fEndTime = micros();
122  }
123  }
124 
126  void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b)
127  {
128  if (n < fNumLEDs)
129  {
130  fLED[n] = CRGB(r, g, b);
131  }
132  }
133 
135  void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b, uint8_t w)
136  {
137  setPixelColor(n, Color(r, g, b, w));
138  }
139 
141  void setPixelColor(uint16_t n, uint32_t c)
142  {
143  if (n < fNumLEDs)
144  {
145  fLED[n] = packedToColor(c);
146  }
147  }
148 
150  void fill(uint32_t c = 0, uint16_t first = 0, uint16_t count = 0)
151  {
152  if (first >= fNumLEDs)
153  return;
154 
155  if (first == 0 && count == 0)
156  {
157  // if auto, fill from start to end
158  count = fNumLEDs;
159  }
160  else if (first != 0 && count == 0)
161  {
162  // if only first is set, fill from first to end
163  count = fNumLEDs - first;
164  }
165  else
166  {
167  // if both are set, fill from first to end without overrunning buffer
168  count = min(count, uint16_t(fNumLEDs - first));
169  }
170 
171  fill_solid(fLED + first, count, packedToColor(c));
172  }
173 
175  inline void setBrightness(uint8_t bright)
176  {
177  fBrightness = bright;
178  }
179 
181  void clear()
182  {
183  fill_solid(fLED, fNumLEDs, CRGB::Black);
184  }
185 
197  void updateLength(uint16_t n)
198  {
199  if (n <= fMaxLEDs)
200  {
201  fNumLEDs = n;
202  clear();
203  }
204  }
205 
207  bool canShow()
208  {
209  if (fEndTime > micros())
210  {
211  fEndTime = micros();
212  }
213  return (micros() - fEndTime) >= 300UL;
214  }
215 
217  inline uint8_t* getPixels() const
218  {
219  return (uint8_t*)fLED;
220  }
221 
223  inline uint8_t getBrightness() const
224  {
225  return fBrightness;
226  }
227 
229  inline uint16_t numPixels() const
230  {
231  return fNumLEDs;
232  }
233 
235  uint32_t getPixelColor(uint16_t n) const
236  {
237  if (n >= fNumLEDs)
238  return Color(CRGB::Black);
239  return Color(fLED[n]);
240  }
241 
243  static uint8_t sine8(uint8_t x) { return Adafruit_NeoPixel::sine8(x); }
244 
246  static uint8_t gamma8(uint8_t x) { return Adafruit_NeoPixel::gamma8(x); }
247 
248 
250  static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) { return Adafruit_NeoPixel::Color(r, g, b); }
251 
253  static uint32_t Color(uint8_t r, uint8_t g, uint8_t b, uint8_t w) { return Adafruit_NeoPixel::Color(r, g, b, w); }
254 
256  static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255) { return Adafruit_NeoPixel::ColorHSV(hue, sat, val); }
257 
259  static uint32_t gamma32(uint32_t x) { return Adafruit_NeoPixel::gamma32(x); }
260 
261 
279  inline CRGB* getLeds() const
280  {
281  return fLED;
282  }
283 
296  inline CLEDController* getController() const
297  {
298  return fController;
299  }
300 
310  static uint32_t Color(const CRGB& c)
311  {
312  return Adafruit_NeoPixel::Color(c.r, c.g, c.b);
313  }
314 
330  void setBlendWhite(bool blend)
331  {
332  fBlendWhite = blend;
333  }
334 
388  void setPin(uint16_t p);
389 
397  void updateType(neoPixelType T);
398 
401 private:
408  CRGB packedToColor(uint32_t c) const
409  {
410  CRGB color(c);
411  if (fBlendWhite == true)
412  color.addToRGB(c >> 24);
413  return color;
414  }
415 
416  CRGB* const fLED;
417  const uint16_t fMaxLEDs;
418  uint16_t fNumLEDs;
419  CLEDController* fController = nullptr;
420 
421  uint8_t fBrightness = 255;
422  uint32_t fEndTime;
423  bool fBlendWhite = false;
424 };
425 
426 
457 inline constexpr EOrder NeoToEOrder(uint32_t neoOrder)
458 {
459  return
460  // FastLED Color Orders
461  neoOrder == ((uint32_t) RGB) ? RGB :
462  neoOrder == ((uint32_t) RBG) ? RBG :
463  neoOrder == ((uint32_t) GRB) ? GRB :
464  neoOrder == ((uint32_t) GBR) ? GBR :
465  neoOrder == ((uint32_t) BRG) ? BRG :
466  neoOrder == ((uint32_t) BGR) ? BGR :
467 
468  // NeoPixel Color Orders at Speed 'NEO_KHZ800'
469  neoOrder == (NEO_RGB + NEO_KHZ800) ? RGB :
470  neoOrder == (NEO_RBG + NEO_KHZ800) ? RBG :
471  neoOrder == (NEO_GRB + NEO_KHZ800) ? GRB :
472  neoOrder == (NEO_GBR + NEO_KHZ800) ? GBR :
473  neoOrder == (NEO_BRG + NEO_KHZ800) ? BRG :
474  neoOrder == (NEO_BGR + NEO_KHZ800) ? BGR :
475 
476  // NeoPixel Color Orders at Speed 'NEO_KHZ400'
477  neoOrder == (NEO_RGB + NEO_KHZ400) ? RGB :
478  neoOrder == (NEO_RBG + NEO_KHZ400) ? RBG :
479  neoOrder == (NEO_GRB + NEO_KHZ400) ? GRB :
480  neoOrder == (NEO_GBR + NEO_KHZ400) ? GBR :
481  neoOrder == (NEO_BRG + NEO_KHZ400) ? BRG :
482  neoOrder == (NEO_BGR + NEO_KHZ400) ? BGR :
483 
484  GRB; // 'neoOrder' is not a valid color selection, using GRB as default
485 }
486 
487 
522 template<uint16_t NUM_LEDS, uint8_t DATA_PIN, uint32_t RGB_ORDER = GRB>
523 class FastLED_NeoPixel : public FastLED_NeoPixel_Variant {
524 public:
525  FastLED_NeoPixel() : FastLED_NeoPixel_Variant(fLEDData, NUM_LEDS)
526  {
527  memset(fLEDData, 0, sizeof(fLEDData)); // set all LEDs to black
528  }
529 
534  void begin()
535  {
536  FastLED_NeoPixel_Variant::begin(
537  FastLED.addLeds<WS2812B, DATA_PIN, NeoToEOrder(RGB_ORDER)>(fLEDData, NUM_LEDS));
538  }
539 
541  inline uint8_t getPin() const
542  {
543  return DATA_PIN;
544  }
545 
546 private:
547  CRGB fLEDData[NUM_LEDS];
548 };
549 
550 
551 #endif
552 
553 #endif
ReelTwo.h
NeoPixel_FastLED.h