(╯°□°)╯︵ ┻━┻
Dependencies: mbed
SerialEvent.h@0:d43885c9d5d8, 2016-06-22 (annotated)
- Committer:
- Andi104
- Date:
- Wed Jun 22 06:34:36 2016 +0000
- Revision:
- 0:d43885c9d5d8
(??????? ???
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Andi104 | 0:d43885c9d5d8 | 1 | #include "mbed.h" |
Andi104 | 0:d43885c9d5d8 | 2 | #ifndef SERIALEVENT_H |
Andi104 | 0:d43885c9d5d8 | 3 | #define SERIALEVENT_H |
Andi104 | 0:d43885c9d5d8 | 4 | |
Andi104 | 0:d43885c9d5d8 | 5 | const uint8_t STRMAX = 20; |
Andi104 | 0:d43885c9d5d8 | 6 | const char EOT = '.'; |
Andi104 | 0:d43885c9d5d8 | 7 | const char CRLF = '\n'; |
Andi104 | 0:d43885c9d5d8 | 8 | // ---------------- Serial RS232 Event Class -------------------------- |
Andi104 | 0:d43885c9d5d8 | 9 | class SerialEvent { |
Andi104 | 0:d43885c9d5d8 | 10 | Serial _pc; |
Andi104 | 0:d43885c9d5d8 | 11 | void _risingISR(); |
Andi104 | 0:d43885c9d5d8 | 12 | char _str[STRMAX]; |
Andi104 | 0:d43885c9d5d8 | 13 | volatile bool _strOkFlag; |
Andi104 | 0:d43885c9d5d8 | 14 | int _index; |
Andi104 | 0:d43885c9d5d8 | 15 | |
Andi104 | 0:d43885c9d5d8 | 16 | |
Andi104 | 0:d43885c9d5d8 | 17 | public: |
Andi104 | 0:d43885c9d5d8 | 18 | SerialEvent(PinName tx, PinName rx) : _pc(tx, rx) { // create the Serial on the pin specified to SwEvent |
Andi104 | 0:d43885c9d5d8 | 19 | _pc.attach(this, &SerialEvent::pc_recv); // attach DataReceive-function of this SerialEvent instance |
Andi104 | 0:d43885c9d5d8 | 20 | _strOkFlag = false; |
Andi104 | 0:d43885c9d5d8 | 21 | _index=0; |
Andi104 | 0:d43885c9d5d8 | 22 | |
Andi104 | 0:d43885c9d5d8 | 23 | } |
Andi104 | 0:d43885c9d5d8 | 24 | void pc_recv(); |
Andi104 | 0:d43885c9d5d8 | 25 | void getString(char st[]); |
Andi104 | 0:d43885c9d5d8 | 26 | int checkFlag(); // must in do-condition (while(true)-loop) continuously interrogated |
Andi104 | 0:d43885c9d5d8 | 27 | }; |
Andi104 | 0:d43885c9d5d8 | 28 | // ---------------- Serial Event Class Methodes -------------------------- |
Andi104 | 0:d43885c9d5d8 | 29 | void SerialEvent::getString(char st[]) { |
Andi104 | 0:d43885c9d5d8 | 30 | for( int i=0; i <= _index; i++) |
Andi104 | 0:d43885c9d5d8 | 31 | st[i] = _str[i]; |
Andi104 | 0:d43885c9d5d8 | 32 | _index=0; |
Andi104 | 0:d43885c9d5d8 | 33 | } |
Andi104 | 0:d43885c9d5d8 | 34 | |
Andi104 | 0:d43885c9d5d8 | 35 | void SerialEvent::pc_recv() { |
Andi104 | 0:d43885c9d5d8 | 36 | char c; |
Andi104 | 0:d43885c9d5d8 | 37 | while(_pc.readable()){ |
Andi104 | 0:d43885c9d5d8 | 38 | c = _pc.getc(); |
Andi104 | 0:d43885c9d5d8 | 39 | if((c != CRLF) && (_index < STRMAX)) { |
Andi104 | 0:d43885c9d5d8 | 40 | _str[_index++] = c; |
Andi104 | 0:d43885c9d5d8 | 41 | } |
Andi104 | 0:d43885c9d5d8 | 42 | } |
Andi104 | 0:d43885c9d5d8 | 43 | if(( c == EOT)) { // end: . string not empty |
Andi104 | 0:d43885c9d5d8 | 44 | if(_index >= 1) { |
Andi104 | 0:d43885c9d5d8 | 45 | _strOkFlag = true; |
Andi104 | 0:d43885c9d5d8 | 46 | _str[--_index] = 0; |
Andi104 | 0:d43885c9d5d8 | 47 | } |
Andi104 | 0:d43885c9d5d8 | 48 | } |
Andi104 | 0:d43885c9d5d8 | 49 | } |
Andi104 | 0:d43885c9d5d8 | 50 | |
Andi104 | 0:d43885c9d5d8 | 51 | int SerialEvent::checkFlag() { |
Andi104 | 0:d43885c9d5d8 | 52 | if( _strOkFlag ) { |
Andi104 | 0:d43885c9d5d8 | 53 | _strOkFlag = false; |
Andi104 | 0:d43885c9d5d8 | 54 | return 1; |
Andi104 | 0:d43885c9d5d8 | 55 | } |
Andi104 | 0:d43885c9d5d8 | 56 | return 0; |
Andi104 | 0:d43885c9d5d8 | 57 | } |
Andi104 | 0:d43885c9d5d8 | 58 | #endif |