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