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