Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of Pokitto by
mbed-pokitto/api/CircularBuffer.h@5:7e5c566b1760, 2017-10-07 (annotated)
- Committer:
- Pokitto
- Date:
- Sat Oct 07 21:31:12 2017 +0000
- Revision:
- 5:7e5c566b1760
mbed-pokitto integrated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 5:7e5c566b1760 | 1 | /* mbed Microcontroller Library |
Pokitto | 5:7e5c566b1760 | 2 | * Copyright (c) 2015 ARM Limited |
Pokitto | 5:7e5c566b1760 | 3 | * |
Pokitto | 5:7e5c566b1760 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Pokitto | 5:7e5c566b1760 | 5 | * you may not use this file except in compliance with the License. |
Pokitto | 5:7e5c566b1760 | 6 | * You may obtain a copy of the License at |
Pokitto | 5:7e5c566b1760 | 7 | * |
Pokitto | 5:7e5c566b1760 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Pokitto | 5:7e5c566b1760 | 9 | * |
Pokitto | 5:7e5c566b1760 | 10 | * Unless required by applicable law or agreed to in writing, software |
Pokitto | 5:7e5c566b1760 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
Pokitto | 5:7e5c566b1760 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Pokitto | 5:7e5c566b1760 | 13 | * See the License for the specific language governing permissions and |
Pokitto | 5:7e5c566b1760 | 14 | * limitations under the License. |
Pokitto | 5:7e5c566b1760 | 15 | */ |
Pokitto | 5:7e5c566b1760 | 16 | #ifndef MBED_CIRCULARBUFFER_H |
Pokitto | 5:7e5c566b1760 | 17 | #define MBED_CIRCULARBUFFER_H |
Pokitto | 5:7e5c566b1760 | 18 | |
Pokitto | 5:7e5c566b1760 | 19 | namespace mbed { |
Pokitto | 5:7e5c566b1760 | 20 | |
Pokitto | 5:7e5c566b1760 | 21 | /** Templated Circular buffer class |
Pokitto | 5:7e5c566b1760 | 22 | */ |
Pokitto | 5:7e5c566b1760 | 23 | template<typename T, uint32_t BufferSize, typename CounterType = uint32_t> |
Pokitto | 5:7e5c566b1760 | 24 | class CircularBuffer { |
Pokitto | 5:7e5c566b1760 | 25 | public: |
Pokitto | 5:7e5c566b1760 | 26 | CircularBuffer() : _head(0), _tail(0), _full(false) { |
Pokitto | 5:7e5c566b1760 | 27 | } |
Pokitto | 5:7e5c566b1760 | 28 | |
Pokitto | 5:7e5c566b1760 | 29 | ~CircularBuffer() { |
Pokitto | 5:7e5c566b1760 | 30 | } |
Pokitto | 5:7e5c566b1760 | 31 | |
Pokitto | 5:7e5c566b1760 | 32 | /** Push the transaction to the buffer. This overwrites the buffer if it's |
Pokitto | 5:7e5c566b1760 | 33 | * full |
Pokitto | 5:7e5c566b1760 | 34 | * |
Pokitto | 5:7e5c566b1760 | 35 | * @param data Data to be pushed to the buffer |
Pokitto | 5:7e5c566b1760 | 36 | */ |
Pokitto | 5:7e5c566b1760 | 37 | void push(const T& data) { |
Pokitto | 5:7e5c566b1760 | 38 | if (full()) { |
Pokitto | 5:7e5c566b1760 | 39 | _tail++; |
Pokitto | 5:7e5c566b1760 | 40 | _tail %= BufferSize; |
Pokitto | 5:7e5c566b1760 | 41 | } |
Pokitto | 5:7e5c566b1760 | 42 | _pool[_head++] = data; |
Pokitto | 5:7e5c566b1760 | 43 | _head %= BufferSize; |
Pokitto | 5:7e5c566b1760 | 44 | if (_head == _tail) { |
Pokitto | 5:7e5c566b1760 | 45 | _full = true; |
Pokitto | 5:7e5c566b1760 | 46 | } |
Pokitto | 5:7e5c566b1760 | 47 | } |
Pokitto | 5:7e5c566b1760 | 48 | |
Pokitto | 5:7e5c566b1760 | 49 | /** Pop the transaction from the buffer |
Pokitto | 5:7e5c566b1760 | 50 | * |
Pokitto | 5:7e5c566b1760 | 51 | * @param data Data to be pushed to the buffer |
Pokitto | 5:7e5c566b1760 | 52 | * @return True if the buffer is not empty and data contains a transaction, false otherwise |
Pokitto | 5:7e5c566b1760 | 53 | */ |
Pokitto | 5:7e5c566b1760 | 54 | bool pop(T& data) { |
Pokitto | 5:7e5c566b1760 | 55 | if (!empty()) { |
Pokitto | 5:7e5c566b1760 | 56 | data = _pool[_tail++]; |
Pokitto | 5:7e5c566b1760 | 57 | _tail %= BufferSize; |
Pokitto | 5:7e5c566b1760 | 58 | _full = false; |
Pokitto | 5:7e5c566b1760 | 59 | return true; |
Pokitto | 5:7e5c566b1760 | 60 | } |
Pokitto | 5:7e5c566b1760 | 61 | return false; |
Pokitto | 5:7e5c566b1760 | 62 | } |
Pokitto | 5:7e5c566b1760 | 63 | |
Pokitto | 5:7e5c566b1760 | 64 | /** Check if the buffer is empty |
Pokitto | 5:7e5c566b1760 | 65 | * |
Pokitto | 5:7e5c566b1760 | 66 | * @return True if the buffer is empty, false if not |
Pokitto | 5:7e5c566b1760 | 67 | */ |
Pokitto | 5:7e5c566b1760 | 68 | bool empty() { |
Pokitto | 5:7e5c566b1760 | 69 | return (_head == _tail) && !_full; |
Pokitto | 5:7e5c566b1760 | 70 | } |
Pokitto | 5:7e5c566b1760 | 71 | |
Pokitto | 5:7e5c566b1760 | 72 | /** Check if the buffer is full |
Pokitto | 5:7e5c566b1760 | 73 | * |
Pokitto | 5:7e5c566b1760 | 74 | * @return True if the buffer is full, false if not |
Pokitto | 5:7e5c566b1760 | 75 | */ |
Pokitto | 5:7e5c566b1760 | 76 | bool full() { |
Pokitto | 5:7e5c566b1760 | 77 | return _full; |
Pokitto | 5:7e5c566b1760 | 78 | } |
Pokitto | 5:7e5c566b1760 | 79 | |
Pokitto | 5:7e5c566b1760 | 80 | /** Reset the buffer |
Pokitto | 5:7e5c566b1760 | 81 | * |
Pokitto | 5:7e5c566b1760 | 82 | */ |
Pokitto | 5:7e5c566b1760 | 83 | void reset() { |
Pokitto | 5:7e5c566b1760 | 84 | _head = 0; |
Pokitto | 5:7e5c566b1760 | 85 | _tail = 0; |
Pokitto | 5:7e5c566b1760 | 86 | _full = false; |
Pokitto | 5:7e5c566b1760 | 87 | } |
Pokitto | 5:7e5c566b1760 | 88 | |
Pokitto | 5:7e5c566b1760 | 89 | private: |
Pokitto | 5:7e5c566b1760 | 90 | T _pool[BufferSize]; |
Pokitto | 5:7e5c566b1760 | 91 | volatile CounterType _head; |
Pokitto | 5:7e5c566b1760 | 92 | volatile CounterType _tail; |
Pokitto | 5:7e5c566b1760 | 93 | volatile bool _full; |
Pokitto | 5:7e5c566b1760 | 94 | }; |
Pokitto | 5:7e5c566b1760 | 95 | |
Pokitto | 5:7e5c566b1760 | 96 | } |
Pokitto | 5:7e5c566b1760 | 97 | |
Pokitto | 5:7e5c566b1760 | 98 | #endif |