vote controller

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Revision:
3:062b94b59b1b
Parent:
2:5b7d055dbc91
diff -r 5b7d055dbc91 -r 062b94b59b1b debounce_button.cpp
--- a/debounce_button.cpp	Tue Oct 31 09:01:56 2017 +0000
+++ b/debounce_button.cpp	Thu Oct 11 16:06:17 2018 +0000
@@ -15,6 +15,25 @@
         going to make optimisations.
 */
 
+volatile bool button1_pressed = false;
+volatile bool button1_enabled = true;
+volatile bool button1_busy = false; 
+volatile int multiclick_state = 0;
+int counter = 0;
+DigitalOut myled(LED1);
+Timeout debouncer_delay;
+Timeout busy_delay;
+DigitalOut led2(LED2);
+
+void showPresses() {
+    for(int i = 0; i < multiclick_state; i++) {
+        led2 = 1;
+        wait(1);
+        led2 = 0;
+        wait(1);
+    }
+}
+
 /**
     TODO
     ----
@@ -26,7 +45,11 @@
         -   turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
 */
 void button1_multiclick_reset_cb(void) {
-    
+    multiclick_state = counter;
+    counter = 0;
+    button1_busy = false;
+    myled = 0;
+    //showPresses();
 }
 
 /**
@@ -34,9 +57,8 @@
     ----
     This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
 */
-void button1_enabled_cb(void)
-{
-    
+void button1_enabled_cb(void) {
+    button1_enabled = true;
 }
 
 /**
@@ -54,5 +76,15 @@
 */
 void button1_onpressed_cb(void)
 {
-    
+    if (button1_enabled) { // Disabled while the button is bouncing
+        button1_enabled = false;
+        button1_pressed = true; // To be read by the main loop
+        debouncer_delay.attach(callback(button1_enabled_cb), 0.05); // Debounce time 50 ms
+        counter++;
+        if(!button1_busy) {
+            myled = 1;
+            button1_busy = true;
+            busy_delay.attach(callback(button1_multiclick_reset_cb), 1);
+        }
+    }
 }
\ No newline at end of file