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 LPD8806_Test by
main.cpp@1:c70352e536c0, 2011-12-16 (annotated)
- Committer:
- ehbmbed2
- Date:
- Fri Dec 16 03:31:25 2011 +0000
- Revision:
- 1:c70352e536c0
- Parent:
- 0:005ed7611285
- Child:
- 2:c9d9fa02a4e4
corrected typos and formatting
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ehbmbed2 | 1:c70352e536c0 | 1 | // Mbed program to test LPD8806-based RGB LED Strips |
ehbmbed2 | 1:c70352e536c0 | 2 | // (c) 2011 Jelmer Tiete |
ehbmbed2 | 1:c70352e536c0 | 3 | // This library is ported from the Arduino implementation of Adafruit Industries |
ehbmbed2 | 1:c70352e536c0 | 4 | // found at: http://github.com/adafruit/LPD8806 |
ehbmbed2 | 1:c70352e536c0 | 5 | // and their strips: http://www.adafruit.com/products/306 |
ehbmbed2 | 1:c70352e536c0 | 6 | // Released under the MIT License: http://mbed.org/license/mit |
ehbmbed2 | 1:c70352e536c0 | 7 | |
ehbmbed2 | 1:c70352e536c0 | 8 | /*****************************************************************************/ |
ehbmbed2 | 1:c70352e536c0 | 9 | |
ehbmbed2 | 1:c70352e536c0 | 10 | #include "LPD8806.h" |
ehbmbed2 | 1:c70352e536c0 | 11 | |
ehbmbed2 | 1:c70352e536c0 | 12 | LPD8806 strip = LPD8806(5); |
ehbmbed2 | 1:c70352e536c0 | 13 | |
ehbmbed2 | 1:c70352e536c0 | 14 | // Chase a dot down the strip |
ehbmbed2 | 1:c70352e536c0 | 15 | // good for testing purposes |
ehbmbed2 | 1:c70352e536c0 | 16 | void colorChase(uint32_t c, uint8_t delay) { |
ehbmbed2 | 1:c70352e536c0 | 17 | int i; |
ehbmbed2 | 1:c70352e536c0 | 18 | |
ehbmbed2 | 1:c70352e536c0 | 19 | for (i=0; i < strip.numPixels(); i++) { |
ehbmbed2 | 1:c70352e536c0 | 20 | strip.setPixelColor(i, 0); // turn all pixels off |
ehbmbed2 | 1:c70352e536c0 | 21 | } |
ehbmbed2 | 1:c70352e536c0 | 22 | |
ehbmbed2 | 1:c70352e536c0 | 23 | for (i=0; i < strip.numPixels(); i++) { |
ehbmbed2 | 1:c70352e536c0 | 24 | strip.setPixelColor(i, c); |
ehbmbed2 | 1:c70352e536c0 | 25 | if (i == 0) { |
ehbmbed2 | 1:c70352e536c0 | 26 | strip.setPixelColor(strip.numPixels()-1, 0); |
ehbmbed2 | 1:c70352e536c0 | 27 | } else { |
ehbmbed2 | 1:c70352e536c0 | 28 | strip.setPixelColor(i-1, 0); |
ehbmbed2 | 1:c70352e536c0 | 29 | } |
ehbmbed2 | 1:c70352e536c0 | 30 | strip.show(); |
ehbmbed2 | 1:c70352e536c0 | 31 | wait_ms(delay); |
ehbmbed2 | 1:c70352e536c0 | 32 | } |
ehbmbed2 | 1:c70352e536c0 | 33 | } |
ehbmbed2 | 1:c70352e536c0 | 34 | |
ehbmbed2 | 1:c70352e536c0 | 35 | // fill the dots one after the other with said color |
ehbmbed2 | 1:c70352e536c0 | 36 | // good for testing purposes |
ehbmbed2 | 1:c70352e536c0 | 37 | void colorWipe(uint32_t c, uint8_t delay) { |
ehbmbed2 | 1:c70352e536c0 | 38 | int i; |
ehbmbed2 | 1:c70352e536c0 | 39 | |
ehbmbed2 | 1:c70352e536c0 | 40 | for (i=0; i < strip.numPixels(); i++) { |
ehbmbed2 | 1:c70352e536c0 | 41 | strip.setPixelColor(i, c); |
ehbmbed2 | 1:c70352e536c0 | 42 | strip.show(); |
ehbmbed2 | 1:c70352e536c0 | 43 | wait_ms(delay); |
ehbmbed2 | 1:c70352e536c0 | 44 | } |
ehbmbed2 | 1:c70352e536c0 | 45 | } |
ehbmbed2 | 1:c70352e536c0 | 46 | |
ehbmbed2 | 1:c70352e536c0 | 47 | //Input a value 0 to 384 to get a color value. |
ehbmbed2 | 1:c70352e536c0 | 48 | //The colours are a transition r - g -b - back to r |
ehbmbed2 | 1:c70352e536c0 | 49 | |
ehbmbed2 | 1:c70352e536c0 | 50 | uint32_t Wheel(uint16_t WheelPos) { |
ehbmbed2 | 1:c70352e536c0 | 51 | uint8_t b=0; |
ehbmbed2 | 1:c70352e536c0 | 52 | uint8_t g=0; |
ehbmbed2 | 1:c70352e536c0 | 53 | uint8_t r = 0; |
ehbmbed2 | 1:c70352e536c0 | 54 | switch (WheelPos / 128) { |
ehbmbed2 | 1:c70352e536c0 | 55 | case 0: |
ehbmbed2 | 1:c70352e536c0 | 56 | r = 127 - WheelPos % 128; //Red down |
ehbmbed2 | 1:c70352e536c0 | 57 | g = WheelPos % 128; // Green up |
ehbmbed2 | 1:c70352e536c0 | 58 | b = 0; //blue off |
ehbmbed2 | 1:c70352e536c0 | 59 | break; |
ehbmbed2 | 1:c70352e536c0 | 60 | case 1: |
ehbmbed2 | 1:c70352e536c0 | 61 | g = 127 - WheelPos % 128; //green down |
ehbmbed2 | 1:c70352e536c0 | 62 | b = WheelPos % 128; //blue up |
ehbmbed2 | 1:c70352e536c0 | 63 | r = 0; //red off |
ehbmbed2 | 1:c70352e536c0 | 64 | break; |
ehbmbed2 | 1:c70352e536c0 | 65 | case 2: |
ehbmbed2 | 1:c70352e536c0 | 66 | b = 127 - WheelPos % 128; //blue down |
ehbmbed2 | 1:c70352e536c0 | 67 | r = WheelPos % 128; //red up |
ehbmbed2 | 1:c70352e536c0 | 68 | g = 0; //green off |
ehbmbed2 | 1:c70352e536c0 | 69 | break; |
ehbmbed2 | 1:c70352e536c0 | 70 | } |
ehbmbed2 | 1:c70352e536c0 | 71 | return(strip.Color(r,g,b)); |
ehbmbed2 | 1:c70352e536c0 | 72 | } |
ehbmbed2 | 1:c70352e536c0 | 73 | |
ehbmbed2 | 1:c70352e536c0 | 74 | void rainbow(uint8_t delay) { |
ehbmbed2 | 1:c70352e536c0 | 75 | int i, j; |
ehbmbed2 | 1:c70352e536c0 | 76 | |
ehbmbed2 | 1:c70352e536c0 | 77 | for (j=0; j < 384; j++) { // 3 cycles of all 384 colors in the wheel |
ehbmbed2 | 1:c70352e536c0 | 78 | for (i=0; i < strip.numPixels(); i++) { |
ehbmbed2 | 1:c70352e536c0 | 79 | strip.setPixelColor(i, Wheel( (i + j) % 384)); |
ehbmbed2 | 1:c70352e536c0 | 80 | } |
ehbmbed2 | 1:c70352e536c0 | 81 | strip.show(); // write all the pixels out |
ehbmbed2 | 1:c70352e536c0 | 82 | wait_ms(delay); |
ehbmbed2 | 1:c70352e536c0 | 83 | } |
ehbmbed2 | 1:c70352e536c0 | 84 | } |
ehbmbed2 | 1:c70352e536c0 | 85 | |
ehbmbed2 | 1:c70352e536c0 | 86 | // Slightly different, this one makes the rainbow wheel equally distributed |
ehbmbed2 | 1:c70352e536c0 | 87 | // along the chain |
ehbmbed2 | 1:c70352e536c0 | 88 | void rainbowCycle(uint8_t delay) { |
ehbmbed2 | 1:c70352e536c0 | 89 | uint16_t i, j; |
ehbmbed2 | 1:c70352e536c0 | 90 | |
ehbmbed2 | 1:c70352e536c0 | 91 | for (j=0; j < 384 * 5; j++) { // 5 cycles of all 384 colors in the wheel |
ehbmbed2 | 1:c70352e536c0 | 92 | for (i=0; i < strip.numPixels(); i++) { |
ehbmbed2 | 1:c70352e536c0 | 93 | // tricky math! we use each pixel as a fraction of the full 384-color wheel |
ehbmbed2 | 1:c70352e536c0 | 94 | // (thats the i / strip.numPixels() part) |
ehbmbed2 | 1:c70352e536c0 | 95 | // Then add in j which makes the colors go around per pixel |
ehbmbed2 | 1:c70352e536c0 | 96 | // the % 384 is to make the wheel cycle around |
ehbmbed2 | 1:c70352e536c0 | 97 | strip.setPixelColor(i, Wheel( ((i * 384 / strip.numPixels()) + j) % 384) ); |
ehbmbed2 | 1:c70352e536c0 | 98 | } |
ehbmbed2 | 1:c70352e536c0 | 99 | strip.show(); // write all the pixels out |
ehbmbed2 | 1:c70352e536c0 | 100 | wait_ms(delay); |
ehbmbed2 | 1:c70352e536c0 | 101 | } |
ehbmbed2 | 1:c70352e536c0 | 102 | } |
ehbmbed2 | 1:c70352e536c0 | 103 | |
ehbmbed2 | 1:c70352e536c0 | 104 | // "Larson scanner" = Cylon/KITT bouncing light effect |
ehbmbed2 | 1:c70352e536c0 | 105 | void scanner(uint8_t r, uint8_t g, uint8_t b, uint8_t delay) { |
ehbmbed2 | 1:c70352e536c0 | 106 | int i, j, pos, dir; |
ehbmbed2 | 1:c70352e536c0 | 107 | |
ehbmbed2 | 1:c70352e536c0 | 108 | pos = 0; |
ehbmbed2 | 1:c70352e536c0 | 109 | dir = 1; |
ehbmbed2 | 1:c70352e536c0 | 110 | |
ehbmbed2 | 1:c70352e536c0 | 111 | for (i=0; i<((strip.numPixels()-1) * 8); i++) { |
ehbmbed2 | 1:c70352e536c0 | 112 | // Draw 5 pixels centered on pos. setPixelColor() will clip |
ehbmbed2 | 1:c70352e536c0 | 113 | // any pixels off the ends of the strip, no worries there. |
ehbmbed2 | 1:c70352e536c0 | 114 | // we'll make the colors dimmer at the edges for a nice pulse |
ehbmbed2 | 1:c70352e536c0 | 115 | // look |
ehbmbed2 | 1:c70352e536c0 | 116 | strip.setPixelColor(pos - 2, strip.Color(r/4, g/4, b/4)); |
ehbmbed2 | 1:c70352e536c0 | 117 | strip.setPixelColor(pos - 1, strip.Color(r/2, g/2, b/2)); |
ehbmbed2 | 1:c70352e536c0 | 118 | strip.setPixelColor(pos, strip.Color(r, g, b)); |
ehbmbed2 | 1:c70352e536c0 | 119 | strip.setPixelColor(pos + 1, strip.Color(r/2, g/2, b/2)); |
ehbmbed2 | 1:c70352e536c0 | 120 | strip.setPixelColor(pos + 2, strip.Color(r/4, g/4, b/4)); |
ehbmbed2 | 1:c70352e536c0 | 121 | |
ehbmbed2 | 1:c70352e536c0 | 122 | strip.show(); |
ehbmbed2 | 1:c70352e536c0 | 123 | wait_ms(delay); |
ehbmbed2 | 1:c70352e536c0 | 124 | // If we wanted to be sneaky we could erase just the tail end |
ehbmbed2 | 1:c70352e536c0 | 125 | // pixel, but it's much easier just to erase the whole thing |
ehbmbed2 | 1:c70352e536c0 | 126 | // and draw a new one next time. |
ehbmbed2 | 1:c70352e536c0 | 127 | for (j=-2; j<= 2; j++) |
ehbmbed2 | 1:c70352e536c0 | 128 | strip.setPixelColor(pos+j, strip.Color(0,0,0)); |
ehbmbed2 | 1:c70352e536c0 | 129 | // Bounce off ends of strip |
ehbmbed2 | 1:c70352e536c0 | 130 | pos += dir; |
ehbmbed2 | 1:c70352e536c0 | 131 | if (pos < 0) { |
ehbmbed2 | 1:c70352e536c0 | 132 | pos = 1; |
ehbmbed2 | 1:c70352e536c0 | 133 | dir = -dir; |
ehbmbed2 | 1:c70352e536c0 | 134 | } else if (pos >= strip.numPixels()) { |
ehbmbed2 | 1:c70352e536c0 | 135 | pos = strip.numPixels() - 2; |
ehbmbed2 | 1:c70352e536c0 | 136 | dir = -dir; |
ehbmbed2 | 1:c70352e536c0 | 137 | } |
ehbmbed2 | 1:c70352e536c0 | 138 | } |
ehbmbed2 | 1:c70352e536c0 | 139 | } |
ehbmbed2 | 1:c70352e536c0 | 140 | |
ehbmbed2 | 1:c70352e536c0 | 141 | int main() { |
ehbmbed2 | 1:c70352e536c0 | 142 | |
ehbmbed2 | 1:c70352e536c0 | 143 | // Start up the LED strip |
ehbmbed2 | 1:c70352e536c0 | 144 | strip.begin(); |
ehbmbed2 | 1:c70352e536c0 | 145 | |
ehbmbed2 | 1:c70352e536c0 | 146 | // Update the strip, to start they are all 'off' |
ehbmbed2 | 1:c70352e536c0 | 147 | strip.show(); |
ehbmbed2 | 1:c70352e536c0 | 148 | while (1) { |
ehbmbed2 | 1:c70352e536c0 | 149 | |
ehbmbed2 | 1:c70352e536c0 | 150 | colorChase(strip.Color(127,127,127), 100); |
ehbmbed2 | 1:c70352e536c0 | 151 | |
ehbmbed2 | 1:c70352e536c0 | 152 | // Send a simple pixel chase in... |
ehbmbed2 | 1:c70352e536c0 | 153 | colorChase(strip.Color(127,0,0), 100); // full brightness red |
ehbmbed2 | 1:c70352e536c0 | 154 | colorChase(strip.Color(127,127,0), 100); // orange |
ehbmbed2 | 1:c70352e536c0 | 155 | colorChase(strip.Color(0,127,0), 100); // green |
ehbmbed2 | 1:c70352e536c0 | 156 | colorChase(strip.Color(0,127,127), 100); // teal |
ehbmbed2 | 1:c70352e536c0 | 157 | colorChase(strip.Color(0,0,127), 100); // blue |
ehbmbed2 | 1:c70352e536c0 | 158 | colorChase(strip.Color(127,0,127), 100); // violet |
ehbmbed2 | 1:c70352e536c0 | 159 | |
ehbmbed2 | 1:c70352e536c0 | 160 | // fill the entire strip with... |
ehbmbed2 | 1:c70352e536c0 | 161 | colorWipe(strip.Color(127,0,0), 100); // red |
ehbmbed2 | 1:c70352e536c0 | 162 | colorWipe(strip.Color(0, 127,0), 100); // green |
ehbmbed2 | 1:c70352e536c0 | 163 | colorWipe(strip.Color(0,0,127), 100); // blue |
ehbmbed2 | 1:c70352e536c0 | 164 | |
ehbmbed2 | 1:c70352e536c0 | 165 | rainbow(10); |
ehbmbed2 | 1:c70352e536c0 | 166 | rainbowCycle(5); // make it go through the cycle fairly fast |
ehbmbed2 | 1:c70352e536c0 | 167 | |
ehbmbed2 | 1:c70352e536c0 | 168 | // Back-and-forth lights |
ehbmbed2 | 1:c70352e536c0 | 169 | scanner(127,0,0, 50); // red, slow |
ehbmbed2 | 1:c70352e536c0 | 170 | scanner(0,0,127, 5); // blue, fast |
ehbmbed2 | 1:c70352e536c0 | 171 | } |
ehbmbed2 | 1:c70352e536c0 | 172 | } |
ehbmbed2 | 1:c70352e536c0 | 173 |