Listens for instructions over serial and controls some NeoPixels.
Dependencies: NeoStrip SerialDriver mbed-rtos mbed
main.cpp@1:6feb61c0e4a1, 2016-01-30 (annotated)
- Committer:
- rossng
- Date:
- Sat Jan 30 20:26:13 2016 +0000
- Revision:
- 1:6feb61c0e4a1
- Parent:
- 0:0513e7187b87
- Child:
- 2:868c8a43e4b7
Can receive simple message over serial
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rossng | 0:0513e7187b87 | 1 | #include "mbed.h" |
rossng | 0:0513e7187b87 | 2 | #include "SerialDriver.h" |
rossng | 0:0513e7187b87 | 3 | |
rossng | 1:6feb61c0e4a1 | 4 | SerialDriver rpi(p9, p10); |
rossng | 1:6feb61c0e4a1 | 5 | SerialDriver pc(USBTX, USBRX); |
rossng | 0:0513e7187b87 | 6 | DigitalOut myled(LED1); |
rossng | 0:0513e7187b87 | 7 | |
rossng | 0:0513e7187b87 | 8 | int flash(); |
rossng | 0:0513e7187b87 | 9 | |
rossng | 0:0513e7187b87 | 10 | int main() |
rossng | 0:0513e7187b87 | 11 | { |
rossng | 0:0513e7187b87 | 12 | // setup serial port |
rossng | 1:6feb61c0e4a1 | 13 | rpi.baud(115200); |
rossng | 0:0513e7187b87 | 14 | |
rossng | 0:0513e7187b87 | 15 | char* line_buffer = (char*) malloc(100*sizeof(char)); |
rossng | 0:0513e7187b87 | 16 | |
rossng | 1:6feb61c0e4a1 | 17 | char c = rpi.getc(); |
rossng | 0:0513e7187b87 | 18 | int pos = 0; |
rossng | 0:0513e7187b87 | 19 | |
rossng | 0:0513e7187b87 | 20 | while (c != '\n' && c != '\r') { |
rossng | 0:0513e7187b87 | 21 | line_buffer[pos] = c; |
rossng | 1:6feb61c0e4a1 | 22 | c = rpi.getc(); |
rossng | 1:6feb61c0e4a1 | 23 | pos++; |
rossng | 0:0513e7187b87 | 24 | } |
rossng | 0:0513e7187b87 | 25 | |
rossng | 1:6feb61c0e4a1 | 26 | line_buffer[pos] = '\0'; |
rossng | 1:6feb61c0e4a1 | 27 | |
rossng | 1:6feb61c0e4a1 | 28 | rpi.printf("ack\n"); |
rossng | 0:0513e7187b87 | 29 | |
rossng | 0:0513e7187b87 | 30 | int result = strcmp(line_buffer, "hello"); |
rossng | 0:0513e7187b87 | 31 | |
rossng | 0:0513e7187b87 | 32 | if (result == 0) { |
rossng | 0:0513e7187b87 | 33 | flash(); |
rossng | 1:6feb61c0e4a1 | 34 | } else { |
rossng | 1:6feb61c0e4a1 | 35 | pc.printf("Did not match.\n"); |
rossng | 1:6feb61c0e4a1 | 36 | pc.printf(line_buffer); |
rossng | 0:0513e7187b87 | 37 | } |
rossng | 0:0513e7187b87 | 38 | } |
rossng | 0:0513e7187b87 | 39 | |
rossng | 0:0513e7187b87 | 40 | |
rossng | 0:0513e7187b87 | 41 | int flash() { |
rossng | 0:0513e7187b87 | 42 | while(1) { |
rossng | 0:0513e7187b87 | 43 | myled = 1; |
rossng | 0:0513e7187b87 | 44 | wait(0.2); |
rossng | 0:0513e7187b87 | 45 | myled = 0; |
rossng | 0:0513e7187b87 | 46 | wait(0.2); |
rossng | 0:0513e7187b87 | 47 | } |
rossng | 0:0513e7187b87 | 48 | } |