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:
Fri Jan 03 16:12:56 2014 +0000
Revision:
28:dbe551a3dd64
Parent:
27:88c2abdf5eb9
Child:
29:a76075c853ee
removed unnecessary color constructors

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bikeNomad 22:abfed71656bd 1 #include "mbed.h"
bikeNomad 22:abfed71656bd 2 #include "WS2811.h"
bikeNomad 22:abfed71656bd 3 #include "Colors.h"
bikeNomad 24:feb1dae0403a 4 #include "TSISensor.h"
bikeNomad 27:88c2abdf5eb9 5 #include "MMA8451Q.h"
bikeNomad 27:88c2abdf5eb9 6
bikeNomad 27:88c2abdf5eb9 7 #define MMA8451_I2C_ADDRESS (0x1d<<1)
bikeNomad 22:abfed71656bd 8
bikeNomad 23:33df42ff2541 9 // per LED: 3 * 20 mA = 60mA max
bikeNomad 23:33df42ff2541 10 // 60 LEDs: 60 * 60mA = 3600 mA max
bikeNomad 25:751c89f7e654 11 // 120 LEDs: 7200 mA max
bikeNomad 22:abfed71656bd 12 unsigned const nLEDs = MAX_LEDS_PER_STRIP;
bikeNomad 22:abfed71656bd 13
bikeNomad 22:abfed71656bd 14 // I/O pin usage
bikeNomad 22:abfed71656bd 15 // PTD0 TPM0 CH0 monitor
bikeNomad 22:abfed71656bd 16 // PTD1 TPM0 CH1 monitor
bikeNomad 22:abfed71656bd 17 // PTD2 data output
bikeNomad 22:abfed71656bd 18 // PTD3 debug output
bikeNomad 22:abfed71656bd 19
bikeNomad 25:751c89f7e654 20 unsigned const DATA_OUT_PIN1 = 2; // PTD2
bikeNomad 25:751c89f7e654 21 unsigned const DATA_OUT_PIN2 = 3; // PTD3
bikeNomad 22:abfed71656bd 22
bikeNomad 22:abfed71656bd 23 Serial pc(USBTX, USBRX);
bikeNomad 25:751c89f7e654 24 Timer timeRunning;
bikeNomad 22:abfed71656bd 25
bikeNomad 24:feb1dae0403a 26 TSISensor touchSensor;
bikeNomad 27:88c2abdf5eb9 27 MMA8451Q acc(PTE25, PTE24, MMA8451_I2C_ADDRESS);
bikeNomad 27:88c2abdf5eb9 28
bikeNomad 24:feb1dae0403a 29 Ticker touchTicker;
bikeNomad 24:feb1dae0403a 30 float touchPercentage;
bikeNomad 25:751c89f7e654 31 unsigned frames;
bikeNomad 25:751c89f7e654 32
bikeNomad 25:751c89f7e654 33 float const minBrite = 0.2;
bikeNomad 25:751c89f7e654 34 float const maxBrite = 0.5;
bikeNomad 24:feb1dae0403a 35 float brite;
bikeNomad 24:feb1dae0403a 36
bikeNomad 24:feb1dae0403a 37 void readTouchSensor()
bikeNomad 24:feb1dae0403a 38 {
bikeNomad 24:feb1dae0403a 39 touchPercentage *= 0.9;
bikeNomad 24:feb1dae0403a 40 touchPercentage += touchSensor.readPercentage() * 0.1;
bikeNomad 25:751c89f7e654 41 brite = minBrite + (maxBrite - minBrite) * touchPercentage;
bikeNomad 24:feb1dae0403a 42 }
bikeNomad 24:feb1dae0403a 43
bikeNomad 22:abfed71656bd 44 // @brief sets different colors in each of the LEDs of a strip
bikeNomad 22:abfed71656bd 45 // @param strip the light strip
bikeNomad 22:abfed71656bd 46 // @param sat saturation, 0.0 - 1.0
bikeNomad 22:abfed71656bd 47 // @param brite brightness, 0.0 - 1.0
bikeNomad 22:abfed71656bd 48 // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees
bikeNomad 22:abfed71656bd 49 static void showRainbow(WS2811 &strip, float sat, float brite, float hueShift)
bikeNomad 22:abfed71656bd 50 {
bikeNomad 22:abfed71656bd 51 unsigned nLEDs = strip.numPixels();
bikeNomad 22:abfed71656bd 52 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 22:abfed71656bd 53 uint8_t r, g, b;
bikeNomad 22:abfed71656bd 54 float hue = ((float)i / (float)nLEDs) + hueShift;
bikeNomad 22:abfed71656bd 55 HSBtoRGB(hue, sat, brite, &r, &g, &b);
bikeNomad 28:dbe551a3dd64 56 strip.setPixelColor(i, r, g, b);
bikeNomad 22:abfed71656bd 57 }
bikeNomad 22:abfed71656bd 58 strip.show();
bikeNomad 22:abfed71656bd 59 }
bikeNomad 20:b9d76e567637 60
bikeNomad 24:feb1dae0403a 61 static void showSolidColor(WS2811 &strip, uint8_t r, uint8_t g, uint8_t b)
bikeNomad 24:feb1dae0403a 62 {
bikeNomad 24:feb1dae0403a 63 unsigned nLEDs = strip.numPixels();
bikeNomad 24:feb1dae0403a 64 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 28:dbe551a3dd64 65 strip.setPixelColor(i, r, g, b);
bikeNomad 24:feb1dae0403a 66 }
bikeNomad 24:feb1dae0403a 67 strip.show();
bikeNomad 24:feb1dae0403a 68 }
bikeNomad 24:feb1dae0403a 69
bikeNomad 22:abfed71656bd 70 int main(void)
bikeNomad 22:abfed71656bd 71 {
bikeNomad 22:abfed71656bd 72 pc.baud(115200);
bikeNomad 25:751c89f7e654 73 WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1);
bikeNomad 25:751c89f7e654 74 WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2);
bikeNomad 22:abfed71656bd 75
bikeNomad 24:feb1dae0403a 76 touchTicker.attach(&readTouchSensor, 0.1);
bikeNomad 25:751c89f7e654 77 lightStrip1.begin();
bikeNomad 25:751c89f7e654 78 lightStrip2.begin();
bikeNomad 22:abfed71656bd 79
bikeNomad 23:33df42ff2541 80 float sat = 1.0;
bikeNomad 22:abfed71656bd 81
bikeNomad 23:33df42ff2541 82 timeRunning.start();
bikeNomad 23:33df42ff2541 83 bool printed = false;
bikeNomad 22:abfed71656bd 84
bikeNomad 24:feb1dae0403a 85 uint8_t r =0;
bikeNomad 24:feb1dae0403a 86 uint8_t g =0;
bikeNomad 24:feb1dae0403a 87 uint8_t b =0;
bikeNomad 24:feb1dae0403a 88 for (;;) {
bikeNomad 25:751c89f7e654 89 if (r < 40)
bikeNomad 24:feb1dae0403a 90 r++;
bikeNomad 25:751c89f7e654 91 else if (g < 40)
bikeNomad 24:feb1dae0403a 92 g++;
bikeNomad 25:751c89f7e654 93 else if (b < 40)
bikeNomad 24:feb1dae0403a 94 b++;
bikeNomad 24:feb1dae0403a 95 else {
bikeNomad 24:feb1dae0403a 96 unsigned running = timeRunning.read_us();
bikeNomad 24:feb1dae0403a 97 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 24:feb1dae0403a 98 break;
bikeNomad 24:feb1dae0403a 99 }
bikeNomad 24:feb1dae0403a 100
bikeNomad 24:feb1dae0403a 101 wait(0.1);
bikeNomad 25:751c89f7e654 102 showSolidColor(lightStrip1, r, g, b);
bikeNomad 25:751c89f7e654 103 showSolidColor(lightStrip2, r, g, b);
bikeNomad 25:751c89f7e654 104 WS2811::startDMA();
bikeNomad 25:751c89f7e654 105
bikeNomad 24:feb1dae0403a 106 frames++;
bikeNomad 24:feb1dae0403a 107 }
bikeNomad 24:feb1dae0403a 108
bikeNomad 24:feb1dae0403a 109 timeRunning.reset();
bikeNomad 24:feb1dae0403a 110 frames = 0;
bikeNomad 24:feb1dae0403a 111
bikeNomad 22:abfed71656bd 112 for (;;) {
bikeNomad 23:33df42ff2541 113 unsigned running = timeRunning.read_us();
bikeNomad 27:88c2abdf5eb9 114 // float hueShift = running / rainbowPeriod;
bikeNomad 23:33df42ff2541 115 if (!printed && running >= 10000000U) {
bikeNomad 23:33df42ff2541 116 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 23:33df42ff2541 117 printed = true;
bikeNomad 23:33df42ff2541 118 }
bikeNomad 27:88c2abdf5eb9 119 float xyz[3];
bikeNomad 27:88c2abdf5eb9 120 acc.getAccAllAxis(xyz);
bikeNomad 27:88c2abdf5eb9 121 showRainbow(lightStrip1, sat, brite, xyz[0]);
bikeNomad 27:88c2abdf5eb9 122 showRainbow(lightStrip2, sat, brite, xyz[1]);
bikeNomad 25:751c89f7e654 123 WS2811::startDMA();
bikeNomad 25:751c89f7e654 124
bikeNomad 23:33df42ff2541 125 frames ++;
bikeNomad 23:33df42ff2541 126 }
bikeNomad 22:abfed71656bd 127 }
bikeNomad 22:abfed71656bd 128