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