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