Adafruit DotStar Arduino Library
Adafruit_DotStar.h
Go to the documentation of this file.
1 
21 #ifndef _ADAFRUIT_DOT_STAR_H_
22 #define _ADAFRUIT_DOT_STAR_H_
23 
24 #include "Arduino.h"
25 
26 #include <Adafruit_SPIDevice.h>
27 
28 // Color-order flag for LED pixels (optional extra parameter to constructor):
29 // Bits 0,1 = R index (0-2), bits 2,3 = G index, bits 4,5 = B index
30 #define DOTSTAR_RGB (0 | (1 << 2) | (2 << 4))
31 #define DOTSTAR_RBG (0 | (2 << 2) | (1 << 4))
32 #define DOTSTAR_GRB (1 | (0 << 2) | (2 << 4))
33 #define DOTSTAR_GBR (2 | (0 << 2) | (1 << 4))
34 #define DOTSTAR_BRG (1 | (2 << 2) | (0 << 4))
35 #define DOTSTAR_BGR (2 | (1 << 2) | (0 << 4))
36 #define DOTSTAR_MONO 0
37 
38 // These two tables are declared outside the Adafruit_DotStar class
39 // because some boards may require oldschool compilers that don't
40 // handle the C++11 constexpr keyword.
41 
42 /* A PROGMEM (flash mem) table containing 8-bit unsigned sine wave (0-255).
43  Copy & paste this snippet into a Python REPL to regenerate:
44 import math
45 for x in range(256):
46  print("{:3},".format(int((math.sin(x/128.0*math.pi)+1.0)*127.5+0.5))),
47  if x&15 == 15: print
48 */
49 static const uint8_t PROGMEM _DotStarSineTable[256] = {
50  128, 131, 134, 137, 140, 143, 146, 149, 152, 155, 158, 162, 165, 167, 170,
51  173, 176, 179, 182, 185, 188, 190, 193, 196, 198, 201, 203, 206, 208, 211,
52  213, 215, 218, 220, 222, 224, 226, 228, 230, 232, 234, 235, 237, 238, 240,
53  241, 243, 244, 245, 246, 248, 249, 250, 250, 251, 252, 253, 253, 254, 254,
54  254, 255, 255, 255, 255, 255, 255, 255, 254, 254, 254, 253, 253, 252, 251,
55  250, 250, 249, 248, 246, 245, 244, 243, 241, 240, 238, 237, 235, 234, 232,
56  230, 228, 226, 224, 222, 220, 218, 215, 213, 211, 208, 206, 203, 201, 198,
57  196, 193, 190, 188, 185, 182, 179, 176, 173, 170, 167, 165, 162, 158, 155,
58  152, 149, 146, 143, 140, 137, 134, 131, 128, 124, 121, 118, 115, 112, 109,
59  106, 103, 100, 97, 93, 90, 88, 85, 82, 79, 76, 73, 70, 67, 65,
60  62, 59, 57, 54, 52, 49, 47, 44, 42, 40, 37, 35, 33, 31, 29,
61  27, 25, 23, 21, 20, 18, 17, 15, 14, 12, 11, 10, 9, 7, 6,
62  5, 5, 4, 3, 2, 2, 1, 1, 1, 0, 0, 0, 0, 0, 0,
63  0, 1, 1, 1, 2, 2, 3, 4, 5, 5, 6, 7, 9, 10, 11,
64  12, 14, 15, 17, 18, 20, 21, 23, 25, 27, 29, 31, 33, 35, 37,
65  40, 42, 44, 47, 49, 52, 54, 57, 59, 62, 65, 67, 70, 73, 76,
66  79, 82, 85, 88, 90, 93, 97, 100, 103, 106, 109, 112, 115, 118, 121,
67  124};
68 
69 /* Similar to above, but for an 8-bit gamma-correction table.
70  Copy & paste this snippet into a Python REPL to regenerate:
71 import math
72 gamma=2.6
73 for x in range(256):
74  print("{:3},".format(int(math.pow((x)/255.0,gamma)*255.0+0.5))),
75  if x&15 == 15: print
76 */
77 static const uint8_t PROGMEM _DotStarGammaTable[256] = {
78  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79  0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
80  1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 3,
81  3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6,
82  6, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10,
83  11, 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17,
84  17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 24, 24, 25,
85  25, 26, 27, 27, 28, 29, 29, 30, 31, 31, 32, 33, 34, 34, 35,
86  36, 37, 38, 38, 39, 40, 41, 42, 42, 43, 44, 45, 46, 47, 48,
87  49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
88  64, 65, 66, 68, 69, 70, 71, 72, 73, 75, 76, 77, 78, 80, 81,
89  82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, 97, 99, 100, 102,
90  103, 105, 106, 108, 109, 111, 112, 114, 115, 117, 119, 120, 122, 124, 125,
91  127, 129, 130, 132, 134, 136, 137, 139, 141, 143, 145, 146, 148, 150, 152,
92  154, 156, 158, 160, 162, 164, 166, 168, 170, 172, 174, 176, 178, 180, 182,
93  184, 186, 188, 191, 193, 195, 197, 199, 202, 204, 206, 209, 211, 213, 215,
94  218, 220, 223, 225, 227, 230, 232, 235, 237, 240, 242, 245, 247, 250, 252,
95  255};
96 
102 
103 public:
104 #if !defined(SPI_INTERFACES_COUNT) || \
105  (defined(SPI_INTERFACES_COUNT) && (SPI_INTERFACES_COUNT > 0))
106  // HW SPI available
107  Adafruit_DotStar(uint16_t n, uint8_t o = DOTSTAR_BRG, SPIClass *spi = &SPI);
108 #else
109  Adafruit_DotStar(uint16_t n, uint8_t o = DOTSTAR_BRG, SPIClass *spi = NULL);
110 #endif
111  Adafruit_DotStar(uint16_t n, uint8_t d, uint8_t c, uint8_t o = DOTSTAR_BRG);
112  ~Adafruit_DotStar(void);
113 
114  void begin(void);
115  void show(void);
116  void setPixelColor(uint16_t n, uint32_t c);
117  void setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b);
118  void fill(uint32_t c = 0, uint16_t first = 0, uint16_t count = 0);
119  void setBrightness(uint8_t);
120  void clear();
121  void updateLength(uint16_t n);
122  void updatePins(void);
123  void updatePins(uint8_t d, uint8_t c);
138  uint8_t *getPixels(void) const { return pixels; };
139  uint8_t getBrightness(void) const;
144  uint16_t numPixels(void) const { return numLEDs; };
145  uint32_t getPixelColor(uint16_t n) const;
158  static uint8_t sine8(uint8_t x) {
159  return pgm_read_byte(&_DotStarSineTable[x]); // 0-255 in, 0-255 out
160  }
172  static uint8_t gamma8(uint8_t x) {
173  return pgm_read_byte(&_DotStarGammaTable[x]); // 0-255 in, 0-255 out
174  }
186  static uint32_t Color(uint8_t r, uint8_t g, uint8_t b) {
187  return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b;
188  }
189  static uint32_t ColorHSV(uint16_t hue, uint8_t sat = 255, uint8_t val = 255);
190  static uint32_t gamma32(uint32_t x);
191 
192  void rainbow(uint16_t first_hue = 0, int8_t reps = 1,
193  uint8_t saturation = 255, uint8_t brightness = 255,
194  boolean gammify = true);
195 
196 private:
197  Adafruit_SPIDevice *spi_dev = NULL;
198  uint16_t numLEDs;
199  uint8_t brightness;
200  uint8_t *pixels;
201  uint8_t rOffset;
202  uint8_t gOffset;
203  uint8_t bOffset;
204 };
205 
206 #endif // _ADAFRUIT_DOT_STAR_H_
void begin(void)
Initialize Adafruit_DotStar object – sets data and clock pins to outputs and initializes hardware SP...
Definition: Adafruit_DotStar.cpp:98
uint8_t getBrightness(void) const
Retrieve the last-set brightness value for the strip.
Definition: Adafruit_DotStar.cpp:437
void updatePins(void)
Switch over to hardware SPI. DotStars must be connected to MOSI, SCK pins. Data in pixel buffer is un...
Definition: Adafruit_DotStar.cpp:111
void setBrightness(uint8_t)
Adjust output brightness. Does not immediately affect what&#39;s currently displayed on the LEDs...
Definition: Adafruit_DotStar.cpp:423
static uint8_t sine8(uint8_t x)
An 8-bit integer sine wave function, not directly compatible with standard trigonometric units like r...
Definition: Adafruit_DotStar.h:158
void updateLength(uint16_t n)
Change the length of a previously-declared Adafruit_DotStar strip object. Old data is deallocated and...
Definition: Adafruit_DotStar.cpp:141
void rainbow(uint16_t first_hue=0, int8_t reps=1, uint8_t saturation=255, uint8_t brightness=255, boolean gammify=true)
Fill DotStar strip with one or more cycles of hues. Everyone loves the rainbow swirl so much...
Definition: Adafruit_DotStar.cpp:485
void clear()
Fill the whole DotStar strip with 0 / black / off.
Definition: Adafruit_DotStar.cpp:224
void setPixelColor(uint16_t n, uint32_t c)
Set a pixel&#39;s color using a 32-bit &#39;packed&#39; RGB value.
Definition: Adafruit_DotStar.cpp:255
#define DOTSTAR_BRG
Transmit as B,R,G.
Definition: Adafruit_DotStar.h:34
uint16_t numPixels(void) const
Return the number of pixels in an Adafruit_DotStar strip object.
Definition: Adafruit_DotStar.h:144
void show(void)
Transmit pixel data in RAM to DotStars.
Definition: Adafruit_DotStar.cpp:174
uint8_t * getPixels(void) const
Get a pointer directly to the DotStar data buffer in RAM. Pixel data is stored in a device-native for...
Definition: Adafruit_DotStar.h:138
static uint8_t gamma8(uint8_t x)
An 8-bit gamma-correction function for basic pixel brightness adjustment. Makes color transitions app...
Definition: Adafruit_DotStar.h:172
Adafruit_DotStar(uint16_t n, uint8_t o=DOTSTAR_BRG, SPIClass *spi=&SPI)
DotStar constructor for hardware SPI. Must be connected to MOSI, SCK pins.
Definition: Adafruit_DotStar.cpp:56
static uint32_t Color(uint8_t r, uint8_t g, uint8_t b)
Convert separate red, green and blue values into a single "packed" 32-bit RGB color.
Definition: Adafruit_DotStar.h:186
~Adafruit_DotStar(void)
Deallocate Adafruit_DotStar object, set data and clock pins back to INPUT.
Definition: Adafruit_DotStar.cpp:88
void fill(uint32_t c=0, uint16_t first=0, uint16_t count=0)
Fill all or part of the DotStar strip with a color.
Definition: Adafruit_DotStar.cpp:275
static uint32_t ColorHSV(uint16_t hue, uint8_t sat=255, uint8_t val=255)
Convert hue, saturation and value into a packed 32-bit RGB color that can be passed to setPixelColor(...
Definition: Adafruit_DotStar.cpp:320
Class that stores state and functions for interacting with Adafruit DotStars and compatible devices...
Definition: Adafruit_DotStar.h:101
uint32_t getPixelColor(uint16_t n) const
Query the color of a previously-set pixel.
Definition: Adafruit_DotStar.cpp:402
static uint32_t gamma32(uint32_t x)
A gamma-correction function for 32-bit packed RGB colors. Makes color transitions appear more percept...
Definition: Adafruit_DotStar.cpp:452