Home Alert System

Dependencies:   PWM_Tone_Library DHT

Committer:
aziz111
Date:
Fri Mar 08 17:15:02 2019 +0000
Revision:
5:569a4894abc1
Parent:
3:78f223d34f36
Final

Who changed what in which revision?

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