IoT Home Alarm System

Dependents:   IoTBurglar_and_Fire_AlarmSystem

Committer:
kbrahmbhatt6
Date:
Fri Apr 29 06:59:59 2016 +0000
Revision:
0:2f388b030837
1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kbrahmbhatt6 0:2f388b030837 1 /* mbed Microcontroller Library
kbrahmbhatt6 0:2f388b030837 2 * Copyright (c) 2006-2013 ARM Limited
kbrahmbhatt6 0:2f388b030837 3 *
kbrahmbhatt6 0:2f388b030837 4 * Licensed under the Apache License, Version 2.0 (the "License");
kbrahmbhatt6 0:2f388b030837 5 * you may not use this file except in compliance with the License.
kbrahmbhatt6 0:2f388b030837 6 * You may obtain a copy of the License at
kbrahmbhatt6 0:2f388b030837 7 *
kbrahmbhatt6 0:2f388b030837 8 * http://www.apache.org/licenses/LICENSE-2.0
kbrahmbhatt6 0:2f388b030837 9 *
kbrahmbhatt6 0:2f388b030837 10 * Unless required by applicable law or agreed to in writing, software
kbrahmbhatt6 0:2f388b030837 11 * distributed under the License is distributed on an "AS IS" BASIS,
kbrahmbhatt6 0:2f388b030837 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
kbrahmbhatt6 0:2f388b030837 13 * See the License for the specific language governing permissions and
kbrahmbhatt6 0:2f388b030837 14 * limitations under the License.
kbrahmbhatt6 0:2f388b030837 15 */
kbrahmbhatt6 0:2f388b030837 16 #ifndef MBED_INTERRUPTIN_H
kbrahmbhatt6 0:2f388b030837 17 #define MBED_INTERRUPTIN_H
kbrahmbhatt6 0:2f388b030837 18
kbrahmbhatt6 0:2f388b030837 19 #include "platform.h"
kbrahmbhatt6 0:2f388b030837 20
kbrahmbhatt6 0:2f388b030837 21 #if DEVICE_INTERRUPTIN
kbrahmbhatt6 0:2f388b030837 22
kbrahmbhatt6 0:2f388b030837 23 #include "gpio_api.h"
kbrahmbhatt6 0:2f388b030837 24 #include "gpio_irq_api.h"
kbrahmbhatt6 0:2f388b030837 25 #include "FunctionPointer.h"
kbrahmbhatt6 0:2f388b030837 26
kbrahmbhatt6 0:2f388b030837 27 namespace mbed {
kbrahmbhatt6 0:2f388b030837 28
kbrahmbhatt6 0:2f388b030837 29 /** A digital interrupt input, used to call a function on a rising or falling edge
kbrahmbhatt6 0:2f388b030837 30 *
kbrahmbhatt6 0:2f388b030837 31 * Example:
kbrahmbhatt6 0:2f388b030837 32 * @code
kbrahmbhatt6 0:2f388b030837 33 * // Flash an LED while waiting for events
kbrahmbhatt6 0:2f388b030837 34 *
kbrahmbhatt6 0:2f388b030837 35 * #include "mbed.h"
kbrahmbhatt6 0:2f388b030837 36 *
kbrahmbhatt6 0:2f388b030837 37 * InterruptIn event(p16);
kbrahmbhatt6 0:2f388b030837 38 * DigitalOut led(LED1);
kbrahmbhatt6 0:2f388b030837 39 *
kbrahmbhatt6 0:2f388b030837 40 * void trigger() {
kbrahmbhatt6 0:2f388b030837 41 * printf("triggered!\n");
kbrahmbhatt6 0:2f388b030837 42 * }
kbrahmbhatt6 0:2f388b030837 43 *
kbrahmbhatt6 0:2f388b030837 44 * int main() {
kbrahmbhatt6 0:2f388b030837 45 * event.rise(&trigger);
kbrahmbhatt6 0:2f388b030837 46 * while(1) {
kbrahmbhatt6 0:2f388b030837 47 * led = !led;
kbrahmbhatt6 0:2f388b030837 48 * wait(0.25);
kbrahmbhatt6 0:2f388b030837 49 * }
kbrahmbhatt6 0:2f388b030837 50 * }
kbrahmbhatt6 0:2f388b030837 51 * @endcode
kbrahmbhatt6 0:2f388b030837 52 */
kbrahmbhatt6 0:2f388b030837 53 class InterruptIn {
kbrahmbhatt6 0:2f388b030837 54
kbrahmbhatt6 0:2f388b030837 55 public:
kbrahmbhatt6 0:2f388b030837 56
kbrahmbhatt6 0:2f388b030837 57 /** Create an InterruptIn connected to the specified pin
kbrahmbhatt6 0:2f388b030837 58 *
kbrahmbhatt6 0:2f388b030837 59 * @param pin InterruptIn pin to connect to
kbrahmbhatt6 0:2f388b030837 60 * @param name (optional) A string to identify the object
kbrahmbhatt6 0:2f388b030837 61 */
kbrahmbhatt6 0:2f388b030837 62 InterruptIn(PinName pin);
kbrahmbhatt6 0:2f388b030837 63 virtual ~InterruptIn();
kbrahmbhatt6 0:2f388b030837 64
kbrahmbhatt6 0:2f388b030837 65 int read();
kbrahmbhatt6 0:2f388b030837 66 #ifdef MBED_OPERATORS
kbrahmbhatt6 0:2f388b030837 67 operator int();
kbrahmbhatt6 0:2f388b030837 68
kbrahmbhatt6 0:2f388b030837 69 #endif
kbrahmbhatt6 0:2f388b030837 70
kbrahmbhatt6 0:2f388b030837 71 /** Attach a function to call when a rising edge occurs on the input
kbrahmbhatt6 0:2f388b030837 72 *
kbrahmbhatt6 0:2f388b030837 73 * @param fptr A pointer to a void function, or 0 to set as none
kbrahmbhatt6 0:2f388b030837 74 */
kbrahmbhatt6 0:2f388b030837 75 void rise(void (*fptr)(void));
kbrahmbhatt6 0:2f388b030837 76
kbrahmbhatt6 0:2f388b030837 77 /** Attach a member function to call when a rising edge occurs on the input
kbrahmbhatt6 0:2f388b030837 78 *
kbrahmbhatt6 0:2f388b030837 79 * @param tptr pointer to the object to call the member function on
kbrahmbhatt6 0:2f388b030837 80 * @param mptr pointer to the member function to be called
kbrahmbhatt6 0:2f388b030837 81 */
kbrahmbhatt6 0:2f388b030837 82 template<typename T>
kbrahmbhatt6 0:2f388b030837 83 void rise(T* tptr, void (T::*mptr)(void)) {
kbrahmbhatt6 0:2f388b030837 84 _rise.attach(tptr, mptr);
kbrahmbhatt6 0:2f388b030837 85 gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
kbrahmbhatt6 0:2f388b030837 86 }
kbrahmbhatt6 0:2f388b030837 87
kbrahmbhatt6 0:2f388b030837 88 /** Attach a function to call when a falling edge occurs on the input
kbrahmbhatt6 0:2f388b030837 89 *
kbrahmbhatt6 0:2f388b030837 90 * @param fptr A pointer to a void function, or 0 to set as none
kbrahmbhatt6 0:2f388b030837 91 */
kbrahmbhatt6 0:2f388b030837 92 void fall(void (*fptr)(void));
kbrahmbhatt6 0:2f388b030837 93
kbrahmbhatt6 0:2f388b030837 94 /** Attach a member function to call when a falling edge occurs on the input
kbrahmbhatt6 0:2f388b030837 95 *
kbrahmbhatt6 0:2f388b030837 96 * @param tptr pointer to the object to call the member function on
kbrahmbhatt6 0:2f388b030837 97 * @param mptr pointer to the member function to be called
kbrahmbhatt6 0:2f388b030837 98 */
kbrahmbhatt6 0:2f388b030837 99 template<typename T>
kbrahmbhatt6 0:2f388b030837 100 void fall(T* tptr, void (T::*mptr)(void)) {
kbrahmbhatt6 0:2f388b030837 101 _fall.attach(tptr, mptr);
kbrahmbhatt6 0:2f388b030837 102 gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
kbrahmbhatt6 0:2f388b030837 103 }
kbrahmbhatt6 0:2f388b030837 104
kbrahmbhatt6 0:2f388b030837 105 /** Set the input pin mode
kbrahmbhatt6 0:2f388b030837 106 *
kbrahmbhatt6 0:2f388b030837 107 * @param mode PullUp, PullDown, PullNone
kbrahmbhatt6 0:2f388b030837 108 */
kbrahmbhatt6 0:2f388b030837 109 void mode(PinMode pull);
kbrahmbhatt6 0:2f388b030837 110
kbrahmbhatt6 0:2f388b030837 111 /** Enable IRQ. This method depends on hw implementation, might enable one
kbrahmbhatt6 0:2f388b030837 112 * port interrupts. For further information, check gpio_irq_enable().
kbrahmbhatt6 0:2f388b030837 113 */
kbrahmbhatt6 0:2f388b030837 114 void enable_irq();
kbrahmbhatt6 0:2f388b030837 115
kbrahmbhatt6 0:2f388b030837 116 /** Disable IRQ. This method depends on hw implementation, might disable one
kbrahmbhatt6 0:2f388b030837 117 * port interrupts. For further information, check gpio_irq_disable().
kbrahmbhatt6 0:2f388b030837 118 */
kbrahmbhatt6 0:2f388b030837 119 void disable_irq();
kbrahmbhatt6 0:2f388b030837 120
kbrahmbhatt6 0:2f388b030837 121 static void _irq_handler(uint32_t id, gpio_irq_event event);
kbrahmbhatt6 0:2f388b030837 122
kbrahmbhatt6 0:2f388b030837 123 protected:
kbrahmbhatt6 0:2f388b030837 124 gpio_t gpio;
kbrahmbhatt6 0:2f388b030837 125 gpio_irq_t gpio_irq;
kbrahmbhatt6 0:2f388b030837 126
kbrahmbhatt6 0:2f388b030837 127 FunctionPointer _rise;
kbrahmbhatt6 0:2f388b030837 128 FunctionPointer _fall;
kbrahmbhatt6 0:2f388b030837 129 };
kbrahmbhatt6 0:2f388b030837 130
kbrahmbhatt6 0:2f388b030837 131 } // namespace mbed
kbrahmbhatt6 0:2f388b030837 132
kbrahmbhatt6 0:2f388b030837 133 #endif
kbrahmbhatt6 0:2f388b030837 134
kbrahmbhatt6 0:2f388b030837 135 #endif