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