CDY version that shares functionality with Counter

Dependencies:   SDFileSystem_HelloWorld mbed FATFileSystem

Committer:
Charles David Young
Date:
Mon Nov 05 09:52:17 2018 -0700
Revision:
3:c547dba5d39b
Parent:
0:aa13e1c335cd
debug

Who changed what in which revision?

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