Add a bunch of APNs

Fork of C027_Support by Xinlei Cao

SerialPipe.cpp

Committer:
mazgch
Date:
2013-11-14
Revision:
11:b084552b03fe
Parent:
9:e7a5959ffae1
Child:
12:684b31d5482b

File content as of revision 11:b084552b03fe:

#pragma once 

#include "SerialPipe.h"

SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize, const char* name) 
    : Serial(tx,rx,name), _pipeRx(rxSize), _pipeTx(txSize)
{
    attach(this, &SerialPipe::rxIrqBuf, RxIrq);
    attach(this, &SerialPipe::txIrqBuf, TxIrq);
}

SerialPipe::~SerialPipe(void)
{
    attach(NULL, RxIrq);
    attach(NULL, TxIrq);
}

// tx channel
int SerialPipe::put(const char* b, int n, bool t)    
{ 
    int c = n;
    while (c && t)
    {
        int i = _pipeTx.put(b, c, false);
        b += i;
        c -= i;
        // give a chance to start tx
        __disable_irq();
        txIrqBuf();
        __enable_irq();
    }
    return (n - c);
}

void SerialPipe::txIrqBuf(void)
{
    while (serial_writable(&_serial) && _pipeTx.readable())
    {
        char ch = _pipeTx.getc();
        serial_putc(&_serial, ch);
    }
}

// rx channel
int SerialPipe::readable(void)                      
{ 
    return _pipeRx.readable(); 
} 

int SerialPipe::getc(void)                          
{ 
    return _pipeRx.getc(); 
} 

int SerialPipe::get(char* b, int s, bool t) 
{ 
    return _pipeRx.get(b,s,t); 
}

void SerialPipe::rxIrqBuf(void)
{
    while (serial_readable(&_serial))
    {
        char ch = serial_getc(&_serial);
        if (_pipeRx.writeable())
            _pipeRx.putc(ch);
        else 
            /* overflow */;
    }
}

// -----------------------------------------------------------------------

SerialPipeEx::SerialPipeEx(PinName tx, PinName rx, int rxSize, int txSize, const char* name) 
    : SerialPipe(tx,rx,rxSize,txSize,name)
{}

int SerialPipeEx::getLine(char* b, int s)
{
    int o = 0;
    int i = 0;
    int l = _pipeRx.start();
    while ((i < l) && (o < s))
    {
        int t = _pipeRx.next();
        i ++;
        if (t == '\r')     // terminate commands with carriage return
        {
             _pipeRx.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;
}