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_Plus_Dialing by
SerialPipe.cpp
- Committer:
- mazgch
- Date:
- 2013-12-06
- Revision:
- 16:4a7ba1887e81
- Parent:
- 15:5eda64e5b9d1
- Child:
- 17:296d94a006b4
File content as of revision 16:4a7ba1887e81:
#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; } int SerialPipeEx::getResp(char* buffer, int length) { return getResp(buffer, length, &_pipeRx); } int SerialPipeEx::getResp(char* buffer, int length, Pipe<char>* pipe) { int o = 0; int i = 0; int l = pipe->start(); static const char erTxt[] = "ERROR\r\n"; static const char okTxt[] = "OK\r\n"; int er = 0; int ok = 0; while ((i < pipe->size()) && (o < length)) { int t = pipe->next(); i ++; buffer[o++] = t; ok = (t == okTxt[ok]) ? ok + 1 : 0; er = (t == erTxt[er]) ? er + 1 : 0; if ((okTxt[ok] == '\0') || (erTxt[er] == '\0')) { pipe->done(); if (length > o) buffer[o] = '\0'; return o; } } o = 0; if (length > 0) buffer[0] = '\0'; return WAIT; }