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.
Dependencies: PixelArray WS2812 mbed
main.cpp
00001 #include "mbed.h" 00002 #include "WS2812.h" 00003 #include "PixelArray.h" 00004 00005 #define WS2812_BUF 77 //number of LEDs in the array 00006 #define NUM_COLORS 2 //number of colors to store in the array 00007 00008 DigitalOut usrLed(LED1); 00009 PixelArray px(WS2812_BUF); 00010 00011 // See the program page for information on the timing numbers 00012 WS2812 ws(D9, WS2812_BUF, 6,17,9,14); //nucleo-f411re 00013 00014 int color_set(uint8_t red,uint8_t green, uint8_t blue) 00015 { 00016 return ((red<<16) + (green<<8) + blue); 00017 } 00018 00019 // 0 <= stepNumber <= lastStepNumber 00020 int interpolate(int startValue, int endValue, int stepNumber, int lastStepNumber) 00021 { 00022 return (endValue - startValue) * stepNumber / lastStepNumber + startValue; 00023 } 00024 00025 int main() 00026 { 00027 uint8_t ir = 0; 00028 uint8_t ig = 0; 00029 uint8_t ib = 0; 00030 00031 ws.useII(WS2812::PER_PIXEL); // use per-pixel intensity scaling 00032 00033 // set up the colours we want to draw with 00034 int colorbuf[NUM_COLORS] = {0xff0000,0x0000ff}; 00035 00036 //get starting RGB components for interpolation 00037 std::size_t c1 = colorbuf[0]; 00038 std::size_t r1 = (c1 & 0xff0000) >> 16; 00039 std::size_t g1 = (c1 & 0x00ff00) >> 8; 00040 std::size_t b1 = (c1 & 0x0000ff); 00041 00042 //get ending RGB components for interpolation 00043 std::size_t c2 = colorbuf[1]; 00044 std::size_t r2 = (c2 & 0xff0000) >> 16; 00045 std::size_t g2 = (c2 & 0x00ff00) >> 8; 00046 std::size_t b2 = (c2 & 0x0000ff); 00047 00048 for (int i = 0; i <= WS2812_BUF; i++) 00049 { 00050 ir = interpolate(r1, r2, i, WS2812_BUF); 00051 ig = interpolate(g1, g2, i, WS2812_BUF); 00052 ib = interpolate(b1, b2, i, WS2812_BUF); 00053 00054 //write the color value for each pixel 00055 px.Set(i, color_set(ir,ig,ib)); 00056 00057 //write the II value for each pixel 00058 px.SetI(i, 32); 00059 } 00060 00061 for (int i = WS2812_BUF; i >= 0; i--) 00062 { 00063 ws.write(px.getBuf()); 00064 } 00065 00066 usrLed = 1; 00067 }
Generated on Mon Jul 18 2022 23:40:05 by
1.7.2