SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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