Robert Bui / Multi_WS2811

Fork of Multi_WS2811 by Richard Thompson

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LedStrip.cpp Source File

LedStrip.cpp

00001 #include "LedStrip.h"
00002 
00003 LedStrip::LedStrip(uint16_t pixelCount) :
00004     numLEDs(pixelCount)
00005 {
00006     // Allocate 3 bytes per pixel:
00007     pixels = (uint8_t *)malloc(numPixelBytes());
00008     if (pixels) {
00009         memset(pixels, 0x00, numPixelBytes()); // Init to RGB 'off' state
00010     }
00011 }
00012 
00013 LedStrip::~LedStrip()
00014 {
00015     free(pixels);
00016 }
00017 
00018 uint32_t LedStrip::total_luminance()
00019 {
00020     uint32_t running_total;
00021     running_total = 0;
00022     for (int i=0; i< numPixelBytes(); i++)
00023         running_total += pixels[i];
00024     return running_total;
00025 }
00026 
00027 // Convert R,G,B to combined 32-bit color
00028 uint32_t LedStrip::Color(uint8_t red, uint8_t green, uint8_t blue)
00029 {
00030     return ((uint32_t)green << 16) | ((uint32_t)red << 8) | (uint32_t)blue;
00031 }
00032 
00033 // Store the rgb component in our array
00034 void LedStrip::setPixelColor(uint16_t pixNum, uint8_t red, uint8_t green, uint8_t blue)
00035 {
00036     if (pixNum < numLEDs) {
00037         pixels[pixNum*3  ] = green;
00038         pixels[pixNum*3+1] = red;
00039         pixels[pixNum*3+2] = blue;
00040     }
00041 }
00042 
00043 void LedStrip::setPixelR(uint16_t pixNum, uint8_t red)
00044 {
00045     if (pixNum < numLEDs) {
00046         pixels[pixNum*3+1] = red;
00047     }
00048 }
00049 
00050 void LedStrip::setPixelG(uint16_t pixNum, uint8_t green)
00051 {
00052     if (pixNum < numLEDs) {
00053         pixels[pixNum*3] = green;
00054     }
00055 }
00056 
00057 void LedStrip::setPixelB(uint16_t pixNum, uint8_t blue)
00058 {
00059     if (pixNum < numLEDs) {
00060         pixels[pixNum*3+2] = blue;
00061     }
00062 }
00063 
00064 void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t count)
00065 {
00066     if (count > numLEDs) return;
00067     memcpy(pixels, buffer, (size_t) (count*3));
00068 }
00069 
00070 void LedStrip::setPixelColor(uint16_t pixNum, uint32_t color)
00071 {
00072     if (pixNum < numLEDs) {
00073         pixels[pixNum*3  ] = (color >> 16);
00074         pixels[pixNum*3+1] = (color >>  8);
00075         pixels[pixNum*3+2] =  color;
00076     }
00077 }