A library for talking to Multi-Tech's Cellular SocketModem Devices.
Dependents: M2X_dev axeda_wrapper_dev MTS_M2x_Example1 MTS_Cellular_Connect_Example ... more
MTSCircularBuffer.cpp
00001 /* Universal Socket Modem Interface Library 00002 * Copyright (c) 2013 Multi-Tech Systems 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include "MTSCircularBuffer.h" 00018 00019 using namespace mts; 00020 00021 MTSCircularBuffer::MTSCircularBuffer(int bufferSize) : bufferSize(bufferSize), readIndex(0), writeIndex(0), bytes(0), _threshold(-1), _op(Vars::GREATER) 00022 { 00023 buffer = new char[bufferSize]; 00024 } 00025 00026 MTSCircularBuffer::~MTSCircularBuffer() 00027 { 00028 delete[] buffer; 00029 } 00030 00031 int MTSCircularBuffer::capacity() 00032 { 00033 return bufferSize; 00034 } 00035 00036 int MTSCircularBuffer::read(char* data, int length) 00037 { 00038 int i = 0; 00039 while ((i < length) && (bytes > 0)) { 00040 if (readIndex == bufferSize) { 00041 readIndex = 0; 00042 } 00043 data[i++] = buffer[readIndex++]; 00044 bytes--; 00045 checkThreshold(); 00046 } 00047 return i; 00048 } 00049 00050 int MTSCircularBuffer::read(char& data) 00051 { 00052 if (bytes == 0) { 00053 return 0; 00054 } 00055 if (readIndex == bufferSize) { 00056 readIndex = 0; 00057 } 00058 data = buffer[readIndex++]; 00059 bytes--; 00060 checkThreshold(); 00061 return 1; 00062 } 00063 00064 int MTSCircularBuffer::write(const char* data, int length) 00065 { 00066 int i = 0; 00067 while((i < length) && (bytes < bufferSize)) { 00068 if(writeIndex == bufferSize) { 00069 writeIndex = 0; 00070 } 00071 buffer[writeIndex++] = data[i++]; 00072 bytes++; 00073 checkThreshold(); 00074 } 00075 return i; 00076 } 00077 00078 int MTSCircularBuffer::write(char data) 00079 { 00080 if (bytes == bufferSize) { 00081 return 0; 00082 } 00083 if(writeIndex == bufferSize) { 00084 writeIndex = 0; 00085 } 00086 buffer[writeIndex++] = data; 00087 bytes++; 00088 checkThreshold(); 00089 return 1; 00090 } 00091 00092 int MTSCircularBuffer::remaining() 00093 { 00094 return bufferSize - bytes; 00095 } 00096 00097 int MTSCircularBuffer::size() 00098 { 00099 return bytes; 00100 } 00101 00102 bool MTSCircularBuffer::isFull() 00103 { 00104 if (bytes == bufferSize) { 00105 return true; 00106 } else { 00107 return false; 00108 } 00109 } 00110 00111 bool MTSCircularBuffer::isEmpty() 00112 { 00113 if (bytes == 0) { 00114 return true; 00115 } else { 00116 return false; 00117 } 00118 } 00119 00120 void MTSCircularBuffer::clear() 00121 { 00122 writeIndex = readIndex = bytes = 0; 00123 } 00124 00125 void MTSCircularBuffer::checkThreshold() 00126 { 00127 if (_threshold == -1) { 00128 return; 00129 } 00130 switch (_op) { 00131 case Vars::GREATER: 00132 if (bytes > _threshold) { 00133 notify.call(); 00134 } 00135 break; 00136 case Vars::LESS: 00137 if (bytes < _threshold) { 00138 notify.call(); 00139 } 00140 break; 00141 case Vars::GREATER_EQUAL: 00142 if (bytes >= _threshold) { 00143 notify.call(); 00144 } 00145 break; 00146 case Vars::LESS_EQUAL: 00147 if (bytes <= _threshold) { 00148 notify.call(); 00149 } 00150 break; 00151 case Vars::EQUAL: 00152 if (bytes == _threshold) { 00153 notify.call(); 00154 } 00155 break; 00156 } 00157 } 00158
Generated on Tue Jul 12 2022 21:46:23 by 1.7.2