C027 updated to work with latest mBed libraries

Dependents:   Cellular_HelloMQTT UBLOXModemDriver UBLOXMQTTDriver

Fork of C027_Support by u-blox

SerialPipe.cpp

Committer:
mazgch
Date:
2013-11-22
Revision:
15:5eda64e5b9d1
Parent:
14:69c3e57ef0f5
Child:
16:4a7ba1887e81

File content as of revision 15:5eda64e5b9d1:

#pragma once 

#include "SerialPipe.h"

SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize) 
    : _SerialPipeBase(tx,rx), _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)    
{
    c = _pipeTx.putc(c);
    txStart();
    return c;
}

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

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

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

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

int SerialPipe::getc(void)                          
{ 
    if (!_pipeRx.readable())
        return EOF;
    return _pipeRx.getc(); 
} 

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

void SerialPipe::rxIrqBuf(void)
{
    while (_SerialPipeBase::readable())
    {
        char c = _SerialPipeBase::_base_getc();
        if (_pipeRx.writeable())
            _pipeRx.putc(c);
        else 
            /* overflow */;
    }
}

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

SerialPipeEx::SerialPipeEx(PinName tx, PinName rx, int rxSize, int txSize) 
    : SerialPipe(tx,rx,rxSize,txSize)
{}

int SerialPipeEx::getLine(char* buffer, int length)
{
    return getLine(buffer, length, &_pipeRx);
}

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