Optimised fork of bikeNomad's WS2811 LED control library. Supports KL25Z and KL46Z
Fork of Multi_WS2811 by
Optimised to use far less RAM than the original.
Capable of running up to 8 strings of 240 LEDs each with plenty of RAM to spare on the KL46Z.
Should run at least three strings of 240 LEDs on the KL25Z (RAM limited)
LedStrip.cpp@0:a8535703f23b, 2014-01-04 (annotated)
- Committer:
- bikeNomad
- Date:
- Sat Jan 04 00:40:08 2014 +0000
- Revision:
- 0:a8535703f23b
- Child:
- 7:58623ad7f310
Initial revision of library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bikeNomad | 0:a8535703f23b | 1 | #include "LedStrip.h" |
bikeNomad | 0:a8535703f23b | 2 | |
bikeNomad | 0:a8535703f23b | 3 | LedStrip::LedStrip(int n) |
bikeNomad | 0:a8535703f23b | 4 | { |
bikeNomad | 0:a8535703f23b | 5 | // Allocate 3 bytes per pixel: |
bikeNomad | 0:a8535703f23b | 6 | numLEDs = n; |
bikeNomad | 0:a8535703f23b | 7 | pixels = (uint8_t *)malloc(numPixelBytes()); |
bikeNomad | 0:a8535703f23b | 8 | if (pixels) { |
bikeNomad | 0:a8535703f23b | 9 | memset(pixels, 0x00, numPixelBytes()); // Init to RGB 'off' state |
bikeNomad | 0:a8535703f23b | 10 | } |
bikeNomad | 0:a8535703f23b | 11 | } |
bikeNomad | 0:a8535703f23b | 12 | |
bikeNomad | 0:a8535703f23b | 13 | LedStrip::~LedStrip() |
bikeNomad | 0:a8535703f23b | 14 | { |
bikeNomad | 0:a8535703f23b | 15 | free(pixels); |
bikeNomad | 0:a8535703f23b | 16 | } |
bikeNomad | 0:a8535703f23b | 17 | |
bikeNomad | 0:a8535703f23b | 18 | uint32_t LedStrip::total_luminance(void) |
bikeNomad | 0:a8535703f23b | 19 | { |
bikeNomad | 0:a8535703f23b | 20 | uint32_t running_total; |
bikeNomad | 0:a8535703f23b | 21 | running_total = 0; |
bikeNomad | 0:a8535703f23b | 22 | for (int i=0; i< numPixelBytes(); i++) |
bikeNomad | 0:a8535703f23b | 23 | running_total += pixels[i]; |
bikeNomad | 0:a8535703f23b | 24 | return running_total; |
bikeNomad | 0:a8535703f23b | 25 | } |
bikeNomad | 0:a8535703f23b | 26 | |
bikeNomad | 0:a8535703f23b | 27 | // Convert R,G,B to combined 32-bit color |
bikeNomad | 0:a8535703f23b | 28 | uint32_t LedStrip::Color(uint8_t r, uint8_t g, uint8_t b) |
bikeNomad | 0:a8535703f23b | 29 | { |
bikeNomad | 0:a8535703f23b | 30 | // Take the lowest 7 bits of each value and append them end to end |
bikeNomad | 0:a8535703f23b | 31 | // We have the top bit set high (its a 'parity-like' bit in the protocol |
bikeNomad | 0:a8535703f23b | 32 | // and must be set!) |
bikeNomad | 0:a8535703f23b | 33 | return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b; |
bikeNomad | 0:a8535703f23b | 34 | } |
bikeNomad | 0:a8535703f23b | 35 | |
bikeNomad | 0:a8535703f23b | 36 | // store the rgb component in our array |
bikeNomad | 0:a8535703f23b | 37 | void LedStrip::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) |
bikeNomad | 0:a8535703f23b | 38 | { |
bikeNomad | 0:a8535703f23b | 39 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
bikeNomad | 0:a8535703f23b | 40 | |
bikeNomad | 0:a8535703f23b | 41 | pixels[n*3 ] = g; |
bikeNomad | 0:a8535703f23b | 42 | pixels[n*3+1] = r; |
bikeNomad | 0:a8535703f23b | 43 | pixels[n*3+2] = b; |
bikeNomad | 0:a8535703f23b | 44 | } |
bikeNomad | 0:a8535703f23b | 45 | |
bikeNomad | 0:a8535703f23b | 46 | void LedStrip::setPixelR(uint16_t n, uint8_t r) |
bikeNomad | 0:a8535703f23b | 47 | { |
bikeNomad | 0:a8535703f23b | 48 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
bikeNomad | 0:a8535703f23b | 49 | |
bikeNomad | 0:a8535703f23b | 50 | pixels[n*3+1] = r; |
bikeNomad | 0:a8535703f23b | 51 | } |
bikeNomad | 0:a8535703f23b | 52 | |
bikeNomad | 0:a8535703f23b | 53 | void LedStrip::setPixelG(uint16_t n, uint8_t g) |
bikeNomad | 0:a8535703f23b | 54 | { |
bikeNomad | 0:a8535703f23b | 55 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
bikeNomad | 0:a8535703f23b | 56 | |
bikeNomad | 0:a8535703f23b | 57 | pixels[n*3] = g; |
bikeNomad | 0:a8535703f23b | 58 | } |
bikeNomad | 0:a8535703f23b | 59 | |
bikeNomad | 0:a8535703f23b | 60 | void LedStrip::setPixelB(uint16_t n, uint8_t b) |
bikeNomad | 0:a8535703f23b | 61 | { |
bikeNomad | 0:a8535703f23b | 62 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
bikeNomad | 0:a8535703f23b | 63 | |
bikeNomad | 0:a8535703f23b | 64 | pixels[n*3+2] = b; |
bikeNomad | 0:a8535703f23b | 65 | } |
bikeNomad | 0:a8535703f23b | 66 | |
bikeNomad | 0:a8535703f23b | 67 | void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t n) |
bikeNomad | 0:a8535703f23b | 68 | { |
bikeNomad | 0:a8535703f23b | 69 | if (n >= numLEDs) return; |
bikeNomad | 0:a8535703f23b | 70 | memcpy(pixels, buffer, (size_t) (n*3)); |
bikeNomad | 0:a8535703f23b | 71 | } |
bikeNomad | 0:a8535703f23b | 72 | |
bikeNomad | 0:a8535703f23b | 73 | void LedStrip::setPixelColor(uint16_t n, uint32_t c) |
bikeNomad | 0:a8535703f23b | 74 | { |
bikeNomad | 0:a8535703f23b | 75 | if (n >= numLEDs) return; // '>=' because arrays are 0-indexed |
bikeNomad | 0:a8535703f23b | 76 | |
bikeNomad | 0:a8535703f23b | 77 | pixels[n*3 ] = (c >> 16); |
bikeNomad | 0:a8535703f23b | 78 | pixels[n*3+1] = (c >> 8); |
bikeNomad | 0:a8535703f23b | 79 | pixels[n*3+2] = c; |
bikeNomad | 0:a8535703f23b | 80 | } |