Ad van der Weiden / Mbed 2 deprecated tcpft

Dependencies:   mbed ConfigFile

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers msgSerial.h Source File

msgSerial.h

00001 #ifndef MSGSERIAL_H
00002 #define MSGSERIAL_H
00003 #include "mbed.h"
00004 #define SZ  7
00005 class msgSerial: public Serial {
00006 public:
00007 private:
00008     void (*onMsgReceived)(char*);
00009     char in[SZ], out[SZ];
00010     int ii, io;
00011     void readByte() {
00012         if (ii<SZ) {
00013             in[ii++] = getc();
00014             if (in[0] != 0x90) {//not a valid message
00015                 ii = 0;
00016                 return;
00017             }
00018             if (ii==SZ) {
00019                 if (onMsgReceived)
00020                     onMsgReceived(in);
00021                 ii = 0;
00022             }
00023         }
00024     }
00025     void writeByte() {
00026         if (io<SZ)
00027             putc(out[io++]);
00028     }
00029 public:
00030     msgSerial(PinName out, PinName in, void (*cb)(char*)): Serial(out, in) {
00031         onMsgReceived = cb;
00032         attach(this, &msgSerial::readByte, RxIrq);
00033         attach(this, &msgSerial::writeByte, TxIrq);
00034         ii = io = 0;
00035     }
00036     virtual ~msgSerial(){
00037         onMsgReceived = 0;
00038         attach(0, RxIrq);
00039         attach(0, TxIrq);
00040         }
00041     void write(char *m) {
00042         memcpy(out, m, SZ);
00043         io = 0;
00044         writeByte();
00045     }
00046 };
00047 #endif