Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

cleaned up and commented library. fixed example

  • Loading branch information...
commit ea7f85a6ea39e62037bbad9c5c8ca8d42d1b393d 1 parent 7c9b700
Limor "Ladyada" Fried ladyada authored
Showing with 67 additions and 55 deletions.
  1. +16 −9 LPD8806.cpp
  2. +5 −0 LPD8806.h
  3. +46 −46 examples/strandtest/strandtest.pde
25 LPD8806.cpp
View
@@ -6,20 +6,13 @@
/*****************************************************************************/
-// the arrays of bytes that hold each LED's 24 bit color values
-static uint8_t *pixels;
-static uint16_t numLEDs;
-
-static uint8_t dataPin, clockPin;
-
-static byte lastdata = 0;
-
LPD8806::LPD8806(uint16_t n, uint8_t dpin, uint8_t cpin) {
dataPin = dpin;
clockPin = cpin;
numLEDs = n;
+ // malloc 3 bytes per pixel so we dont have to hardcode the length
pixels = (uint8_t *)malloc(numLEDs * 3); // 3 bytes per pixel
for (uint16_t i=0; i < numLEDs; i++) {
setPixelColor(i, 0, 0, 0);
@@ -38,6 +31,9 @@ uint16_t LPD8806::numPixels(void) {
uint32_t LPD8806::Color(byte r, byte g, byte b)
{
//Take the lowest 7 bits of each value and append them end to end
+ // We have the top bit set high (its a 'parity-like' bit in the protocol
+ // and must be set!)
+
uint32_t x;
x = g | 0x80;
x <<= 8;
@@ -48,7 +44,7 @@ uint32_t LPD8806::Color(byte r, byte g, byte b)
return(x);
}
-
+// Basic, push SPI data out
void LPD8806::write8(uint8_t d) {
for (uint8_t i=0; i<8; i++) {
if (d & _BV(7-i))
@@ -60,29 +56,40 @@ void LPD8806::write8(uint8_t d) {
}
}
+// This is how data is pushed to the strip.
+// Unfortunately, the company that makes the chip didnt release the
+// protocol document or you need to sign an NDA or something stupid
+// like that, but we reverse engineered this from a strip
+// controller and it seems to work very nicely!
void LPD8806::show(void) {
uint16_t i;
+ // get the strip's attention
write8(0);
write8(0);
write8(0);
write8(0);
+ // write 24 bits per pixel
for (i=0; i<numLEDs; i++ ) {
write8(pixels[i*3]);
write8(pixels[i*3+1]);
write8(pixels[i*3+2]);
}
+ // to 'latch' the data, we send just zeros
for (i=0; i < (numLEDs*2); i++ ) {
write8(0);
write8(0);
write8(0);
}
+ // we need to have a delay here, 10ms seems to do the job
+ // shorter may be OK as well - need to experiment :(
delay(10);
}
+// store the rgb component in our array
void LPD8806::setPixelColor(uint16_t n, uint8_t r, uint8_t g, uint8_t b) {
uint32_t data;
5 LPD8806.h
View
@@ -3,6 +3,11 @@
class LPD8806 {
private:
void write8(byte);
+ // the arrays of bytes that hold each LED's 24 bit color values
+ uint8_t *pixels;
+ uint16_t numLEDs;
+ uint8_t dataPin, clockPin;
+
public:
LPD8806(uint16_t n, uint8_t dpin, uint8_t cpin);
void begin();
92 examples/strandtest/strandtest.pde
View
@@ -1,38 +1,20 @@
-#include <TimerOne.h>
-#include "LPD6803.h"
+#include "LPD8806.h"
-//Example to control LPD6803-based RGB LED Modules in a strand
-// Original code by Bliptronics.com Ben Moyes 2009
-//Use this as you wish, but please give credit, or at least buy some of my LEDs!
-
-// Code cleaned up and Object-ified by ladyada, should be a bit easier to use
+// Example to control LPD8806-based RGB LED Modules in a strip
/*****************************************************************************/
// Choose which 2 pins you will use for output.
// Can be any valid output pins.
-int dataPin = 2; // 'yellow' wire
-int clockPin = 3; // 'green' wire
-// Don't forget to connect 'blue' to ground and 'red' to +5V
-
-// Timer 1 is also used by the strip to send pixel clocks
-
-// Set the first variable to the NUMBER of pixels. 20 = 20 pixels in a row
-LPD6803 strip = LPD6803(20, dataPin, clockPin);
+int dataPin = 2;
+int clockPin = 3;
+// Set the first variable to the NUMBER of pixels. 32 = 32 pixels in a row
+// The LED strips are 32 LEDs per meter but you can extend/cut the strip
+LPD8806 strip = LPD8806(32, dataPin, clockPin);
void setup() {
-
- // The Arduino needs to clock out the data to the pixels
- // this happens in interrupt timer 1, we can change how often
- // to call the interrupt. setting CPUmax to 100 will take nearly all all the
- // time to do the pixel updates and a nicer/faster display,
- // especially with strands of over 100 dots.
- // (Note that the max is 'pessimistic', its probably 10% or 20% less in reality)
-
- strip.setCPUmax(50); // start with 50% CPU usage. up this if the strand flickers or is slow
-
- // Start up the LED counter
+ // Start up the LED strip
strip.begin();
// Update the strip, to start they are all 'off'
@@ -41,15 +23,20 @@ void setup() {
void loop() {
- // Some example procedures showing how to display to the pixels
-
- colorWipe(Color(63, 0, 0), 50);
- colorWipe(Color(0, 63, 0), 50);
- colorWipe(Color(0, 0, 63), 50);
-
- rainbow(50);
-
- rainbowCycle(50);
+ colorChase(strip.Color(127,127,127), 10);
+
+ // Send a simple pixel chase in...
+ colorChase(strip.Color(127,0,0), 10); // full brightness red
+ colorChase(strip.Color(127,127,0), 10); // orange
+ colorChase(strip.Color(0,127,0), 10); // green
+ colorChase(strip.Color(0,127,127), 10); // teal
+ colorChase(strip.Color(0,0,127), 10); // blue
+ colorChase(strip.Color(127,0,127), 10); // violet
+
+ // fill the entire strip with...
+ colorWipe(strip.Color(127,0,0), 10); // red
+ colorWipe(strip.Color(0, 127,0), 10); // green
+ colorWipe(strip.Color(0,0,127), 10); // blue
}
void rainbow(uint8_t wait) {
@@ -84,7 +71,7 @@ void rainbowCycle(uint8_t wait) {
// fill the dots one after the other with said color
// good for testing purposes
-void colorWipe(uint16_t c, uint8_t wait) {
+void colorWipe(uint32_t c, uint8_t wait) {
int i;
for (i=0; i < strip.numPixels(); i++) {
@@ -94,15 +81,29 @@ void colorWipe(uint16_t c, uint8_t wait) {
}
}
-/* Helper functions */
-
-// Create a 15 bit color value from R,G,B
-unsigned int Color(byte r, byte g, byte b)
-{
- //Take the lowest 5 bits of each value and append them end to end
- return( ((unsigned int)g & 0x1F )<<10 | ((unsigned int)b & 0x1F)<<5 | (unsigned int)r & 0x1F);
+// Chase a dot down the strip
+// good for testing purposes
+void colorChase(uint32_t c, uint8_t wait) {
+ int i;
+
+ for (i=0; i < strip.numPixels(); i++) {
+ strip.setPixelColor(i, 0); // turn all pixels off
+ }
+
+ for (i=0; i < strip.numPixels(); i++) {
+ strip.setPixelColor(i, c);
+ if (i == 0) {
+ strip.setPixelColor(strip.numPixels()-1, 0);
+ } else {
+ strip.setPixelColor(i-1, 0);
+ }
+ strip.show();
+ delay(wait);
+ }
}
+/* Helper functions */
+
//Input a value 0 to 127 to get a color value.
//The colours are a transition r - g -b - back to r
unsigned int Wheel(byte WheelPos)
@@ -126,6 +127,5 @@ unsigned int Wheel(byte WheelPos)
g=0; //green off
break;
}
- return(Color(r,g,b));
-}
-
+ return(strip.Color(r,g,b));
+}
Please sign in to comment.
Something went wrong with that request. Please try again.