temp

Dependencies:   mbed SDFileSystem MS5607 ADXL345_I2C FATFileSystem

Committer:
IKobayashi
Date:
Mon Mar 16 23:37:42 2020 +0900
Revision:
0:c88c3b616c00
copy

Who changed what in which revision?

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