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