Test program for the LPD8806 library. Standard connected to 1st hardware SPI module. Data -> p5 and Clock -> p7

Dependencies:   mbed LPD8806

Committer:
ehbmbed2
Date:
Fri Dec 16 10:43:27 2011 +0000
Revision:
2:c9d9fa02a4e4
Parent:
1:c70352e536c0
Updated comments

Who changed what in which revision?

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