Add a bunch of APNs
Fork of C027_Support by
SerialPipe.h@2:b6012cd91657, 2013-10-25 (annotated)
- Committer:
- mazgch
- Date:
- Fri Oct 25 08:47:22 2013 +0000
- Revision:
- 2:b6012cd91657
- Parent:
- 0:cb2d45baaca3
- Child:
- 9:e7a5959ffae1
GPS for Serial and I2c
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 0:cb2d45baaca3 | 1 | #pragma once |
mazgch | 0:cb2d45baaca3 | 2 | |
mazgch | 2:b6012cd91657 | 3 | #include "mbed.h" |
mazgch | 0:cb2d45baaca3 | 4 | #include "Pipe.h" |
mazgch | 0:cb2d45baaca3 | 5 | #include <ctype.h> |
mazgch | 0:cb2d45baaca3 | 6 | |
mazgch | 0:cb2d45baaca3 | 7 | class SerialPipe : public Serial |
mazgch | 0:cb2d45baaca3 | 8 | { |
mazgch | 0:cb2d45baaca3 | 9 | protected: |
mazgch | 2:b6012cd91657 | 10 | Pipe<char> _pipe; |
mazgch | 0:cb2d45baaca3 | 11 | private: |
mazgch | 0:cb2d45baaca3 | 12 | void rxIrqBuf(void) |
mazgch | 0:cb2d45baaca3 | 13 | { |
mazgch | 0:cb2d45baaca3 | 14 | while (serial_readable(&_serial)) |
mazgch | 2:b6012cd91657 | 15 | _pipe.putc(serial_getc(&_serial)); |
mazgch | 0:cb2d45baaca3 | 16 | } |
mazgch | 0:cb2d45baaca3 | 17 | public: |
mazgch | 0:cb2d45baaca3 | 18 | SerialPipe(PinName tx, PinName rx, int rxSize = 128, const char* name = NULL) |
mazgch | 2:b6012cd91657 | 19 | : Serial(tx,rx,name), _pipe(rxSize) |
mazgch | 0:cb2d45baaca3 | 20 | { |
mazgch | 0:cb2d45baaca3 | 21 | attach(this, &SerialPipe::rxIrqBuf, RxIrq); |
mazgch | 0:cb2d45baaca3 | 22 | } |
mazgch | 0:cb2d45baaca3 | 23 | virtual ~SerialPipe(void) |
mazgch | 0:cb2d45baaca3 | 24 | { |
mazgch | 0:cb2d45baaca3 | 25 | attach(NULL, RxIrq); |
mazgch | 0:cb2d45baaca3 | 26 | } |
mazgch | 0:cb2d45baaca3 | 27 | // tx channel |
mazgch | 0:cb2d45baaca3 | 28 | int writeBuf(char* b, int s) |
mazgch | 0:cb2d45baaca3 | 29 | { |
mazgch | 0:cb2d45baaca3 | 30 | for (int i = 0; i < s; i ++) |
mazgch | 0:cb2d45baaca3 | 31 | putc(b[i]); |
mazgch | 0:cb2d45baaca3 | 32 | return s; |
mazgch | 0:cb2d45baaca3 | 33 | } |
mazgch | 0:cb2d45baaca3 | 34 | // rx channel |
mazgch | 2:b6012cd91657 | 35 | int readable(void) { return _pipe.readable() ? 1 : 0; } |
mazgch | 2:b6012cd91657 | 36 | int getc(void) { return _pipe.getc(); } |
mazgch | 2:b6012cd91657 | 37 | int readBuf(char* b, int s) { return _pipe.get(b,s); } |
mazgch | 0:cb2d45baaca3 | 38 | |
mazgch | 0:cb2d45baaca3 | 39 | #define WAIT -1 |
mazgch | 0:cb2d45baaca3 | 40 | #define NOT_FOUND 0 |
mazgch | 0:cb2d45baaca3 | 41 | |
mazgch | 0:cb2d45baaca3 | 42 | // special parsing |
mazgch | 0:cb2d45baaca3 | 43 | int getLine(char* b, int s) |
mazgch | 0:cb2d45baaca3 | 44 | { |
mazgch | 0:cb2d45baaca3 | 45 | int o = 0; |
mazgch | 0:cb2d45baaca3 | 46 | int i = 0; |
mazgch | 2:b6012cd91657 | 47 | int l = _pipe.start(); |
mazgch | 0:cb2d45baaca3 | 48 | while ((i < l) && (o < s)) |
mazgch | 0:cb2d45baaca3 | 49 | { |
mazgch | 2:b6012cd91657 | 50 | int t = _pipe.next(); |
mazgch | 0:cb2d45baaca3 | 51 | i ++; |
mazgch | 0:cb2d45baaca3 | 52 | if (t == '\r') // terminate commands with carriage return |
mazgch | 0:cb2d45baaca3 | 53 | { |
mazgch | 2:b6012cd91657 | 54 | _pipe.done(); |
mazgch | 0:cb2d45baaca3 | 55 | return o; // if enter send the zero char |
mazgch | 0:cb2d45baaca3 | 56 | } |
mazgch | 0:cb2d45baaca3 | 57 | else if (t == '\n') // skip/filter new line |
mazgch | 0:cb2d45baaca3 | 58 | /* skip */; |
mazgch | 0:cb2d45baaca3 | 59 | else if (t != '\b') // normal char (no backspace) |
mazgch | 0:cb2d45baaca3 | 60 | b[o++] = t; |
mazgch | 0:cb2d45baaca3 | 61 | else if (o > 0) // backspace |
mazgch | 0:cb2d45baaca3 | 62 | o --; // remove it |
mazgch | 0:cb2d45baaca3 | 63 | } |
mazgch | 0:cb2d45baaca3 | 64 | o = 0; |
mazgch | 0:cb2d45baaca3 | 65 | return WAIT; |
mazgch | 0:cb2d45baaca3 | 66 | } |
mazgch | 0:cb2d45baaca3 | 67 | }; |