asdf

Dependencies:   mbed

main.cpp

Committer:
andreashatzl
Date:
2015-06-10
Revision:
0:24f914e903b4
Child:
1:756c9df9881f

File content as of revision 0:24f914e903b4:

/* 
Serial Communication with a PC
The mbed Microcontroller can communicate with a host PC through a "USB Virtual Serial Port" 
over the same USB cable that is used for programming.
This enables you to:
a) Print out messages to a host PC terminal (useful for debugging!)
b) Read input from the host PC keyboard
c) Communicate with applications and programming languages running on the host PC 
   that can communicate with a serial port, e.g. perl, python, java, c# and so on.
.. see http://mbed.org/handbook/SerialPC 

The C stdin, stdout and stderr file handles are also defaulted to the PC serial connection
standard calls are: printf, scanf, getc, putc, ..
*/

#include "mbed.h"
// global vars and objects
BusOut doLeds(LED1,LED2,LED3,LED4);
DigitalOut led2(LED2);
Serial pc(USBTX, USBRX); // tx, rx    ; is default !!! (9600, 8N1)
char recChar=0;
bool recFlag=false;
char recArr[20];
int index=0;

// functions
void flushSerialBuffer() { 
    while (pc.readable()) { 
        pc.getc(); 
    } 
}
    
void readData() {
    recChar = pc.getc();
    recArr[index] = recChar;
    index++;
    
        recFlag = true;
        recArr[index] = 0;
        index = 0;
        pc.printf(" - That's the input: %s\r\n", recArr);  
        
        if(recArr[0]=='a')
        {
            doLeds = 0;
            };
        if(recArr[0]=='b')
        {
            doLeds = 16;
            };
        
        flushSerialBuffer();
        
    
}
    
int main() {
//    pc.baud(115200);
    pc.baud(38400);
    pc.format(8, SerialBase::Odd, 2);
    led2 = 1;

    flushSerialBuffer();
    pc.printf("Hatzl Andreas: Gruppe A\r\n Bitte geben Sie einen Befehl ein:\r\n");
    pc.attach(&readData);
    
    while(1) {
        if (recFlag) {
            flushSerialBuffer();
//            pc.printf(" - That's the input: %s\r\n", recArr);  // non reantrant function
            recFlag = false;
            led2 = !led2;
        }
    }
}