Robrecht Daems

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Revision:
2:b47f54fdea91
Parent:
1:34e76c0cbe5a
diff -r 34e76c0cbe5a -r b47f54fdea91 debounce_button.cpp
--- a/debounce_button.cpp	Sun Oct 29 23:01:06 2017 +0000
+++ b/debounce_button.cpp	Mon Oct 30 16:44:56 2017 +0000
@@ -35,7 +35,48 @@
     This function is called when the button has been pressed by the user.
 */
 
+Timeout timeout1;
+Timeout timeout2;
+DigitalOut busy_led(LED1);
+volatile bool button1_pressed=false;   // Used in the main loop
+volatile bool button1_enabled=true;   // Used for debouncing
+volatile int multiclick_state=0;   // Counts how many clicks occured in the time slot, used in main loop
+volatile int last_multiclick_state=0;   // Counts how many clicks occured in the previous timeslot
+volatile bool button1_busy = false;      // Informs the mainloop that the user is clicking the button
+volatile bool result_ready=false;
+
 void button1_onpressed_cb(void)
 {
-    
-}
\ No newline at end of file
+    if(button1_enabled)
+    {
+        if(multiclick_state == 0) //first press this second
+        {
+            button1_busy=true;
+            busy_led = 1;
+            timeout1.attach(callback(&button1_multiclick_reset_cb), 1.0);           
+        }
+        multiclick_state += 1;
+        button1_enabled=false;        
+        timeout2.attach(callback(&button1_enabled_cb), 0.075);
+    }
+}
+
+/**
+    Resets the amount of clicks, but stores this value for the usage in the main loop
+*/
+void button1_multiclick_reset_cb()
+{
+    last_multiclick_state = multiclick_state;
+    multiclick_state = 0;
+    button1_busy=false;
+    result_ready=true;
+    busy_led=0;
+}
+
+/**
+    Enables the button again after a timeout, used for debouncing the button 
+*/
+void button1_enabled_cb()
+{
+    button1_enabled=true;
+}  
\ No newline at end of file