SDCard version

Fork of gr-peach-opencv-project-sd-card by the do

Committer:
thedo
Date:
Fri Jul 21 01:26:54 2017 +0000
Revision:
167:2ee3e82cb6f5
gr-peach-opencv-project-sd-card

Who changed what in which revision?

UserRevisionLine numberNew contents of line
thedo 167:2ee3e82cb6f5 1 /* mbed Microcontroller Library
thedo 167:2ee3e82cb6f5 2 * Copyright (c) 2015 ARM Limited
thedo 167:2ee3e82cb6f5 3 *
thedo 167:2ee3e82cb6f5 4 * Licensed under the Apache License, Version 2.0 (the "License");
thedo 167:2ee3e82cb6f5 5 * you may not use this file except in compliance with the License.
thedo 167:2ee3e82cb6f5 6 * You may obtain a copy of the License at
thedo 167:2ee3e82cb6f5 7 *
thedo 167:2ee3e82cb6f5 8 * http://www.apache.org/licenses/LICENSE-2.0
thedo 167:2ee3e82cb6f5 9 *
thedo 167:2ee3e82cb6f5 10 * Unless required by applicable law or agreed to in writing, software
thedo 167:2ee3e82cb6f5 11 * distributed under the License is distributed on an "AS IS" BASIS,
thedo 167:2ee3e82cb6f5 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
thedo 167:2ee3e82cb6f5 13 * See the License for the specific language governing permissions and
thedo 167:2ee3e82cb6f5 14 * limitations under the License.
thedo 167:2ee3e82cb6f5 15 */
thedo 167:2ee3e82cb6f5 16 #ifndef MBED_CIRCULARBUFFER_H
thedo 167:2ee3e82cb6f5 17 #define MBED_CIRCULARBUFFER_H
thedo 167:2ee3e82cb6f5 18
thedo 167:2ee3e82cb6f5 19 #include "platform/mbed_critical.h"
thedo 167:2ee3e82cb6f5 20
thedo 167:2ee3e82cb6f5 21 namespace mbed {
thedo 167:2ee3e82cb6f5 22 /** \addtogroup platform */
thedo 167:2ee3e82cb6f5 23
thedo 167:2ee3e82cb6f5 24 /** Templated Circular buffer class
thedo 167:2ee3e82cb6f5 25 *
thedo 167:2ee3e82cb6f5 26 * @note Synchronization level: Interrupt safe
thedo 167:2ee3e82cb6f5 27 * @ingroup platform
thedo 167:2ee3e82cb6f5 28 */
thedo 167:2ee3e82cb6f5 29 template<typename T, uint32_t BufferSize, typename CounterType = uint32_t>
thedo 167:2ee3e82cb6f5 30 class CircularBuffer {
thedo 167:2ee3e82cb6f5 31 public:
thedo 167:2ee3e82cb6f5 32 CircularBuffer() : _head(0), _tail(0), _full(false) {
thedo 167:2ee3e82cb6f5 33 }
thedo 167:2ee3e82cb6f5 34
thedo 167:2ee3e82cb6f5 35 ~CircularBuffer() {
thedo 167:2ee3e82cb6f5 36 }
thedo 167:2ee3e82cb6f5 37
thedo 167:2ee3e82cb6f5 38 /** Push the transaction to the buffer. This overwrites the buffer if it's
thedo 167:2ee3e82cb6f5 39 * full
thedo 167:2ee3e82cb6f5 40 *
thedo 167:2ee3e82cb6f5 41 * @param data Data to be pushed to the buffer
thedo 167:2ee3e82cb6f5 42 */
thedo 167:2ee3e82cb6f5 43 void push(const T& data) {
thedo 167:2ee3e82cb6f5 44 core_util_critical_section_enter();
thedo 167:2ee3e82cb6f5 45 if (full()) {
thedo 167:2ee3e82cb6f5 46 _tail++;
thedo 167:2ee3e82cb6f5 47 _tail %= BufferSize;
thedo 167:2ee3e82cb6f5 48 }
thedo 167:2ee3e82cb6f5 49 _pool[_head++] = data;
thedo 167:2ee3e82cb6f5 50 _head %= BufferSize;
thedo 167:2ee3e82cb6f5 51 if (_head == _tail) {
thedo 167:2ee3e82cb6f5 52 _full = true;
thedo 167:2ee3e82cb6f5 53 }
thedo 167:2ee3e82cb6f5 54 core_util_critical_section_exit();
thedo 167:2ee3e82cb6f5 55 }
thedo 167:2ee3e82cb6f5 56
thedo 167:2ee3e82cb6f5 57 /** Pop the transaction from the buffer
thedo 167:2ee3e82cb6f5 58 *
thedo 167:2ee3e82cb6f5 59 * @param data Data to be pushed to the buffer
thedo 167:2ee3e82cb6f5 60 * @return True if the buffer is not empty and data contains a transaction, false otherwise
thedo 167:2ee3e82cb6f5 61 */
thedo 167:2ee3e82cb6f5 62 bool pop(T& data) {
thedo 167:2ee3e82cb6f5 63 bool data_popped = false;
thedo 167:2ee3e82cb6f5 64 core_util_critical_section_enter();
thedo 167:2ee3e82cb6f5 65 if (!empty()) {
thedo 167:2ee3e82cb6f5 66 data = _pool[_tail++];
thedo 167:2ee3e82cb6f5 67 _tail %= BufferSize;
thedo 167:2ee3e82cb6f5 68 _full = false;
thedo 167:2ee3e82cb6f5 69 data_popped = true;
thedo 167:2ee3e82cb6f5 70 }
thedo 167:2ee3e82cb6f5 71 core_util_critical_section_exit();
thedo 167:2ee3e82cb6f5 72 return data_popped;
thedo 167:2ee3e82cb6f5 73 }
thedo 167:2ee3e82cb6f5 74
thedo 167:2ee3e82cb6f5 75 /** Check if the buffer is empty
thedo 167:2ee3e82cb6f5 76 *
thedo 167:2ee3e82cb6f5 77 * @return True if the buffer is empty, false if not
thedo 167:2ee3e82cb6f5 78 */
thedo 167:2ee3e82cb6f5 79 bool empty() const {
thedo 167:2ee3e82cb6f5 80 core_util_critical_section_enter();
thedo 167:2ee3e82cb6f5 81 bool is_empty = (_head == _tail) && !_full;
thedo 167:2ee3e82cb6f5 82 core_util_critical_section_exit();
thedo 167:2ee3e82cb6f5 83 return is_empty;
thedo 167:2ee3e82cb6f5 84 }
thedo 167:2ee3e82cb6f5 85
thedo 167:2ee3e82cb6f5 86 /** Check if the buffer is full
thedo 167:2ee3e82cb6f5 87 *
thedo 167:2ee3e82cb6f5 88 * @return True if the buffer is full, false if not
thedo 167:2ee3e82cb6f5 89 */
thedo 167:2ee3e82cb6f5 90 bool full() const {
thedo 167:2ee3e82cb6f5 91 core_util_critical_section_enter();
thedo 167:2ee3e82cb6f5 92 bool full = _full;
thedo 167:2ee3e82cb6f5 93 core_util_critical_section_exit();
thedo 167:2ee3e82cb6f5 94 return full;
thedo 167:2ee3e82cb6f5 95 }
thedo 167:2ee3e82cb6f5 96
thedo 167:2ee3e82cb6f5 97 /** Reset the buffer
thedo 167:2ee3e82cb6f5 98 *
thedo 167:2ee3e82cb6f5 99 */
thedo 167:2ee3e82cb6f5 100 void reset() {
thedo 167:2ee3e82cb6f5 101 core_util_critical_section_enter();
thedo 167:2ee3e82cb6f5 102 _head = 0;
thedo 167:2ee3e82cb6f5 103 _tail = 0;
thedo 167:2ee3e82cb6f5 104 _full = false;
thedo 167:2ee3e82cb6f5 105 core_util_critical_section_exit();
thedo 167:2ee3e82cb6f5 106 }
thedo 167:2ee3e82cb6f5 107
thedo 167:2ee3e82cb6f5 108 private:
thedo 167:2ee3e82cb6f5 109 T _pool[BufferSize];
thedo 167:2ee3e82cb6f5 110 volatile CounterType _head;
thedo 167:2ee3e82cb6f5 111 volatile CounterType _tail;
thedo 167:2ee3e82cb6f5 112 volatile bool _full;
thedo 167:2ee3e82cb6f5 113 };
thedo 167:2ee3e82cb6f5 114
thedo 167:2ee3e82cb6f5 115 }
thedo 167:2ee3e82cb6f5 116
thedo 167:2ee3e82cb6f5 117 #endif
thedo 167:2ee3e82cb6f5 118
thedo 167:2ee3e82cb6f5 119