MultiTech / MTS-Serial

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MTSBufferedIO.cpp Source File

MTSBufferedIO.cpp

00001 #include "mbed.h"
00002 #include "MTSBufferedIO.h"
00003 #include "Utils.h"
00004 
00005 using namespace mts;
00006 
00007 MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize)
00008 : txBuffer(txBufferSize)
00009 , rxBuffer(rxBufferSize)
00010 {
00011 
00012 }
00013 
00014 MTSBufferedIO::~MTSBufferedIO()
00015 {
00016 }
00017 
00018 int MTSBufferedIO::writef(const char* format, ...) {
00019     char buff[512];
00020 
00021     va_list ap;
00022     va_start(ap, format);
00023     int len = vsnprintf(buff, 512, format, ap);
00024     while (!writeable())
00025         ;
00026     write(buff, std::min<int>(len,512));
00027     va_end(ap);
00028     return len;
00029 }
00030 
00031 int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis) 
00032 {
00033     //Writes until empty or timeout is reached (different implementation planned once tx isr is working)
00034     int bytesWritten = 0;
00035     Timer tmr;
00036     tmr.start();
00037     length = mts_max(0,length);
00038     do {
00039         int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
00040         if(bytesWrittenSwBuffer > 0) {
00041             handleWrite();
00042             int bytesRemainingSwBuffer = txBuffer.size();
00043             txBuffer.clear();
00044             bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer);
00045         }
00046     } while((unsigned)tmr.read_ms() <= timeoutMillis && bytesWritten < length);
00047     return bytesWritten;
00048 }
00049 
00050 int MTSBufferedIO::write(const char* data, int length)
00051 {   
00052     //Blocks until all bytes are written (different implementation planned once tx isr is working)
00053     int bytesWritten = 0;
00054     length = mts_max(0,length);
00055     do {
00056         int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten);
00057         handleWrite();
00058         int bytesRemainingSwBuffer = txBuffer.size();
00059         txBuffer.clear();
00060         bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer;
00061     } while(bytesWritten < length);
00062     return length;
00063 }
00064 
00065 int MTSBufferedIO::write(char data, unsigned int timeoutMillis) 
00066 {
00067     return write(&data, 1, timeoutMillis);
00068 }
00069 
00070 int MTSBufferedIO::write(char data)
00071 {
00072     return write(&data, 1);
00073 }
00074 
00075 int MTSBufferedIO::writeable() {
00076     return txBuffer.remaining();   
00077 }
00078 
00079 int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis) 
00080 {
00081     int bytesRead = 0;
00082     Timer tmr;
00083     tmr.start();
00084     length = mts_max(0,length);
00085     do {
00086         bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
00087     } while((unsigned)tmr.read_ms() <= timeoutMillis && bytesRead < length);
00088     return bytesRead;
00089 }
00090 
00091 int MTSBufferedIO::read(char* data, int length)
00092 {
00093     int bytesRead = 0;
00094     length = mts_max(0,length);
00095     while(bytesRead < length) {
00096         bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead);
00097     }
00098     return length;
00099 }
00100 
00101 int MTSBufferedIO::read(char& data, unsigned int timeoutMillis) 
00102 {
00103     return read(&data, 1, timeoutMillis);
00104 }
00105 
00106 int MTSBufferedIO::read(char& data)
00107 {
00108     return rxBuffer.read(&data, 1);
00109 }
00110 
00111 int MTSBufferedIO::readable() {
00112     return rxBuffer.size();   
00113 }
00114 
00115 bool MTSBufferedIO::txEmpty()
00116 {
00117     return txBuffer.isEmpty();
00118 }
00119 
00120 bool MTSBufferedIO::rxEmpty()
00121 {
00122     return rxBuffer.isEmpty();
00123 }
00124 
00125 bool MTSBufferedIO::txFull()
00126 {
00127     return txBuffer.isFull();
00128 }
00129 
00130 bool MTSBufferedIO::rxFull()
00131 {
00132     return rxBuffer.isFull();
00133 }
00134 
00135 void MTSBufferedIO::txClear()
00136 {
00137     txBuffer.clear();
00138 }
00139 
00140 void MTSBufferedIO::rxClear()
00141 {
00142     rxBuffer.clear();
00143 }