Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of C027_Support by
SerialPipe.cpp
- Committer:
- mazgch
- Date:
- 2013-11-19
- Revision:
- 14:69c3e57ef0f5
- Parent:
- 13:e2446fcdc246
- Child:
- 15:5eda64e5b9d1
File content as of revision 14:69c3e57ef0f5:
#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)
{
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;
}
