mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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