MTS_SPI_Slave unfinished
Fork of MTS-Serial by
MTSBufferedIO.cpp@14:d5a86071845e, 2017-11-16 (annotated)
- Committer:
- ScottHoppeMultitech
- Date:
- Thu Nov 16 17:02:41 2017 +0000
- Revision:
- 14:d5a86071845e
- Parent:
- 11:4afbbafcd6b3
- Child:
- 12:e12b79a4ab4f
added Thread member
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Mike Fiore |
4:d348d001283b | 1 | #include "mbed.h" |
Mike Fiore |
1:d34b566d6f47 | 2 | #include "MTSBufferedIO.h" |
Mike Fiore |
3:8e3cb3371b09 | 3 | #include "Utils.h" |
Mike Fiore |
1:d34b566d6f47 | 4 | |
Mike Fiore |
1:d34b566d6f47 | 5 | using namespace mts; |
Mike Fiore |
1:d34b566d6f47 | 6 | |
Mike Fiore |
1:d34b566d6f47 | 7 | MTSBufferedIO::MTSBufferedIO(int txBufferSize, int rxBufferSize) |
Mike Fiore |
1:d34b566d6f47 | 8 | : txBuffer(txBufferSize) |
Mike Fiore |
1:d34b566d6f47 | 9 | , rxBuffer(rxBufferSize) |
Mike Fiore |
1:d34b566d6f47 | 10 | { |
Mike Fiore |
1:d34b566d6f47 | 11 | } |
Mike Fiore |
1:d34b566d6f47 | 12 | |
Mike Fiore |
1:d34b566d6f47 | 13 | MTSBufferedIO::~MTSBufferedIO() |
Mike Fiore |
1:d34b566d6f47 | 14 | { |
Mike Fiore |
1:d34b566d6f47 | 15 | } |
Mike Fiore |
1:d34b566d6f47 | 16 | |
Mike Fiore |
11:4afbbafcd6b3 | 17 | int MTSBufferedIO::writef(const char* format, ...) { |
Mike Fiore |
11:4afbbafcd6b3 | 18 | char buff[256]; |
Mike Fiore |
11:4afbbafcd6b3 | 19 | |
Mike Fiore |
11:4afbbafcd6b3 | 20 | va_list ap; |
Mike Fiore |
11:4afbbafcd6b3 | 21 | va_start(ap, format); |
Mike Fiore |
11:4afbbafcd6b3 | 22 | int len = vsnprintf(buff, 256, format, ap); |
Mike Fiore |
11:4afbbafcd6b3 | 23 | while (!writeable()) |
Mike Fiore |
11:4afbbafcd6b3 | 24 | ; |
Mike Fiore |
11:4afbbafcd6b3 | 25 | write(buff, len); |
Mike Fiore |
11:4afbbafcd6b3 | 26 | va_end(ap); |
Mike Fiore |
11:4afbbafcd6b3 | 27 | return len; |
Mike Fiore |
11:4afbbafcd6b3 | 28 | } |
Mike Fiore |
11:4afbbafcd6b3 | 29 | |
Mike Fiore |
1:d34b566d6f47 | 30 | int MTSBufferedIO::write(const char* data, int length, unsigned int timeoutMillis) |
Mike Fiore |
1:d34b566d6f47 | 31 | { |
Mike Fiore |
1:d34b566d6f47 | 32 | //Writes until empty or timeout is reached (different implementation planned once tx isr is working) |
Mike Fiore |
1:d34b566d6f47 | 33 | int bytesWritten = 0; |
Mike Fiore |
1:d34b566d6f47 | 34 | Timer tmr; |
Mike Fiore |
1:d34b566d6f47 | 35 | tmr.start(); |
Mike Fiore |
10:b18a1064dfc6 | 36 | length = mts_max(0,length); |
Mike Fiore |
1:d34b566d6f47 | 37 | do { |
Mike Fiore |
1:d34b566d6f47 | 38 | int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten); |
Mike Fiore |
1:d34b566d6f47 | 39 | if(bytesWrittenSwBuffer > 0) { |
Mike Fiore |
1:d34b566d6f47 | 40 | handleWrite(); |
Mike Fiore |
1:d34b566d6f47 | 41 | int bytesRemainingSwBuffer = txBuffer.size(); |
Mike Fiore |
1:d34b566d6f47 | 42 | txBuffer.clear(); |
Mike Fiore |
1:d34b566d6f47 | 43 | bytesWritten += (bytesWrittenSwBuffer - bytesRemainingSwBuffer); |
Mike Fiore |
1:d34b566d6f47 | 44 | } |
Mike Fiore |
1:d34b566d6f47 | 45 | } while(tmr.read_ms() <= timeoutMillis && bytesWritten < length); |
Mike Fiore |
1:d34b566d6f47 | 46 | return bytesWritten; |
Mike Fiore |
1:d34b566d6f47 | 47 | } |
Mike Fiore |
1:d34b566d6f47 | 48 | |
Mike Fiore |
1:d34b566d6f47 | 49 | int MTSBufferedIO::write(const char* data, int length) |
Mike Fiore |
1:d34b566d6f47 | 50 | { |
Mike Fiore |
1:d34b566d6f47 | 51 | //Blocks until all bytes are written (different implementation planned once tx isr is working) |
Mike Fiore |
1:d34b566d6f47 | 52 | int bytesWritten = 0; |
Mike Fiore |
10:b18a1064dfc6 | 53 | length = mts_max(0,length); |
Mike Fiore |
1:d34b566d6f47 | 54 | do { |
Mike Fiore |
1:d34b566d6f47 | 55 | int bytesWrittenSwBuffer = txBuffer.write(&data[bytesWritten], length - bytesWritten); |
Mike Fiore |
1:d34b566d6f47 | 56 | handleWrite(); |
Mike Fiore |
1:d34b566d6f47 | 57 | int bytesRemainingSwBuffer = txBuffer.size(); |
Mike Fiore |
1:d34b566d6f47 | 58 | txBuffer.clear(); |
Mike Fiore |
1:d34b566d6f47 | 59 | bytesWritten += bytesWrittenSwBuffer - bytesRemainingSwBuffer; |
Mike Fiore |
1:d34b566d6f47 | 60 | } while(bytesWritten < length); |
Mike Fiore |
1:d34b566d6f47 | 61 | return length; |
Mike Fiore |
1:d34b566d6f47 | 62 | } |
Mike Fiore |
1:d34b566d6f47 | 63 | |
Mike Fiore |
1:d34b566d6f47 | 64 | int MTSBufferedIO::write(char data, unsigned int timeoutMillis) |
Mike Fiore |
1:d34b566d6f47 | 65 | { |
Mike Fiore |
1:d34b566d6f47 | 66 | return write(&data, 1, timeoutMillis); |
Mike Fiore |
1:d34b566d6f47 | 67 | } |
Mike Fiore |
1:d34b566d6f47 | 68 | |
Mike Fiore |
1:d34b566d6f47 | 69 | int MTSBufferedIO::write(char data) |
Mike Fiore |
1:d34b566d6f47 | 70 | { |
Mike Fiore |
1:d34b566d6f47 | 71 | return write(&data, 1); |
Mike Fiore |
1:d34b566d6f47 | 72 | } |
Mike Fiore |
1:d34b566d6f47 | 73 | |
Mike Fiore |
1:d34b566d6f47 | 74 | int MTSBufferedIO::writeable() { |
Mike Fiore |
1:d34b566d6f47 | 75 | return txBuffer.remaining(); |
Mike Fiore |
1:d34b566d6f47 | 76 | } |
Mike Fiore |
1:d34b566d6f47 | 77 | |
Mike Fiore |
1:d34b566d6f47 | 78 | int MTSBufferedIO::read(char* data, int length, unsigned int timeoutMillis) |
Mike Fiore |
1:d34b566d6f47 | 79 | { |
Mike Fiore |
1:d34b566d6f47 | 80 | int bytesRead = 0; |
Mike Fiore |
1:d34b566d6f47 | 81 | Timer tmr; |
Mike Fiore |
1:d34b566d6f47 | 82 | tmr.start(); |
Mike Fiore |
10:b18a1064dfc6 | 83 | length = mts_max(0,length); |
Mike Fiore |
1:d34b566d6f47 | 84 | do { |
Mike Fiore |
1:d34b566d6f47 | 85 | bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead); |
Mike Fiore |
1:d34b566d6f47 | 86 | } while(tmr.read_ms() <= timeoutMillis && bytesRead < length); |
Mike Fiore |
1:d34b566d6f47 | 87 | return bytesRead; |
Mike Fiore |
1:d34b566d6f47 | 88 | } |
Mike Fiore |
1:d34b566d6f47 | 89 | |
Mike Fiore |
1:d34b566d6f47 | 90 | int MTSBufferedIO::read(char* data, int length) |
Mike Fiore |
1:d34b566d6f47 | 91 | { |
Mike Fiore |
1:d34b566d6f47 | 92 | int bytesRead = 0; |
Mike Fiore |
10:b18a1064dfc6 | 93 | length = mts_max(0,length); |
Mike Fiore |
1:d34b566d6f47 | 94 | while(bytesRead < length) { |
Mike Fiore |
1:d34b566d6f47 | 95 | bytesRead += rxBuffer.read(&data[bytesRead], length - bytesRead); |
Mike Fiore |
1:d34b566d6f47 | 96 | } |
Mike Fiore |
1:d34b566d6f47 | 97 | return length; |
Mike Fiore |
1:d34b566d6f47 | 98 | } |
Mike Fiore |
1:d34b566d6f47 | 99 | |
Mike Fiore |
1:d34b566d6f47 | 100 | int MTSBufferedIO::read(char& data, unsigned int timeoutMillis) |
Mike Fiore |
1:d34b566d6f47 | 101 | { |
Mike Fiore |
1:d34b566d6f47 | 102 | return read(&data, 1, timeoutMillis); |
Mike Fiore |
1:d34b566d6f47 | 103 | } |
Mike Fiore |
1:d34b566d6f47 | 104 | |
Mike Fiore |
1:d34b566d6f47 | 105 | int MTSBufferedIO::read(char& data) |
Mike Fiore |
1:d34b566d6f47 | 106 | { |
Mike Fiore |
1:d34b566d6f47 | 107 | return rxBuffer.read(&data, 1); |
Mike Fiore |
1:d34b566d6f47 | 108 | } |
Mike Fiore |
1:d34b566d6f47 | 109 | |
Mike Fiore |
1:d34b566d6f47 | 110 | int MTSBufferedIO::readable() { |
Mike Fiore |
1:d34b566d6f47 | 111 | return rxBuffer.size(); |
Mike Fiore |
1:d34b566d6f47 | 112 | } |
Mike Fiore |
1:d34b566d6f47 | 113 | |
Mike Fiore |
1:d34b566d6f47 | 114 | bool MTSBufferedIO::txEmpty() |
Mike Fiore |
1:d34b566d6f47 | 115 | { |
Mike Fiore |
1:d34b566d6f47 | 116 | return txBuffer.isEmpty(); |
Mike Fiore |
1:d34b566d6f47 | 117 | } |
Mike Fiore |
1:d34b566d6f47 | 118 | |
Mike Fiore |
1:d34b566d6f47 | 119 | bool MTSBufferedIO::rxEmpty() |
Mike Fiore |
1:d34b566d6f47 | 120 | { |
Mike Fiore |
1:d34b566d6f47 | 121 | return rxBuffer.isEmpty(); |
Mike Fiore |
1:d34b566d6f47 | 122 | } |
Mike Fiore |
1:d34b566d6f47 | 123 | |
Mike Fiore |
1:d34b566d6f47 | 124 | bool MTSBufferedIO::txFull() |
Mike Fiore |
1:d34b566d6f47 | 125 | { |
Mike Fiore |
1:d34b566d6f47 | 126 | return txBuffer.isFull(); |
Mike Fiore |
1:d34b566d6f47 | 127 | } |
Mike Fiore |
1:d34b566d6f47 | 128 | |
Mike Fiore |
1:d34b566d6f47 | 129 | bool MTSBufferedIO::rxFull() |
Mike Fiore |
1:d34b566d6f47 | 130 | { |
Mike Fiore |
1:d34b566d6f47 | 131 | return rxBuffer.isFull(); |
Mike Fiore |
1:d34b566d6f47 | 132 | } |
Mike Fiore |
1:d34b566d6f47 | 133 | |
Mike Fiore |
1:d34b566d6f47 | 134 | void MTSBufferedIO::txClear() |
Mike Fiore |
1:d34b566d6f47 | 135 | { |
Mike Fiore |
1:d34b566d6f47 | 136 | txBuffer.clear(); |
Mike Fiore |
1:d34b566d6f47 | 137 | } |
Mike Fiore |
1:d34b566d6f47 | 138 | |
Mike Fiore |
1:d34b566d6f47 | 139 | void MTSBufferedIO::rxClear() |
Mike Fiore |
1:d34b566d6f47 | 140 | { |
Mike Fiore |
1:d34b566d6f47 | 141 | rxBuffer.clear(); |
Mike Fiore |
1:d34b566d6f47 | 142 | } |