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