
A demo for serial, switch/case and for loops
Dependencies: mbed
RemoteControl.cpp@0:2616ded45fd4, 2014-06-13 (annotated)
- Committer:
- ethanharstad
- Date:
- Fri Jun 13 19:54:36 2014 +0000
- Revision:
- 0:2616ded45fd4
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ethanharstad | 0:2616ded45fd4 | 1 | #include "mbed.h" |
ethanharstad | 0:2616ded45fd4 | 2 | |
ethanharstad | 0:2616ded45fd4 | 3 | Serial pc(USBTX, USBRX); |
ethanharstad | 0:2616ded45fd4 | 4 | DigitalOut out(LED1); |
ethanharstad | 0:2616ded45fd4 | 5 | |
ethanharstad | 0:2616ded45fd4 | 6 | void handleInput(char in); |
ethanharstad | 0:2616ded45fd4 | 7 | void sing(); |
ethanharstad | 0:2616ded45fd4 | 8 | |
ethanharstad | 0:2616ded45fd4 | 9 | int main() { |
ethanharstad | 0:2616ded45fd4 | 10 | pc.printf("Ready!\n"); |
ethanharstad | 0:2616ded45fd4 | 11 | while(true) { |
ethanharstad | 0:2616ded45fd4 | 12 | if(pc.readable()) { |
ethanharstad | 0:2616ded45fd4 | 13 | char in = pc.getc(); |
ethanharstad | 0:2616ded45fd4 | 14 | handleInput(in); |
ethanharstad | 0:2616ded45fd4 | 15 | } |
ethanharstad | 0:2616ded45fd4 | 16 | } |
ethanharstad | 0:2616ded45fd4 | 17 | } |
ethanharstad | 0:2616ded45fd4 | 18 | |
ethanharstad | 0:2616ded45fd4 | 19 | void handleInput(char in) { |
ethanharstad | 0:2616ded45fd4 | 20 | switch(in) { |
ethanharstad | 0:2616ded45fd4 | 21 | case 'h': |
ethanharstad | 0:2616ded45fd4 | 22 | case 'H': |
ethanharstad | 0:2616ded45fd4 | 23 | out = 1; |
ethanharstad | 0:2616ded45fd4 | 24 | break; |
ethanharstad | 0:2616ded45fd4 | 25 | case 'l': |
ethanharstad | 0:2616ded45fd4 | 26 | case 'L': |
ethanharstad | 0:2616ded45fd4 | 27 | out = 0; |
ethanharstad | 0:2616ded45fd4 | 28 | break; |
ethanharstad | 0:2616ded45fd4 | 29 | case 's': |
ethanharstad | 0:2616ded45fd4 | 30 | case 'S': |
ethanharstad | 0:2616ded45fd4 | 31 | sing(); |
ethanharstad | 0:2616ded45fd4 | 32 | break; |
ethanharstad | 0:2616ded45fd4 | 33 | default: |
ethanharstad | 0:2616ded45fd4 | 34 | pc.printf("I don't know: \"%c\"\n", in); |
ethanharstad | 0:2616ded45fd4 | 35 | } |
ethanharstad | 0:2616ded45fd4 | 36 | } |
ethanharstad | 0:2616ded45fd4 | 37 | |
ethanharstad | 0:2616ded45fd4 | 38 | void sing() { |
ethanharstad | 0:2616ded45fd4 | 39 | for(int i = 'a'; i <= 'z'; i++) { // Loop through the alphabet |
ethanharstad | 0:2616ded45fd4 | 40 | pc.printf("%c", i); // Say the letter |
ethanharstad | 0:2616ded45fd4 | 41 | wait(0.1); // Wait a bit |
ethanharstad | 0:2616ded45fd4 | 42 | } // End the for loop |
ethanharstad | 0:2616ded45fd4 | 43 | pc.printf("\n"); // Print a new line |
ethanharstad | 0:2616ded45fd4 | 44 | } |