class to debouce interrupt pin

Files at this revision

API Documentation at this revision

Comitter:
JeroenAero
Date:
Tue Jan 05 14:38:14 2021 +0000
Commit message:
Debounce working, only NULL functions will fault;

Changed in this revision

Debounce.cpp Show annotated file Show diff for this revision Revisions of this file
Debounce.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r dc197298a382 Debounce.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debounce.cpp	Tue Jan 05 14:38:14 2021 +0000
@@ -0,0 +1,47 @@
+#include "mbed.h"
+#include "Debounce.h"
+
+Debounce::Debounce(PinName Pin, int DebounceMS, void (*RiseFunction)(), void (*FallFunction)()) : Input(Pin)
+{
+    Input.mode(PullUp); //Set the input to pull up or pull down.
+    DebounceTime = (float)DebounceMS / 1000;    //Calculate the debouncetime in seconds.
+    Input.rise(callback(this,&Debounce::InputRise));
+    Input.fall(callback(this,&Debounce::InputFall));  
+    RiseFunctionPointer = RiseFunction;
+    FallFunctionPointer = FallFunction;
+}
+
+void Debounce::InputRise()
+{
+    //This function will get called when the input rises.
+    InputRiseTick.attach(callback(this, &Debounce::RiseTick), DebounceTime); //Attach a ticker with the desired check time and call the function risetick when time's up.
+}
+
+void Debounce::InputFall()
+{
+    //This function will get called when the input falls.
+    InputFallTick.attach(callback(this, &Debounce::FallTick), DebounceTime); //Attach a ticker with the desired check time and call the function risetick when time's up.
+}
+
+void Debounce::RiseTick()
+{
+    //This function will get called after a set time from the last rise event.
+    InputRiseTick.detach(); //detach the ticker, as we only use it once.
+    if (Input) //Check wheter or not the input is still high.
+    {
+        //Input is still high after the last rise event.
+        (*RiseFunctionPointer)();  //Call the rise function. 
+          
+    }
+}
+
+void Debounce::FallTick()
+{
+    //This function will get called after a set time from the last fall event.
+    InputFallTick.detach(); //detach the ticker, as we only use it once.
+    if (!Input) //Check wheter or not the input is still low.
+    {
+        //Input is still low after the last fall event.
+        (*FallFunctionPointer)();   
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r dc197298a382 Debounce.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Debounce.h	Tue Jan 05 14:38:14 2021 +0000
@@ -0,0 +1,27 @@
+#ifndef MBED_DEBOUNCE_H
+#define MBED_DEBOUNCE_H
+
+#include "mbed.h"
+
+class Debounce {
+
+    public:
+        Debounce(PinName Pin, int DebounceMS, void (*RiseFunction)(), void (*FallFunction)());
+  
+    private:
+        InterruptIn Input;  
+        Ticker InputRiseTick;
+        Ticker InputFallTick;
+        
+        float DebounceTime;
+        
+        void InputRise();
+        void InputFall();
+        void RiseTick();
+        void FallTick();
+        
+        void (*FallFunctionPointer)();
+        void (*RiseFunctionPointer)();
+};
+
+#endif
\ No newline at end of file