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
skrawool 0:3954a906acc2 2 /** \addtogroup platform */
skrawool 0:3954a906acc2 3 /** @{*/
skrawool 0:3954a906acc2 4 /* mbed Microcontroller Library
skrawool 0:3954a906acc2 5 * Copyright (c) 2006-2013 ARM Limited
skrawool 0:3954a906acc2 6 *
skrawool 0:3954a906acc2 7 * Licensed under the Apache License, Version 2.0 (the "License");
skrawool 0:3954a906acc2 8 * you may not use this file except in compliance with the License.
skrawool 0:3954a906acc2 9 * You may obtain a copy of the License at
skrawool 0:3954a906acc2 10 *
skrawool 0:3954a906acc2 11 * http://www.apache.org/licenses/LICENSE-2.0
skrawool 0:3954a906acc2 12 *
skrawool 0:3954a906acc2 13 * Unless required by applicable law or agreed to in writing, software
skrawool 0:3954a906acc2 14 * distributed under the License is distributed on an "AS IS" BASIS,
skrawool 0:3954a906acc2 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
skrawool 0:3954a906acc2 16 * See the License for the specific language governing permissions and
skrawool 0:3954a906acc2 17 * limitations under the License.
skrawool 0:3954a906acc2 18 */
skrawool 0:3954a906acc2 19 #ifndef SINGLETONPTR_H
skrawool 0:3954a906acc2 20 #define SINGLETONPTR_H
skrawool 0:3954a906acc2 21
skrawool 0:3954a906acc2 22 #include <stdint.h>
skrawool 0:3954a906acc2 23 #include <new>
skrawool 0:3954a906acc2 24 #include "platform/mbed_assert.h"
skrawool 0:3954a906acc2 25 #ifdef MBED_CONF_RTOS_PRESENT
skrawool 0:3954a906acc2 26 #include "cmsis_os.h"
skrawool 0:3954a906acc2 27 #endif
skrawool 0:3954a906acc2 28
skrawool 0:3954a906acc2 29 #ifdef MBED_CONF_RTOS_PRESENT
skrawool 0:3954a906acc2 30 extern osMutexId singleton_mutex_id;
skrawool 0:3954a906acc2 31 #endif
skrawool 0:3954a906acc2 32
skrawool 0:3954a906acc2 33 /** Lock the singleton mutex
skrawool 0:3954a906acc2 34 *
skrawool 0:3954a906acc2 35 * This function is typically used to provide
skrawool 0:3954a906acc2 36 * exclusive access when initializing a
skrawool 0:3954a906acc2 37 * global object.
skrawool 0:3954a906acc2 38 */
skrawool 0:3954a906acc2 39 inline static void singleton_lock(void)
skrawool 0:3954a906acc2 40 {
skrawool 0:3954a906acc2 41 #ifdef MBED_CONF_RTOS_PRESENT
skrawool 0:3954a906acc2 42 osMutexWait(singleton_mutex_id, osWaitForever);
skrawool 0:3954a906acc2 43 #endif
skrawool 0:3954a906acc2 44 }
skrawool 0:3954a906acc2 45
skrawool 0:3954a906acc2 46 /** Unlock the singleton mutex
skrawool 0:3954a906acc2 47 *
skrawool 0:3954a906acc2 48 * This function is typically used to provide
skrawool 0:3954a906acc2 49 * exclusive access when initializing a
skrawool 0:3954a906acc2 50 * global object.
skrawool 0:3954a906acc2 51 */
skrawool 0:3954a906acc2 52 inline static void singleton_unlock(void)
skrawool 0:3954a906acc2 53 {
skrawool 0:3954a906acc2 54 #ifdef MBED_CONF_RTOS_PRESENT
skrawool 0:3954a906acc2 55 osMutexRelease (singleton_mutex_id);
skrawool 0:3954a906acc2 56 #endif
skrawool 0:3954a906acc2 57 }
skrawool 0:3954a906acc2 58
skrawool 0:3954a906acc2 59 /** Utility class for creating an using a singleton
skrawool 0:3954a906acc2 60 *
skrawool 0:3954a906acc2 61 * @Note Synchronization level: Thread safe
skrawool 0:3954a906acc2 62 *
skrawool 0:3954a906acc2 63 * @Note: This class must only be used in a static context -
skrawool 0:3954a906acc2 64 * this class must never be allocated or created on the
skrawool 0:3954a906acc2 65 * stack.
skrawool 0:3954a906acc2 66 *
skrawool 0:3954a906acc2 67 * @Note: This class is lazily initialized on first use.
skrawool 0:3954a906acc2 68 * This class is a POD type so if it is not used it will
skrawool 0:3954a906acc2 69 * be garbage collected.
skrawool 0:3954a906acc2 70 */
skrawool 0:3954a906acc2 71 template <class T>
skrawool 0:3954a906acc2 72 struct SingletonPtr {
skrawool 0:3954a906acc2 73
skrawool 0:3954a906acc2 74 /** Get a pointer to the underlying singleton
skrawool 0:3954a906acc2 75 *
skrawool 0:3954a906acc2 76 * @returns
skrawool 0:3954a906acc2 77 * A pointer to the singleton
skrawool 0:3954a906acc2 78 */
skrawool 0:3954a906acc2 79 T* get() {
skrawool 0:3954a906acc2 80 if (NULL == _ptr) {
skrawool 0:3954a906acc2 81 singleton_lock();
skrawool 0:3954a906acc2 82 if (NULL == _ptr) {
skrawool 0:3954a906acc2 83 _ptr = new (_data) T();
skrawool 0:3954a906acc2 84 }
skrawool 0:3954a906acc2 85 singleton_unlock();
skrawool 0:3954a906acc2 86 }
skrawool 0:3954a906acc2 87 // _ptr was not zero initialized or was
skrawool 0:3954a906acc2 88 // corrupted if this assert is hit
skrawool 0:3954a906acc2 89 MBED_ASSERT(_ptr == (T *)&_data);
skrawool 0:3954a906acc2 90 return _ptr;
skrawool 0:3954a906acc2 91 }
skrawool 0:3954a906acc2 92
skrawool 0:3954a906acc2 93 /** Get a pointer to the underlying singleton
skrawool 0:3954a906acc2 94 *
skrawool 0:3954a906acc2 95 * @returns
skrawool 0:3954a906acc2 96 * A pointer to the singleton
skrawool 0:3954a906acc2 97 */
skrawool 0:3954a906acc2 98 T* operator->() {
skrawool 0:3954a906acc2 99 return get();
skrawool 0:3954a906acc2 100 }
skrawool 0:3954a906acc2 101
skrawool 0:3954a906acc2 102 // This is zero initialized when in global scope
skrawool 0:3954a906acc2 103 T *_ptr;
skrawool 0:3954a906acc2 104 // Force data to be 4 byte aligned
skrawool 0:3954a906acc2 105 uint32_t _data[(sizeof(T) + sizeof(uint32_t) - 1) / sizeof(uint32_t)];
skrawool 0:3954a906acc2 106 };
skrawool 0:3954a906acc2 107
skrawool 0:3954a906acc2 108 #endif
skrawool 0:3954a906acc2 109
skrawool 0:3954a906acc2 110 /** @}*/