PIR grove sensor that sends the sensed data to Thingspeak.com through the wifi module ESP 8266

Dependencies:   mbed

Committer:
skrawool
Date:
Mon Dec 05 16:39:27 2016 +0000
Revision:
0:3954a906acc2
PIR grove sensor that sends the sensed data to thingspeak through the wifi module ESP 8266

Who changed what in which revision?

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