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