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
MDM.cpp@20:535ef78655df, 2014-03-17 (annotated)
- Committer:
- mazgch
- Date:
- Mon Mar 17 13:18:04 2014 +0000
- Revision:
- 20:535ef78655df
- Parent:
- 18:e5697801df29
- Child:
- 24:0e287a85ac9e
fix angle api, only one MDM constructor
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 18:e5697801df29 | 1 | #include "mbed.h" |
mazgch | 18:e5697801df29 | 2 | #include <ctype.h> |
mazgch | 18:e5697801df29 | 3 | #include "MDM.h" |
mazgch | 18:e5697801df29 | 4 | |
mazgch | 18:e5697801df29 | 5 | int MDMParser::send(const char* buf, int len) |
mazgch | 18:e5697801df29 | 6 | { |
mazgch | 18:e5697801df29 | 7 | return _send(buf, len); |
mazgch | 18:e5697801df29 | 8 | } |
mazgch | 18:e5697801df29 | 9 | |
mazgch | 18:e5697801df29 | 10 | int MDMParser::_getLine(Pipe<char>* pipe, char* buffer, int length) |
mazgch | 18:e5697801df29 | 11 | { |
mazgch | 18:e5697801df29 | 12 | int o = 0; |
mazgch | 18:e5697801df29 | 13 | int i = 0; |
mazgch | 18:e5697801df29 | 14 | int l = pipe->start(); |
mazgch | 18:e5697801df29 | 15 | while ((i < l) && (o < length)) |
mazgch | 18:e5697801df29 | 16 | { |
mazgch | 18:e5697801df29 | 17 | int t = pipe->next(); |
mazgch | 18:e5697801df29 | 18 | i ++; |
mazgch | 18:e5697801df29 | 19 | if (t == '\r') // terminate commands with carriage return |
mazgch | 18:e5697801df29 | 20 | { |
mazgch | 18:e5697801df29 | 21 | pipe->done(); |
mazgch | 18:e5697801df29 | 22 | if (length > o) |
mazgch | 18:e5697801df29 | 23 | buffer[o] = '\0'; |
mazgch | 18:e5697801df29 | 24 | return o; // if enter send the zero char |
mazgch | 18:e5697801df29 | 25 | } |
mazgch | 18:e5697801df29 | 26 | else if (t == '\n') // skip/filter new line |
mazgch | 18:e5697801df29 | 27 | /* skip */; |
mazgch | 18:e5697801df29 | 28 | else if (t != '\b') // normal char (no backspace) |
mazgch | 18:e5697801df29 | 29 | buffer[o++] = t; |
mazgch | 18:e5697801df29 | 30 | else if (o > 0) // backspace |
mazgch | 18:e5697801df29 | 31 | o --; // remove it |
mazgch | 18:e5697801df29 | 32 | } |
mazgch | 18:e5697801df29 | 33 | o = 0; |
mazgch | 18:e5697801df29 | 34 | if (length > 0) |
mazgch | 18:e5697801df29 | 35 | buffer[0] = '\0'; |
mazgch | 18:e5697801df29 | 36 | return WAIT; |
mazgch | 18:e5697801df29 | 37 | } |
mazgch | 18:e5697801df29 | 38 | |
mazgch | 18:e5697801df29 | 39 | int MDMParser::_getResp(Pipe<char>* pipe, char* buffer, int length) |
mazgch | 18:e5697801df29 | 40 | { |
mazgch | 18:e5697801df29 | 41 | int o = 0; |
mazgch | 18:e5697801df29 | 42 | int i = 0; |
mazgch | 18:e5697801df29 | 43 | int l = pipe->start(); |
mazgch | 18:e5697801df29 | 44 | static const char erTxt[] = "ERROR\r\n"; |
mazgch | 18:e5697801df29 | 45 | static const char okTxt[] = "OK\r\n"; |
mazgch | 18:e5697801df29 | 46 | int er = 0; |
mazgch | 18:e5697801df29 | 47 | int ok = 0; |
mazgch | 18:e5697801df29 | 48 | while ((i < pipe->size()) && (o < length)) |
mazgch | 18:e5697801df29 | 49 | { |
mazgch | 18:e5697801df29 | 50 | int t = pipe->next(); |
mazgch | 18:e5697801df29 | 51 | i ++; |
mazgch | 18:e5697801df29 | 52 | buffer[o++] = t; |
mazgch | 18:e5697801df29 | 53 | ok = (t == okTxt[ok]) ? ok + 1 : 0; |
mazgch | 18:e5697801df29 | 54 | er = (t == erTxt[er]) ? er + 1 : 0; |
mazgch | 18:e5697801df29 | 55 | if ((okTxt[ok] == '\0') || (erTxt[er] == '\0')) |
mazgch | 18:e5697801df29 | 56 | { |
mazgch | 18:e5697801df29 | 57 | pipe->done(); |
mazgch | 18:e5697801df29 | 58 | if (length > o) |
mazgch | 18:e5697801df29 | 59 | buffer[o] = '\0'; |
mazgch | 18:e5697801df29 | 60 | return o; |
mazgch | 18:e5697801df29 | 61 | } |
mazgch | 18:e5697801df29 | 62 | } |
mazgch | 18:e5697801df29 | 63 | o = 0; |
mazgch | 18:e5697801df29 | 64 | if (length > 0) |
mazgch | 18:e5697801df29 | 65 | buffer[0] = '\0'; |
mazgch | 18:e5697801df29 | 66 | return WAIT; |
mazgch | 18:e5697801df29 | 67 | } |
mazgch | 18:e5697801df29 | 68 | |
mazgch | 18:e5697801df29 | 69 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 70 | // Serial Implementation |
mazgch | 18:e5697801df29 | 71 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 72 | |
mazgch | 18:e5697801df29 | 73 | MDMSerial::MDMSerial(PinName tx /*= MDMTXD*/, PinName rx /*= MDMRXD*/, int baudrate /*= MDMBAUD*/, |
mazgch | 18:e5697801df29 | 74 | PinName rts /*= MDMRTS*/, PinName cts /*= MDMCTS*/, int rxSize /*= 256*/, int txSize /*= 128*/) : |
mazgch | 18:e5697801df29 | 75 | SerialPipe(tx, rx, rts, cts, rxSize, txSize) |
mazgch | 18:e5697801df29 | 76 | { |
mazgch | 18:e5697801df29 | 77 | baud(baudrate); |
mazgch | 18:e5697801df29 | 78 | } |
mazgch | 18:e5697801df29 | 79 | |
mazgch | 18:e5697801df29 | 80 | int MDMSerial::_send(const void* buf, int len) |
mazgch | 18:e5697801df29 | 81 | { |
mazgch | 18:e5697801df29 | 82 | return put((const char*)buf, len, true/*=blocking*/); |
mazgch | 18:e5697801df29 | 83 | } |
mazgch | 18:e5697801df29 | 84 | |
mazgch | 18:e5697801df29 | 85 | int MDMSerial::getLine(char* buffer, int length) |
mazgch | 18:e5697801df29 | 86 | { |
mazgch | 18:e5697801df29 | 87 | return _getLine(&_pipeRx, buffer, length); |
mazgch | 18:e5697801df29 | 88 | } |
mazgch | 18:e5697801df29 | 89 | |
mazgch | 18:e5697801df29 | 90 | int MDMSerial::getResp(char* buffer, int length) |
mazgch | 18:e5697801df29 | 91 | { |
mazgch | 18:e5697801df29 | 92 | return _getResp(&_pipeRx, buffer, length); |
mazgch | 18:e5697801df29 | 93 | } |
mazgch | 18:e5697801df29 | 94 | |
mazgch | 18:e5697801df29 | 95 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 96 | // USB Implementation |
mazgch | 18:e5697801df29 | 97 | // ---------------------------------------------------------------- |
mazgch | 18:e5697801df29 | 98 | |
mazgch | 18:e5697801df29 | 99 | #ifdef HAVE_MDMUSB |
mazgch | 18:e5697801df29 | 100 | // TODO properly implement with USB |
mazgch | 18:e5697801df29 | 101 | MDMUsb::MDMUsb(void) { } |
mazgch | 18:e5697801df29 | 102 | int MDMUsb::_send(const void* buf, int len) { return len; } |
mazgch | 18:e5697801df29 | 103 | int MDMUsb::getLine(char* buffer, int length) { return NOT_FOUND; } |
mazgch | 18:e5697801df29 | 104 | int MDMUsb::getResp(char* buffer, int length) { return NOT_FOUND; } |
mazgch | 18:e5697801df29 | 105 | #endif |