Vadim Gouskov / Mbed OS PGO6_VoteController_template

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers debounce_button.cpp Source File

debounce_button.cpp

00001 #include "debounce_button.h"
00002 
00003 volatile bool button1_pressed;   // Used in the main loop
00004 volatile bool button1_enabled;   // Used for debouncing
00005 volatile int multiclick_state;   // Counts how many clicks occured in the time slot, used in main loop
00006 volatile bool button1_busy;      // Informs the mainloop that the user is clicking the button
00007 DigitalOut status_led(LED1);
00008 
00009 //debugging
00010 volatile bool debounced_flag;   // Used in the main loop
00011 volatile bool done_counting;   // Used in the main loop
00012 
00013 Timeout button_enabled_to;
00014 Timeout multiclick_reset_to;
00015 
00016 /**
00017     Some tips and tricks:
00018     -   To use the built-in LED:
00019             DigitalOut led1(LED1);
00020             ...
00021             led1 = 1;
00022     -   To delay the call of a function:
00023             Timeout someTimeout;
00024             ...
00025             someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds
00026     -   The variables that are used in interrupt callbacks have to be volatile, 
00027         because these variables can change at any time. Therefore, the compiler is not 
00028         going to make optimisations.
00029 */
00030 
00031 /**
00032     TODO
00033     ----
00034     This function:
00035         -   stores the amount of clicks in a variable which is read by the main loop.
00036         -   resets the click counter which is used inside this file.
00037         -   lowers a flag which tells the main loop that the user stopped pressing the button
00038             such that it can proceed its program.
00039         -   turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
00040 */
00041 void button1_multiclick_reset_cb(void) {
00042     button1_busy = false;
00043     done_counting = true;
00044 //    status_led = 0;
00045 }
00046 
00047 /**
00048     TODO
00049     ----
00050     This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
00051 */
00052 void button1_enabled_cb(void)
00053 {
00054     button1_enabled = true;
00055 //    printf("button_enabled");
00056 }
00057 
00058 /**
00059     TODO
00060     ----
00061     This function:
00062         -   turns the built-in LED on, so the user gets informed that the program has started with counting clicks
00063         -   disables the button such that the debouncer is active
00064         -   enables the button again after a certain amount of time 
00065             (use interrupts with "button1_enabled_cb()" as callback.
00066         -   counts the amount of clicks within a period of 1 second
00067         -   informs the main loop that the button has been pressed
00068         -   informs the main loop that the user is clicking the button.
00069             Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
00070 */
00071 void button1_onpressed_cb(void)
00072 {
00073 //    printf("button_pressed");
00074     if(button1_enabled){
00075         debounced_flag = true;
00076         
00077 //        status_led = 1;
00078         button1_enabled = false;
00079         button_enabled_to.attach(callback(&button1_enabled_cb), 0.1);
00080         multiclick_state += 1;
00081         if (multiclick_state == 1){
00082             // clicked first time
00083             multiclick_reset_to.attach(callback(&button1_multiclick_reset_cb), 1.0); // call this 1 sec after the first click 
00084             button1_busy = true;
00085         }
00086 //        printf("button_pressed_debounced");
00087     }
00088 }