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.
Dependents: mtsas mtsas thermostat_fan_demo--fan mtsas ... more
MTSCircularBuffer.cpp
00001 #include "mbed.h" 00002 #include "MTSCircularBuffer.h" 00003 00004 using namespace mts; 00005 00006 MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(GREATER) 00007 { 00008 buffer = new char[bufferSize]; 00009 } 00010 00011 MTSCircularBuffer::~MTSCircularBuffer() 00012 { 00013 delete[] buffer; 00014 } 00015 00016 int MTSCircularBuffer::read(char* data, int length) 00017 { 00018 int i = 0; 00019 while ((i < length) && (bytes > 0)) { 00020 if (readIndex == bufferSize) { 00021 readIndex = 0; 00022 } 00023 __disable_irq(); 00024 data[i++] = buffer[readIndex++]; 00025 bytes--; 00026 __enable_irq(); 00027 checkThreshold(); 00028 } 00029 return i; 00030 } 00031 00032 int MTSCircularBuffer::read(char& data) 00033 { 00034 if (bytes == 0) { 00035 return 0; 00036 } 00037 if (readIndex == bufferSize) { 00038 readIndex = 0; 00039 } 00040 __disable_irq(); 00041 data = buffer[readIndex++]; 00042 bytes--; 00043 __enable_irq(); 00044 checkThreshold(); 00045 return 1; 00046 } 00047 00048 int MTSCircularBuffer::write(const char* data, int length) 00049 { 00050 int i = 0; 00051 while((i < length) && (bytes < bufferSize)) { 00052 if(writeIndex == bufferSize) { 00053 writeIndex = 0; 00054 } 00055 __disable_irq(); 00056 buffer[writeIndex++] = data[i++]; 00057 bytes++; 00058 __enable_irq(); 00059 checkThreshold(); 00060 } 00061 return i; 00062 } 00063 00064 int MTSCircularBuffer::write(char data) 00065 { 00066 if (bytes == bufferSize) { 00067 return 0; 00068 } 00069 if(writeIndex == bufferSize) { 00070 writeIndex = 0; 00071 } 00072 __disable_irq(); 00073 buffer[writeIndex++] = data; 00074 bytes++; 00075 __enable_irq(); 00076 checkThreshold(); 00077 return 1; 00078 } 00079 00080 int MTSCircularBuffer::capacity() 00081 { 00082 return bufferSize; 00083 } 00084 00085 int MTSCircularBuffer::remaining() 00086 { 00087 return bufferSize - bytes; 00088 } 00089 00090 int MTSCircularBuffer::size() 00091 { 00092 return bytes; 00093 } 00094 00095 bool MTSCircularBuffer::isFull() 00096 { 00097 if (bytes == bufferSize) { 00098 return true; 00099 } else { 00100 return false; 00101 } 00102 } 00103 00104 bool MTSCircularBuffer::isEmpty() 00105 { 00106 if (bytes == 0) { 00107 return true; 00108 } else { 00109 return false; 00110 } 00111 } 00112 00113 void MTSCircularBuffer::clear() 00114 { 00115 writeIndex = readIndex = bytes = 0; 00116 } 00117 00118 void MTSCircularBuffer::checkThreshold() 00119 { 00120 if (_threshold == -1) { 00121 return; 00122 } 00123 switch (_op) { 00124 case GREATER: 00125 if (bytes > _threshold) { 00126 notify.call(); 00127 } 00128 break; 00129 case LESS: 00130 if (bytes < _threshold) { 00131 notify.call(); 00132 } 00133 break; 00134 case GREATER_EQUAL: 00135 if (bytes >= _threshold) { 00136 notify.call(); 00137 } 00138 break; 00139 case LESS_EQUAL: 00140 if (bytes <= _threshold) { 00141 notify.call(); 00142 } 00143 break; 00144 case EQUAL: 00145 if (bytes == _threshold) { 00146 notify.call(); 00147 } 00148 break; 00149 } 00150 } 00151
Generated on Thu Jul 14 2022 17:11:00 by
1.7.2