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:
antoniorohit
Date:
Fri Nov 14 08:18:35 2014 +0000
Revision:
32:7b60b9add4b5
Parent:
30:52e9205a8059
Child:
33:7973b70d4fab
Rev0;

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
antoniorohit 32:7b60b9add4b5 63 static void clearScreen(WS2811 &strip)
bikeNomad 24:feb1dae0403a 64 {
bikeNomad 24:feb1dae0403a 65 unsigned nLEDs = strip.numPixels();
bikeNomad 24:feb1dae0403a 66 for (unsigned i = 0; i < nLEDs; i++) {
antoniorohit 32:7b60b9add4b5 67 strip.setPixelColor(i, 0, 0, 0);
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 {
antoniorohit 32:7b60b9add4b5 74 pc.baud(9600);
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
antoniorohit 32:7b60b9add4b5 87
antoniorohit 32:7b60b9add4b5 88 uint8_t q = 0;
antoniorohit 32:7b60b9add4b5 89 char char_buff[6];
bikeNomad 24:feb1dae0403a 90 for (;;) {
antoniorohit 32:7b60b9add4b5 91 if(pc.readable()){
antoniorohit 32:7b60b9add4b5 92 if(pc.getc()==255){
antoniorohit 32:7b60b9add4b5 93 while(q < 6){
antoniorohit 32:7b60b9add4b5 94 while(!pc.readable());
antoniorohit 32:7b60b9add4b5 95 char_buff[q] = pc.getc();
antoniorohit 32:7b60b9add4b5 96 pc.putc(char_buff[q]);
antoniorohit 32:7b60b9add4b5 97 if(char_buff[q] == 254)
antoniorohit 32:7b60b9add4b5 98 break;
antoniorohit 32:7b60b9add4b5 99 q++;
antoniorohit 32:7b60b9add4b5 100 }
antoniorohit 32:7b60b9add4b5 101
antoniorohit 32:7b60b9add4b5 102 if(q >= 4){
antoniorohit 32:7b60b9add4b5 103 lightStrip1.setPixelColor(int(char_buff[0])+8*char_buff[1], int(char_buff[2]), int(char_buff[3]), int(char_buff[4]));
antoniorohit 32:7b60b9add4b5 104 lightStrip1.show();
antoniorohit 32:7b60b9add4b5 105 }
antoniorohit 32:7b60b9add4b5 106 else{
antoniorohit 32:7b60b9add4b5 107 clearScreen(lightStrip1);
antoniorohit 32:7b60b9add4b5 108 }
antoniorohit 32:7b60b9add4b5 109 q = 0;
antoniorohit 32:7b60b9add4b5 110 WS2811::startDMA();
antoniorohit 32:7b60b9add4b5 111 }
bikeNomad 24:feb1dae0403a 112 }
bikeNomad 24:feb1dae0403a 113
bikeNomad 25:751c89f7e654 114
bikeNomad 23:33df42ff2541 115 frames ++;
bikeNomad 23:33df42ff2541 116 }
bikeNomad 22:abfed71656bd 117 }
bikeNomad 22:abfed71656bd 118