Simple 8x8 LED Matrix controller which interfaces with a Processing GUI over serial to display sketches

Dependencies:   Multi_WS2811 mbed

Fork of Multi_WS2811_test by Ned Konz

Committer:
bikeNomad
Date:
Thu Jan 02 00:50:09 2014 +0000
Revision:
21:4541da183397
Parent:
20:b9d76e567637
Child:
22:abfed71656bd
Got parallel mode working!

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 21:4541da183397 1 #include "mbed.h"
bikeNomad 21:4541da183397 2 #include "WS2811.h"
bikeNomad 21:4541da183397 3 #include "Colors.h"
bikeNomad 21:4541da183397 4
bikeNomad 21:4541da183397 5 // per LED: 3x 20 mA = 60mA
bikeNomad 21:4541da183397 6 // 4x full brightness = 0.33A at 5V.
bikeNomad 21:4541da183397 7 unsigned const nLEDs = 40;
bikeNomad 21:4541da183397 8
bikeNomad 21:4541da183397 9 // I/O pin usage
bikeNomad 21:4541da183397 10 // PTD0 TPM0 CH0 monitor
bikeNomad 21:4541da183397 11 // PTD1 TPM0 CH1 monitor
bikeNomad 21:4541da183397 12 // PTD2 data output
bikeNomad 21:4541da183397 13 // PTD3 debug output
bikeNomad 21:4541da183397 14
bikeNomad 21:4541da183397 15 unsigned const DATA_OUT_PIN = 2; // PTD2
bikeNomad 21:4541da183397 16
bikeNomad 21:4541da183397 17 Serial pc(USBTX, USBRX);
bikeNomad 21:4541da183397 18
bikeNomad 21:4541da183397 19 // @brief sets different colors in each of the LEDs of a strip
bikeNomad 21:4541da183397 20 // @param strip the light strip
bikeNomad 21:4541da183397 21 // @param sat saturation, 0.0 - 1.0
bikeNomad 21:4541da183397 22 // @param brite brightness, 0.0 - 1.0
bikeNomad 21:4541da183397 23 // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees
bikeNomad 21:4541da183397 24 void showRainbow(WS2811 &strip, float sat, float brite, float hueShift)
bikeNomad 21:4541da183397 25 {
bikeNomad 21:4541da183397 26 unsigned nLEDs = strip.numPixels();
bikeNomad 21:4541da183397 27 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 21:4541da183397 28 uint8_t r, g, b;
bikeNomad 21:4541da183397 29 float hue = ((float)i / (float)nLEDs) + hueShift;
bikeNomad 21:4541da183397 30 HSBtoRGB(hue, sat, brite, &r, &g, &b);
bikeNomad 21:4541da183397 31 strip.setPixelColor(i, LedStrip::Color(r, g, b));
bikeNomad 21:4541da183397 32 }
bikeNomad 21:4541da183397 33 strip.show();
bikeNomad 21:4541da183397 34 }
bikeNomad 21:4541da183397 35
bikeNomad 21:4541da183397 36 void showTestPattern(WS2811 &strip)
bikeNomad 21:4541da183397 37 {
bikeNomad 21:4541da183397 38 unsigned nLEDs = strip.numPixels();
bikeNomad 21:4541da183397 39 for (unsigned i = 0; i < nLEDs; i++)
bikeNomad 21:4541da183397 40 {
bikeNomad 21:4541da183397 41 strip.setPixelColor(i, LedStrip::Color(0xff,0,0xAA));
bikeNomad 21:4541da183397 42 }
bikeNomad 21:4541da183397 43 strip.show();
bikeNomad 21:4541da183397 44 }
bikeNomad 21:4541da183397 45
bikeNomad 21:4541da183397 46 int main(void)
bikeNomad 21:4541da183397 47 {
bikeNomad 21:4541da183397 48 pc.baud(115200);
bikeNomad 21:4541da183397 49 pc.printf("\r\nLEDs: %d\r\n", nLEDs);
bikeNomad 21:4541da183397 50
bikeNomad 21:4541da183397 51 WS2811 lightStrip(nLEDs, DATA_OUT_PIN);
bikeNomad 21:4541da183397 52 Timer advance;
bikeNomad 21:4541da183397 53 advance.start();
bikeNomad 21:4541da183397 54
bikeNomad 21:4541da183397 55 lightStrip.begin();
bikeNomad 21:4541da183397 56
bikeNomad 21:4541da183397 57 // time per LED is about 44 usec, with 310 usec gap between.
bikeNomad 21:4541da183397 58 float sat = 1.0;
bikeNomad 21:4541da183397 59 float brite = 0.25;
bikeNomad 21:4541da183397 60 float hueShift = 0.0;
bikeNomad 21:4541da183397 61 float hueShiftIncrement = 1.0 / (360.0 / nLEDs);
bikeNomad 21:4541da183397 62
bikeNomad 21:4541da183397 63 for (;;) {
bikeNomad 21:4541da183397 64 advance.reset();
bikeNomad 21:4541da183397 65
bikeNomad 21:4541da183397 66 // showTestPattern(lightStrip);
bikeNomad 21:4541da183397 67 showRainbow(lightStrip, sat, brite, hueShift);
bikeNomad 21:4541da183397 68 hueShift += hueShiftIncrement;
bikeNomad 21:4541da183397 69
bikeNomad 21:4541da183397 70 while (advance.read_ms() < 100)
bikeNomad 21:4541da183397 71 __NOP();
bikeNomad 21:4541da183397 72 }
bikeNomad 21:4541da183397 73 }
bikeNomad 20:b9d76e567637 74