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