This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers CircularBuffer.h Source File

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 #include "critical.h"
00020 
00021 namespace mbed {
00022 
00023 /** Templated Circular buffer class
00024  *
00025  *  @Note Synchronization level: Interrupt safe
00026  */
00027 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
00028 class CircularBuffer {
00029 public:
00030     CircularBuffer() : _head(0), _tail(0), _full(false) {
00031     }
00032 
00033     ~CircularBuffer() {
00034     }
00035 
00036     /** Push the transaction to the buffer. This overwrites the buffer if it's
00037      *  full
00038      *
00039      * @param data Data to be pushed to the buffer
00040      */
00041     void push(const T& data) {
00042         core_util_critical_section_enter();
00043         if (full()) {
00044             _tail++;
00045             _tail %= BufferSize;
00046         }
00047         _pool[_head++] = data;
00048         _head %= BufferSize;
00049         if (_head == _tail) {
00050             _full = true;
00051         }
00052         core_util_critical_section_exit();
00053     }
00054 
00055     /** Pop the transaction from the buffer
00056      *
00057      * @param data Data to be pushed to the buffer
00058      * @return True if the buffer is not empty and data contains a transaction, false otherwise
00059      */
00060     bool pop(T& data) {
00061         bool data_popped = false;
00062         core_util_critical_section_enter();
00063         if (!empty()) {
00064             data = _pool[_tail++];
00065             _tail %= BufferSize;
00066             _full = false;
00067             data_popped = true;
00068         }
00069         core_util_critical_section_exit();
00070         return data_popped;
00071     }
00072 
00073     /** Check if the buffer is empty
00074      *
00075      * @return True if the buffer is empty, false if not
00076      */
00077     bool empty() {
00078         core_util_critical_section_enter();
00079         bool is_empty = (_head == _tail) && !_full;
00080         core_util_critical_section_exit();
00081         return is_empty;
00082     }
00083 
00084     /** Check if the buffer is full
00085      *
00086      * @return True if the buffer is full, false if not
00087      */
00088     bool full() {
00089         core_util_critical_section_enter();
00090         bool full = _full;
00091         core_util_critical_section_exit();
00092         return full;
00093     }
00094 
00095     /** Reset the buffer
00096      *
00097      */
00098     void reset() {
00099         core_util_critical_section_enter();
00100         _head = 0;
00101         _tail = 0;
00102         _full = false;
00103         core_util_critical_section_exit();
00104     }
00105 
00106 private:
00107     T _pool[BufferSize];
00108     volatile CounterType _head;
00109     volatile CounterType _tail;
00110     volatile bool _full;
00111 };
00112 
00113 }
00114 
00115 #endif