Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

cleaned up and commented library. fixed example

  • Loading branch information...
commit ea7f85a6ea39e62037bbad9c5c8ca8d42d1b393d 1 parent 7c9b700
Limor "Ladyada" Fried ladyada authored

Showing 3 changed files with 67 additions and 55 deletions. Show diff stats Hide diff stats

  1. +16 9 LPD8806.cpp
  2. +5 0 LPD8806.h
  3. +46 46 examples/strandtest/strandtest.pde
25 LPD8806.cpp
@@ -6,20 +6,13 @@
6 6
7 7 /*****************************************************************************/
8 8
9   -// the arrays of bytes that hold each LED's 24 bit color values
10   -static uint8_t *pixels;
11   -static uint16_t numLEDs;
12   -
13   -static uint8_t dataPin, clockPin;
14   -
15   -static byte lastdata = 0;
16   -
17 9
18 10 LPD8806::LPD8806(uint16_t n, uint8_t dpin, uint8_t cpin) {
19 11 dataPin = dpin;
20 12 clockPin = cpin;
21 13 numLEDs = n;
22 14
  15 + // malloc 3 bytes per pixel so we dont have to hardcode the length
23 16 pixels = (uint8_t *)malloc(numLEDs * 3); // 3 bytes per pixel
24 17 for (uint16_t i=0; i < numLEDs; i++) {
25 18 setPixelColor(i, 0, 0, 0);
@@ -38,6 +31,9 @@ uint16_t LPD8806::numPixels(void) {
38 31 uint32_t LPD8806::Color(byte r, byte g, byte b)
39 32 {
40 33 //Take the lowest 7 bits of each value and append them end to end
  34 + // We have the top bit set high (its a 'parity-like' bit in the protocol
  35 + // and must be set!)
  36 +
41 37 uint32_t x;
42 38 x = g | 0x80;
43 39 x <<= 8;
@@ -48,7 +44,7 @@ uint32_t LPD8806::Color(byte r, byte g, byte b)
48 44 return(x);
49 45 }
50 46
51   -
  47 +// Basic, push SPI data out
52 48 void LPD8806::write8(uint8_t d) {
53 49 for (uint8_t i=0; i<8; i++) {
54 50 if (d & _BV(7-i))
@@ -60,29 +56,40 @@ void LPD8806::write8(uint8_t d) {
60 56 }
61 57 }
62 58
  59 +// This is how data is pushed to the strip.
  60 +// Unfortunately, the company that makes the chip didnt release the
  61 +// protocol document or you need to sign an NDA or something stupid
  62 +// like that, but we reverse engineered this from a strip
  63 +// controller and it seems to work very nicely!
63 64 void LPD8806::show(void) {
64 65 uint16_t i;
65 66
  67 + // get the strip's attention
66 68 write8(0);
67 69 write8(0);
68 70 write8(0);
69 71 write8(0);
70 72
  73 + // write 24 bits per pixel
71 74 for (i=0; i<numLEDs; i++ ) {
72 75 write8(pixels[i*3]);
73 76 write8(pixels[i*3+1]);
74 77 write8(pixels[i*3+2]);
75 78 }
76 79
  80 + // to 'latch' the data, we send just zeros
77 81 for (i=0; i < (numLEDs*2); i++ ) {
78 82 write8(0);
79 83 write8(0);
80 84 write8(0);
81 85 }
82 86
  87 + // we need to have a delay here, 10ms seems to do the job
  88 + // shorter may be OK as well - need to experiment :(
83 89 delay(10);
84 90 }
85 91
  92 +// store the rgb component in our array
86 93 void LPD8806::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
87 94 uint32_t data;
88 95
5 LPD8806.h
@@ -3,6 +3,11 @@
3 3 class LPD8806 {
4 4 private:
5 5 void write8(byte);
  6 + // the arrays of bytes that hold each LED's 24 bit color values
  7 + uint8_t *pixels;
  8 + uint16_t numLEDs;
  9 + uint8_t dataPin, clockPin;
  10 +
6 11 public:
7 12 LPD8806(uint16_t n, uint8_t dpin, uint8_t cpin);
8 13 void begin();
92 examples/strandtest/strandtest.pde
... ... @@ -1,38 +1,20 @@
1   -#include <TimerOne.h>
2   -#include "LPD6803.h"
  1 +#include "LPD8806.h"
3 2
4   -//Example to control LPD6803-based RGB LED Modules in a strand
5   -// Original code by Bliptronics.com Ben Moyes 2009
6   -//Use this as you wish, but please give credit, or at least buy some of my LEDs!
7   -
8   -// Code cleaned up and Object-ified by ladyada, should be a bit easier to use
  3 +// Example to control LPD8806-based RGB LED Modules in a strip
9 4
10 5 /*****************************************************************************/
11 6
12 7 // Choose which 2 pins you will use for output.
13 8 // Can be any valid output pins.
14   -int dataPin = 2; // 'yellow' wire
15   -int clockPin = 3; // 'green' wire
16   -// Don't forget to connect 'blue' to ground and 'red' to +5V
17   -
18   -// Timer 1 is also used by the strip to send pixel clocks
19   -
20   -// Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
21   -LPD6803 strip = LPD6803(20, dataPin, clockPin);
  9 +int dataPin = 2;
  10 +int clockPin = 3;
22 11
  12 +// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
  13 +// The LED strips are 32 LEDs per meter but you can extend/cut the strip
  14 +LPD8806 strip = LPD8806(32, dataPin, clockPin);
23 15
24 16 void setup() {
25   -
26   - // The Arduino needs to clock out the data to the pixels
27   - // this happens in interrupt timer 1, we can change how often
28   - // to call the interrupt. setting CPUmax to 100 will take nearly all all the
29   - // time to do the pixel updates and a nicer/faster display,
30   - // especially with strands of over 100 dots.
31   - // (Note that the max is 'pessimistic', its probably 10% or 20% less in reality)
32   -
33   - strip.setCPUmax(50); // start with 50% CPU usage. up this if the strand flickers or is slow
34   -
35   - // Start up the LED counter
  17 + // Start up the LED strip
36 18 strip.begin();
37 19
38 20 // Update the strip, to start they are all 'off'
@@ -41,15 +23,20 @@ void setup() {
41 23
42 24
43 25 void loop() {
44   - // Some example procedures showing how to display to the pixels
45   -
46   - colorWipe(Color(63, 0, 0), 50);
47   - colorWipe(Color(0, 63, 0), 50);
48   - colorWipe(Color(0, 0, 63), 50);
49   -
50   - rainbow(50);
51   -
52   - rainbowCycle(50);
  26 + colorChase(strip.Color(127,127,127), 10);
  27 +
  28 + // Send a simple pixel chase in...
  29 + colorChase(strip.Color(127,0,0), 10); // full brightness red
  30 + colorChase(strip.Color(127,127,0), 10); // orange
  31 + colorChase(strip.Color(0,127,0), 10); // green
  32 + colorChase(strip.Color(0,127,127), 10); // teal
  33 + colorChase(strip.Color(0,0,127), 10); // blue
  34 + colorChase(strip.Color(127,0,127), 10); // violet
  35 +
  36 + // fill the entire strip with...
  37 + colorWipe(strip.Color(127,0,0), 10); // red
  38 + colorWipe(strip.Color(0, 127,0), 10); // green
  39 + colorWipe(strip.Color(0,0,127), 10); // blue
53 40 }
54 41
55 42 void rainbow(uint8_t wait) {
@@ -84,7 +71,7 @@ void rainbowCycle(uint8_t wait) {
84 71
85 72 // fill the dots one after the other with said color
86 73 // good for testing purposes
87   -void colorWipe(uint16_t c, uint8_t wait) {
  74 +void colorWipe(uint32_t c, uint8_t wait) {
88 75 int i;
89 76
90 77 for (i=0; i < strip.numPixels(); i++) {
@@ -94,15 +81,29 @@ void colorWipe(uint16_t c, uint8_t wait) {
94 81 }
95 82 }
96 83
97   -/* Helper functions */
98   -
99   -// Create a 15 bit color value from R,G,B
100   -unsigned int Color(byte r, byte g, byte b)
101   -{
102   - //Take the lowest 5 bits of each value and append them end to end
103   - return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
  84 +// Chase a dot down the strip
  85 +// good for testing purposes
  86 +void colorChase(uint32_t c, uint8_t wait) {
  87 + int i;
  88 +
  89 + for (i=0; i < strip.numPixels(); i++) {
  90 + strip.setPixelColor(i, 0); // turn all pixels off
  91 + }
  92 +
  93 + for (i=0; i < strip.numPixels(); i++) {
  94 + strip.setPixelColor(i, c);
  95 + if (i == 0) {
  96 + strip.setPixelColor(strip.numPixels()-1, 0);
  97 + } else {
  98 + strip.setPixelColor(i-1, 0);
  99 + }
  100 + strip.show();
  101 + delay(wait);
  102 + }
104 103 }
105 104
  105 +/* Helper functions */
  106 +
106 107 //Input a value 0 to 127 to get a color value.
107 108 //The colours are a transition r - g -b - back to r
108 109 unsigned int Wheel(byte WheelPos)
@@ -126,6 +127,5 @@ unsigned int Wheel(byte WheelPos)
126 127 g=0; //green off
127 128 break;
128 129 }
129   - return(Color(r,g,b));
130   -}
131   -
  130 + return(strip.Color(r,g,b));
  131 +}

0 comments on commit ea7f85a

Please sign in to comment.
Something went wrong with that request. Please try again.