![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
RGB LEDs Control
Dependencies: mbed QEI PololuLedStrip
main.cpp@1:3d89bcd21002, 2013-02-28 (annotated)
- Committer:
- DavidEGrayson
- Date:
- Thu Feb 28 00:04:20 2013 +0000
- Revision:
- 1:3d89bcd21002
- Parent:
- 0:a3a9c486fc68
- Child:
- 2:69cc4148a7fa
Not much;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DavidEGrayson | 0:a3a9c486fc68 | 1 | #include "mbed.h" |
DavidEGrayson | 0:a3a9c486fc68 | 2 | #include "PololuLedStrip.h" |
DavidEGrayson | 0:a3a9c486fc68 | 3 | |
DavidEGrayson | 0:a3a9c486fc68 | 4 | PololuLedStrip ledStrip(p8); |
DavidEGrayson | 0:a3a9c486fc68 | 5 | |
DavidEGrayson | 0:a3a9c486fc68 | 6 | #define LED_COUNT 60 |
DavidEGrayson | 0:a3a9c486fc68 | 7 | rgb_color colors[LED_COUNT]; |
DavidEGrayson | 0:a3a9c486fc68 | 8 | |
DavidEGrayson | 0:a3a9c486fc68 | 9 | Timer timer; |
DavidEGrayson | 0:a3a9c486fc68 | 10 | |
DavidEGrayson | 0:a3a9c486fc68 | 11 | // Converts a color from the HSV representation to RGB. |
DavidEGrayson | 0:a3a9c486fc68 | 12 | rgb_color hsvToRgb(float h, float s, float v) |
DavidEGrayson | 0:a3a9c486fc68 | 13 | { |
DavidEGrayson | 0:a3a9c486fc68 | 14 | int i = floor(h * 6); |
DavidEGrayson | 0:a3a9c486fc68 | 15 | float f = h * 6 - i; |
DavidEGrayson | 0:a3a9c486fc68 | 16 | float p = v * (1 - s); |
DavidEGrayson | 0:a3a9c486fc68 | 17 | float q = v * (1 - f * s); |
DavidEGrayson | 0:a3a9c486fc68 | 18 | float t = v * (1 - (1 - f) * s); |
DavidEGrayson | 0:a3a9c486fc68 | 19 | float r, g, b; |
DavidEGrayson | 0:a3a9c486fc68 | 20 | switch(i % 6){ |
DavidEGrayson | 0:a3a9c486fc68 | 21 | case 0: r = v; g = t; b = p; break; |
DavidEGrayson | 0:a3a9c486fc68 | 22 | case 1: r = q; g = v; b = p; break; |
DavidEGrayson | 0:a3a9c486fc68 | 23 | case 2: r = p; g = v; b = t; break; |
DavidEGrayson | 0:a3a9c486fc68 | 24 | case 3: r = p; g = q; b = v; break; |
DavidEGrayson | 0:a3a9c486fc68 | 25 | case 4: r = t; g = p; b = v; break; |
DavidEGrayson | 0:a3a9c486fc68 | 26 | case 5: r = v; g = p; b = q; break; |
DavidEGrayson | 0:a3a9c486fc68 | 27 | } |
DavidEGrayson | 0:a3a9c486fc68 | 28 | return (rgb_color){r * 255, g * 255, b * 255}; |
DavidEGrayson | 0:a3a9c486fc68 | 29 | } |
DavidEGrayson | 0:a3a9c486fc68 | 30 | |
DavidEGrayson | 0:a3a9c486fc68 | 31 | int main() |
DavidEGrayson | 0:a3a9c486fc68 | 32 | { |
DavidEGrayson | 0:a3a9c486fc68 | 33 | timer.start(); |
DavidEGrayson | 0:a3a9c486fc68 | 34 | |
DavidEGrayson | 0:a3a9c486fc68 | 35 | while(1) |
DavidEGrayson | 0:a3a9c486fc68 | 36 | { |
DavidEGrayson | 0:a3a9c486fc68 | 37 | // Update the colors array. |
DavidEGrayson | 1:3d89bcd21002 | 38 | uint32_t time = timer.read_ms(); |
DavidEGrayson | 0:a3a9c486fc68 | 39 | for(int i = 0; i < LED_COUNT; i++) |
DavidEGrayson | 0:a3a9c486fc68 | 40 | { |
DavidEGrayson | 0:a3a9c486fc68 | 41 | uint8_t phase = (time >> 4) - (i << 2); |
DavidEGrayson | 0:a3a9c486fc68 | 42 | colors[i] = hsvToRgb(phase / 256.0, 1.0, 1.0); |
DavidEGrayson | 0:a3a9c486fc68 | 43 | } |
DavidEGrayson | 0:a3a9c486fc68 | 44 | |
DavidEGrayson | 0:a3a9c486fc68 | 45 | // Send the colors to the LED strip. |
DavidEGrayson | 0:a3a9c486fc68 | 46 | ledStrip.write(colors, LED_COUNT); |
DavidEGrayson | 0:a3a9c486fc68 | 47 | wait_ms(10); |
DavidEGrayson | 0:a3a9c486fc68 | 48 | } |
DavidEGrayson | 0:a3a9c486fc68 | 49 | } |