mbed library for slider v2

Dependents:   kl46z_slider_v2

Committer:
mturner5
Date:
Wed Sep 14 07:04:27 2016 +0000
Revision:
0:b7116bd48af6
Tried to use the timer.

Who changed what in which revision?

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