share
Dependencies: RotationMat_ SDFileSystem mbed
Adafruit_DotStar.h@0:cbd607fc4caa, 2016-02-03 (annotated)
- Committer:
- shuhei2306
- Date:
- Wed Feb 03 11:39:23 2016 +0000
- Revision:
- 0:cbd607fc4caa
share;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
shuhei2306 | 0:cbd607fc4caa | 1 | #include "mbed.h" |
shuhei2306 | 0:cbd607fc4caa | 2 | |
shuhei2306 | 0:cbd607fc4caa | 3 | // Color-order flag for LED pixels (optional extra parameter to constructor): |
shuhei2306 | 0:cbd607fc4caa | 4 | // Bits 0,1 = R index (0-2), bits 2,3 = G index, bits 4,5 = B index |
shuhei2306 | 0:cbd607fc4caa | 5 | #define DOTSTAR_BGR (0 | (1 << 2) | (2 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 6 | #define DOTSTAR_GBR (0 | (2 << 2) | (1 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 7 | #define DOTSTAR_BRG (1 | (0 << 2) | (2 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 8 | #define DOTSTAR_RBG (2 | (0 << 2) | (1 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 9 | #define DOTSTAR_GRB (1 | (2 << 2) | (0 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 10 | #define DOTSTAR_RGB (2 | (1 << 2) | (0 << 4)) |
shuhei2306 | 0:cbd607fc4caa | 11 | #define DOTSTAR_MONO 0 // Single-color strip WIP DO NOT USE YET |
shuhei2306 | 0:cbd607fc4caa | 12 | SPI spi(p5,p6,p7); |
shuhei2306 | 0:cbd607fc4caa | 13 | |
shuhei2306 | 0:cbd607fc4caa | 14 | class Adafruit_DotStar |
shuhei2306 | 0:cbd607fc4caa | 15 | { |
shuhei2306 | 0:cbd607fc4caa | 16 | |
shuhei2306 | 0:cbd607fc4caa | 17 | public: |
shuhei2306 | 0:cbd607fc4caa | 18 | |
shuhei2306 | 0:cbd607fc4caa | 19 | Adafruit_DotStar(uint16_t n, uint8_t o=DOTSTAR_RGB); |
shuhei2306 | 0:cbd607fc4caa | 20 | ~Adafruit_DotStar(void); |
shuhei2306 | 0:cbd607fc4caa | 21 | void |
shuhei2306 | 0:cbd607fc4caa | 22 | begin(void), // Prime pins/SPI for output |
shuhei2306 | 0:cbd607fc4caa | 23 | clear(), // Set all pixel data to zero |
shuhei2306 | 0:cbd607fc4caa | 24 | setBrightness(uint8_t), // Set global brightness 0-255 |
shuhei2306 | 0:cbd607fc4caa | 25 | setPixelColor(uint16_t n, uint32_t c), |
shuhei2306 | 0:cbd607fc4caa | 26 | setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b), |
shuhei2306 | 0:cbd607fc4caa | 27 | show(void), // Issue color data to strip |
shuhei2306 | 0:cbd607fc4caa | 28 | updateLength(uint16_t n); // Change length |
shuhei2306 | 0:cbd607fc4caa | 29 | uint32_t |
shuhei2306 | 0:cbd607fc4caa | 30 | Color(uint8_t r, uint8_t g, uint8_t b), // R,G,B to 32-bit color |
shuhei2306 | 0:cbd607fc4caa | 31 | getPixelColor(uint16_t n) const; // Return 32-bit pixel color |
shuhei2306 | 0:cbd607fc4caa | 32 | uint16_t |
shuhei2306 | 0:cbd607fc4caa | 33 | numPixels(void); // Return number of pixels |
shuhei2306 | 0:cbd607fc4caa | 34 | uint8_t |
shuhei2306 | 0:cbd607fc4caa | 35 | getBrightness(void) const, // Return global brightness |
shuhei2306 | 0:cbd607fc4caa | 36 | *getPixels(void) const; // Return pixel data pointer |
shuhei2306 | 0:cbd607fc4caa | 37 | |
shuhei2306 | 0:cbd607fc4caa | 38 | private: |
shuhei2306 | 0:cbd607fc4caa | 39 | |
shuhei2306 | 0:cbd607fc4caa | 40 | uint16_t |
shuhei2306 | 0:cbd607fc4caa | 41 | numLEDs; // Number of pixels |
shuhei2306 | 0:cbd607fc4caa | 42 | uint8_t |
shuhei2306 | 0:cbd607fc4caa | 43 | brightness, // Global brightness setting |
shuhei2306 | 0:cbd607fc4caa | 44 | *pixels, // LED RGB values (3 bytes ea.) |
shuhei2306 | 0:cbd607fc4caa | 45 | rOffset, // Index of red in 3-byte pixel |
shuhei2306 | 0:cbd607fc4caa | 46 | gOffset, // Index of green byte |
shuhei2306 | 0:cbd607fc4caa | 47 | bOffset; // Index of blue byte |
shuhei2306 | 0:cbd607fc4caa | 48 | |
shuhei2306 | 0:cbd607fc4caa | 49 | void |
shuhei2306 | 0:cbd607fc4caa | 50 | spi_init(void); |
shuhei2306 | 0:cbd607fc4caa | 51 | }; |
shuhei2306 | 0:cbd607fc4caa | 52 | |
shuhei2306 | 0:cbd607fc4caa | 53 | Adafruit_DotStar::Adafruit_DotStar(uint16_t n, uint8_t o): |
shuhei2306 | 0:cbd607fc4caa | 54 | brightness(0), pixels(NULL), |
shuhei2306 | 0:cbd607fc4caa | 55 | rOffset(o & 3), gOffset((o >> 2) & 3), bOffset((o >> 4) & 3) |
shuhei2306 | 0:cbd607fc4caa | 56 | { |
shuhei2306 | 0:cbd607fc4caa | 57 | updateLength(n); |
shuhei2306 | 0:cbd607fc4caa | 58 | } |
shuhei2306 | 0:cbd607fc4caa | 59 | |
shuhei2306 | 0:cbd607fc4caa | 60 | Adafruit_DotStar::~Adafruit_DotStar(void) // Destructor |
shuhei2306 | 0:cbd607fc4caa | 61 | { |
shuhei2306 | 0:cbd607fc4caa | 62 | if(pixels) delete[] pixels; |
shuhei2306 | 0:cbd607fc4caa | 63 | } |
shuhei2306 | 0:cbd607fc4caa | 64 | |
shuhei2306 | 0:cbd607fc4caa | 65 | void Adafruit_DotStar::begin(void) // Initialize SPI |
shuhei2306 | 0:cbd607fc4caa | 66 | { |
shuhei2306 | 0:cbd607fc4caa | 67 | spi_init(); |
shuhei2306 | 0:cbd607fc4caa | 68 | } |
shuhei2306 | 0:cbd607fc4caa | 69 | |
shuhei2306 | 0:cbd607fc4caa | 70 | void Adafruit_DotStar::updateLength(uint16_t n) |
shuhei2306 | 0:cbd607fc4caa | 71 | { |
shuhei2306 | 0:cbd607fc4caa | 72 | if(pixels) delete[] pixels; |
shuhei2306 | 0:cbd607fc4caa | 73 | uint16_t bytes = (rOffset == gOffset) ? |
shuhei2306 | 0:cbd607fc4caa | 74 | n + ((n + 3) / 4) : // MONO: 10 bits/pixel, round up to next byte |
shuhei2306 | 0:cbd607fc4caa | 75 | n * 3; // COLOR: 3 bytes/pixel |
shuhei2306 | 0:cbd607fc4caa | 76 | if(pixels = new uint8_t[bytes]) { |
shuhei2306 | 0:cbd607fc4caa | 77 | numLEDs = n; |
shuhei2306 | 0:cbd607fc4caa | 78 | clear(); |
shuhei2306 | 0:cbd607fc4caa | 79 | } else { |
shuhei2306 | 0:cbd607fc4caa | 80 | numLEDs = 0; |
shuhei2306 | 0:cbd607fc4caa | 81 | } |
shuhei2306 | 0:cbd607fc4caa | 82 | } |
shuhei2306 | 0:cbd607fc4caa | 83 | |
shuhei2306 | 0:cbd607fc4caa | 84 | // SPI STUFF --------------------------------------------------------------- |
shuhei2306 | 0:cbd607fc4caa | 85 | void Adafruit_DotStar::spi_init(void) |
shuhei2306 | 0:cbd607fc4caa | 86 | { |
shuhei2306 | 0:cbd607fc4caa | 87 | // Setup the spi for 8 bit data, |
shuhei2306 | 0:cbd607fc4caa | 88 | // second edge capture, with a 1MHz clock rate |
shuhei2306 | 0:cbd607fc4caa | 89 | spi.format(8, 0); // mode0 POL:positive PHA:ラッチ先行 |
shuhei2306 | 0:cbd607fc4caa | 90 | // spi.frequency(19000000); |
shuhei2306 | 0:cbd607fc4caa | 91 | spi.frequency(1000000); |
shuhei2306 | 0:cbd607fc4caa | 92 | } |
shuhei2306 | 0:cbd607fc4caa | 93 | |
shuhei2306 | 0:cbd607fc4caa | 94 | void Adafruit_DotStar::show(void) |
shuhei2306 | 0:cbd607fc4caa | 95 | { |
shuhei2306 | 0:cbd607fc4caa | 96 | |
shuhei2306 | 0:cbd607fc4caa | 97 | if(!pixels) return; |
shuhei2306 | 0:cbd607fc4caa | 98 | |
shuhei2306 | 0:cbd607fc4caa | 99 | uint8_t *ptr = pixels, i; // -> LED data |
shuhei2306 | 0:cbd607fc4caa | 100 | uint16_t n = numLEDs; // Counter |
shuhei2306 | 0:cbd607fc4caa | 101 | uint16_t b16 = (uint16_t)brightness; // Type-convert for fixed-point math |
shuhei2306 | 0:cbd607fc4caa | 102 | |
shuhei2306 | 0:cbd607fc4caa | 103 | for(i=0; i<4; i++) spi.write(0); // Start-frame marker |
shuhei2306 | 0:cbd607fc4caa | 104 | if(brightness) { // Scale pixel brightness on output |
shuhei2306 | 0:cbd607fc4caa | 105 | do { // For each pixel... |
shuhei2306 | 0:cbd607fc4caa | 106 | spi.write(0xFF); // Pixel start |
shuhei2306 | 0:cbd607fc4caa | 107 | for(i=0; i<3; i++) spi.write((*ptr++ * b16) >> 8); // Scale, write |
shuhei2306 | 0:cbd607fc4caa | 108 | } while(--n); |
shuhei2306 | 0:cbd607fc4caa | 109 | } else { // Full brightness (no scaling) |
shuhei2306 | 0:cbd607fc4caa | 110 | do { // For each pixel... |
shuhei2306 | 0:cbd607fc4caa | 111 | spi.write(0xFF); // Pixel start |
shuhei2306 | 0:cbd607fc4caa | 112 | for(i=0; i<3; i++) spi.write(*ptr++); // R,G,B |
shuhei2306 | 0:cbd607fc4caa | 113 | } while(--n); |
shuhei2306 | 0:cbd607fc4caa | 114 | } |
shuhei2306 | 0:cbd607fc4caa | 115 | for(i=0; i<4; i++) spi.write(0xFF); // End-frame marker (see note above) |
shuhei2306 | 0:cbd607fc4caa | 116 | |
shuhei2306 | 0:cbd607fc4caa | 117 | } |
shuhei2306 | 0:cbd607fc4caa | 118 | |
shuhei2306 | 0:cbd607fc4caa | 119 | void Adafruit_DotStar::clear() // Write 0s (off) to full pixel buffer |
shuhei2306 | 0:cbd607fc4caa | 120 | { |
shuhei2306 | 0:cbd607fc4caa | 121 | memset(pixels, 0, (rOffset == gOffset) ? |
shuhei2306 | 0:cbd607fc4caa | 122 | numLEDs + ((numLEDs + 3) / 4) : // MONO: 10 bits/pixel |
shuhei2306 | 0:cbd607fc4caa | 123 | numLEDs * 3); // COLOR: 3 bytes/pixel |
shuhei2306 | 0:cbd607fc4caa | 124 | } |
shuhei2306 | 0:cbd607fc4caa | 125 | |
shuhei2306 | 0:cbd607fc4caa | 126 | // Set pixel color, separate R,G,B values (0-255 ea.) |
shuhei2306 | 0:cbd607fc4caa | 127 | void Adafruit_DotStar::setPixelColor( |
shuhei2306 | 0:cbd607fc4caa | 128 | uint16_t n, uint8_t r, uint8_t g, uint8_t b) |
shuhei2306 | 0:cbd607fc4caa | 129 | { |
shuhei2306 | 0:cbd607fc4caa | 130 | if(n < numLEDs) { |
shuhei2306 | 0:cbd607fc4caa | 131 | uint8_t *p = &pixels[n * 3]; |
shuhei2306 | 0:cbd607fc4caa | 132 | p[rOffset] = r; |
shuhei2306 | 0:cbd607fc4caa | 133 | p[gOffset] = g; |
shuhei2306 | 0:cbd607fc4caa | 134 | p[bOffset] = b; |
shuhei2306 | 0:cbd607fc4caa | 135 | } |
shuhei2306 | 0:cbd607fc4caa | 136 | } |
shuhei2306 | 0:cbd607fc4caa | 137 | |
shuhei2306 | 0:cbd607fc4caa | 138 | // Set pixel color, 'packed' RGB value (0x000000 - 0xFFFFFF) |
shuhei2306 | 0:cbd607fc4caa | 139 | void Adafruit_DotStar::setPixelColor(uint16_t n, uint32_t c) |
shuhei2306 | 0:cbd607fc4caa | 140 | { |
shuhei2306 | 0:cbd607fc4caa | 141 | if(n < numLEDs) { |
shuhei2306 | 0:cbd607fc4caa | 142 | uint8_t *p = &pixels[n * 3]; |
shuhei2306 | 0:cbd607fc4caa | 143 | p[rOffset] = (uint8_t)(c >> 16); |
shuhei2306 | 0:cbd607fc4caa | 144 | p[gOffset] = (uint8_t)(c >> 8); |
shuhei2306 | 0:cbd607fc4caa | 145 | p[bOffset] = (uint8_t)c; |
shuhei2306 | 0:cbd607fc4caa | 146 | } |
shuhei2306 | 0:cbd607fc4caa | 147 | } |
shuhei2306 | 0:cbd607fc4caa | 148 | |
shuhei2306 | 0:cbd607fc4caa | 149 | // Convert separate R,G,B to packed value |
shuhei2306 | 0:cbd607fc4caa | 150 | uint32_t Adafruit_DotStar::Color(uint8_t r, uint8_t g, uint8_t b) |
shuhei2306 | 0:cbd607fc4caa | 151 | { |
shuhei2306 | 0:cbd607fc4caa | 152 | return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; |
shuhei2306 | 0:cbd607fc4caa | 153 | } |
shuhei2306 | 0:cbd607fc4caa | 154 | |
shuhei2306 | 0:cbd607fc4caa | 155 | // Read color from previously-set pixel, returns packed RGB value. |
shuhei2306 | 0:cbd607fc4caa | 156 | uint32_t Adafruit_DotStar::getPixelColor(uint16_t n) const |
shuhei2306 | 0:cbd607fc4caa | 157 | { |
shuhei2306 | 0:cbd607fc4caa | 158 | if(n >= numLEDs) return 0; |
shuhei2306 | 0:cbd607fc4caa | 159 | uint8_t *p = &pixels[n * 3]; |
shuhei2306 | 0:cbd607fc4caa | 160 | return ((uint32_t)p[rOffset] << 16) | |
shuhei2306 | 0:cbd607fc4caa | 161 | ((uint32_t)p[gOffset] << 8) | |
shuhei2306 | 0:cbd607fc4caa | 162 | (uint32_t)p[bOffset]; |
shuhei2306 | 0:cbd607fc4caa | 163 | } |
shuhei2306 | 0:cbd607fc4caa | 164 | |
shuhei2306 | 0:cbd607fc4caa | 165 | uint16_t Adafruit_DotStar::numPixels(void) // Ret. strip length |
shuhei2306 | 0:cbd607fc4caa | 166 | { |
shuhei2306 | 0:cbd607fc4caa | 167 | return numLEDs; |
shuhei2306 | 0:cbd607fc4caa | 168 | } |
shuhei2306 | 0:cbd607fc4caa | 169 | |
shuhei2306 | 0:cbd607fc4caa | 170 | // Set global strip brightness. This does not have an immediate effect; |
shuhei2306 | 0:cbd607fc4caa | 171 | // must be followed by a call to show(). Not a fan of this...for various |
shuhei2306 | 0:cbd607fc4caa | 172 | // reasons I think it's better handled in one's sketch, but it's here for |
shuhei2306 | 0:cbd607fc4caa | 173 | // parity with the NeoPixel library. Good news is that brightness setting |
shuhei2306 | 0:cbd607fc4caa | 174 | // in this library is 'non destructive' -- it's applied as color data is |
shuhei2306 | 0:cbd607fc4caa | 175 | // being issued to the strip, not during setPixel(), and also means that |
shuhei2306 | 0:cbd607fc4caa | 176 | // getPixelColor() returns the exact value originally stored. |
shuhei2306 | 0:cbd607fc4caa | 177 | void Adafruit_DotStar::setBrightness(uint8_t b) |
shuhei2306 | 0:cbd607fc4caa | 178 | { |
shuhei2306 | 0:cbd607fc4caa | 179 | // Stored brightness value is different than what's passed. This |
shuhei2306 | 0:cbd607fc4caa | 180 | // optimizes the actual scaling math later, allowing a fast 8x8-bit |
shuhei2306 | 0:cbd607fc4caa | 181 | // multiply and taking the MSB. 'brightness' is a uint8_t, adding 1 |
shuhei2306 | 0:cbd607fc4caa | 182 | // here may (intentionally) roll over...so 0 = max brightness (color |
shuhei2306 | 0:cbd607fc4caa | 183 | // values are interpreted literally; no scaling), 1 = min brightness |
shuhei2306 | 0:cbd607fc4caa | 184 | // (off), 255 = just below max brightness. |
shuhei2306 | 0:cbd607fc4caa | 185 | brightness = b + 1; |
shuhei2306 | 0:cbd607fc4caa | 186 | } |
shuhei2306 | 0:cbd607fc4caa | 187 | |
shuhei2306 | 0:cbd607fc4caa | 188 | uint8_t Adafruit_DotStar::getBrightness(void) const |
shuhei2306 | 0:cbd607fc4caa | 189 | { |
shuhei2306 | 0:cbd607fc4caa | 190 | return brightness - 1; // Reverse above operation |
shuhei2306 | 0:cbd607fc4caa | 191 | } |
shuhei2306 | 0:cbd607fc4caa | 192 | |
shuhei2306 | 0:cbd607fc4caa | 193 | // Return pointer to the library's pixel data buffer. Use carefully, |
shuhei2306 | 0:cbd607fc4caa | 194 | // much opportunity for mayhem. It's mostly for code that needs fast |
shuhei2306 | 0:cbd607fc4caa | 195 | // transfers, e.g. SD card to LEDs. Color data is in BGR order. |
shuhei2306 | 0:cbd607fc4caa | 196 | uint8_t *Adafruit_DotStar::getPixels(void) const |
shuhei2306 | 0:cbd607fc4caa | 197 | { |
shuhei2306 | 0:cbd607fc4caa | 198 | return pixels; |
shuhei2306 | 0:cbd607fc4caa | 199 | } |