Vadim Gouskov 15-10-18 Vote Controller Version 1

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Files at this revision

API Documentation at this revision

Comitter:
VadimGouskov
Date:
Mon Oct 15 15:53:31 2018 +0000
Parent:
2:5b7d055dbc91
Commit message:
15-10-17 VoteController Version 1

Changed in this revision

debounce_button.cpp Show annotated file Show diff for this revision Revisions of this file
debounce_button.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 5b7d055dbc91 -r e234aaf2a634 debounce_button.cpp
--- a/debounce_button.cpp	Tue Oct 31 09:01:56 2017 +0000
+++ b/debounce_button.cpp	Mon Oct 15 15:53:31 2018 +0000
@@ -1,5 +1,18 @@
 #include "debounce_button.h"
 
+volatile bool button1_pressed;   // Used in the main loop
+volatile bool button1_enabled;   // Used for debouncing
+volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
+volatile bool button1_busy;      // Informs the mainloop that the user is clicking the button
+DigitalOut status_led(LED1);
+
+//debugging
+volatile bool debounced_flag;   // Used in the main loop
+volatile bool done_counting;   // Used in the main loop
+
+Timeout button_enabled_to;
+Timeout multiclick_reset_to;
+
 /**
     Some tips and tricks:
     -   To use the built-in LED:
@@ -26,7 +39,9 @@
         -   turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
 */
 void button1_multiclick_reset_cb(void) {
-    
+    button1_busy = false;
+    done_counting = true;
+//    status_led = 0;
 }
 
 /**
@@ -36,7 +51,8 @@
 */
 void button1_enabled_cb(void)
 {
-    
+    button1_enabled = true;
+//    printf("button_enabled");
 }
 
 /**
@@ -54,5 +70,19 @@
 */
 void button1_onpressed_cb(void)
 {
-    
+//    printf("button_pressed");
+    if(button1_enabled){
+        debounced_flag = true;
+        
+//        status_led = 1;
+        button1_enabled = false;
+        button_enabled_to.attach(callback(&button1_enabled_cb), 0.1);
+        multiclick_state += 1;
+        if (multiclick_state == 1){
+            // clicked first time
+            multiclick_reset_to.attach(callback(&button1_multiclick_reset_cb), 1.0); // call this 1 sec after the first click 
+            button1_busy = true;
+        }
+//        printf("button_pressed_debounced");
+    }
 }
\ No newline at end of file
diff -r 5b7d055dbc91 -r e234aaf2a634 debounce_button.h
--- a/debounce_button.h	Tue Oct 31 09:01:56 2017 +0000
+++ b/debounce_button.h	Mon Oct 15 15:53:31 2018 +0000
@@ -16,4 +16,9 @@
 
 void button1_multiclick_reset_cb(void); // Resets the amount of clicks, but stores this value for the usage in the main loop
 void button1_enabled_cb(void);          // Enables the button again after a timeout, used for debouncing the button 
-void button1_onpressed_cb(void);        // Callback which is called when the user presses the button
\ No newline at end of file
+void button1_onpressed_cb(void);        // Callback which is called when the user presses the button
+
+// debugging
+extern volatile bool debounced_flag;   // Used in the main loop
+extern volatile bool done_counting;   // Used in the main loop
+
diff -r 5b7d055dbc91 -r e234aaf2a634 main.cpp
--- a/main.cpp	Tue Oct 31 09:01:56 2017 +0000
+++ b/main.cpp	Mon Oct 15 15:53:31 2018 +0000
@@ -43,6 +43,31 @@
 
 int main(int argc, char* argv[])
 {
-
+    InterruptIn button(USER_BUTTON);
+    button.fall(callback(&button1_onpressed_cb));
+    button1_busy = false;
+    button1_enabled = true;
+    button1_pressed = false;
+    multiclick_state = 0;
+//    status_led = 0;   
+    
+    while(1){
+            if(debounced_flag){
+//                printf("debounced \r\n");
+//                printf("clicks %d\r\n", multiclick_state);
+//                printf("button_enabled %d\r\n", button1_enabled);
+//                printf("button_busy %d\r\n", button1_busy);
+                debounced_flag = false;
+                
+            }
+//                printf("button_enabled %d\r\n", button1_enabled);
+            if(done_counting){
+                printf("counted %d \r\n", multiclick_state);
+                // handle click amount here
+                multiclick_state = 0;
+                done_counting = false;         
+            }
+    }
+    
     return 0;
 }