Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@4:dfb672184380, 2014-04-14 (annotated)
- Committer:
- silverpanda
- Date:
- Mon Apr 14 00:45:29 2014 +0000
- Revision:
- 4:dfb672184380
- Parent:
- 3:7188bd978801
- Child:
- 5:03b7c237c4c4
added comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
silverpanda | 0:6898e364f29a | 1 | // Print "Hello World" to the PC |
silverpanda | 0:6898e364f29a | 2 | |
silverpanda | 0:6898e364f29a | 3 | #include "mbed.h" |
silverpanda | 0:6898e364f29a | 4 | |
silverpanda | 0:6898e364f29a | 5 | Serial usbSerial(USBTX, USBRX); |
silverpanda | 1:20033a89af0e | 6 | Ticker messageTicker, scanTicker; |
silverpanda | 3:7188bd978801 | 7 | bool scanUSBSerialRxFlag; |
silverpanda | 0:6898e364f29a | 8 | |
silverpanda | 0:6898e364f29a | 9 | void sendText(char *thisText) |
silverpanda | 0:6898e364f29a | 10 | { |
silverpanda | 4:dfb672184380 | 11 | // this can send any text |
silverpanda | 0:6898e364f29a | 12 | usbSerial.printf(thisText); |
silverpanda | 0:6898e364f29a | 13 | } |
silverpanda | 0:6898e364f29a | 14 | //----------------------------------------------------------------------------- |
silverpanda | 0:6898e364f29a | 15 | |
silverpanda | 1:20033a89af0e | 16 | void sendHelloWorld() |
silverpanda | 0:6898e364f29a | 17 | { |
silverpanda | 4:dfb672184380 | 18 | // sends the first greeting |
silverpanda | 3:7188bd978801 | 19 | sendText("** Hello World **\n\n> "); |
silverpanda | 1:20033a89af0e | 20 | } |
silverpanda | 1:20033a89af0e | 21 | //----------------------------------------------------------------------------- |
silverpanda | 1:20033a89af0e | 22 | |
silverpanda | 1:20033a89af0e | 23 | void scanUSBSerialRx() |
silverpanda | 1:20033a89af0e | 24 | { |
silverpanda | 4:dfb672184380 | 25 | // check if there is something to read |
silverpanda | 1:20033a89af0e | 26 | if(usbSerial.readable()) { |
silverpanda | 4:dfb672184380 | 27 | |
silverpanda | 4:dfb672184380 | 28 | // if so ... |
silverpanda | 1:20033a89af0e | 29 | char character = usbSerial.getc(); |
silverpanda | 4:dfb672184380 | 30 | |
silverpanda | 4:dfb672184380 | 31 | // see if this is a semi colon or a carriage return |
silverpanda | 4:dfb672184380 | 32 | // if so, give a new line cursor |
silverpanda | 4:dfb672184380 | 33 | if((character == ';') || (charac ter == 13)) usbSerial.printf("\n> "); |
silverpanda | 4:dfb672184380 | 34 | |
silverpanda | 4:dfb672184380 | 35 | // if not, just print the character |
silverpanda | 2:53d8e47c5171 | 36 | else usbSerial.printf("%c", character); |
silverpanda | 1:20033a89af0e | 37 | } |
silverpanda | 4:dfb672184380 | 38 | |
silverpanda | 4:dfb672184380 | 39 | // reset the flag |
silverpanda | 1:20033a89af0e | 40 | scanUSBSerialRxFlag = false; |
silverpanda | 1:20033a89af0e | 41 | } |
silverpanda | 1:20033a89af0e | 42 | //----------------------------------------------------------------------------- |
silverpanda | 1:20033a89af0e | 43 | |
silverpanda | 1:20033a89af0e | 44 | void setScanUSBSerialRxFlag() |
silverpanda | 1:20033a89af0e | 45 | { |
silverpanda | 1:20033a89af0e | 46 | scanUSBSerialRxFlag = true; |
silverpanda | 1:20033a89af0e | 47 | } |
silverpanda | 1:20033a89af0e | 48 | //----------------------------------------------------------------------------- |
silverpanda | 1:20033a89af0e | 49 | |
silverpanda | 1:20033a89af0e | 50 | void initMain() |
silverpanda | 1:20033a89af0e | 51 | { |
silverpanda | 4:dfb672184380 | 52 | // increase the baud rate for the USB serial port |
silverpanda | 1:20033a89af0e | 53 | usbSerial.baud(115200); |
silverpanda | 1:20033a89af0e | 54 | |
silverpanda | 4:dfb672184380 | 55 | // send greeting with first cursor |
silverpanda | 2:53d8e47c5171 | 56 | sendHelloWorld(); |
silverpanda | 1:20033a89af0e | 57 | |
silverpanda | 4:dfb672184380 | 58 | // start polling for characters |
silverpanda | 1:20033a89af0e | 59 | scanTicker.attach(&setScanUSBSerialRxFlag, 0.01); |
silverpanda | 1:20033a89af0e | 60 | scanUSBSerialRxFlag = false; |
silverpanda | 0:6898e364f29a | 61 | } |
silverpanda | 0:6898e364f29a | 62 | //----------------------------------------------------------------------------- |
silverpanda | 0:6898e364f29a | 63 | |
silverpanda | 0:6898e364f29a | 64 | int main() { |
silverpanda | 1:20033a89af0e | 65 | initMain(); |
silverpanda | 1:20033a89af0e | 66 | |
silverpanda | 4:dfb672184380 | 67 | while(true) { |
silverpanda | 4:dfb672184380 | 68 | |
silverpanda | 4:dfb672184380 | 69 | // check the flag |
silverpanda | 1:20033a89af0e | 70 | if(scanUSBSerialRxFlag) scanUSBSerialRx(); |
silverpanda | 4:dfb672184380 | 71 | |
silverpanda | 4:dfb672184380 | 72 | // give the main loop some time |
silverpanda | 1:20033a89af0e | 73 | wait(0.02); |
silverpanda | 0:6898e364f29a | 74 | } |
silverpanda | 0:6898e364f29a | 75 | } |
silverpanda | 0:6898e364f29a | 76 | //----------------------------------------------------------------------------- |