Tag: Processing

Adafruit and various other vendors sell RGB LED strips with one LPD8806 chip per 2 RGB LED modules. The chips shift in 1 byte per led color (3 per led module) and use 7 bits of the byte to drive the intensity of the LED via PWM. On the other side the chips shift out the bits 6 bytes delayed. 1 meter of these strips has 32 LED’s and 16 chips, forming a 384 byte long shift register. One (or more) zero bytes latch the pattern in the strip, eliminating the need to constantly refresh the strip. The chip could use a blog post on it’s own.

The single strip examples.

Adafruit have some excellent examples driving one led strip with the arduino SPI. You can scale this solution by daisy chaining strips but there are come complications with this approach. One is that all the power for all strips (1 Ampere per meter or more) will flow through the first strip. The other is that if you want to run strips in parallel you’ll need one Arduino per strand. The last problem is that the Arduino is too small and slow to handle large bitmaps by itself and a bit cumbersome as an IO extender.

In this solution a Teensy is used to drive 8 strips simultaneously. One of the 8 bit ports is used to drive the data pin on 8 led strips, one pin on one other port is used to drive all clock pins. This reduces the number of boards per strand.

Paul’s code worked right away (a classic example of loop unrolling, trading code size for execution speed). The only challenge now was to hook it all up and write a example program that creates an image and serializes it to the teensy.

Teensy and USB

Below is an image of the first USB frame in the led strip image. The time the teensy needs for pushing out one USB frame is 58 us. There are 64 bytes in this frame, driving a little more than 2 columns with 8 leds each.

You can just see the “H”, “E” and “L” in the pattern driving the LED strips.

One image in the LED strip

Above is one complete LED strip image, the last block (right) only contains o’s and latches the pattern in the strip. The pauses between the USB frames are caused by the combination of the Teensy USB, the OS USB stack and the Processing application. USB hubs and main board chipsets can cause additional artifacts.

Pictures and movies

Brightness and dynamic range

The LPD8806 have 7 bits of PWM resolution allowing for 221 (about 2 million) colors. Seems like plenty. The problem is that the LED strips are pretty bright, when the the LED are driven full brightness my eyes and the camera are pretty much blinded. So for indoor use I’m using it at about 10%, effectively killing off 3 of the 7 bits. That explains the posterising you can see in movies below. The competing chip, the WS2801, has 8 bits PWM resolution and may suffer less from posterising,

Problems, complications and stuff to fix later.

The led strip driver combines 4 parts, each with their own complications and limitations. Theses are :

Java :

Java has different primitive integer types but does ALL operations on those types as if they are ints. As we are working with bytes here (serial stream is a byte stream and the teensy moves bytes from the USB buffer to the port) well need to do a lot of “v & 0xFF” to mask out the unneeded bits and “(byte) x” to cast to a byte.

Also, in Java the byte is signed. For the operations we are doing this doesn’t really matter (with one exception below) but the bytes are presented in the debugger and print as signed integers. 0xFF will appear as -1.
Logical bit shifts are of course also done on 32 bit integers. Because the max shift is 32 bits, Java only looks at the lowest 5 bits of the shift amount by masking out the other bits with 0x1F. This also means shifting with negative amounts is not possible.

Finally, because the shift operation is done on an integer and the the integer is signed, we do not want sign extension on the right shift. Use the true binary “>>>” instead of the numerical “>>”. In other words, “>>>” shifts the real bits, “>>” keeps the sign as it was.

USB :

The maximum packet size USB serial device is 64 bytes. The teensy code is built to only handle complete 64 byte packets. Shorter packets will have to be padded to multiples of 64 bytes.

Teensy :

The teensy waits for a full buffer and the dumps the bytes to a port. The frame timing is determined by the teensy clock frequency and by “nop” instuctions. The timing between the frames is complicated by the USB stack on the host and device, the OS and USB hubs.