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@34:0f1968ca238a, 2014-11-15 (annotated)
- Committer:
- antoniorohit
- Date:
- Sat Nov 15 02:04:37 2014 +0000
- Revision:
- 34:0f1968ca238a
- Parent:
- 33:7973b70d4fab
Removed unneccesary files;
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 | |
bikeNomad | 23:33df42ff2541 | 4 | // per LED: 3 * 20 mA = 60mA max |
bikeNomad | 23:33df42ff2541 | 5 | // 60 LEDs: 60 * 60mA = 3600 mA max |
bikeNomad | 25:751c89f7e654 | 6 | // 120 LEDs: 7200 mA max |
bikeNomad | 22:abfed71656bd | 7 | unsigned const nLEDs = MAX_LEDS_PER_STRIP; |
bikeNomad | 22:abfed71656bd | 8 | |
antoniorohit | 33:7973b70d4fab | 9 | unsigned const DATA_OUT_PIN1 = 7; |
antoniorohit | 33:7973b70d4fab | 10 | unsigned const DATA_OUT_PIN2 = 0; |
antoniorohit | 33:7973b70d4fab | 11 | unsigned const DATA_OUT_PIN3 = 3; |
antoniorohit | 33:7973b70d4fab | 12 | unsigned const DATA_OUT_PIN4 = 4; |
antoniorohit | 33:7973b70d4fab | 13 | unsigned const DATA_OUT_PIN5 = 5; |
antoniorohit | 33:7973b70d4fab | 14 | unsigned const DATA_OUT_PIN6 = 6; |
antoniorohit | 33:7973b70d4fab | 15 | unsigned const DATA_OUT_PIN7 = 10; |
antoniorohit | 33:7973b70d4fab | 16 | unsigned const DATA_OUT_PIN8 = 11; |
bikeNomad | 22:abfed71656bd | 17 | |
bikeNomad | 22:abfed71656bd | 18 | Serial pc(USBTX, USBRX); |
bikeNomad | 20:b9d76e567637 | 19 | |
antoniorohit | 32:7b60b9add4b5 | 20 | static void clearScreen(WS2811 &strip) |
bikeNomad | 24:feb1dae0403a | 21 | { |
bikeNomad | 24:feb1dae0403a | 22 | unsigned nLEDs = strip.numPixels(); |
bikeNomad | 24:feb1dae0403a | 23 | for (unsigned i = 0; i < nLEDs; i++) { |
antoniorohit | 32:7b60b9add4b5 | 24 | strip.setPixelColor(i, 0, 0, 0); |
bikeNomad | 24:feb1dae0403a | 25 | } |
bikeNomad | 24:feb1dae0403a | 26 | strip.show(); |
bikeNomad | 24:feb1dae0403a | 27 | } |
bikeNomad | 24:feb1dae0403a | 28 | |
bikeNomad | 22:abfed71656bd | 29 | int main(void) |
bikeNomad | 22:abfed71656bd | 30 | { |
antoniorohit | 32:7b60b9add4b5 | 31 | pc.baud(9600); |
antoniorohit | 33:7973b70d4fab | 32 | WS2811 *lightStrip; |
bikeNomad | 25:751c89f7e654 | 33 | WS2811 lightStrip1(nLEDs, DATA_OUT_PIN1); |
bikeNomad | 25:751c89f7e654 | 34 | WS2811 lightStrip2(nLEDs, DATA_OUT_PIN2); |
antoniorohit | 33:7973b70d4fab | 35 | WS2811 lightStrip3(nLEDs, DATA_OUT_PIN3); |
antoniorohit | 33:7973b70d4fab | 36 | WS2811 lightStrip4(nLEDs, DATA_OUT_PIN4); |
antoniorohit | 33:7973b70d4fab | 37 | WS2811 lightStrip5(nLEDs, DATA_OUT_PIN5); |
antoniorohit | 33:7973b70d4fab | 38 | WS2811 lightStrip6(nLEDs, DATA_OUT_PIN6); |
antoniorohit | 33:7973b70d4fab | 39 | WS2811 lightStrip7(nLEDs, DATA_OUT_PIN7); |
antoniorohit | 33:7973b70d4fab | 40 | WS2811 lightStrip8(nLEDs, DATA_OUT_PIN8); |
antoniorohit | 33:7973b70d4fab | 41 | |
bikeNomad | 25:751c89f7e654 | 42 | lightStrip1.begin(); |
bikeNomad | 25:751c89f7e654 | 43 | lightStrip2.begin(); |
antoniorohit | 33:7973b70d4fab | 44 | lightStrip3.begin(); |
antoniorohit | 33:7973b70d4fab | 45 | lightStrip4.begin(); |
antoniorohit | 33:7973b70d4fab | 46 | lightStrip5.begin(); |
antoniorohit | 33:7973b70d4fab | 47 | lightStrip6.begin(); |
antoniorohit | 33:7973b70d4fab | 48 | lightStrip7.begin(); |
antoniorohit | 33:7973b70d4fab | 49 | lightStrip8.begin(); |
antoniorohit | 32:7b60b9add4b5 | 50 | |
antoniorohit | 32:7b60b9add4b5 | 51 | uint8_t q = 0; |
antoniorohit | 33:7973b70d4fab | 52 | char char_buff[10]; |
antoniorohit | 33:7973b70d4fab | 53 | char curr_char = 0; |
antoniorohit | 33:7973b70d4fab | 54 | |
bikeNomad | 24:feb1dae0403a | 55 | for (;;) { |
antoniorohit | 33:7973b70d4fab | 56 | /* I have defined a custom UART packet protocol |
antoniorohit | 33:7973b70d4fab | 57 | * A general packet may look like: |
antoniorohit | 34:0f1968ca238a | 58 | * |
antoniorohit | 33:7973b70d4fab | 59 | * array[0] x-coordinate [0, 31] |
antoniorohit | 33:7973b70d4fab | 60 | * array[1] y-coordinate [0, 15] |
antoniorohit | 33:7973b70d4fab | 61 | * array[2] R (5-bit) |
antoniorohit | 33:7973b70d4fab | 62 | * array[3] G (5-bit) |
antoniorohit | 33:7973b70d4fab | 63 | * array[4] B (5-bit) |
antoniorohit | 33:7973b70d4fab | 64 | * array[5] 254 (delimiter) |
antoniorohit | 33:7973b70d4fab | 65 | */ |
antoniorohit | 33:7973b70d4fab | 66 | while(curr_char != 255 && q < 7) |
antoniorohit | 33:7973b70d4fab | 67 | { |
antoniorohit | 33:7973b70d4fab | 68 | /* If there is UART traffic */ |
antoniorohit | 33:7973b70d4fab | 69 | if(pc.readable()) |
antoniorohit | 33:7973b70d4fab | 70 | { |
antoniorohit | 33:7973b70d4fab | 71 | curr_char = pc.getc(); |
antoniorohit | 33:7973b70d4fab | 72 | char_buff[q] = curr_char; |
antoniorohit | 33:7973b70d4fab | 73 | q++; |
antoniorohit | 33:7973b70d4fab | 74 | } |
antoniorohit | 33:7973b70d4fab | 75 | } |
antoniorohit | 33:7973b70d4fab | 76 | |
antoniorohit | 33:7973b70d4fab | 77 | if((char_buff[0] == 254) && (q >= 5)){ |
antoniorohit | 33:7973b70d4fab | 78 | switch(int(char_buff[2])){ |
antoniorohit | 33:7973b70d4fab | 79 | case 0: |
antoniorohit | 33:7973b70d4fab | 80 | lightStrip = &lightStrip1; |
antoniorohit | 33:7973b70d4fab | 81 | break; |
antoniorohit | 33:7973b70d4fab | 82 | case 1: |
antoniorohit | 33:7973b70d4fab | 83 | lightStrip = &lightStrip2; |
antoniorohit | 33:7973b70d4fab | 84 | break; |
antoniorohit | 33:7973b70d4fab | 85 | case 2: |
antoniorohit | 33:7973b70d4fab | 86 | lightStrip = &lightStrip3; |
antoniorohit | 33:7973b70d4fab | 87 | break; |
antoniorohit | 33:7973b70d4fab | 88 | case 3: |
antoniorohit | 33:7973b70d4fab | 89 | lightStrip = &lightStrip4; |
antoniorohit | 33:7973b70d4fab | 90 | break; |
antoniorohit | 33:7973b70d4fab | 91 | case 4: |
antoniorohit | 33:7973b70d4fab | 92 | lightStrip = &lightStrip5; |
antoniorohit | 33:7973b70d4fab | 93 | break; |
antoniorohit | 33:7973b70d4fab | 94 | case 5: |
antoniorohit | 33:7973b70d4fab | 95 | lightStrip = &lightStrip6; |
antoniorohit | 33:7973b70d4fab | 96 | break; |
antoniorohit | 33:7973b70d4fab | 97 | case 6: |
antoniorohit | 33:7973b70d4fab | 98 | lightStrip = &lightStrip7; |
antoniorohit | 33:7973b70d4fab | 99 | break; |
antoniorohit | 33:7973b70d4fab | 100 | case 7: |
antoniorohit | 33:7973b70d4fab | 101 | lightStrip = &lightStrip8; |
antoniorohit | 33:7973b70d4fab | 102 | break; |
antoniorohit | 33:7973b70d4fab | 103 | default: |
antoniorohit | 33:7973b70d4fab | 104 | lightStrip = &lightStrip1; |
antoniorohit | 33:7973b70d4fab | 105 | q = 0; |
antoniorohit | 33:7973b70d4fab | 106 | break; |
antoniorohit | 33:7973b70d4fab | 107 | } |
antoniorohit | 33:7973b70d4fab | 108 | |
antoniorohit | 33:7973b70d4fab | 109 | (*lightStrip).setPixelColor(int(char_buff[1]), int(char_buff[3]), int(char_buff[4]), int(char_buff[5])); |
antoniorohit | 33:7973b70d4fab | 110 | |
antoniorohit | 33:7973b70d4fab | 111 | (*lightStrip).show(); |
antoniorohit | 33:7973b70d4fab | 112 | pc.putc(48+int(char_buff[0])); |
antoniorohit | 33:7973b70d4fab | 113 | } |
antoniorohit | 33:7973b70d4fab | 114 | else{ |
antoniorohit | 33:7973b70d4fab | 115 | //clearScreen(*lightStrip); |
antoniorohit | 33:7973b70d4fab | 116 | } |
antoniorohit | 33:7973b70d4fab | 117 | q = 0; |
antoniorohit | 33:7973b70d4fab | 118 | curr_char = 0; |
antoniorohit | 33:7973b70d4fab | 119 | WS2811::startDMA(); |
antoniorohit | 32:7b60b9add4b5 | 120 | |
bikeNomad | 23:33df42ff2541 | 121 | } |
bikeNomad | 22:abfed71656bd | 122 | } |
bikeNomad | 22:abfed71656bd | 123 |