Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
elijahorr
Date:
Thu Apr 14 07:28:54 2016 +0000
Revision:
121:672067c3ada4
Parent:
101:7cff1c4259d7
.

Who changed what in which revision?

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