Debounce InterruptIn

Dependents:   led_sigfox Allumag_lampe_sigfox Case_study_02_Turnstile B18_MP3_PLAYER ... more

Files at this revision

API Documentation at this revision

Comitter:
kandangath
Date:
Tue Feb 18 16:32:33 2014 +0000
Parent:
9:970b74eb0407
Child:
11:af6d7dc34062
Commit message:
Allow DebouncedInterrupt to be set with just the PinName

Changed in this revision

DebounceInterrupts.cpp Show diff for this revision Revisions of this file
DebounceInterrupts.h Show diff for this revision Revisions of this file
DebouncedInterrupt.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedInterrupt.h Show annotated file Show diff for this revision Revisions of this file
--- a/DebounceInterrupts.cpp	Tue Feb 18 07:06:32 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,47 +0,0 @@
-#include "DebounceInterrupts.h"
-
-Timeout timeout;
-
-DebounceInterrupts::DebounceInterrupts(void (*fptr)(void), 
-                                       InterruptIn *interruptIn, 
-                                       const interruptTrigger& trigger,
-                                       const unsigned int& debounce_ms)
-{
-    fCallback = fptr;
-    _last_debounce_count = _debounce_count = 0;
-    
-    switch(trigger) {
-     case INT_RISE:
-        interruptIn->rise(this, &DebounceInterrupts::_onInterrupt);
-        break;
-     case INT_FALL:
-        interruptIn->fall(this, &DebounceInterrupts::_onInterrupt);
-        break;
-     default:
-        break;
-    }
-
-    _debounce_us = 1000*debounce_ms;
-}
-
-DebounceInterrupts::~DebounceInterrupts()
-{
-}
-
-void DebounceInterrupts::_callback()
-{
-    _last_debounce_count = _debounce_count;
-    _debounce_count = 0;
-    fCallback();
-}
-
-void DebounceInterrupts::_onInterrupt()
-{
-    _debounce_count++;
-    timeout.attach_us(this, &DebounceInterrupts::_callback, _debounce_us);
-}
-
-unsigned int DebounceInterrupts::get_debounce()
-{
-    return _last_debounce_count;
-}
--- a/DebounceInterrupts.h	Tue Feb 18 07:06:32 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,56 +0,0 @@
-
-/**
- * Debounces an InterruptIn
- */
-
-/* 
-Example:
-InterruptIn up_button(p15);
-
-void onUp()
-{
-    // Do Something
-}
-
-int main()
-{
-    DebounceInterrupts upD(&onUp, &up_button, INT_FALL, 100);
-    while(1) {
-        ...
-    }
-}
-*/
- 
-#ifndef DEBOUNCE_INTERRUPTS_H
-#define DEBOUNCE_INTERRUPTS_H
-
-#include <stdint.h>
-#include "mbed.h"
-
-enum interruptTrigger{
-    INT_FALL = 0,
-    INT_RISE = 1
-};
-
-class DebounceInterrupts {
-private:
-    unsigned int _debounce_us;
-    unsigned int _debounce_count;
-    unsigned int _last_debounce_count;
-    
-    void (*fCallback)(void);
-    void _onInterrupt(void);
-    void _callback(void);
-public:
-    DebounceInterrupts(void (*fptr)(void),                  /* function to be called after debounced InterruptIn */
-                       InterruptIn *interrupt,              /* InterruptIn to monitor */
-                       const interruptTrigger& trigger,     /* true: rise, false: fall */
-                       const uint32_t& debounce_ms=10);     /* stability duration required */
-    ~DebounceInterrupts();
-    /*
-    * Get number of de-bounced interrupts
-    * @return: debounced count
-    */
-    unsigned int get_debounce();
-};
-#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedInterrupt.cpp	Tue Feb 18 16:32:33 2014 +0000
@@ -0,0 +1,62 @@
+/**
+* DebouncedInterrupt.cpp
+**/
+#include "DebouncedInterrupt.h"
+
+Timeout timeout;
+
+DebouncedInterrupt::DebouncedInterrupt(PinName pin)
+{
+    _in = new InterruptIn(pin);
+}
+
+DebouncedInterrupt::~DebouncedInterrupt()
+{
+}
+
+void DebouncedInterrupt::attach(void (*fptr)(void), 
+                                       const interruptTrigger& trigger,
+                                       const unsigned int& debounce_ms)
+{
+    if(fptr) {
+        fCallback = fptr;
+        _last_debounce_count = _debounce_count = 0;
+        
+        switch(trigger) {
+         case INT_RISE:
+            _in->rise(this, &DebouncedInterrupt::_onInterrupt);
+            break;
+         case INT_FALL:
+            _in->fall(this, &DebouncedInterrupt::_onInterrupt);
+            break;
+         default:
+            break;
+        }
+    
+        _debounce_us = 1000*debounce_ms;
+    }
+}
+
+void DebouncedInterrupt::reset()
+{
+    timeout.detach();
+}
+
+unsigned int DebouncedInterrupt::get_debounce()
+{
+    return _last_debounce_count;
+}
+
+void DebouncedInterrupt::_callback()
+{
+    _last_debounce_count = _debounce_count;
+    _debounce_count = 0;
+    fCallback();
+}
+
+void DebouncedInterrupt::_onInterrupt()
+{
+    _debounce_count++;
+    timeout.attach_us(this, &DebouncedInterrupt::_callback, _debounce_us);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedInterrupt.h	Tue Feb 18 16:32:33 2014 +0000
@@ -0,0 +1,62 @@
+/**
+* DebouncedInterrupt.h
+* Monitors and debounces an InterruptIn
+**/
+
+/**
+Example:
+InterruptIn up_button(p15);
+
+void onUp()
+{
+    // Do Something
+}
+
+int main()
+{
+    DebouncedInterrupt upD(&onUp, &up_button, INT_FALL, 100);
+    while(1) {
+        ...
+    }
+}
+**/
+ 
+#ifndef DEBOUNCED_INTERRUPT_H
+#define DEBOUNCED_INTERRUPT_H
+
+#include <stdint.h>
+#include "mbed.h"
+
+enum interruptTrigger{
+    INT_FALL = 0,
+    INT_RISE = 1
+};
+
+class DebouncedInterrupt {
+private:
+    unsigned int _debounce_us;
+    unsigned int _debounce_count;
+    unsigned int _last_debounce_count;
+    InterruptIn *_in;
+    
+    void (*fCallback)(void);
+    void _onInterrupt(void);
+    void _callback(void);
+public:
+    DebouncedInterrupt(PinName pin);
+    ~DebouncedInterrupt();
+    
+    // Start monitoring the interupt and attach a callback
+    void attach(void (*fptr)(void), const interruptTrigger& trigger, const uint32_t& debounce_ms=10);
+   
+    // Stop monitoring the interrupt
+    void reset();
+    
+    
+    /*
+    * Get number of de-bounced interrupts
+    * @return: debounced count
+    */
+    unsigned int get_debounce();
+};
+#endif
\ No newline at end of file