Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

SerialPipe.h

Committer:
mazgch
Date:
2013-10-25
Revision:
2:b6012cd91657
Parent:
0:cb2d45baaca3
Child:
9:e7a5959ffae1

File content as of revision 2:b6012cd91657:

#pragma once 

#include "mbed.h"
#include "Pipe.h"
#include <ctype.h>

class SerialPipe : public Serial
{
protected:
    Pipe<char> _pipe;
private:
    void rxIrqBuf(void)
    {
        while (serial_readable(&_serial))
            _pipe.putc(serial_getc(&_serial));
    }
public:
    SerialPipe(PinName tx, PinName rx, int rxSize = 128, const char* name = NULL) 
        : Serial(tx,rx,name), _pipe(rxSize)
    {
        attach(this, &SerialPipe::rxIrqBuf, RxIrq);
    }
    virtual ~SerialPipe(void)
    {
        attach(NULL, RxIrq);
    }
    // tx channel
    int writeBuf(char* b, int s)    
    { 
        for (int i = 0; i < s; i ++)
            putc(b[i]);
        return s; 
    }
    // rx channel
    int readable(void)              { return _pipe.readable() ? 1 : 0; } 
    int getc(void)                  { return _pipe.getc(); } 
    int readBuf(char* b, int s)     { return _pipe.get(b,s); }

    #define WAIT      -1
    #define NOT_FOUND  0

    // special parsing
    int getLine(char* b, int s)
    {
        int o = 0;
        int i = 0;
        int l = _pipe.start();
        while ((i < l) && (o < s))
        {
            int t = _pipe.next();
            i ++;
            if (t == '\r')     // terminate commands with carriage return
            {
                 _pipe.done();
                 return o;          // if enter send the zero char
            }
            else if (t == '\n')     // skip/filter new line 
                 /* skip */;
            else if (t != '\b')     // normal char (no backspace)
                b[o++] = t;
            else if (o > 0)         // backspace
                o --;               // remove it
        }
        o = 0;
        return WAIT;
    }
};