The port of adafruit dotstar library for arduino https://github.com/adafruit/Adafruit_DotStar
Fork of DotStar by
DotStar.cpp@2:51722225d46c, 2017-01-04 (annotated)
- Committer:
- pwalne
- Date:
- Wed Jan 04 20:53:35 2017 +0000
- Revision:
- 2:51722225d46c
- Parent:
- 1:d09c288b8eb3
Changes for mbed LPC1768
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
hideakitai | 0:bae97ff743a6 | 1 | /*------------------------------------------------------------------------ |
hideakitai | 0:bae97ff743a6 | 2 | Arduino library to control Adafruit Dot Star addressable RGB LEDs. |
hideakitai | 0:bae97ff743a6 | 3 | |
hideakitai | 0:bae97ff743a6 | 4 | Written by Limor Fried and Phil Burgess for Adafruit Industries. |
hideakitai | 0:bae97ff743a6 | 5 | |
hideakitai | 0:bae97ff743a6 | 6 | Adafruit invests time and resources providing this open source code, |
hideakitai | 0:bae97ff743a6 | 7 | please support Adafruit and open-source hardware by purchasing products |
hideakitai | 0:bae97ff743a6 | 8 | from Adafruit! |
hideakitai | 0:bae97ff743a6 | 9 | |
hideakitai | 0:bae97ff743a6 | 10 | ------------------------------------------------------------------------ |
hideakitai | 0:bae97ff743a6 | 11 | This file is part of the Adafruit Dot Star library. |
hideakitai | 0:bae97ff743a6 | 12 | |
hideakitai | 0:bae97ff743a6 | 13 | Adafruit Dot Star is free software: you can redistribute it and/or |
hideakitai | 0:bae97ff743a6 | 14 | modify it under the terms of the GNU Lesser General Public License |
hideakitai | 0:bae97ff743a6 | 15 | as published by the Free Software Foundation, either version 3 of |
hideakitai | 0:bae97ff743a6 | 16 | the License, or (at your option) any later version. |
hideakitai | 0:bae97ff743a6 | 17 | |
hideakitai | 0:bae97ff743a6 | 18 | Adafruit Dot Star is distributed in the hope that it will be useful, |
hideakitai | 0:bae97ff743a6 | 19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
hideakitai | 0:bae97ff743a6 | 20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
hideakitai | 0:bae97ff743a6 | 21 | GNU Lesser General Public License for more details. |
hideakitai | 0:bae97ff743a6 | 22 | |
hideakitai | 0:bae97ff743a6 | 23 | You should have received a copy of the GNU Lesser General Public |
hideakitai | 0:bae97ff743a6 | 24 | License along with DotStar. If not, see <http://www.gnu.org/licenses/>. |
hideakitai | 0:bae97ff743a6 | 25 | ------------------------------------------------------------------------*/ |
hideakitai | 0:bae97ff743a6 | 26 | |
hideakitai | 1:d09c288b8eb3 | 27 | #include "mbed.h" |
hideakitai | 0:bae97ff743a6 | 28 | #include "DotStar.h" |
hideakitai | 0:bae97ff743a6 | 29 | |
hideakitai | 0:bae97ff743a6 | 30 | // Constructor for hardware SPI -- must connect to MOSI, SCK pins |
hideakitai | 1:d09c288b8eb3 | 31 | Adafruit_DotStar::Adafruit_DotStar(uint16_t n, PinName miso, PinName mosi, PinName sclk, int hz, uint8_t o) |
hideakitai | 1:d09c288b8eb3 | 32 | : numLEDs(n) |
hideakitai | 1:d09c288b8eb3 | 33 | , dataPin(USE_HW_SPI) |
hideakitai | 1:d09c288b8eb3 | 34 | , clockPin(USE_HW_SPI) |
hideakitai | 1:d09c288b8eb3 | 35 | , spi(NULL) |
hideakitai | 1:d09c288b8eb3 | 36 | , miso_(miso) |
hideakitai | 1:d09c288b8eb3 | 37 | , mosi_(mosi) |
hideakitai | 1:d09c288b8eb3 | 38 | , sclk_(sclk) |
hideakitai | 1:d09c288b8eb3 | 39 | , data_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 40 | , sclk_out(NULL) |
pwalne | 2:51722225d46c | 41 | , port(Port0) |
hideakitai | 1:d09c288b8eb3 | 42 | , port_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 43 | , d_mask(0) |
hideakitai | 1:d09c288b8eb3 | 44 | , c_mask(0) |
hideakitai | 1:d09c288b8eb3 | 45 | , b_use_port(false) |
hideakitai | 1:d09c288b8eb3 | 46 | , brightness(0) |
hideakitai | 1:d09c288b8eb3 | 47 | , pixels(NULL) |
hideakitai | 1:d09c288b8eb3 | 48 | , rOffset(o & 3) |
hideakitai | 1:d09c288b8eb3 | 49 | , gOffset((o >> 2) & 3) |
hideakitai | 1:d09c288b8eb3 | 50 | , bOffset((o >> 4) & 3) |
hideakitai | 0:bae97ff743a6 | 51 | { |
hideakitai | 1:d09c288b8eb3 | 52 | updateLength(n); |
hideakitai | 0:bae97ff743a6 | 53 | } |
hideakitai | 0:bae97ff743a6 | 54 | |
hideakitai | 0:bae97ff743a6 | 55 | // Constructor for 'soft' (bitbang) SPI -- any two pins can be used |
hideakitai | 1:d09c288b8eb3 | 56 | Adafruit_DotStar::Adafruit_DotStar(uint16_t n, PinName data, PinName clock, uint8_t o) |
hideakitai | 1:d09c288b8eb3 | 57 | : dataPin(data) |
hideakitai | 1:d09c288b8eb3 | 58 | , clockPin(clock) |
hideakitai | 1:d09c288b8eb3 | 59 | , spi(NULL) |
hideakitai | 1:d09c288b8eb3 | 60 | , miso_(NC) |
hideakitai | 1:d09c288b8eb3 | 61 | , mosi_(NC) |
hideakitai | 1:d09c288b8eb3 | 62 | , sclk_(NC) |
hideakitai | 1:d09c288b8eb3 | 63 | , data_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 64 | , sclk_out(NULL) |
pwalne | 2:51722225d46c | 65 | , port(Port0) |
hideakitai | 1:d09c288b8eb3 | 66 | , port_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 67 | , d_mask(0) |
hideakitai | 1:d09c288b8eb3 | 68 | , c_mask(0) |
hideakitai | 1:d09c288b8eb3 | 69 | , b_use_port(false) |
hideakitai | 1:d09c288b8eb3 | 70 | , brightness(0) |
hideakitai | 1:d09c288b8eb3 | 71 | , pixels(NULL) |
hideakitai | 1:d09c288b8eb3 | 72 | , rOffset(o & 3) |
hideakitai | 1:d09c288b8eb3 | 73 | , gOffset((o >> 2) & 3) |
hideakitai | 1:d09c288b8eb3 | 74 | , bOffset((o >> 4) & 3) |
hideakitai | 0:bae97ff743a6 | 75 | { |
hideakitai | 1:d09c288b8eb3 | 76 | updateLength(n); |
hideakitai | 1:d09c288b8eb3 | 77 | } |
hideakitai | 1:d09c288b8eb3 | 78 | |
hideakitai | 1:d09c288b8eb3 | 79 | // Constructor for 'soft' (bitbang) SPI -- any two pins can be used |
hideakitai | 1:d09c288b8eb3 | 80 | Adafruit_DotStar::Adafruit_DotStar(uint16_t n, PortName p, int dmask, int cmask, uint8_t o) |
hideakitai | 1:d09c288b8eb3 | 81 | : dataPin(NC) |
hideakitai | 1:d09c288b8eb3 | 82 | , clockPin(NC) |
hideakitai | 1:d09c288b8eb3 | 83 | , spi(NULL) |
hideakitai | 1:d09c288b8eb3 | 84 | , miso_(NC) |
hideakitai | 1:d09c288b8eb3 | 85 | , mosi_(NC) |
hideakitai | 1:d09c288b8eb3 | 86 | , sclk_(NC) |
hideakitai | 1:d09c288b8eb3 | 87 | , data_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 88 | , sclk_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 89 | , port(p) |
hideakitai | 1:d09c288b8eb3 | 90 | , port_out(NULL) |
hideakitai | 1:d09c288b8eb3 | 91 | , d_mask(dmask) |
hideakitai | 1:d09c288b8eb3 | 92 | , c_mask(cmask) |
hideakitai | 1:d09c288b8eb3 | 93 | , b_use_port(true) |
hideakitai | 1:d09c288b8eb3 | 94 | , brightness(0) |
hideakitai | 1:d09c288b8eb3 | 95 | , pixels(NULL) |
hideakitai | 1:d09c288b8eb3 | 96 | , rOffset(o & 3) |
hideakitai | 1:d09c288b8eb3 | 97 | , gOffset((o >> 2) & 3) |
hideakitai | 1:d09c288b8eb3 | 98 | , bOffset((o >> 4) & 3) |
hideakitai | 1:d09c288b8eb3 | 99 | { |
hideakitai | 1:d09c288b8eb3 | 100 | updateLength(n); |
hideakitai | 0:bae97ff743a6 | 101 | } |
hideakitai | 0:bae97ff743a6 | 102 | |
hideakitai | 0:bae97ff743a6 | 103 | Adafruit_DotStar::~Adafruit_DotStar(void) { // Destructor |
hideakitai | 1:d09c288b8eb3 | 104 | if(pixels) free(pixels); |
hideakitai | 1:d09c288b8eb3 | 105 | if(dataPin == USE_HW_SPI) hw_spi_end(); |
hideakitai | 1:d09c288b8eb3 | 106 | else sw_spi_end(); |
hideakitai | 0:bae97ff743a6 | 107 | } |
hideakitai | 0:bae97ff743a6 | 108 | |
hideakitai | 0:bae97ff743a6 | 109 | void Adafruit_DotStar::begin(void) { // Initialize SPI |
hideakitai | 1:d09c288b8eb3 | 110 | if(dataPin == USE_HW_SPI) hw_spi_init(); |
hideakitai | 1:d09c288b8eb3 | 111 | else sw_spi_init(); |
hideakitai | 1:d09c288b8eb3 | 112 | } |
hideakitai | 1:d09c288b8eb3 | 113 | |
hideakitai | 1:d09c288b8eb3 | 114 | // Change to hardware SPI -- must connect to MOSI, SCK pins |
hideakitai | 1:d09c288b8eb3 | 115 | void Adafruit_DotStar::updatePins(void) { |
hideakitai | 1:d09c288b8eb3 | 116 | sw_spi_end(); |
hideakitai | 1:d09c288b8eb3 | 117 | dataPin = USE_HW_SPI; |
hideakitai | 1:d09c288b8eb3 | 118 | hw_spi_init(); |
hideakitai | 1:d09c288b8eb3 | 119 | } |
hideakitai | 1:d09c288b8eb3 | 120 | |
hideakitai | 1:d09c288b8eb3 | 121 | // Change to 'soft' (bitbang) SPI -- any two pins can be used |
hideakitai | 1:d09c288b8eb3 | 122 | void Adafruit_DotStar::updatePins(PinName data, PinName clock) { |
hideakitai | 1:d09c288b8eb3 | 123 | hw_spi_end(); |
hideakitai | 1:d09c288b8eb3 | 124 | dataPin = data; |
hideakitai | 1:d09c288b8eb3 | 125 | clockPin = clock; |
hideakitai | 1:d09c288b8eb3 | 126 | sw_spi_init(); |
hideakitai | 0:bae97ff743a6 | 127 | } |
hideakitai | 0:bae97ff743a6 | 128 | |
hideakitai | 0:bae97ff743a6 | 129 | // Length can be changed post-constructor for similar reasons (sketch |
hideakitai | 0:bae97ff743a6 | 130 | // config not hardcoded). But DON'T use this for "recycling" strip RAM... |
hideakitai | 0:bae97ff743a6 | 131 | // all that reallocation is likely to fragment and eventually fail. |
hideakitai | 0:bae97ff743a6 | 132 | // Instead, set length once to longest strip. |
hideakitai | 0:bae97ff743a6 | 133 | void Adafruit_DotStar::updateLength(uint16_t n) { |
hideakitai | 1:d09c288b8eb3 | 134 | if(pixels) free(pixels); |
hideakitai | 1:d09c288b8eb3 | 135 | uint16_t bytes = (rOffset == gOffset) ? |
hideakitai | 1:d09c288b8eb3 | 136 | n + ((n + 3) / 4) : // MONO: 10 bits/pixel, round up to next byte |
hideakitai | 1:d09c288b8eb3 | 137 | n * 3; // COLOR: 3 bytes/pixel |
hideakitai | 1:d09c288b8eb3 | 138 | if((pixels = (uint8_t *)malloc(bytes))) { |
hideakitai | 1:d09c288b8eb3 | 139 | numLEDs = n; |
hideakitai | 1:d09c288b8eb3 | 140 | clear(); |
hideakitai | 1:d09c288b8eb3 | 141 | } else { |
hideakitai | 1:d09c288b8eb3 | 142 | numLEDs = 0; |
hideakitai | 1:d09c288b8eb3 | 143 | } |
hideakitai | 0:bae97ff743a6 | 144 | } |
hideakitai | 0:bae97ff743a6 | 145 | |
hideakitai | 0:bae97ff743a6 | 146 | // SPI STUFF --------------------------------------------------------------- |
hideakitai | 0:bae97ff743a6 | 147 | |
hideakitai | 0:bae97ff743a6 | 148 | void Adafruit_DotStar::hw_spi_init(void) { // Initialize hardware SPI |
hideakitai | 1:d09c288b8eb3 | 149 | spi = new SPI(mosi_, miso_, sclk_); // mosi, miso, sclk |
hideakitai | 1:d09c288b8eb3 | 150 | spi->format(8, 0); // 8bit, mode:0 |
hideakitai | 1:d09c288b8eb3 | 151 | spi->frequency(8000000); // Hz |
hideakitai | 0:bae97ff743a6 | 152 | } |
hideakitai | 0:bae97ff743a6 | 153 | |
hideakitai | 0:bae97ff743a6 | 154 | void Adafruit_DotStar::hw_spi_end(void) { // Stop hardware SPI |
hideakitai | 1:d09c288b8eb3 | 155 | if (spi) delete spi; |
hideakitai | 1:d09c288b8eb3 | 156 | } |
hideakitai | 1:d09c288b8eb3 | 157 | |
hideakitai | 1:d09c288b8eb3 | 158 | void Adafruit_DotStar::sw_spi_init(void) { // Init 'soft' (bitbang) SPI |
hideakitai | 1:d09c288b8eb3 | 159 | if (b_use_port) { |
hideakitai | 1:d09c288b8eb3 | 160 | port_out = new PortOut(port, (d_mask | c_mask)); |
hideakitai | 1:d09c288b8eb3 | 161 | } else { |
hideakitai | 1:d09c288b8eb3 | 162 | data_out = new DigitalOut(dataPin); |
hideakitai | 1:d09c288b8eb3 | 163 | sclk_out = new DigitalOut(clockPin); |
hideakitai | 1:d09c288b8eb3 | 164 | } |
hideakitai | 0:bae97ff743a6 | 165 | } |
hideakitai | 0:bae97ff743a6 | 166 | |
hideakitai | 1:d09c288b8eb3 | 167 | void Adafruit_DotStar::sw_spi_end() { // Stop 'soft' SPI |
hideakitai | 1:d09c288b8eb3 | 168 | if (port_out) delete(port_out); |
hideakitai | 1:d09c288b8eb3 | 169 | if (data_out) delete(data_out); |
hideakitai | 1:d09c288b8eb3 | 170 | if (sclk_out) delete(sclk_out); |
hideakitai | 1:d09c288b8eb3 | 171 | } |
hideakitai | 0:bae97ff743a6 | 172 | |
hideakitai | 1:d09c288b8eb3 | 173 | //inline void Adafruit_DotStar::sw_spi_out(uint8_t n) { // Bitbang SPI write |
hideakitai | 1:d09c288b8eb3 | 174 | // if (b_use_port) { |
hideakitai | 1:d09c288b8eb3 | 175 | // for(uint8_t i=8; i--; n <<= 1) { |
hideakitai | 1:d09c288b8eb3 | 176 | // int mask = (n & 0x80) ? (d_mask | c_mask) : c_mask; |
hideakitai | 1:d09c288b8eb3 | 177 | // *port_out = mask; |
hideakitai | 1:d09c288b8eb3 | 178 | // *port_out = (mask & ~c_mask); |
hideakitai | 1:d09c288b8eb3 | 179 | // } |
hideakitai | 1:d09c288b8eb3 | 180 | // } else { |
hideakitai | 1:d09c288b8eb3 | 181 | // for(uint8_t i=8; i--; n <<= 1) { |
hideakitai | 1:d09c288b8eb3 | 182 | // if(n & 0x80) *data_out = 1; |
hideakitai | 1:d09c288b8eb3 | 183 | // else *data_out = 0; |
hideakitai | 1:d09c288b8eb3 | 184 | // *sclk_out = 1; |
hideakitai | 1:d09c288b8eb3 | 185 | // *sclk_out = 0; |
hideakitai | 1:d09c288b8eb3 | 186 | // } |
hideakitai | 1:d09c288b8eb3 | 187 | // } |
hideakitai | 1:d09c288b8eb3 | 188 | //} |
hideakitai | 0:bae97ff743a6 | 189 | |
hideakitai | 0:bae97ff743a6 | 190 | /* ISSUE DATA TO LED STRIP ------------------------------------------------- |
hideakitai | 0:bae97ff743a6 | 191 | |
hideakitai | 0:bae97ff743a6 | 192 | Although the LED driver has an additional per-pixel 5-bit brightness |
hideakitai | 0:bae97ff743a6 | 193 | setting, it is NOT used or supported here because it's a brain-dead |
hideakitai | 0:bae97ff743a6 | 194 | misfeature that's counter to the whole point of Dot Stars, which is to |
hideakitai | 0:bae97ff743a6 | 195 | have a much faster PWM rate than NeoPixels. It gates the high-speed |
hideakitai | 0:bae97ff743a6 | 196 | PWM output through a second, much slower PWM (about 400 Hz), rendering |
hideakitai | 0:bae97ff743a6 | 197 | it useless for POV. This brings NOTHING to the table that can't be |
hideakitai | 0:bae97ff743a6 | 198 | already handled better in one's sketch code. If you really can't live |
hideakitai | 0:bae97ff743a6 | 199 | without this abomination, you can fork the library and add it for your |
hideakitai | 0:bae97ff743a6 | 200 | own use, but any pull requests for this will NOT be merged, nuh uh! |
hideakitai | 0:bae97ff743a6 | 201 | */ |
hideakitai | 0:bae97ff743a6 | 202 | |
hideakitai | 1:d09c288b8eb3 | 203 | //inline void Adafruit_DotStar::show() { |
hideakitai | 1:d09c288b8eb3 | 204 | // if(!pixels) return; |
hideakitai | 1:d09c288b8eb3 | 205 | // |
hideakitai | 1:d09c288b8eb3 | 206 | // uint8_t *ptr = pixels; // -> LED data |
hideakitai | 1:d09c288b8eb3 | 207 | // uint16_t n = numLEDs; // Counter |
hideakitai | 1:d09c288b8eb3 | 208 | // uint16_t b16 = (uint16_t)brightness; // Type-convert for fixed-point math |
hideakitai | 1:d09c288b8eb3 | 209 | // |
hideakitai | 1:d09c288b8eb3 | 210 | // if(dataPin == USE_HW_SPI) { |
hideakitai | 1:d09c288b8eb3 | 211 | // |
hideakitai | 1:d09c288b8eb3 | 212 | // for(size_t i=0; i<4; i++) spi->write(0x00); // 4 byte start-frame marker |
hideakitai | 1:d09c288b8eb3 | 213 | // if(brightness) { // Scale pixel brightness on output |
hideakitai | 1:d09c288b8eb3 | 214 | // for (size_t i=n; i>0; i--) { |
hideakitai | 1:d09c288b8eb3 | 215 | // spi->write(0xFF); // Pixel start |
hideakitai | 1:d09c288b8eb3 | 216 | // for(size_t j=0; j<3; j++) spi->write((*ptr++ * b16) >> 8); // Scale, write RGB |
hideakitai | 1:d09c288b8eb3 | 217 | // } |
hideakitai | 1:d09c288b8eb3 | 218 | // } else { // Full brightness (no scaling) |
hideakitai | 1:d09c288b8eb3 | 219 | // for (size_t i=n; i>0; i--) { |
hideakitai | 1:d09c288b8eb3 | 220 | // spi->write(0xFF); // Pixel start |
hideakitai | 1:d09c288b8eb3 | 221 | // for(size_t j=0; j<3; j++) spi->write(*ptr++); // Write R,G,B |
hideakitai | 1:d09c288b8eb3 | 222 | // } |
hideakitai | 1:d09c288b8eb3 | 223 | // } |
hideakitai | 1:d09c288b8eb3 | 224 | // // Four end-frame bytes are seemingly indistinguishable from a white |
hideakitai | 1:d09c288b8eb3 | 225 | // // pixel, and empirical testing suggests it can be left out...but it's |
hideakitai | 1:d09c288b8eb3 | 226 | // // always a good idea to follow the datasheet, in case future hardware |
hideakitai | 1:d09c288b8eb3 | 227 | // // revisions are more strict (e.g. might mandate use of end-frame |
hideakitai | 1:d09c288b8eb3 | 228 | // // before start-frame marker). i.e. let's not remove this. |
hideakitai | 1:d09c288b8eb3 | 229 | // for(size_t i=0; i<4; i++) spi->write(0xFF); |
hideakitai | 1:d09c288b8eb3 | 230 | // |
hideakitai | 1:d09c288b8eb3 | 231 | // } else { // Soft (bitbang) SPI |
hideakitai | 1:d09c288b8eb3 | 232 | // |
hideakitai | 1:d09c288b8eb3 | 233 | // for(size_t i=0; i<4; i++) sw_spi_out(0); // Start-frame marker |
hideakitai | 1:d09c288b8eb3 | 234 | // if(brightness) { // Scale pixel brightness on output |
hideakitai | 1:d09c288b8eb3 | 235 | // do { // For each pixel... |
hideakitai | 1:d09c288b8eb3 | 236 | // sw_spi_out(0xFF); // Pixel start |
hideakitai | 1:d09c288b8eb3 | 237 | // for(size_t i=0; i<3; i++) sw_spi_out((*ptr++ * b16) >> 8); // Scale, write |
hideakitai | 1:d09c288b8eb3 | 238 | // } while(--n); |
hideakitai | 1:d09c288b8eb3 | 239 | // } else { // Full brightness (no scaling) |
hideakitai | 1:d09c288b8eb3 | 240 | // do { // For each pixel... |
hideakitai | 1:d09c288b8eb3 | 241 | // sw_spi_out(0xFF); // Pixel start |
hideakitai | 1:d09c288b8eb3 | 242 | // for(size_t i=0; i<3; i++) sw_spi_out(*ptr++); // R,G,B |
hideakitai | 1:d09c288b8eb3 | 243 | // } while(--n); |
hideakitai | 1:d09c288b8eb3 | 244 | // } |
hideakitai | 1:d09c288b8eb3 | 245 | // for(size_t i=0; i<4; i++) sw_spi_out(0xFF); // End-frame marker (see note above) |
hideakitai | 1:d09c288b8eb3 | 246 | // } |
hideakitai | 1:d09c288b8eb3 | 247 | //} |
hideakitai | 0:bae97ff743a6 | 248 | |
hideakitai | 1:d09c288b8eb3 | 249 | inline void Adafruit_DotStar::clear() { // Write 0s (off) to full pixel buffer |
hideakitai | 1:d09c288b8eb3 | 250 | memset(pixels, 0, (rOffset == gOffset) ? |
hideakitai | 1:d09c288b8eb3 | 251 | numLEDs + ((numLEDs + 3) / 4) : // MONO: 10 bits/pixel |
hideakitai | 1:d09c288b8eb3 | 252 | numLEDs * 3); // COLOR: 3 bytes/pixel |
hideakitai | 0:bae97ff743a6 | 253 | } |
hideakitai | 0:bae97ff743a6 | 254 | |
hideakitai | 0:bae97ff743a6 | 255 | // Set pixel color, separate R,G,B values (0-255 ea.) |
hideakitai | 1:d09c288b8eb3 | 256 | //inline void Adafruit_DotStar::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) |
hideakitai | 1:d09c288b8eb3 | 257 | //{ |
hideakitai | 1:d09c288b8eb3 | 258 | // if(n < numLEDs) { |
hideakitai | 1:d09c288b8eb3 | 259 | // uint8_t *p = &pixels[n * 3]; |
hideakitai | 1:d09c288b8eb3 | 260 | // p[rOffset] = r; |
hideakitai | 1:d09c288b8eb3 | 261 | // p[gOffset] = g; |
hideakitai | 1:d09c288b8eb3 | 262 | // p[bOffset] = b; |
hideakitai | 1:d09c288b8eb3 | 263 | // } |
hideakitai | 1:d09c288b8eb3 | 264 | //} |
hideakitai | 0:bae97ff743a6 | 265 | |
hideakitai | 0:bae97ff743a6 | 266 | // Set pixel color, 'packed' RGB value (0x000000 - 0xFFFFFF) |
hideakitai | 1:d09c288b8eb3 | 267 | inline void Adafruit_DotStar::setPixelColor(uint16_t n, uint32_t c) { |
hideakitai | 1:d09c288b8eb3 | 268 | if(n < numLEDs) { |
hideakitai | 1:d09c288b8eb3 | 269 | uint8_t *p = &pixels[n * 3]; |
hideakitai | 1:d09c288b8eb3 | 270 | p[rOffset] = (uint8_t)(c >> 16); |
hideakitai | 1:d09c288b8eb3 | 271 | p[gOffset] = (uint8_t)(c >> 8); |
hideakitai | 1:d09c288b8eb3 | 272 | p[bOffset] = (uint8_t)c; |
hideakitai | 1:d09c288b8eb3 | 273 | } |
hideakitai | 0:bae97ff743a6 | 274 | } |
hideakitai | 0:bae97ff743a6 | 275 | |
hideakitai | 0:bae97ff743a6 | 276 | // Convert separate R,G,B to packed value |
hideakitai | 1:d09c288b8eb3 | 277 | inline uint32_t Adafruit_DotStar::Color(uint8_t r, uint8_t g, uint8_t b) { |
hideakitai | 1:d09c288b8eb3 | 278 | return ((uint32_t)r << 16) | ((uint32_t)g << 8) | b; |
hideakitai | 0:bae97ff743a6 | 279 | } |
hideakitai | 0:bae97ff743a6 | 280 | |
hideakitai | 0:bae97ff743a6 | 281 | // Read color from previously-set pixel, returns packed RGB value. |
hideakitai | 1:d09c288b8eb3 | 282 | inline uint32_t Adafruit_DotStar::getPixelColor(uint16_t n) const { |
hideakitai | 1:d09c288b8eb3 | 283 | if(n >= numLEDs) return 0; |
hideakitai | 1:d09c288b8eb3 | 284 | uint8_t *p = &pixels[n * 3]; |
hideakitai | 1:d09c288b8eb3 | 285 | return ((uint32_t)p[rOffset] << 16) | |
hideakitai | 1:d09c288b8eb3 | 286 | ((uint32_t)p[gOffset] << 8) | |
hideakitai | 1:d09c288b8eb3 | 287 | (uint32_t)p[bOffset]; |
hideakitai | 0:bae97ff743a6 | 288 | } |
hideakitai | 0:bae97ff743a6 | 289 | |
hideakitai | 1:d09c288b8eb3 | 290 | inline uint16_t Adafruit_DotStar::numPixels(void) { // Ret. strip length |
hideakitai | 1:d09c288b8eb3 | 291 | return numLEDs; |
hideakitai | 0:bae97ff743a6 | 292 | } |
hideakitai | 0:bae97ff743a6 | 293 | |
hideakitai | 0:bae97ff743a6 | 294 | // Set global strip brightness. This does not have an immediate effect; |
hideakitai | 0:bae97ff743a6 | 295 | // must be followed by a call to show(). Not a fan of this...for various |
hideakitai | 0:bae97ff743a6 | 296 | // reasons I think it's better handled in one's sketch, but it's here for |
hideakitai | 0:bae97ff743a6 | 297 | // parity with the NeoPixel library. Good news is that brightness setting |
hideakitai | 0:bae97ff743a6 | 298 | // in this library is 'non destructive' -- it's applied as color data is |
hideakitai | 0:bae97ff743a6 | 299 | // being issued to the strip, not during setPixel(), and also means that |
hideakitai | 0:bae97ff743a6 | 300 | // getPixelColor() returns the exact value originally stored. |
hideakitai | 1:d09c288b8eb3 | 301 | inline void Adafruit_DotStar::setBrightness(uint8_t b) { |
hideakitai | 1:d09c288b8eb3 | 302 | // Stored brightness value is different than what's passed. This |
hideakitai | 1:d09c288b8eb3 | 303 | // optimizes the actual scaling math later, allowing a fast 8x8-bit |
hideakitai | 1:d09c288b8eb3 | 304 | // multiply and taking the MSB. 'brightness' is a uint8_t, adding 1 |
hideakitai | 1:d09c288b8eb3 | 305 | // here may (intentionally) roll over...so 0 = max brightness (color |
hideakitai | 1:d09c288b8eb3 | 306 | // values are interpreted literally; no scaling), 1 = min brightness |
hideakitai | 1:d09c288b8eb3 | 307 | // (off), 255 = just below max brightness. |
hideakitai | 1:d09c288b8eb3 | 308 | brightness = b + 1; |
hideakitai | 0:bae97ff743a6 | 309 | } |
hideakitai | 0:bae97ff743a6 | 310 | |
hideakitai | 1:d09c288b8eb3 | 311 | inline uint8_t Adafruit_DotStar::getBrightness(void) const { |
hideakitai | 1:d09c288b8eb3 | 312 | return brightness - 1; // Reverse above operation |
hideakitai | 0:bae97ff743a6 | 313 | } |
hideakitai | 0:bae97ff743a6 | 314 | |
hideakitai | 0:bae97ff743a6 | 315 | // Return pointer to the library's pixel data buffer. Use carefully, |
hideakitai | 0:bae97ff743a6 | 316 | // much opportunity for mayhem. It's mostly for code that needs fast |
hideakitai | 0:bae97ff743a6 | 317 | // transfers, e.g. SD card to LEDs. Color data is in BGR order. |
hideakitai | 1:d09c288b8eb3 | 318 | inline uint8_t *Adafruit_DotStar::getPixels(void) const { |
hideakitai | 1:d09c288b8eb3 | 319 | return pixels; |
hideakitai | 0:bae97ff743a6 | 320 | } |