mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

InterruptIn.h

Committer:
emilmont
Date:
2012-10-23
Revision:
7:73c5efe92a6c
Parent:
0:8024c367e29f
Child:
8:c14af7958ef5

File content as of revision 7:73c5efe92a6c:

/* mbed Microcontroller Library - InterruptIn
 * Copyright (c) 2006-2011 ARM Limited. All rights reserved.
 */
#ifndef MBED_INTERRUPTIN_H
#define MBED_INTERRUPTIN_H

#include "device.h"

#if DEVICE_INTERRUPTIN

#include "gpio_api.h"
#include "gpio_irq_api.h"

#include "platform.h"
#include "Base.h"
#include "FunctionPointer.h"

namespace mbed {

/* Class: InterruptIn
 *  A digital interrupt input, used to call a function on a rising or falling edge
 *
 * Example:
 * > // Flash an LED while waiting for events
 * >
 * > #include "mbed.h"
 * >
 * > InterruptIn event(p16);
 * > DigitalOut led(LED1);
 * >
 * > void trigger() {
 * >     printf("triggered!\n");
 * > }
 * >
 * > int main() {
 * >     event.rise(&trigger);
 * >     while(1) {
 * >         led = !led;
 * >         wait(0.25);
 * >     }
 * > }
 */
class InterruptIn : public Base {

public:

    /* Constructor: InterruptIn
     *  Create an InterruptIn connected to the specified pin
     *
     * Variables:
     *  pin - InterruptIn pin to connect to
     *  name - (optional) A string to identify the object
     */
    InterruptIn(PinName pin, const char *name = NULL);
    virtual ~InterruptIn();
 
     int read();
#ifdef MBED_OPERATORS
    operator int();

#endif
     
    /* Function: rise
     *  Attach a function to call when a rising edge occurs on the input
     *
     * Variables:
     *  fptr - A pointer to a void function, or 0 to set as none
     */
    void rise(void (*fptr)(void));

    /* Function: rise
     *  Attach a member function to call when a rising edge occurs on the input
     *     
     * Variables:
     *  tptr - pointer to the object to call the member function on
     *  mptr - pointer to the member function to be called
     */
    template<typename T>
    void rise(T* tptr, void (T::*mptr)(void)) {
        _rise.attach(tptr, mptr);
        gpio_irq_set(&gpio_irq, IRQ_RISE, 1);
    }

    /* Function: fall
     *  Attach a function to call when a falling edge occurs on the input
     *
     * Variables:
     *  fptr - A pointer to a void function, or 0 to set as none
     */
    void fall(void (*fptr)(void));

    /* Function: fall
     *  Attach a member function to call when a falling edge occurs on the input
     *     
     * Variables:
     *  tptr - pointer to the object to call the member function on
     *  mptr - pointer to the member function to be called
     */
    template<typename T>
    void fall(T* tptr, void (T::*mptr)(void)) {
        _fall.attach(tptr, mptr);
        gpio_irq_set(&gpio_irq, IRQ_FALL, 1);
    }

    /* Function: mode
     *  Set the input pin mode
     *
     * Variables:
     *  mode - PullUp, PullDown, PullNone
     */
    void mode(PinMode pull);
    
    static void _irq_handler(uint32_t id, gpio_irq_event event);
    
protected:
    gpio_object gpio;
    gpio_irq_object gpio_irq;
    
    FunctionPointer _rise;
    FunctionPointer _fall;
};

} // namespace mbed

#endif

#endif