Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Multi_WS2811 by
LedStrip.cpp
00001 #include "LedStrip.h" 00002 00003 LedStrip::LedStrip(int n) 00004 { 00005 // Allocate 3 bytes per pixel: 00006 numLEDs = n; 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(void) 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 r, uint8_t g, uint8_t b) 00029 { 00030 // Take the lowest 7 bits of each value and append them end to end 00031 // We have the top bit set high (its a 'parity-like' bit in the protocol 00032 // and must be set!) 00033 return ((uint32_t)g << 16) | ((uint32_t)r << 8) | (uint32_t)b; 00034 } 00035 00036 // store the rgb component in our array 00037 void LedStrip::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) 00038 { 00039 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed 00040 00041 pixels[n*3 ] = g; 00042 pixels[n*3+1] = r; 00043 pixels[n*3+2] = b; 00044 } 00045 00046 void LedStrip::setPixelR(uint16_t n, uint8_t r) 00047 { 00048 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed 00049 00050 pixels[n*3+1] = r; 00051 } 00052 00053 void LedStrip::setPixelG(uint16_t n, uint8_t g) 00054 { 00055 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed 00056 00057 pixels[n*3] = g; 00058 } 00059 00060 void LedStrip::setPixelB(uint16_t n, uint8_t b) 00061 { 00062 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed 00063 00064 pixels[n*3+2] = b; 00065 } 00066 00067 void LedStrip::setPackedPixels(uint8_t * buffer, uint32_t n) 00068 { 00069 if (n >= numLEDs) return; 00070 memcpy(pixels, buffer, (size_t) (n*3)); 00071 } 00072 00073 void LedStrip::setPixelColor(uint16_t n, uint32_t c) 00074 { 00075 if (n >= numLEDs) return; // '>=' because arrays are 0-indexed 00076 00077 pixels[n*3 ] = (c >> 16); 00078 pixels[n*3+1] = (c >> 8); 00079 pixels[n*3+2] = c; 00080 }
Generated on Sat Jul 16 2022 15:45:20 by
1.7.2
