Serial library for MTS Socket Modem Arduino Shield devices from Multi-Tech Systems

Dependents:   mDot_AT_firmware mtsas mtsas MTDOT-EVB-LinkCheck-AL ... more

Committer:
Mike Fiore
Date:
Mon May 19 11:18:10 2014 -0500
Revision:
1:d34b566d6f47
Child:
3:8e3cb3371b09
add MTSBufferedIO, MTSSerial, and MTSSerialFlowControl

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Mike Fiore 1:d34b566d6f47 1 #include "MTSBufferedIO.h"
Mike Fiore 1:d34b566d6f47 2 #include "Vars.h"
Mike Fiore 1:d34b566d6f47 3
Mike Fiore 1:d34b566d6f47 4 using namespace mts;
Mike Fiore 1:d34b566d6f47 5
Mike Fiore 1:d34b566d6f47 6 MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize)
Mike Fiore 1:d34b566d6f47 7 : txBuffer(txBufferSize)
Mike Fiore 1:d34b566d6f47 8 , rxBuffer(rxBufferSize)
Mike Fiore 1:d34b566d6f47 9 {
Mike Fiore 1:d34b566d6f47 10 }
Mike Fiore 1:d34b566d6f47 11
Mike Fiore 1:d34b566d6f47 12 MTSBufferedIO::~MTSBufferedIO()
Mike Fiore 1:d34b566d6f47 13 {
Mike Fiore 1:d34b566d6f47 14 }
Mike Fiore 1:d34b566d6f47 15
Mike Fiore 1:d34b566d6f47 16 int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 17 {
Mike Fiore 1:d34b566d6f47 18 //Writes until empty or timeout is reached (different implementation planned once tx isr is working)
Mike Fiore 1:d34b566d6f47 19 int bytesWritten = 0;
Mike Fiore 1:d34b566d6f47 20 Timer tmr;
Mike Fiore 1:d34b566d6f47 21 tmr.start();
Mike Fiore 1:d34b566d6f47 22 length = MAX(0,length);
Mike Fiore 1:d34b566d6f47 23 do {
Mike Fiore 1:d34b566d6f47 24 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
Mike Fiore 1:d34b566d6f47 25 if(bytesWrittenSwBuffer > 0) {
Mike Fiore 1:d34b566d6f47 26 handleWrite();
Mike Fiore 1:d34b566d6f47 27 int bytesRemainingSwBuffer = txBuffer.size();
Mike Fiore 1:d34b566d6f47 28 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 29 bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer);
Mike Fiore 1:d34b566d6f47 30 }
Mike Fiore 1:d34b566d6f47 31 } while(tmr.read_ms() <= timeoutMillis && bytesWritten < length);
Mike Fiore 1:d34b566d6f47 32 return bytesWritten;
Mike Fiore 1:d34b566d6f47 33 }
Mike Fiore 1:d34b566d6f47 34
Mike Fiore 1:d34b566d6f47 35 int MTSBufferedIO::write(const char* data, int length)
Mike Fiore 1:d34b566d6f47 36 {
Mike Fiore 1:d34b566d6f47 37 //Blocks until all bytes are written (different implementation planned once tx isr is working)
Mike Fiore 1:d34b566d6f47 38 int bytesWritten = 0;
Mike Fiore 1:d34b566d6f47 39 length = MAX(0,length);
Mike Fiore 1:d34b566d6f47 40 do {
Mike Fiore 1:d34b566d6f47 41 int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
Mike Fiore 1:d34b566d6f47 42 handleWrite();
Mike Fiore 1:d34b566d6f47 43 int bytesRemainingSwBuffer = txBuffer.size();
Mike Fiore 1:d34b566d6f47 44 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 45 bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer;
Mike Fiore 1:d34b566d6f47 46 } while(bytesWritten < length);
Mike Fiore 1:d34b566d6f47 47 return length;
Mike Fiore 1:d34b566d6f47 48 }
Mike Fiore 1:d34b566d6f47 49
Mike Fiore 1:d34b566d6f47 50 int MTSBufferedIO::write(char data, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 51 {
Mike Fiore 1:d34b566d6f47 52 return write(&data, 1, timeoutMillis);
Mike Fiore 1:d34b566d6f47 53 }
Mike Fiore 1:d34b566d6f47 54
Mike Fiore 1:d34b566d6f47 55 int MTSBufferedIO::write(char data)
Mike Fiore 1:d34b566d6f47 56 {
Mike Fiore 1:d34b566d6f47 57 return write(&data, 1);
Mike Fiore 1:d34b566d6f47 58 }
Mike Fiore 1:d34b566d6f47 59
Mike Fiore 1:d34b566d6f47 60 int MTSBufferedIO::writeable() {
Mike Fiore 1:d34b566d6f47 61 return txBuffer.remaining();
Mike Fiore 1:d34b566d6f47 62 }
Mike Fiore 1:d34b566d6f47 63
Mike Fiore 1:d34b566d6f47 64 int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 65 {
Mike Fiore 1:d34b566d6f47 66 int bytesRead = 0;
Mike Fiore 1:d34b566d6f47 67 Timer tmr;
Mike Fiore 1:d34b566d6f47 68 tmr.start();
Mike Fiore 1:d34b566d6f47 69 length = MAX(0,length);
Mike Fiore 1:d34b566d6f47 70 do {
Mike Fiore 1:d34b566d6f47 71 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
Mike Fiore 1:d34b566d6f47 72 } while(tmr.read_ms() <= timeoutMillis && bytesRead < length);
Mike Fiore 1:d34b566d6f47 73 return bytesRead;
Mike Fiore 1:d34b566d6f47 74 }
Mike Fiore 1:d34b566d6f47 75
Mike Fiore 1:d34b566d6f47 76 int MTSBufferedIO::read(char* data, int length)
Mike Fiore 1:d34b566d6f47 77 {
Mike Fiore 1:d34b566d6f47 78 int bytesRead = 0;
Mike Fiore 1:d34b566d6f47 79 length = MAX(0,length);
Mike Fiore 1:d34b566d6f47 80 while(bytesRead < length) {
Mike Fiore 1:d34b566d6f47 81 bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
Mike Fiore 1:d34b566d6f47 82 }
Mike Fiore 1:d34b566d6f47 83 return length;
Mike Fiore 1:d34b566d6f47 84 }
Mike Fiore 1:d34b566d6f47 85
Mike Fiore 1:d34b566d6f47 86 int MTSBufferedIO::read(char& data, unsigned int timeoutMillis)
Mike Fiore 1:d34b566d6f47 87 {
Mike Fiore 1:d34b566d6f47 88 return read(&data, 1, timeoutMillis);
Mike Fiore 1:d34b566d6f47 89 }
Mike Fiore 1:d34b566d6f47 90
Mike Fiore 1:d34b566d6f47 91 int MTSBufferedIO::read(char& data)
Mike Fiore 1:d34b566d6f47 92 {
Mike Fiore 1:d34b566d6f47 93 return rxBuffer.read(&data, 1);
Mike Fiore 1:d34b566d6f47 94 }
Mike Fiore 1:d34b566d6f47 95
Mike Fiore 1:d34b566d6f47 96 int MTSBufferedIO::readable() {
Mike Fiore 1:d34b566d6f47 97 return rxBuffer.size();
Mike Fiore 1:d34b566d6f47 98 }
Mike Fiore 1:d34b566d6f47 99
Mike Fiore 1:d34b566d6f47 100 bool MTSBufferedIO::txEmpty()
Mike Fiore 1:d34b566d6f47 101 {
Mike Fiore 1:d34b566d6f47 102 return txBuffer.isEmpty();
Mike Fiore 1:d34b566d6f47 103 }
Mike Fiore 1:d34b566d6f47 104
Mike Fiore 1:d34b566d6f47 105 bool MTSBufferedIO::rxEmpty()
Mike Fiore 1:d34b566d6f47 106 {
Mike Fiore 1:d34b566d6f47 107 return rxBuffer.isEmpty();
Mike Fiore 1:d34b566d6f47 108 }
Mike Fiore 1:d34b566d6f47 109
Mike Fiore 1:d34b566d6f47 110 bool MTSBufferedIO::txFull()
Mike Fiore 1:d34b566d6f47 111 {
Mike Fiore 1:d34b566d6f47 112 return txBuffer.isFull();
Mike Fiore 1:d34b566d6f47 113 }
Mike Fiore 1:d34b566d6f47 114
Mike Fiore 1:d34b566d6f47 115 bool MTSBufferedIO::rxFull()
Mike Fiore 1:d34b566d6f47 116 {
Mike Fiore 1:d34b566d6f47 117 return rxBuffer.isFull();
Mike Fiore 1:d34b566d6f47 118 }
Mike Fiore 1:d34b566d6f47 119
Mike Fiore 1:d34b566d6f47 120 void MTSBufferedIO::txClear()
Mike Fiore 1:d34b566d6f47 121 {
Mike Fiore 1:d34b566d6f47 122 txBuffer.clear();
Mike Fiore 1:d34b566d6f47 123 }
Mike Fiore 1:d34b566d6f47 124
Mike Fiore 1:d34b566d6f47 125 void MTSBufferedIO::rxClear()
Mike Fiore 1:d34b566d6f47 126 {
Mike Fiore 1:d34b566d6f47 127 rxBuffer.clear();
Mike Fiore 1:d34b566d6f47 128 }