IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Sat Oct 29 15:11:28 2016 +0000
Revision:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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