David Grayson / Mbed 2 deprecated LedStripRainbow

Dependencies:   PololuLedStrip mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "PololuLedStrip.h"
00003 
00004 PololuLedStrip ledStrip(D8);
00005 
00006 #define LED_COUNT 60
00007 rgb_color colors[LED_COUNT];
00008 
00009 Timer timer;
00010 
00011 // Converts a color from the HSV representation to RGB.
00012 rgb_color hsvToRgb(float h, float s, float v)
00013 {
00014     int i = floor(h * 6);
00015     float f = h * 6 - i;
00016     float p = v * (1 - s);
00017     float q = v * (1 - f * s);
00018     float t = v * (1 - (1 - f) * s);
00019     float r = 0, g = 0, b = 0;
00020     switch(i % 6){
00021         case 0: r = v; g = t; b = p; break;
00022         case 1: r = q; g = v; b = p; break;
00023         case 2: r = p; g = v; b = t; break;
00024         case 3: r = p; g = q; b = v; break;
00025         case 4: r = t; g = p; b = v; break;
00026         case 5: r = v; g = p; b = q; break;
00027     }
00028     return (rgb_color){r * 255, g * 255, b * 255};
00029 }
00030 
00031 int main()
00032 {
00033     timer.start();
00034 
00035     while(1)
00036     {
00037         // Update the colors array.
00038         uint32_t time = timer.read_ms();       
00039         for(int i = 0; i < LED_COUNT; i++)
00040         {
00041             uint8_t phase = (time >> 4) - (i << 2);
00042             colors[i] = hsvToRgb(phase / 256.0, 1.0, 1.0);
00043         }
00044     
00045         // Send the colors to the LED strip.
00046         ledStrip.write(colors, LED_COUNT);
00047     }
00048 }