* add C027_Support fork

Fork of C027_Support by u-blox

SerialPipe.cpp

Committer:
mazgch
Date:
2013-11-19
Revision:
13:e2446fcdc246
Parent:
12:684b31d5482b
Child:
14:69c3e57ef0f5

File content as of revision 13:e2446fcdc246:

#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::writeable(void)    
{
    return _pipeTx.free();
}

int SerialPipe::putc(int c)    
{
    return _putc(c);
}

int SerialPipe::put(const void* buffer, int length, bool blocking)    
{ 
    int count = length;
    const char* ptr = (const char*)buffer;
    while (count && blocking)
    {
        int written = _pipeTx.put(ptr, count, false);
        ptr += written;
        count -= written;
        txStart();
    }
    return (length - count);
}

int SerialPipe::_putc(int c)    
{
    c = _pipeTx.putc(c);
    txStart();
    return c;
}

void SerialPipe::txIrqBuf(void)
{
    while (Serial::writeable() && _pipeTx.readable())
    {
        char c = _pipeTx.getc();
        Serial::_putc(c);
    }
}

void SerialPipe::txStart(void)
{
    __disable_irq();
    txIrqBuf();
    __enable_irq();
}

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

int SerialPipe::getc(void)                          
{ 
    return _getc(); 
} 

int SerialPipe::get(void* buffer, int length, bool blocking) 
{ 
    return _pipeRx.get((char*)buffer,length,blocking); 
}

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

void SerialPipe::rxIrqBuf(void)
{
    while (Serial::readable())
    {
        char c = Serial::_getc();
        if (_pipeRx.writeable())
            _pipeRx.putc(c);
        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* buffer, int length)
{
    int o = 0;
    int i = 0;
    int l = _pipeRx.start();
    while ((i < l) && (o < length))
    {
        int t = _pipeRx.next();
        i ++;
        if (t == '\r')     // terminate commands with carriage return
        {
            _pipeRx.done();
            if (length > o)
                buffer[o] = '\0';
            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)
            buffer[o++] = t;
        else if (o > 0)         // backspace
            o --;               // remove it
    }
    o = 0;
    if (length > 0)
        buffer[0] = '\0';
    return WAIT;
}