A conversion of the excellent Adafruit WS2801 library for Arduino to work on mbed
Adafruit_WS2801.cpp@0:582e1b9c1cc1, 2013-03-08 (annotated)
- Committer:
- SomeRandomBloke
- Date:
- Fri Mar 08 08:49:04 2013 +0000
- Revision:
- 0:582e1b9c1cc1
- Child:
- 1:6ff477690983
Initial version of library after converting Arduino library to mbed library
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SomeRandomBloke | 0:582e1b9c1cc1 | 1 | #include "mbed.h" |
SomeRandomBloke | 0:582e1b9c1cc1 | 2 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 3 | #include "Adafruit_WS2801.h" |
SomeRandomBloke | 0:582e1b9c1cc1 | 4 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 5 | // Example to control WS2801-based RGB LED Modules in a strand or strip |
SomeRandomBloke | 0:582e1b9c1cc1 | 6 | // Written by Adafruit - MIT license |
SomeRandomBloke | 0:582e1b9c1cc1 | 7 | /*****************************************************************************/ |
SomeRandomBloke | 0:582e1b9c1cc1 | 8 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 9 | // Constructor for use with hardware SPI (specific clock/data pins): |
SomeRandomBloke | 0:582e1b9c1cc1 | 10 | Adafruit_WS2801::Adafruit_WS2801(uint16_t n, uint8_t order): clkpin(PTD4), datapin(PTA12) |
SomeRandomBloke | 0:582e1b9c1cc1 | 11 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 12 | rgb_order = order; |
SomeRandomBloke | 0:582e1b9c1cc1 | 13 | alloc(n); |
SomeRandomBloke | 0:582e1b9c1cc1 | 14 | updatePins(); |
SomeRandomBloke | 0:582e1b9c1cc1 | 15 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 16 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 17 | // Constructor for use with arbitrary clock/data pins: |
SomeRandomBloke | 0:582e1b9c1cc1 | 18 | Adafruit_WS2801::Adafruit_WS2801(uint16_t n, PinName dpin, PinName cpin, uint8_t order) : clkpin(cpin), datapin(dpin) |
SomeRandomBloke | 0:582e1b9c1cc1 | 19 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 20 | rgb_order = order; |
SomeRandomBloke | 0:582e1b9c1cc1 | 21 | alloc(n); |
SomeRandomBloke | 0:582e1b9c1cc1 | 22 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 23 | // updatePins(dpin, cpin); |
SomeRandomBloke | 0:582e1b9c1cc1 | 24 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 25 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 26 | // Constructor for use with a matrix configuration, specify w, h for size of matrix |
SomeRandomBloke | 0:582e1b9c1cc1 | 27 | // assumes configuration where string starts at coordinate 0,0 and continues to w-1,0, w-1,1 |
SomeRandomBloke | 0:582e1b9c1cc1 | 28 | // and on to 0,1, 0,2 and on to w-1,2 and so on. Snaking back and forth till the end. |
SomeRandomBloke | 0:582e1b9c1cc1 | 29 | // other function calls with provide access to pixels via an x,y coordinate system |
SomeRandomBloke | 0:582e1b9c1cc1 | 30 | Adafruit_WS2801::Adafruit_WS2801(uint16_t w, uint16_t h, PinName dpin, PinName cpin, uint8_t order) : clkpin(cpin), datapin(dpin) |
SomeRandomBloke | 0:582e1b9c1cc1 | 31 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 32 | rgb_order = order; |
SomeRandomBloke | 0:582e1b9c1cc1 | 33 | alloc(w * h); |
SomeRandomBloke | 0:582e1b9c1cc1 | 34 | width = w; |
SomeRandomBloke | 0:582e1b9c1cc1 | 35 | height = h; |
SomeRandomBloke | 0:582e1b9c1cc1 | 36 | // updatePins(dpin, cpin); |
SomeRandomBloke | 0:582e1b9c1cc1 | 37 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 38 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 39 | // Allocate 3 bytes per pixel, init to RGB 'off' state: |
SomeRandomBloke | 0:582e1b9c1cc1 | 40 | void Adafruit_WS2801::alloc(uint16_t n) |
SomeRandomBloke | 0:582e1b9c1cc1 | 41 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 42 | begun = false; |
SomeRandomBloke | 0:582e1b9c1cc1 | 43 | numLEDs = ((pixels = (uint8_t *)calloc(n, 3)) != NULL) ? n : 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 44 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 45 | for(int bits = 0; bits <= numLEDs*24; bits++) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 46 | clkpin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 47 | datapin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 48 | clkpin = 1; |
SomeRandomBloke | 0:582e1b9c1cc1 | 49 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 50 | clkpin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 51 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 52 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 53 | // via Michael Vogt/neophob: empty constructor is used when strand length |
SomeRandomBloke | 0:582e1b9c1cc1 | 54 | // isn't known at compile-time; situations where program config might be |
SomeRandomBloke | 0:582e1b9c1cc1 | 55 | // read from internal flash memory or an SD card, or arrive via serial |
SomeRandomBloke | 0:582e1b9c1cc1 | 56 | // command. If using this constructor, MUST follow up with updateLength() |
SomeRandomBloke | 0:582e1b9c1cc1 | 57 | // and updatePins() to establish the strand length and output pins! |
SomeRandomBloke | 0:582e1b9c1cc1 | 58 | // Also, updateOrder() to change RGB vs GRB order (RGB is default). |
SomeRandomBloke | 0:582e1b9c1cc1 | 59 | Adafruit_WS2801::Adafruit_WS2801(void) : clkpin(PTD4), datapin(PTA12) |
SomeRandomBloke | 0:582e1b9c1cc1 | 60 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 61 | begun = false; |
SomeRandomBloke | 0:582e1b9c1cc1 | 62 | numLEDs = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 63 | pixels = NULL; |
SomeRandomBloke | 0:582e1b9c1cc1 | 64 | rgb_order = WS2801_RGB; |
SomeRandomBloke | 0:582e1b9c1cc1 | 65 | updatePins(); // Must assume hardware SPI until pins are set |
SomeRandomBloke | 0:582e1b9c1cc1 | 66 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 67 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 68 | // Release memory (as needed): |
SomeRandomBloke | 0:582e1b9c1cc1 | 69 | Adafruit_WS2801::~Adafruit_WS2801(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 70 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 71 | if (pixels != NULL) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 72 | free(pixels); |
SomeRandomBloke | 0:582e1b9c1cc1 | 73 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 74 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 75 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 76 | // Activate hard/soft SPI as appropriate: |
SomeRandomBloke | 0:582e1b9c1cc1 | 77 | void Adafruit_WS2801::begin(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 78 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 79 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 80 | if(hardwareSPI == true) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 81 | startSPI(); |
SomeRandomBloke | 0:582e1b9c1cc1 | 82 | } else { |
SomeRandomBloke | 0:582e1b9c1cc1 | 83 | // pinMode(datapin, OUTPUT); |
SomeRandomBloke | 0:582e1b9c1cc1 | 84 | // pinMode(clkpin , OUTPUT); |
SomeRandomBloke | 0:582e1b9c1cc1 | 85 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 86 | begun = true; |
SomeRandomBloke | 0:582e1b9c1cc1 | 87 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 88 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 89 | // Change pin assignments post-constructor, switching to hardware SPI: |
SomeRandomBloke | 0:582e1b9c1cc1 | 90 | void Adafruit_WS2801::updatePins(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 91 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 92 | hardwareSPI = true; |
SomeRandomBloke | 0:582e1b9c1cc1 | 93 | datapin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 94 | clkpin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 95 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 96 | // If begin() was previously invoked, init the SPI hardware now: |
SomeRandomBloke | 0:582e1b9c1cc1 | 97 | if(begun == true) startSPI(); |
SomeRandomBloke | 0:582e1b9c1cc1 | 98 | // Otherwise, SPI is NOT initted until begin() is explicitly called. |
SomeRandomBloke | 0:582e1b9c1cc1 | 99 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 100 | // Note: any prior clock/data pin directions are left as-is and are |
SomeRandomBloke | 0:582e1b9c1cc1 | 101 | // NOT restored as inputs! |
SomeRandomBloke | 0:582e1b9c1cc1 | 102 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 103 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 104 | // Change pin assignments post-constructor, using arbitrary pins: |
SomeRandomBloke | 0:582e1b9c1cc1 | 105 | void Adafruit_WS2801::updatePins(PinName dpin, PinName cpin) |
SomeRandomBloke | 0:582e1b9c1cc1 | 106 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 107 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 108 | if(begun == true) { // If begin() was previously invoked... |
SomeRandomBloke | 0:582e1b9c1cc1 | 109 | // If previously using hardware SPI, turn that off: |
SomeRandomBloke | 0:582e1b9c1cc1 | 110 | // if(hardwareSPI == true) SPI.end(); |
SomeRandomBloke | 0:582e1b9c1cc1 | 111 | // Regardless, now enable output on 'soft' SPI pins: |
SomeRandomBloke | 0:582e1b9c1cc1 | 112 | // pinMode(dpin, OUTPUT); |
SomeRandomBloke | 0:582e1b9c1cc1 | 113 | // pinMode(cpin, OUTPUT); |
SomeRandomBloke | 0:582e1b9c1cc1 | 114 | } // Otherwise, pins are not set to outputs until begin() is called. |
SomeRandomBloke | 0:582e1b9c1cc1 | 115 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 116 | // Note: any prior clock/data pin directions are left as-is and are |
SomeRandomBloke | 0:582e1b9c1cc1 | 117 | // NOT restored as inputs! |
SomeRandomBloke | 0:582e1b9c1cc1 | 118 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 119 | hardwareSPI = false; |
SomeRandomBloke | 0:582e1b9c1cc1 | 120 | datapin = dpin; |
SomeRandomBloke | 0:582e1b9c1cc1 | 121 | clkpin = cpin; |
SomeRandomBloke | 0:582e1b9c1cc1 | 122 | // clkport = portOutputRegister(digitalPinToPort(cpin)); |
SomeRandomBloke | 0:582e1b9c1cc1 | 123 | // clkpinmask = digitalPinToBitMask(cpin); |
SomeRandomBloke | 0:582e1b9c1cc1 | 124 | // dataport = portOutputRegister(digitalPinToPort(dpin)); |
SomeRandomBloke | 0:582e1b9c1cc1 | 125 | // datapinmask = digitalPinToBitMask(dpin); |
SomeRandomBloke | 0:582e1b9c1cc1 | 126 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 127 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 128 | // Enable SPI hardware and set up protocol details: |
SomeRandomBloke | 0:582e1b9c1cc1 | 129 | void Adafruit_WS2801::startSPI(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 130 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 131 | // SPI.begin(); |
SomeRandomBloke | 0:582e1b9c1cc1 | 132 | // SPI.setBitOrder(MSBFIRST); |
SomeRandomBloke | 0:582e1b9c1cc1 | 133 | // SPI.setDataMode(SPI_MODE0); |
SomeRandomBloke | 0:582e1b9c1cc1 | 134 | // SPI.setClockDivider(SPI_CLOCK_DIV16); // 1 MHz max, else flicker |
SomeRandomBloke | 0:582e1b9c1cc1 | 135 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 136 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 137 | uint16_t Adafruit_WS2801::numPixels(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 138 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 139 | return numLEDs; |
SomeRandomBloke | 0:582e1b9c1cc1 | 140 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 141 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 142 | // Change strand length (see notes with empty constructor, above): |
SomeRandomBloke | 0:582e1b9c1cc1 | 143 | void Adafruit_WS2801::updateLength(uint16_t n) |
SomeRandomBloke | 0:582e1b9c1cc1 | 144 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 145 | if(pixels != NULL) free(pixels); // Free existing data (if any) |
SomeRandomBloke | 0:582e1b9c1cc1 | 146 | // Allocate new data -- note: ALL PIXELS ARE CLEARED |
SomeRandomBloke | 0:582e1b9c1cc1 | 147 | numLEDs = ((pixels = (uint8_t *)calloc(n, 3)) != NULL) ? n : 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 148 | // 'begun' state does not change -- pins retain prior modes |
SomeRandomBloke | 0:582e1b9c1cc1 | 149 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 150 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 151 | // Change RGB data order (see notes with empty constructor, above): |
SomeRandomBloke | 0:582e1b9c1cc1 | 152 | void Adafruit_WS2801::updateOrder(uint8_t order) |
SomeRandomBloke | 0:582e1b9c1cc1 | 153 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 154 | rgb_order = order; |
SomeRandomBloke | 0:582e1b9c1cc1 | 155 | // Existing LED data, if any, is NOT reformatted to new data order. |
SomeRandomBloke | 0:582e1b9c1cc1 | 156 | // Calling function should clear or fill pixel data anew. |
SomeRandomBloke | 0:582e1b9c1cc1 | 157 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 158 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 159 | void Adafruit_WS2801::show(void) |
SomeRandomBloke | 0:582e1b9c1cc1 | 160 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 161 | uint16_t i, nl3 = numLEDs * 3; // 3 bytes per LED |
SomeRandomBloke | 0:582e1b9c1cc1 | 162 | uint8_t bit; |
SomeRandomBloke | 0:582e1b9c1cc1 | 163 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 164 | // Write 24 bits per pixel: |
SomeRandomBloke | 0:582e1b9c1cc1 | 165 | // if(hardwareSPI) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 166 | // for(i=0; i<nl3; i++) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 167 | // SPDR = pixels[i]; |
SomeRandomBloke | 0:582e1b9c1cc1 | 168 | // while(!(SPSR & (1<<SPIF))); |
SomeRandomBloke | 0:582e1b9c1cc1 | 169 | // } |
SomeRandomBloke | 0:582e1b9c1cc1 | 170 | // } else { |
SomeRandomBloke | 0:582e1b9c1cc1 | 171 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 172 | /* |
SomeRandomBloke | 0:582e1b9c1cc1 | 173 | for(int LED_number = 0 ; LED_number < _STRIP_LENGTH ; LED_number++) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 174 | if(_level != 100) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 175 | int R = ((strip_colors[LED_number] & 0x00ff0000) >> 16)*_level/100; |
SomeRandomBloke | 0:582e1b9c1cc1 | 176 | int G = ((strip_colors[LED_number] & 0x0000ff00) >> 8)*_level/100; |
SomeRandomBloke | 0:582e1b9c1cc1 | 177 | int B = (strip_colors[LED_number] & 0x000000ff)*_level/100; |
SomeRandomBloke | 0:582e1b9c1cc1 | 178 | strip_colors_leveled[LED_number] = (R << 16)|(G << 8)|B; |
SomeRandomBloke | 0:582e1b9c1cc1 | 179 | } else strip_colors_leveled[LED_number] = strip_colors[LED_number]; |
SomeRandomBloke | 0:582e1b9c1cc1 | 180 | int this_led_color = strip_colors_leveled[LED_number]; //24 bits of color data |
SomeRandomBloke | 0:582e1b9c1cc1 | 181 | for(char color_bit = 23 ; color_bit != 255 ; color_bit--) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 182 | _CKI = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 183 | mask = 1 << color_bit; |
SomeRandomBloke | 0:582e1b9c1cc1 | 184 | if(this_led_color & mask)_SDI = 1; |
SomeRandomBloke | 0:582e1b9c1cc1 | 185 | else _SDI = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 186 | _CKI = 1; //Data is latched when clock goes high |
SomeRandomBloke | 0:582e1b9c1cc1 | 187 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 188 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 189 | _CKI = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 190 | wait_us(_reset_delay); //Wait for 1ms to go into reset |
SomeRandomBloke | 0:582e1b9c1cc1 | 191 | */ |
SomeRandomBloke | 0:582e1b9c1cc1 | 192 | for(i=0; i<nl3; i++ ) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 193 | for(bit=0x80; bit; bit >>= 1) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 194 | clkpin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 195 | if(pixels[i] & bit) datapin = 1; |
SomeRandomBloke | 0:582e1b9c1cc1 | 196 | else datapin = 0; |
SomeRandomBloke | 0:582e1b9c1cc1 | 197 | clkpin = 1; |
SomeRandomBloke | 0:582e1b9c1cc1 | 198 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 199 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 200 | // } |
SomeRandomBloke | 0:582e1b9c1cc1 | 201 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 202 | wait_ms(1); // Data is latched by holding clock pin low for 1 millisecond |
SomeRandomBloke | 0:582e1b9c1cc1 | 203 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 204 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 205 | // Set pixel color from separate 8-bit R, G, B components: |
SomeRandomBloke | 0:582e1b9c1cc1 | 206 | void Adafruit_WS2801::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) |
SomeRandomBloke | 0:582e1b9c1cc1 | 207 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 208 | if(n < numLEDs) { // Arrays are 0-indexed, thus NOT '<=' |
SomeRandomBloke | 0:582e1b9c1cc1 | 209 | uint8_t *p = &pixels[n * 3]; |
SomeRandomBloke | 0:582e1b9c1cc1 | 210 | // See notes later regarding color order |
SomeRandomBloke | 0:582e1b9c1cc1 | 211 | if(rgb_order == WS2801_RGB) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 212 | *p++ = r; |
SomeRandomBloke | 0:582e1b9c1cc1 | 213 | *p++ = g; |
SomeRandomBloke | 0:582e1b9c1cc1 | 214 | } else { |
SomeRandomBloke | 0:582e1b9c1cc1 | 215 | *p++ = g; |
SomeRandomBloke | 0:582e1b9c1cc1 | 216 | *p++ = r; |
SomeRandomBloke | 0:582e1b9c1cc1 | 217 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 218 | *p++ = b; |
SomeRandomBloke | 0:582e1b9c1cc1 | 219 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 220 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 221 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 222 | // Set pixel color from separate 8-bit R, G, B components using x,y coordinate system: |
SomeRandomBloke | 0:582e1b9c1cc1 | 223 | void Adafruit_WS2801::setPixelColor(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b) |
SomeRandomBloke | 0:582e1b9c1cc1 | 224 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 225 | bool evenRow = ((y % 2) == 0); |
SomeRandomBloke | 0:582e1b9c1cc1 | 226 | // calculate x offset first |
SomeRandomBloke | 0:582e1b9c1cc1 | 227 | uint16_t offset = x % width; |
SomeRandomBloke | 0:582e1b9c1cc1 | 228 | if (!evenRow) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 229 | offset = (width-1) - offset; |
SomeRandomBloke | 0:582e1b9c1cc1 | 230 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 231 | // add y offset |
SomeRandomBloke | 0:582e1b9c1cc1 | 232 | offset += y * width; |
SomeRandomBloke | 0:582e1b9c1cc1 | 233 | setPixelColor(offset, r, g, b); |
SomeRandomBloke | 0:582e1b9c1cc1 | 234 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 235 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 236 | // Set pixel color from 'packed' 32-bit RGB value: |
SomeRandomBloke | 0:582e1b9c1cc1 | 237 | void Adafruit_WS2801::setPixelColor(uint16_t n, uint32_t c) |
SomeRandomBloke | 0:582e1b9c1cc1 | 238 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 239 | if(n < numLEDs) { // Arrays are 0-indexed, thus NOT '<=' |
SomeRandomBloke | 0:582e1b9c1cc1 | 240 | uint8_t *p = &pixels[n * 3]; |
SomeRandomBloke | 0:582e1b9c1cc1 | 241 | // To keep the show() loop as simple & fast as possible, the |
SomeRandomBloke | 0:582e1b9c1cc1 | 242 | // internal color representation is native to different pixel |
SomeRandomBloke | 0:582e1b9c1cc1 | 243 | // types. For compatibility with existing code, 'packed' RGB |
SomeRandomBloke | 0:582e1b9c1cc1 | 244 | // values passed in or out are always 0xRRGGBB order. |
SomeRandomBloke | 0:582e1b9c1cc1 | 245 | if(rgb_order == WS2801_RGB) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 246 | *p++ = c >> 16; // Red |
SomeRandomBloke | 0:582e1b9c1cc1 | 247 | *p++ = c >> 8; // Green |
SomeRandomBloke | 0:582e1b9c1cc1 | 248 | } else { |
SomeRandomBloke | 0:582e1b9c1cc1 | 249 | *p++ = c >> 8; // Green |
SomeRandomBloke | 0:582e1b9c1cc1 | 250 | *p++ = c >> 16; // Red |
SomeRandomBloke | 0:582e1b9c1cc1 | 251 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 252 | *p++ = c; // Blue |
SomeRandomBloke | 0:582e1b9c1cc1 | 253 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 254 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 255 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 256 | // Set pixel color from 'packed' 32-bit RGB value using x,y coordinate system: |
SomeRandomBloke | 0:582e1b9c1cc1 | 257 | void Adafruit_WS2801::setPixelColor(uint16_t x, uint16_t y, uint32_t c) |
SomeRandomBloke | 0:582e1b9c1cc1 | 258 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 259 | bool evenRow = ((y % 2) == 0); |
SomeRandomBloke | 0:582e1b9c1cc1 | 260 | // calculate x offset first |
SomeRandomBloke | 0:582e1b9c1cc1 | 261 | uint16_t offset = x % width; |
SomeRandomBloke | 0:582e1b9c1cc1 | 262 | if (!evenRow) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 263 | offset = (width-1) - offset; |
SomeRandomBloke | 0:582e1b9c1cc1 | 264 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 265 | // add y offset |
SomeRandomBloke | 0:582e1b9c1cc1 | 266 | offset += y * width; |
SomeRandomBloke | 0:582e1b9c1cc1 | 267 | setPixelColor(offset, c); |
SomeRandomBloke | 0:582e1b9c1cc1 | 268 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 269 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 270 | // Query color from previously-set pixel (returns packed 32-bit RGB value) |
SomeRandomBloke | 0:582e1b9c1cc1 | 271 | uint32_t Adafruit_WS2801::getPixelColor(uint16_t n) |
SomeRandomBloke | 0:582e1b9c1cc1 | 272 | { |
SomeRandomBloke | 0:582e1b9c1cc1 | 273 | if(n < numLEDs) { |
SomeRandomBloke | 0:582e1b9c1cc1 | 274 | uint16_t ofs = n * 3; |
SomeRandomBloke | 0:582e1b9c1cc1 | 275 | // To keep the show() loop as simple & fast as possible, the |
SomeRandomBloke | 0:582e1b9c1cc1 | 276 | // internal color representation is native to different pixel |
SomeRandomBloke | 0:582e1b9c1cc1 | 277 | // types. For compatibility with existing code, 'packed' RGB |
SomeRandomBloke | 0:582e1b9c1cc1 | 278 | // values passed in or out are always 0xRRGGBB order. |
SomeRandomBloke | 0:582e1b9c1cc1 | 279 | return (rgb_order == WS2801_RGB) ? |
SomeRandomBloke | 0:582e1b9c1cc1 | 280 | ((uint32_t)pixels[ofs] << 16) | ((uint16_t) pixels[ofs + 1] << 8) | pixels[ofs + 2] : |
SomeRandomBloke | 0:582e1b9c1cc1 | 281 | (pixels[ofs] << 8) | ((uint32_t)pixels[ofs + 1] << 16) | pixels[ofs + 2]; |
SomeRandomBloke | 0:582e1b9c1cc1 | 282 | } |
SomeRandomBloke | 0:582e1b9c1cc1 | 283 | |
SomeRandomBloke | 0:582e1b9c1cc1 | 284 | return 0; // Pixel # is out of bounds |
SomeRandomBloke | 0:582e1b9c1cc1 | 285 | } |