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