* add C027_Support fork

Fork of C027_Support by u-blox

SerialPipe.cpp

Committer:
mazgch
Date:
2013-11-10
Revision:
9:e7a5959ffae1
Child:
11:b084552b03fe

File content as of revision 9:e7a5959ffae1:

#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 s, bool t)    
{ 
    int c = 0;
    while (s && t)
    {
        c += _pipeTx.put(b, s, false);
        b ++;
        s --;
        // give a chance to start tx
        __disable_irq();
        txIrqBuf();
        __enable_irq();
    }
    return c;
}

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

// 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;
}