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