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
main.cpp@29:a76075c853ee, 2014-01-03 (annotated)
- Committer:
- bikeNomad
- Date:
- Fri Jan 03 19:17:24 2014 +0000
- Revision:
- 29:a76075c853ee
- Parent:
- 28:dbe551a3dd64
- Child:
- 30:52e9205a8059
fixed bit timing to eliminate flickering.
Who changed what in which revision?
User | Revision | Line number | New 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 | 27:88c2abdf5eb9 | 30 | |
bikeNomad | 24:feb1dae0403a | 31 | float touchPercentage; |
bikeNomad | 25:751c89f7e654 | 32 | unsigned frames; |
bikeNomad | 25:751c89f7e654 | 33 | |
bikeNomad | 25:751c89f7e654 | 34 | float const minBrite = 0.2; |
bikeNomad | 25:751c89f7e654 | 35 | float const maxBrite = 0.5; |
bikeNomad | 24:feb1dae0403a | 36 | float brite; |
bikeNomad | 24:feb1dae0403a | 37 | |
bikeNomad | 24:feb1dae0403a | 38 | void readTouchSensor() |
bikeNomad | 24:feb1dae0403a | 39 | { |
bikeNomad | 24:feb1dae0403a | 40 | touchPercentage *= 0.9; |
bikeNomad | 24:feb1dae0403a | 41 | touchPercentage += touchSensor.readPercentage() * 0.1; |
bikeNomad | 25:751c89f7e654 | 42 | brite = minBrite + (maxBrite - minBrite) * touchPercentage; |
bikeNomad | 24:feb1dae0403a | 43 | } |
bikeNomad | 24:feb1dae0403a | 44 | |
bikeNomad | 22:abfed71656bd | 45 | // @brief sets different colors in each of the LEDs of a strip |
bikeNomad | 22:abfed71656bd | 46 | // @param strip the light strip |
bikeNomad | 22:abfed71656bd | 47 | // @param sat saturation, 0.0 - 1.0 |
bikeNomad | 22:abfed71656bd | 48 | // @param brite brightness, 0.0 - 1.0 |
bikeNomad | 22:abfed71656bd | 49 | // @param hueShift shift, 0.0 - 1.0 is equivalent to 0 - 360 degrees |
bikeNomad | 22:abfed71656bd | 50 | static void showRainbow(WS2811 &strip, float sat, float brite, float hueShift) |
bikeNomad | 22:abfed71656bd | 51 | { |
bikeNomad | 22:abfed71656bd | 52 | unsigned nLEDs = strip.numPixels(); |
bikeNomad | 22:abfed71656bd | 53 | for (unsigned i = 0; i < nLEDs; i++) { |
bikeNomad | 22:abfed71656bd | 54 | uint8_t r, g, b; |
bikeNomad | 22:abfed71656bd | 55 | float hue = ((float)i / (float)nLEDs) + hueShift; |
bikeNomad | 22:abfed71656bd | 56 | HSBtoRGB(hue, sat, brite, &r, &g, &b); |
bikeNomad | 28:dbe551a3dd64 | 57 | strip.setPixelColor(i, r, g, b); |
bikeNomad | 22:abfed71656bd | 58 | } |
bikeNomad | 22:abfed71656bd | 59 | strip.show(); |
bikeNomad | 22:abfed71656bd | 60 | } |
bikeNomad | 20:b9d76e567637 | 61 | |
bikeNomad | 24:feb1dae0403a | 62 | static void showSolidColor(WS2811 &strip, uint8_t r, uint8_t g, uint8_t b) |
bikeNomad | 24:feb1dae0403a | 63 | { |
bikeNomad | 24:feb1dae0403a | 64 | unsigned nLEDs = strip.numPixels(); |
bikeNomad | 24:feb1dae0403a | 65 | for (unsigned i = 0; i < nLEDs; i++) { |
bikeNomad | 28:dbe551a3dd64 | 66 | strip.setPixelColor(i, r, g, b); |
bikeNomad | 24:feb1dae0403a | 67 | } |
bikeNomad | 24:feb1dae0403a | 68 | strip.show(); |
bikeNomad | 24:feb1dae0403a | 69 | } |
bikeNomad | 24:feb1dae0403a | 70 | |
bikeNomad | 22:abfed71656bd | 71 | int main(void) |
bikeNomad | 22:abfed71656bd | 72 | { |
bikeNomad | 22:abfed71656bd | 73 | pc.baud(115200); |
bikeNomad | 25:751c89f7e654 | 74 | WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1); |
bikeNomad | 25:751c89f7e654 | 75 | WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2); |
bikeNomad | 22:abfed71656bd | 76 | |
bikeNomad | 25:751c89f7e654 | 77 | lightStrip1.begin(); |
bikeNomad | 25:751c89f7e654 | 78 | lightStrip2.begin(); |
bikeNomad | 29:a76075c853ee | 79 | rled = 1.0; |
bikeNomad | 29:a76075c853ee | 80 | gled = 1.0; |
bikeNomad | 22:abfed71656bd | 81 | |
bikeNomad | 23:33df42ff2541 | 82 | float sat = 1.0; |
bikeNomad | 22:abfed71656bd | 83 | |
bikeNomad | 23:33df42ff2541 | 84 | timeRunning.start(); |
bikeNomad | 22:abfed71656bd | 85 | |
bikeNomad | 24:feb1dae0403a | 86 | uint8_t r =0; |
bikeNomad | 24:feb1dae0403a | 87 | uint8_t g =0; |
bikeNomad | 24:feb1dae0403a | 88 | uint8_t b =0; |
bikeNomad | 24:feb1dae0403a | 89 | for (;;) { |
bikeNomad | 25:751c89f7e654 | 90 | if (r < 40) |
bikeNomad | 24:feb1dae0403a | 91 | r++; |
bikeNomad | 25:751c89f7e654 | 92 | else if (g < 40) |
bikeNomad | 24:feb1dae0403a | 93 | g++; |
bikeNomad | 25:751c89f7e654 | 94 | else if (b < 40) |
bikeNomad | 24:feb1dae0403a | 95 | b++; |
bikeNomad | 24:feb1dae0403a | 96 | else { |
bikeNomad | 24:feb1dae0403a | 97 | unsigned running = timeRunning.read_us(); |
bikeNomad | 24:feb1dae0403a | 98 | pc.printf("%u frames in %u usec = %u frames / sec\r\n", frames, running, frames * 1000000 / running); |
bikeNomad | 24:feb1dae0403a | 99 | break; |
bikeNomad | 24:feb1dae0403a | 100 | } |
bikeNomad | 24:feb1dae0403a | 101 | |
bikeNomad | 24:feb1dae0403a | 102 | wait(0.1); |
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 |