Henk Meewis / Mbed 2 deprecated frdm_echo

Dependencies:   mbed

main.cpp

Committer:
silverpanda
Date:
2014-04-14
Revision:
4:dfb672184380
Parent:
3:7188bd978801
Child:
5:03b7c237c4c4

File content as of revision 4:dfb672184380:

// Print "Hello World" to the PC

#include "mbed.h"

Serial usbSerial(USBTX, USBRX);
Ticker messageTicker, scanTicker;
bool scanUSBSerialRxFlag;

void sendText(char *thisText)
{
    // this can send any text
    usbSerial.printf(thisText);
}
//-----------------------------------------------------------------------------

void sendHelloWorld()
{
    // sends the first greeting
    sendText("** Hello World **\n\n> ");
}
//-----------------------------------------------------------------------------

void scanUSBSerialRx()
{
    // check if there is something to read
    if(usbSerial.readable()) {
        
        // if so ...
        char character = usbSerial.getc();
        
        // see if this is a semi colon or a carriage return
        // if so, give a new line cursor
        if((character == ';') || (charac ter == 13)) usbSerial.printf("\n> ");
        
        // if not, just print the character
        else usbSerial.printf("%c", character);        
    }
    
    // reset the flag
    scanUSBSerialRxFlag = false;
}
//-----------------------------------------------------------------------------

void setScanUSBSerialRxFlag()
{
    scanUSBSerialRxFlag = true;
}
//-----------------------------------------------------------------------------

void initMain()
{
    // increase the baud rate for the USB serial port
    usbSerial.baud(115200);
    
    // send greeting with first cursor
    sendHelloWorld();
    
    // start polling for characters
    scanTicker.attach(&setScanUSBSerialRxFlag, 0.01);
    scanUSBSerialRxFlag = false;
}
//-----------------------------------------------------------------------------

int main() {
    initMain();
    
    while(true) {
        
        // check the flag 
        if(scanUSBSerialRxFlag) scanUSBSerialRx();
        
        // give the main loop some time
        wait(0.02);
    }
}
//-----------------------------------------------------------------------------