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.
Fork of mbed-src by
CircularBuffer.h
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2015 ARM Limited 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 #ifndef MBED_CIRCULARBUFFER_H 00017 #define MBED_CIRCULARBUFFER_H 00018 00019 namespace mbed { 00020 00021 /** Templated Circular buffer class 00022 */ 00023 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> 00024 class CircularBuffer { 00025 public: 00026 CircularBuffer() : _head(0), _tail(0), _full(false) { 00027 } 00028 00029 ~CircularBuffer() { 00030 } 00031 00032 /** Push the transaction to the buffer. This overwrites the buffer if it's 00033 * full 00034 * 00035 * @param data Data to be pushed to the buffer 00036 */ 00037 void push(const T& data) { 00038 if (full()) { 00039 _tail++; 00040 _tail %= BufferSize; 00041 } 00042 _pool[_head++] = data; 00043 _head %= BufferSize; 00044 if (_head == _tail) { 00045 _full = true; 00046 } 00047 } 00048 00049 /** Pop the transaction from the buffer 00050 * 00051 * @param data Data to be pushed to the buffer 00052 * @return True if the buffer is not empty and data contains a transaction, false otherwise 00053 */ 00054 bool pop(T& data) { 00055 if (!empty()) { 00056 data = _pool[_tail++]; 00057 _tail %= BufferSize; 00058 _full = false; 00059 return true; 00060 } 00061 return false; 00062 } 00063 00064 /** Check if the buffer is empty 00065 * 00066 * @return True if the buffer is empty, false if not 00067 */ 00068 bool empty() { 00069 return (_head == _tail) && !_full; 00070 } 00071 00072 /** Check if the buffer is full 00073 * 00074 * @return True if the buffer is full, false if not 00075 */ 00076 bool full() { 00077 return _full; 00078 } 00079 00080 /** Reset the buffer 00081 * 00082 */ 00083 void reset() { 00084 _head = 0; 00085 _tail = 0; 00086 _full = false; 00087 } 00088 00089 private: 00090 T _pool[BufferSize]; 00091 volatile CounterType _head; 00092 volatile CounterType _tail; 00093 volatile bool _full; 00094 }; 00095 00096 } 00097 00098 #endif
Generated on Tue Jul 12 2022 18:13:05 by
