mbed

Dependents:   DHTSensor_Test K64F_eCompass_OneNET_JW

Committer:
mbotkinl
Date:
Wed Feb 25 20:22:22 2015 +0000
Revision:
0:2cc6bb4d7fea
Working code to read Temperature and Humidity readings

Who changed what in which revision?

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