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
Diff: SerialPipe.cpp
- Revision:
- 9:e7a5959ffae1
- Child:
- 11:b084552b03fe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SerialPipe.cpp Sun Nov 10 16:39:42 2013 +0000
@@ -0,0 +1,98 @@
+#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;
+}
