Official Sheffield ARMBand micro:bit program

Committer:
MrBedfordVan
Date:
Mon Oct 17 12:41:20 2016 +0000
Revision:
0:b9164b348919
Official Sheffield ARMBand Micro:bit program

Who changed what in which revision?

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