Add a bunch of APNs
Fork of C027_Support by
SerialPipe.cpp@12:684b31d5482b, 2013-11-15 (annotated)
- Committer:
- mazgch
- Date:
- Fri Nov 15 13:33:39 2013 +0000
- Revision:
- 12:684b31d5482b
- Parent:
- 11:b084552b03fe
- Child:
- 13:e2446fcdc246
zero terminate
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 9:e7a5959ffae1 | 1 | #pragma once |
mazgch | 9:e7a5959ffae1 | 2 | |
mazgch | 9:e7a5959ffae1 | 3 | #include "SerialPipe.h" |
mazgch | 9:e7a5959ffae1 | 4 | |
mazgch | 9:e7a5959ffae1 | 5 | SerialPipe::SerialPipe(PinName tx, PinName rx, int rxSize, int txSize, const char* name) |
mazgch | 9:e7a5959ffae1 | 6 | : Serial(tx,rx,name), _pipeRx(rxSize), _pipeTx(txSize) |
mazgch | 9:e7a5959ffae1 | 7 | { |
mazgch | 9:e7a5959ffae1 | 8 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
mazgch | 9:e7a5959ffae1 | 9 | attach(this, &SerialPipe::txIrqBuf, TxIrq); |
mazgch | 9:e7a5959ffae1 | 10 | } |
mazgch | 9:e7a5959ffae1 | 11 | |
mazgch | 9:e7a5959ffae1 | 12 | SerialPipe::~SerialPipe(void) |
mazgch | 9:e7a5959ffae1 | 13 | { |
mazgch | 9:e7a5959ffae1 | 14 | attach(NULL, RxIrq); |
mazgch | 9:e7a5959ffae1 | 15 | attach(NULL, TxIrq); |
mazgch | 9:e7a5959ffae1 | 16 | } |
mazgch | 9:e7a5959ffae1 | 17 | |
mazgch | 9:e7a5959ffae1 | 18 | // tx channel |
mazgch | 11:b084552b03fe | 19 | int SerialPipe::put(const char* b, int n, bool t) |
mazgch | 9:e7a5959ffae1 | 20 | { |
mazgch | 11:b084552b03fe | 21 | int c = n; |
mazgch | 11:b084552b03fe | 22 | while (c && t) |
mazgch | 9:e7a5959ffae1 | 23 | { |
mazgch | 11:b084552b03fe | 24 | int i = _pipeTx.put(b, c, false); |
mazgch | 11:b084552b03fe | 25 | b += i; |
mazgch | 11:b084552b03fe | 26 | c -= i; |
mazgch | 9:e7a5959ffae1 | 27 | // give a chance to start tx |
mazgch | 9:e7a5959ffae1 | 28 | __disable_irq(); |
mazgch | 9:e7a5959ffae1 | 29 | txIrqBuf(); |
mazgch | 9:e7a5959ffae1 | 30 | __enable_irq(); |
mazgch | 9:e7a5959ffae1 | 31 | } |
mazgch | 11:b084552b03fe | 32 | return (n - c); |
mazgch | 9:e7a5959ffae1 | 33 | } |
mazgch | 9:e7a5959ffae1 | 34 | |
mazgch | 9:e7a5959ffae1 | 35 | void SerialPipe::txIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 36 | { |
mazgch | 9:e7a5959ffae1 | 37 | while (serial_writable(&_serial) && _pipeTx.readable()) |
mazgch | 11:b084552b03fe | 38 | { |
mazgch | 11:b084552b03fe | 39 | char ch = _pipeTx.getc(); |
mazgch | 11:b084552b03fe | 40 | serial_putc(&_serial, ch); |
mazgch | 11:b084552b03fe | 41 | } |
mazgch | 9:e7a5959ffae1 | 42 | } |
mazgch | 9:e7a5959ffae1 | 43 | |
mazgch | 9:e7a5959ffae1 | 44 | // rx channel |
mazgch | 9:e7a5959ffae1 | 45 | int SerialPipe::readable(void) |
mazgch | 9:e7a5959ffae1 | 46 | { |
mazgch | 9:e7a5959ffae1 | 47 | return _pipeRx.readable(); |
mazgch | 9:e7a5959ffae1 | 48 | } |
mazgch | 9:e7a5959ffae1 | 49 | |
mazgch | 9:e7a5959ffae1 | 50 | int SerialPipe::getc(void) |
mazgch | 9:e7a5959ffae1 | 51 | { |
mazgch | 9:e7a5959ffae1 | 52 | return _pipeRx.getc(); |
mazgch | 9:e7a5959ffae1 | 53 | } |
mazgch | 9:e7a5959ffae1 | 54 | |
mazgch | 9:e7a5959ffae1 | 55 | int SerialPipe::get(char* b, int s, bool t) |
mazgch | 9:e7a5959ffae1 | 56 | { |
mazgch | 9:e7a5959ffae1 | 57 | return _pipeRx.get(b,s,t); |
mazgch | 9:e7a5959ffae1 | 58 | } |
mazgch | 9:e7a5959ffae1 | 59 | |
mazgch | 9:e7a5959ffae1 | 60 | void SerialPipe::rxIrqBuf(void) |
mazgch | 9:e7a5959ffae1 | 61 | { |
mazgch | 9:e7a5959ffae1 | 62 | while (serial_readable(&_serial)) |
mazgch | 9:e7a5959ffae1 | 63 | { |
mazgch | 9:e7a5959ffae1 | 64 | char ch = serial_getc(&_serial); |
mazgch | 9:e7a5959ffae1 | 65 | if (_pipeRx.writeable()) |
mazgch | 9:e7a5959ffae1 | 66 | _pipeRx.putc(ch); |
mazgch | 9:e7a5959ffae1 | 67 | else |
mazgch | 9:e7a5959ffae1 | 68 | /* overflow */; |
mazgch | 9:e7a5959ffae1 | 69 | } |
mazgch | 9:e7a5959ffae1 | 70 | } |
mazgch | 9:e7a5959ffae1 | 71 | |
mazgch | 9:e7a5959ffae1 | 72 | // ----------------------------------------------------------------------- |
mazgch | 9:e7a5959ffae1 | 73 | |
mazgch | 9:e7a5959ffae1 | 74 | SerialPipeEx::SerialPipeEx(PinName tx, PinName rx, int rxSize, int txSize, const char* name) |
mazgch | 9:e7a5959ffae1 | 75 | : SerialPipe(tx,rx,rxSize,txSize,name) |
mazgch | 9:e7a5959ffae1 | 76 | {} |
mazgch | 9:e7a5959ffae1 | 77 | |
mazgch | 9:e7a5959ffae1 | 78 | int SerialPipeEx::getLine(char* b, int s) |
mazgch | 9:e7a5959ffae1 | 79 | { |
mazgch | 9:e7a5959ffae1 | 80 | int o = 0; |
mazgch | 9:e7a5959ffae1 | 81 | int i = 0; |
mazgch | 9:e7a5959ffae1 | 82 | int l = _pipeRx.start(); |
mazgch | 9:e7a5959ffae1 | 83 | while ((i < l) && (o < s)) |
mazgch | 9:e7a5959ffae1 | 84 | { |
mazgch | 9:e7a5959ffae1 | 85 | int t = _pipeRx.next(); |
mazgch | 9:e7a5959ffae1 | 86 | i ++; |
mazgch | 9:e7a5959ffae1 | 87 | if (t == '\r') // terminate commands with carriage return |
mazgch | 9:e7a5959ffae1 | 88 | { |
mazgch | 12:684b31d5482b | 89 | _pipeRx.done(); |
mazgch | 12:684b31d5482b | 90 | if (s > o) |
mazgch | 12:684b31d5482b | 91 | b[o] = '\0'; |
mazgch | 12:684b31d5482b | 92 | return o; // if enter send the zero char |
mazgch | 9:e7a5959ffae1 | 93 | } |
mazgch | 9:e7a5959ffae1 | 94 | else if (t == '\n') // skip/filter new line |
mazgch | 9:e7a5959ffae1 | 95 | /* skip */; |
mazgch | 9:e7a5959ffae1 | 96 | else if (t != '\b') // normal char (no backspace) |
mazgch | 9:e7a5959ffae1 | 97 | b[o++] = t; |
mazgch | 9:e7a5959ffae1 | 98 | else if (o > 0) // backspace |
mazgch | 9:e7a5959ffae1 | 99 | o --; // remove it |
mazgch | 9:e7a5959ffae1 | 100 | } |
mazgch | 9:e7a5959ffae1 | 101 | o = 0; |
mazgch | 12:684b31d5482b | 102 | if (s > 0) |
mazgch | 12:684b31d5482b | 103 | b[0] = '\0'; |
mazgch | 9:e7a5959ffae1 | 104 | return WAIT; |
mazgch | 9:e7a5959ffae1 | 105 | } |