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) 2006-2013 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_INTERRUPTMANAGER_H
skrawool 0:3954a906acc2 17 #define MBED_INTERRUPTMANAGER_H
skrawool 0:3954a906acc2 18
skrawool 0:3954a906acc2 19 #include "cmsis.h"
skrawool 0:3954a906acc2 20 #include "platform/CallChain.h"
skrawool 0:3954a906acc2 21 #include "platform/PlatformMutex.h"
skrawool 0:3954a906acc2 22 #include <string.h>
skrawool 0:3954a906acc2 23
skrawool 0:3954a906acc2 24 namespace mbed {
skrawool 0:3954a906acc2 25 /** \addtogroup drivers */
skrawool 0:3954a906acc2 26 /** @{*/
skrawool 0:3954a906acc2 27
skrawool 0:3954a906acc2 28 /** Use this singleton if you need to chain interrupt handlers.
skrawool 0:3954a906acc2 29 *
skrawool 0:3954a906acc2 30 * @Note Synchronization level: Thread safe
skrawool 0:3954a906acc2 31 *
skrawool 0:3954a906acc2 32 * Example (for LPC1768):
skrawool 0:3954a906acc2 33 * @code
skrawool 0:3954a906acc2 34 * #include "InterruptManager.h"
skrawool 0:3954a906acc2 35 * #include "mbed.h"
skrawool 0:3954a906acc2 36 *
skrawool 0:3954a906acc2 37 * Ticker flipper;
skrawool 0:3954a906acc2 38 * DigitalOut led1(LED1);
skrawool 0:3954a906acc2 39 * DigitalOut led2(LED2);
skrawool 0:3954a906acc2 40 *
skrawool 0:3954a906acc2 41 * void flip(void) {
skrawool 0:3954a906acc2 42 * led1 = !led1;
skrawool 0:3954a906acc2 43 * }
skrawool 0:3954a906acc2 44 *
skrawool 0:3954a906acc2 45 * void handler(void) {
skrawool 0:3954a906acc2 46 * led2 = !led1;
skrawool 0:3954a906acc2 47 * }
skrawool 0:3954a906acc2 48 *
skrawool 0:3954a906acc2 49 * int main() {
skrawool 0:3954a906acc2 50 * led1 = led2 = 0;
skrawool 0:3954a906acc2 51 * flipper.attach(&flip, 1.0);
skrawool 0:3954a906acc2 52 * InterruptManager::get()->add_handler(handler, TIMER3_IRQn);
skrawool 0:3954a906acc2 53 * }
skrawool 0:3954a906acc2 54 * @endcode
skrawool 0:3954a906acc2 55 */
skrawool 0:3954a906acc2 56 class InterruptManager {
skrawool 0:3954a906acc2 57 public:
skrawool 0:3954a906acc2 58 /** Return the only instance of this class
skrawool 0:3954a906acc2 59 */
skrawool 0:3954a906acc2 60 static InterruptManager* get();
skrawool 0:3954a906acc2 61
skrawool 0:3954a906acc2 62 /** Destroy the current instance of the interrupt manager
skrawool 0:3954a906acc2 63 */
skrawool 0:3954a906acc2 64 static void destroy();
skrawool 0:3954a906acc2 65
skrawool 0:3954a906acc2 66 /** Add a handler for an interrupt at the end of the handler list
skrawool 0:3954a906acc2 67 *
skrawool 0:3954a906acc2 68 * @param function the handler to add
skrawool 0:3954a906acc2 69 * @param irq interrupt number
skrawool 0:3954a906acc2 70 *
skrawool 0:3954a906acc2 71 * @returns
skrawool 0:3954a906acc2 72 * The function object created for 'function'
skrawool 0:3954a906acc2 73 */
skrawool 0:3954a906acc2 74 pFunctionPointer_t add_handler(void (*function)(void), IRQn_Type irq) {
skrawool 0:3954a906acc2 75 // Underlying call is thread safe
skrawool 0:3954a906acc2 76 return add_common(function, irq);
skrawool 0:3954a906acc2 77 }
skrawool 0:3954a906acc2 78
skrawool 0:3954a906acc2 79 /** Add a handler for an interrupt at the beginning of the handler list
skrawool 0:3954a906acc2 80 *
skrawool 0:3954a906acc2 81 * @param function the handler to add
skrawool 0:3954a906acc2 82 * @param irq interrupt number
skrawool 0:3954a906acc2 83 *
skrawool 0:3954a906acc2 84 * @returns
skrawool 0:3954a906acc2 85 * The function object created for 'function'
skrawool 0:3954a906acc2 86 */
skrawool 0:3954a906acc2 87 pFunctionPointer_t add_handler_front(void (*function)(void), IRQn_Type irq) {
skrawool 0:3954a906acc2 88 // Underlying call is thread safe
skrawool 0:3954a906acc2 89 return add_common(function, irq, true);
skrawool 0:3954a906acc2 90 }
skrawool 0:3954a906acc2 91
skrawool 0:3954a906acc2 92 /** Add a handler for an interrupt at the end of the handler list
skrawool 0:3954a906acc2 93 *
skrawool 0:3954a906acc2 94 * @param tptr pointer to the object that has the handler function
skrawool 0:3954a906acc2 95 * @param mptr pointer to the actual handler function
skrawool 0:3954a906acc2 96 * @param irq interrupt number
skrawool 0:3954a906acc2 97 *
skrawool 0:3954a906acc2 98 * @returns
skrawool 0:3954a906acc2 99 * The function object created for 'tptr' and 'mptr'
skrawool 0:3954a906acc2 100 */
skrawool 0:3954a906acc2 101 template<typename T>
skrawool 0:3954a906acc2 102 pFunctionPointer_t add_handler(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
skrawool 0:3954a906acc2 103 // Underlying call is thread safe
skrawool 0:3954a906acc2 104 return add_common(tptr, mptr, irq);
skrawool 0:3954a906acc2 105 }
skrawool 0:3954a906acc2 106
skrawool 0:3954a906acc2 107 /** Add a handler for an interrupt at the beginning of the handler list
skrawool 0:3954a906acc2 108 *
skrawool 0:3954a906acc2 109 * @param tptr pointer to the object that has the handler function
skrawool 0:3954a906acc2 110 * @param mptr pointer to the actual handler function
skrawool 0:3954a906acc2 111 * @param irq interrupt number
skrawool 0:3954a906acc2 112 *
skrawool 0:3954a906acc2 113 * @returns
skrawool 0:3954a906acc2 114 * The function object created for 'tptr' and 'mptr'
skrawool 0:3954a906acc2 115 */
skrawool 0:3954a906acc2 116 template<typename T>
skrawool 0:3954a906acc2 117 pFunctionPointer_t add_handler_front(T* tptr, void (T::*mptr)(void), IRQn_Type irq) {
skrawool 0:3954a906acc2 118 // Underlying call is thread safe
skrawool 0:3954a906acc2 119 return add_common(tptr, mptr, irq, true);
skrawool 0:3954a906acc2 120 }
skrawool 0:3954a906acc2 121
skrawool 0:3954a906acc2 122 /** Remove a handler from an interrupt
skrawool 0:3954a906acc2 123 *
skrawool 0:3954a906acc2 124 * @param handler the function object for the handler to remove
skrawool 0:3954a906acc2 125 * @param irq the interrupt number
skrawool 0:3954a906acc2 126 *
skrawool 0:3954a906acc2 127 * @returns
skrawool 0:3954a906acc2 128 * true if the handler was found and removed, false otherwise
skrawool 0:3954a906acc2 129 */
skrawool 0:3954a906acc2 130 bool remove_handler(pFunctionPointer_t handler, IRQn_Type irq);
skrawool 0:3954a906acc2 131
skrawool 0:3954a906acc2 132 private:
skrawool 0:3954a906acc2 133 InterruptManager();
skrawool 0:3954a906acc2 134 ~InterruptManager();
skrawool 0:3954a906acc2 135
skrawool 0:3954a906acc2 136 void lock();
skrawool 0:3954a906acc2 137 void unlock();
skrawool 0:3954a906acc2 138
skrawool 0:3954a906acc2 139 // We declare the copy contructor and the assignment operator, but we don't
skrawool 0:3954a906acc2 140 // implement them. This way, if someone tries to copy/assign our instance,
skrawool 0:3954a906acc2 141 // he will get an error at compile time.
skrawool 0:3954a906acc2 142 InterruptManager(const InterruptManager&);
skrawool 0:3954a906acc2 143 InterruptManager& operator =(const InterruptManager&);
skrawool 0:3954a906acc2 144
skrawool 0:3954a906acc2 145 template<typename T>
skrawool 0:3954a906acc2 146 pFunctionPointer_t add_common(T *tptr, void (T::*mptr)(void), IRQn_Type irq, bool front=false) {
skrawool 0:3954a906acc2 147 _mutex.lock();
skrawool 0:3954a906acc2 148 int irq_pos = get_irq_index(irq);
skrawool 0:3954a906acc2 149 bool change = must_replace_vector(irq);
skrawool 0:3954a906acc2 150
skrawool 0:3954a906acc2 151 pFunctionPointer_t pf = front ? _chains[irq_pos]->add_front(tptr, mptr) : _chains[irq_pos]->add(tptr, mptr);
skrawool 0:3954a906acc2 152 if (change)
skrawool 0:3954a906acc2 153 NVIC_SetVector(irq, (uint32_t)&InterruptManager::static_irq_helper);
skrawool 0:3954a906acc2 154 _mutex.unlock();
skrawool 0:3954a906acc2 155 return pf;
skrawool 0:3954a906acc2 156 }
skrawool 0:3954a906acc2 157
skrawool 0:3954a906acc2 158 pFunctionPointer_t add_common(void (*function)(void), IRQn_Type irq, bool front=false);
skrawool 0:3954a906acc2 159 bool must_replace_vector(IRQn_Type irq);
skrawool 0:3954a906acc2 160 int get_irq_index(IRQn_Type irq);
skrawool 0:3954a906acc2 161 void irq_helper();
skrawool 0:3954a906acc2 162 void add_helper(void (*function)(void), IRQn_Type irq, bool front=false);
skrawool 0:3954a906acc2 163 static void static_irq_helper();
skrawool 0:3954a906acc2 164
skrawool 0:3954a906acc2 165 CallChain* _chains[NVIC_NUM_VECTORS];
skrawool 0:3954a906acc2 166 static InterruptManager* _instance;
skrawool 0:3954a906acc2 167 PlatformMutex _mutex;
skrawool 0:3954a906acc2 168 };
skrawool 0:3954a906acc2 169
skrawool 0:3954a906acc2 170 } // namespace mbed
skrawool 0:3954a906acc2 171
skrawool 0:3954a906acc2 172 #endif
skrawool 0:3954a906acc2 173
skrawool 0:3954a906acc2 174
skrawool 0:3954a906acc2 175 /** @}*/