Scott Hoppe / MTS_SPI

Fork of MTS-Serial by Scott Hoppe

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