Ross Gardiner / Mbed 2 deprecated PiballNeoController

Dependencies:   NeoStrip SerialDriver mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "SerialDriver.h"
00003 #include "NeoStrip.h"
00004 #include "gt.h"
00005  
00006 #define NUM_INDIVIDUALS 7
00007 #define NUM_RING 16
00008 #define NUM_GRID 40
00009 #define NUM_ROWS 5
00010 #define NUM_COLUMNS 8
00011 
00012 SerialDriver rpi(p9, p10);
00013 SerialDriver pc(USBTX, USBRX);
00014 DigitalOut led1(LED1);
00015 DigitalOut led2(LED2);
00016 DigitalOut led3(LED3);
00017 DigitalOut led4(LED4);
00018 NeoStrip strip(p18, NUM_INDIVIDUALS + NUM_RING + NUM_GRID);
00019 Ticker t;
00020 
00021 enum Mode { spin, flat };
00022 Mode mode = spin;
00023 
00024 int ring_position = 0;
00025 int grid_col = 0;
00026 
00027 void set_leds(int to);
00028 void set_pattern(NeoStrip strip, uint8_t r, uint8_t g, uint8_t b);
00029 void spin_update();
00030 void flat_update();
00031 void set_mode(Mode m);
00032 
00033 int main() {
00034     pc.baud(115200);
00035     rpi.baud(115200);
00036     pc.printf("MBED: startup\r\n");
00037     
00038     //set_pattern(strip, 255, 255, 255);
00039 
00040     set_mode(flat);
00041     
00042     //char* line_buffer = (char*) malloc(100*sizeof(char));
00043     
00044     while (1) {
00045         char c = rpi.getc();
00046         
00047         while (c != '\x02') {
00048             c = rpi.getc();
00049         }
00050         
00051         c = rpi.getc(); // get actual mode message
00052         
00053         wait(0.1);
00054         rpi.putc('\x3A');
00055         pc.printf("MBED: sent ack after receiving %c\r\n", c);
00056         
00057         
00058         switch (c) {
00059             case 'a':
00060                 pc.printf("MBED: switching to spin mode\r\n");
00061                 set_mode(spin);
00062                 break;
00063             case 'b':
00064                 pc.printf("MBED: switching to flat mode\r\n");
00065                 set_mode(flat);
00066                 break;
00067         }
00068     }
00069 }
00070 
00071 void set_mode(Mode m) {
00072     mode = m;
00073     
00074     switch (mode) {
00075         case spin:
00076             t.detach();
00077             t.attach(&spin_update, 0.05);
00078             break;
00079         case flat:
00080             t.detach();
00081             t.attach(&flat_update, 0.05);
00082             break;
00083     }
00084 }
00085 
00086 void set_leds(int to) {
00087     led1 = led2 = led3 = led4 = to;
00088 }
00089 
00090 void set_pattern(NeoStrip strip, uint8_t r, uint8_t g, uint8_t b) {
00091     strip.setBrightness(0.05);
00092     //strip.setPixels(0, N, gt_img);
00093     for (int p = 0; p < 64; p++) {
00094         strip.setPixel(p, r, g, b);
00095     }
00096     strip.write();
00097 }
00098 
00099 void spin_update() {
00100     strip.setBrightness(0.05);
00101     // update ring
00102     strip.setPixel(NUM_INDIVIDUALS + ring_position, 255, 255, 255);
00103     ring_position = (ring_position + 1) % NUM_RING;
00104     strip.setPixel(NUM_INDIVIDUALS + ring_position, 255, 0, 0);
00105     
00106     // update grid
00107     for (int i = grid_col; i < NUM_GRID; i += NUM_COLUMNS) {   
00108         strip.setPixel(NUM_INDIVIDUALS + NUM_RING + i, 255, 255, 255);
00109     }
00110     grid_col = (grid_col + 1) % NUM_COLUMNS;
00111     
00112     for (int i = grid_col; i < NUM_GRID; i += NUM_COLUMNS) {   
00113         strip.setPixel(NUM_INDIVIDUALS + NUM_RING + i, 255, 0, 0);
00114     }
00115     
00116     strip.write();    
00117 }
00118 
00119 void flat_update() {
00120     strip.setBrightness(0.05);
00121     
00122     for (int i = 0; i < NUM_INDIVIDUALS + NUM_RING + NUM_GRID; i++) {
00123         strip.setPixel(i, 255, 0, 0);
00124     }
00125     
00126     strip.write();
00127 }