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@7:58623ad7f310, 2014-04-02 (annotated)
- Committer:
- Tomo2k
- Date:
- Wed Apr 02 13:22:25 2014 +0000
- Revision:
- 7:58623ad7f310
- Parent:
- 0:a8535703f23b
Updated comments, parameter names and added example usage
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 | |
Tomo2k | 7:58623ad7f310 | 3 | LedStrip::LedStrip(uint16_t pixelCount) : |
Tomo2k | 7:58623ad7f310 | 4 | numLEDs(pixelCount) |
bikeNomad | 0:a8535703f23b | 5 | { |
Tomo2k | 7:58623ad7f310 | 6 | // Allocate 3 bytes per pixel: |
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 | } |
Tomo2k | 7:58623ad7f310 | 17 | |
Tomo2k | 7:58623ad7f310 | 18 | uint32_t LedStrip::total_luminance() |
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 |
Tomo2k | 7:58623ad7f310 | 28 | uint32_t LedStrip::Color(uint8_t red, uint8_t green, uint8_t blue) |
bikeNomad | 0:a8535703f23b | 29 | { |
Tomo2k | 7:58623ad7f310 | 30 | return ((uint32_t)green << 16) | ((uint32_t)red << 8) | (uint32_t)blue; |
bikeNomad | 0:a8535703f23b | 31 | } |
bikeNomad | 0:a8535703f23b | 32 | |
Tomo2k | 7:58623ad7f310 | 33 | // Store the rgb component in our array |
Tomo2k | 7:58623ad7f310 | 34 | void LedStrip::setPixelColor(uint16_t pixNum, uint8_t red, uint8_t green, uint8_t blue) |
bikeNomad | 0:a8535703f23b | 35 | { |
Tomo2k | 7:58623ad7f310 | 36 | if (pixNum < numLEDs) { |
Tomo2k | 7:58623ad7f310 | 37 | pixels[pixNum*3 ] = green; |
Tomo2k | 7:58623ad7f310 | 38 | pixels[pixNum*3+1] = red; |
Tomo2k | 7:58623ad7f310 | 39 | pixels[pixNum*3+2] = blue; |
Tomo2k | 7:58623ad7f310 | 40 | } |
bikeNomad | 0:a8535703f23b | 41 | } |
bikeNomad | 0:a8535703f23b | 42 | |
Tomo2k | 7:58623ad7f310 | 43 | void LedStrip::setPixelR(uint16_t pixNum, uint8_t red) |
bikeNomad | 0:a8535703f23b | 44 | { |
Tomo2k | 7:58623ad7f310 | 45 | if (pixNum < numLEDs) { |
Tomo2k | 7:58623ad7f310 | 46 | pixels[pixNum*3+1] = red; |
Tomo2k | 7:58623ad7f310 | 47 | } |
bikeNomad | 0:a8535703f23b | 48 | } |
bikeNomad | 0:a8535703f23b | 49 | |
Tomo2k | 7:58623ad7f310 | 50 | void LedStrip::setPixelG(uint16_t pixNum, uint8_t green) |
bikeNomad | 0:a8535703f23b | 51 | { |
Tomo2k | 7:58623ad7f310 | 52 | if (pixNum < numLEDs) { |
Tomo2k | 7:58623ad7f310 | 53 | pixels[pixNum*3] = green; |
Tomo2k | 7:58623ad7f310 | 54 | } |
bikeNomad | 0:a8535703f23b | 55 | } |
bikeNomad | 0:a8535703f23b | 56 | |
Tomo2k | 7:58623ad7f310 | 57 | void LedStrip::setPixelB(uint16_t pixNum, uint8_t blue) |
bikeNomad | 0:a8535703f23b | 58 | { |
Tomo2k | 7:58623ad7f310 | 59 | if (pixNum < numLEDs) { |
Tomo2k | 7:58623ad7f310 | 60 | pixels[pixNum*3+2] = blue; |
Tomo2k | 7:58623ad7f310 | 61 | } |
bikeNomad | 0:a8535703f23b | 62 | } |
bikeNomad | 0:a8535703f23b | 63 | |
Tomo2k | 7:58623ad7f310 | 64 | void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t count) |
bikeNomad | 0:a8535703f23b | 65 | { |
Tomo2k | 7:58623ad7f310 | 66 | if (count > numLEDs) return; |
Tomo2k | 7:58623ad7f310 | 67 | memcpy(pixels, buffer, (size_t) (count*3)); |
bikeNomad | 0:a8535703f23b | 68 | } |
bikeNomad | 0:a8535703f23b | 69 | |
Tomo2k | 7:58623ad7f310 | 70 | void LedStrip::setPixelColor(uint16_t pixNum, uint32_t color) |
bikeNomad | 0:a8535703f23b | 71 | { |
Tomo2k | 7:58623ad7f310 | 72 | if (pixNum < numLEDs) { |
Tomo2k | 7:58623ad7f310 | 73 | pixels[pixNum*3 ] = (color >> 16); |
Tomo2k | 7:58623ad7f310 | 74 | pixels[pixNum*3+1] = (color >> 8); |
Tomo2k | 7:58623ad7f310 | 75 | pixels[pixNum*3+2] = color; |
Tomo2k | 7:58623ad7f310 | 76 | } |
bikeNomad | 0:a8535703f23b | 77 | } |