This is a debounced version of DigitalIn for Mechanical Switches. It is simplistic, and should be easy to use.

Dependents:   PID_ENCODER

Files at this revision

API Documentation at this revision

Comitter:
mr63
Date:
Fri Aug 16 17:49:32 2013 +0000
Commit message:
This is a Debounced version of DigitalIn

Changed in this revision

DebouncedIn.cpp Show annotated file Show diff for this revision Revisions of this file
DebouncedIn.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 323110faffe5 DebouncedIn.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.cpp	Fri Aug 16 17:49:32 2013 +0000
@@ -0,0 +1,57 @@
+#include "DebouncedIn.h"
+#include "mbed.h"
+
+#define CHECK_MSEC 5 // Read hardware every 5 msec 
+#define PRESS_MSEC 100 // Stable time before registering pressed 
+#define RELEASE_MSEC 100 // Stable time before registering released
+ 
+
+bool DebounceKeyPress = false;
+ /*v
+  * Constructor
+  */
+ DebouncedIn::DebouncedIn(PinName pin): _pin(pin)
+ {    
+	  _pin.mode(PullDown);
+    _poll.attach(this, &DebouncedIn::CheckState, 0.005);     
+ }
+ 
+ bool DebouncedIn::get()
+ {
+	 return(DebounceKeyPress);
+ }
+ 
+ bool DebouncedIn::get_pin()
+ {
+	 return(_pin);
+ }
+ 
+ 
+ void DebouncedIn::CheckState()
+ {
+	 static int Count = RELEASE_MSEC / CHECK_MSEC;
+	 bool RawState = DebouncedIn::get_pin();
+	 if(RawState==DebounceKeyPress)
+	 {
+		 if(DebounceKeyPress)
+			Count = RELEASE_MSEC / CHECK_MSEC;
+		 else
+			Count = PRESS_MSEC / CHECK_MSEC; 
+	 }
+	 else
+	 {
+		 if(--Count == 0)
+		 {
+			 DebounceKeyPress = RawState;
+			 if(DebounceKeyPress)
+				Count = RELEASE_MSEC / CHECK_MSEC;
+			 else
+				Count = PRESS_MSEC / CHECK_MSEC; 
+				 
+		 }
+	 }
+	 
+ }
+
+
+ 
\ No newline at end of file
diff -r 000000000000 -r 323110faffe5 DebouncedIn.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DebouncedIn.h	Fri Aug 16 17:49:32 2013 +0000
@@ -0,0 +1,18 @@
+#include "mbed.h"
+ 
+     class DebouncedIn {
+         public:      
+              DebouncedIn(PinName pin);
+							bool get();
+				      void CheckState();
+							bool get_pin();	
+               
+         private :    
+                // objects
+                DigitalIn _pin;
+								Ticker _poll;				 
+			 
+     };
+
+		 
+		 
\ No newline at end of file
diff -r 000000000000 -r 323110faffe5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Aug 16 17:49:32 2013 +0000
@@ -0,0 +1,22 @@
+#include "mbed.h"
+#include "DebouncedIn.h"
+
+
+DigitalOut Status(LED1);
+
+DebouncedIn Button(p12);
+
+
+int main()
+{
+
+
+    while(true)
+		{
+			
+				   Status = Button.get();			
+			
+    }
+}
+
+