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:
Sat Jan 04 00:32:16 2014 +0000
Revision:
30:52e9205a8059
Parent:
29:a76075c853ee
Child:
32:7b60b9add4b5
used TPM0 to time guard time at end of DMA.

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 29:a76075c853ee 28 PwmOut rled(LED_RED);
bikeNomad 29:a76075c853ee 29 PwmOut gled(LED_GREEN);
bikeNomad 30:52e9205a8059 30 // LED_BLUE is on PTD1
bikeNomad 27:88c2abdf5eb9 31
bikeNomad 24:feb1dae0403a 32 float touchPercentage;
bikeNomad 25:751c89f7e654 33 unsigned frames;
bikeNomad 25:751c89f7e654 34
bikeNomad 25:751c89f7e654 35 float const minBrite = 0.2;
bikeNomad 25:751c89f7e654 36 float const maxBrite = 0.5;
bikeNomad 24:feb1dae0403a 37 float brite;
bikeNomad 24:feb1dae0403a 38
bikeNomad 24:feb1dae0403a 39 void readTouchSensor()
bikeNomad 24:feb1dae0403a 40 {
bikeNomad 24:feb1dae0403a 41 touchPercentage *= 0.9;
bikeNomad 24:feb1dae0403a 42 touchPercentage += touchSensor.readPercentage() * 0.1;
bikeNomad 25:751c89f7e654 43 brite = minBrite + (maxBrite - minBrite) * touchPercentage;
bikeNomad 24:feb1dae0403a 44 }
bikeNomad 24:feb1dae0403a 45
bikeNomad 22:abfed71656bd 46 // @brief sets different colors in each of the LEDs of a strip
bikeNomad 22:abfed71656bd 47 // @param strip the light strip
bikeNomad 22:abfed71656bd 48 // @param sat saturation, 0.0 - 1.0
bikeNomad 22:abfed71656bd 49 // @param brite brightness, 0.0 - 1.0
bikeNomad 22:abfed71656bd 50 // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees
bikeNomad 22:abfed71656bd 51 static void showRainbow(WS2811 &strip, float sat, float brite, float hueShift)
bikeNomad 22:abfed71656bd 52 {
bikeNomad 22:abfed71656bd 53 unsigned nLEDs = strip.numPixels();
bikeNomad 22:abfed71656bd 54 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 22:abfed71656bd 55 uint8_t r, g, b;
bikeNomad 22:abfed71656bd 56 float hue = ((float)i / (float)nLEDs) + hueShift;
bikeNomad 22:abfed71656bd 57 HSBtoRGB(hue, sat, brite, &r, &g, &b);
bikeNomad 28:dbe551a3dd64 58 strip.setPixelColor(i, r, g, b);
bikeNomad 22:abfed71656bd 59 }
bikeNomad 22:abfed71656bd 60 strip.show();
bikeNomad 22:abfed71656bd 61 }
bikeNomad 20:b9d76e567637 62
bikeNomad 24:feb1dae0403a 63 static void showSolidColor(WS2811 &strip, uint8_t r, uint8_t g, uint8_t b)
bikeNomad 24:feb1dae0403a 64 {
bikeNomad 24:feb1dae0403a 65 unsigned nLEDs = strip.numPixels();
bikeNomad 24:feb1dae0403a 66 for (unsigned i = 0; i < nLEDs; i++) {
bikeNomad 28:dbe551a3dd64 67 strip.setPixelColor(i, r, g, b);
bikeNomad 24:feb1dae0403a 68 }
bikeNomad 24:feb1dae0403a 69 strip.show();
bikeNomad 24:feb1dae0403a 70 }
bikeNomad 24:feb1dae0403a 71
bikeNomad 22:abfed71656bd 72 int main(void)
bikeNomad 22:abfed71656bd 73 {
bikeNomad 22:abfed71656bd 74 pc.baud(115200);
bikeNomad 25:751c89f7e654 75 WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1);
bikeNomad 25:751c89f7e654 76 WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2);
bikeNomad 22:abfed71656bd 77
bikeNomad 25:751c89f7e654 78 lightStrip1.begin();
bikeNomad 25:751c89f7e654 79 lightStrip2.begin();
bikeNomad 29:a76075c853ee 80 rled = 1.0;
bikeNomad 29:a76075c853ee 81 gled = 1.0;
bikeNomad 22:abfed71656bd 82
bikeNomad 23:33df42ff2541 83 float sat = 1.0;
bikeNomad 22:abfed71656bd 84
bikeNomad 23:33df42ff2541 85 timeRunning.start();
bikeNomad 22:abfed71656bd 86
bikeNomad 24:feb1dae0403a 87 uint8_t r =0;
bikeNomad 24:feb1dae0403a 88 uint8_t g =0;
bikeNomad 24:feb1dae0403a 89 uint8_t b =0;
bikeNomad 24:feb1dae0403a 90 for (;;) {
bikeNomad 25:751c89f7e654 91 if (r < 40)
bikeNomad 24:feb1dae0403a 92 r++;
bikeNomad 25:751c89f7e654 93 else if (g < 40)
bikeNomad 24:feb1dae0403a 94 g++;
bikeNomad 25:751c89f7e654 95 else if (b < 40)
bikeNomad 24:feb1dae0403a 96 b++;
bikeNomad 24:feb1dae0403a 97 else {
bikeNomad 24:feb1dae0403a 98 unsigned running = timeRunning.read_us();
bikeNomad 24:feb1dae0403a 99 pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running);
bikeNomad 24:feb1dae0403a 100 break;
bikeNomad 24:feb1dae0403a 101 }
bikeNomad 24:feb1dae0403a 102
bikeNomad 25:751c89f7e654 103 showSolidColor(lightStrip1, r, g, b);
bikeNomad 25:751c89f7e654 104 showSolidColor(lightStrip2, r, g, b);
bikeNomad 25:751c89f7e654 105 WS2811::startDMA();
bikeNomad 25:751c89f7e654 106
bikeNomad 24:feb1dae0403a 107 frames++;
bikeNomad 24:feb1dae0403a 108 }
bikeNomad 24:feb1dae0403a 109
bikeNomad 24:feb1dae0403a 110 timeRunning.reset();
bikeNomad 24:feb1dae0403a 111 frames = 0;
bikeNomad 24:feb1dae0403a 112
bikeNomad 29:a76075c853ee 113 float xyz[3];
bikeNomad 29:a76075c853ee 114 acc.getAccAllAxis(xyz);
bikeNomad 29:a76075c853ee 115 pc.printf("x: %f y: %f z: %f\r\n", xyz[0], xyz[1], xyz[2]);
bikeNomad 29:a76075c853ee 116
bikeNomad 22:abfed71656bd 117 for (;;) {
bikeNomad 27:88c2abdf5eb9 118 acc.getAccAllAxis(xyz);
bikeNomad 29:a76075c853ee 119 rled = 1.0 - abs(xyz[0]);
bikeNomad 29:a76075c853ee 120 gled = 1.0 - abs(xyz[1]);
bikeNomad 29:a76075c853ee 121 readTouchSensor();
bikeNomad 29:a76075c853ee 122 showRainbow(lightStrip1, sat, brite, abs(xyz[0]));
bikeNomad 29:a76075c853ee 123 showRainbow(lightStrip2, sat, brite, abs(xyz[1]));
bikeNomad 25:751c89f7e654 124 WS2811::startDMA();
bikeNomad 25:751c89f7e654 125
bikeNomad 23:33df42ff2541 126 frames ++;
bikeNomad 23:33df42ff2541 127 }
bikeNomad 22:abfed71656bd 128 }
bikeNomad 22:abfed71656bd 129