HIMBED_3AHELI / Mbed 2 deprecated Kager_Project_Chap

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialEvent.h Source File

SerialEvent.h

00001 #include "mbed.h"
00002 #ifndef SERIALEVENT_H
00003 #define SERIALEVENT_H
00004  
00005 const uint8_t STRMAX = 20;  
00006 const char EOT = ' ';
00007 const char CRLF = '\n';
00008 // ---------------- Serial RS232 Event Class  --------------------------
00009 class SerialEvent {
00010         Serial _pc;
00011         void _risingISR();
00012         char _str[STRMAX]; 
00013         volatile bool _strOkFlag;
00014         int _index;
00015  
00016  
00017     public:
00018         SerialEvent(PinName tx, PinName rx) : _pc(tx, rx) { // create the Serial on the pin specified to SwEvent
00019             _pc.attach(this, &SerialEvent::pc_recv);        // attach DataReceive-function of this SerialEvent instance 
00020             _strOkFlag = false;
00021             _index=0;
00022  
00023         }
00024         void pc_recv();
00025         void getString(char st[]);
00026         int checkFlag();                                    // must in do-condition (while(true)-loop) continuously interrogated
00027 };
00028 // ---------------- Serial Event Class Methodes --------------------------
00029 void SerialEvent::getString(char st[]) {
00030     for( int i=0; i <= _index; i++)
00031         st[i] = _str[i];
00032     _index=0;
00033 }
00034  
00035 void SerialEvent::pc_recv() {
00036     char c;
00037     while(_pc.readable()){
00038         c = _pc.getc();
00039         if((c != CRLF) && (_index < STRMAX)) {
00040             _str[_index++] = c;
00041         }
00042     }
00043     if(( c == EOT)) {           // end: . string not empty
00044         if(_index >= 1) {
00045             _strOkFlag = true;
00046             _str[--_index] = 0; 
00047         }
00048     }
00049 }
00050  
00051 int SerialEvent::checkFlag() {
00052     if( _strOkFlag ) {
00053         _strOkFlag = false; 
00054         return 1;
00055     }
00056     return 0;
00057 }
00058 #endif