A replacement for InterruptIn that debounces the interrupt.

Dependents:   D7A_Demo-Get-started CVtoOSCConverter EE3501keypad D7A_Localisation ... more

Fork of DebouncedInterrupt by Anil Kandangath

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DebouncedInterrupt.h Source File

DebouncedInterrupt.h

00001  
00002 #ifndef DEBOUNCED_INTERRUPT_H
00003 #define DEBOUNCED_INTERRUPT_H
00004 
00005 #include <stdint.h>
00006 #include "mbed.h"
00007 
00008 /**
00009 typedef enum {
00010     IRQ_NONE,
00011     IRQ_RISE,
00012     IRQ_FALL
00013 } gpio_irq_event;
00014 **/
00015 
00016 /** A replacement for InterruptIn that debounces the interrupt
00017  *
00018  * Example:
00019  * @code
00020  *
00021  * #include "DebouncedInterrupt.h"
00022  *
00023  * DebouncedInterrupt up_button(USER_BUTTON);
00024  * 
00025  * void onUp()
00026  * {
00027  *    // Do Something
00028  * }
00029  * 
00030  * int main()
00031  * {
00032  *     // Will immediatly call function and ignore other interrupts until timeout
00033  *     up_button.attach(&onUp, IRQ_FALL, 1000, true);
00034  *
00035  *     // Will call function only if button has been held for the specified time
00036  *     //up_button.attach(&onUp, IRQ_FALL, 500, false);
00037  *
00038  *     while(1) {
00039  *         ...
00040  *     }
00041  * }
00042  * @endcode
00043  */
00044 class DebouncedInterrupt {
00045 private:
00046     unsigned int _debounce_us;
00047     bool _immediate;
00048     bool _timeout_expired;
00049     InterruptIn *_in;
00050     DigitalIn *_din;
00051     gpio_irq_event _trigger;
00052     Timeout *_timeout;
00053     
00054     // Diagnostics
00055     volatile unsigned int _bounce_count;
00056     volatile unsigned int _last_bounce_count;
00057     
00058     void _onInterrupt(void);
00059     void _onCallback(void);
00060 public:
00061     DebouncedInterrupt(PinName pin);
00062     ~DebouncedInterrupt();
00063     
00064     // Start monitoring the interupt and attach a callback
00065     void attach(void (*fptr)(void), const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false);
00066     
00067     template<typename T, typename M>
00068     void attach(T *obj, M method, const gpio_irq_event trigger, const uint32_t debounce_ms=10, bool immediate=false) {
00069         _callback = callback(obj, method);
00070         _last_bounce_count = _bounce_count = 0;
00071         _debounce_us = 1000*debounce_ms;
00072         _trigger = trigger;
00073         
00074         switch(trigger)
00075         {
00076             case IRQ_RISE:
00077                 _in->rise(callback(this, &DebouncedInterrupt::_onInterrupt));
00078                 break;
00079             case IRQ_FALL:
00080                 _in->fall(callback(this, &DebouncedInterrupt::_onInterrupt));
00081                 break;
00082             case IRQ_NONE:
00083                 reset(); // Unexpected. Clear callbacks.
00084                 break;
00085         }
00086     }
00087    
00088     // Stop monitoring the interrupt
00089     void reset();
00090     
00091     
00092     /*
00093     * Get number of bounces 
00094     * @return: bounce count
00095     */
00096     unsigned int get_bounce();
00097 protected:
00098 //    https://github.com/mbedmicro/mbed/blob/master/libraries/mbed/api/FunctionPointer.h
00099     Callback<void()> _callback;
00100 };
00101 #endif