Vincent Nys

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
vincentnys
Date:
Tue Oct 31 18:31:34 2017 +0000
Revision:
3:c53f27bdcfb5
Parent:
2:5b7d055dbc91
PGO6

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 2
vincentnys 3:c53f27bdcfb5 3 DigitalOut led1(LED1);
vincentnys 3:c53f27bdcfb5 4 volatile int click_counter = 0;
vincentnys 3:c53f27bdcfb5 5 Timeout multiclick;
vincentnys 3:c53f27bdcfb5 6 Timeout debounce;
vincentnys 3:c53f27bdcfb5 7
jensdehoog 0:fd29cd85e75e 8 /**
jensdehoog 0:fd29cd85e75e 9 Some tips and tricks:
jensdehoog 0:fd29cd85e75e 10 - To use the built-in LED:
jensdehoog 0:fd29cd85e75e 11 DigitalOut led1(LED1);
jensdehoog 0:fd29cd85e75e 12 ...
jensdehoog 0:fd29cd85e75e 13 led1 = 1;
jensdehoog 0:fd29cd85e75e 14 - To delay the call of a function:
jensdehoog 0:fd29cd85e75e 15 Timeout someTimeout;
jensdehoog 0:fd29cd85e75e 16 ...
jensdehoog 2:5b7d055dbc91 17 someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds
jensdehoog 0:fd29cd85e75e 18 - The variables that are used in interrupt callbacks have to be volatile,
jensdehoog 0:fd29cd85e75e 19 because these variables can change at any time. Therefore, the compiler is not
jensdehoog 0:fd29cd85e75e 20 going to make optimisations.
jensdehoog 0:fd29cd85e75e 21 */
jensdehoog 0:fd29cd85e75e 22
jensdehoog 0:fd29cd85e75e 23 /**
jensdehoog 2:5b7d055dbc91 24 TODO
jensdehoog 2:5b7d055dbc91 25 ----
jensdehoog 2:5b7d055dbc91 26 This function:
jensdehoog 2:5b7d055dbc91 27 - stores the amount of clicks in a variable which is read by the main loop.
jensdehoog 2:5b7d055dbc91 28 - resets the click counter which is used inside this file.
jensdehoog 2:5b7d055dbc91 29 - lowers a flag which tells the main loop that the user stopped pressing the button
jensdehoog 2:5b7d055dbc91 30 such that it can proceed its program.
jensdehoog 2:5b7d055dbc91 31 - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
jensdehoog 2:5b7d055dbc91 32 */
vincentnys 3:c53f27bdcfb5 33 void button1_multiclick_reset_cb(void)
vincentnys 3:c53f27bdcfb5 34 {
vincentnys 3:c53f27bdcfb5 35 button1_busy = false;
vincentnys 3:c53f27bdcfb5 36 multiclick_state = click_counter;
vincentnys 3:c53f27bdcfb5 37 click_counter = 0;
vincentnys 3:c53f27bdcfb5 38 led1 = 0;
jensdehoog 2:5b7d055dbc91 39 }
jensdehoog 2:5b7d055dbc91 40
jensdehoog 2:5b7d055dbc91 41 /**
jensdehoog 2:5b7d055dbc91 42 TODO
jensdehoog 2:5b7d055dbc91 43 ----
jensdehoog 2:5b7d055dbc91 44 This function enables the button again, such that unwanted clicks of the bouncing button get ignored.
jensdehoog 0:fd29cd85e75e 45 */
jensdehoog 2:5b7d055dbc91 46 void button1_enabled_cb(void)
jensdehoog 2:5b7d055dbc91 47 {
vincentnys 3:c53f27bdcfb5 48 button1_enabled = true;
jensdehoog 2:5b7d055dbc91 49 }
jensdehoog 0:fd29cd85e75e 50
jensdehoog 2:5b7d055dbc91 51 /**
jensdehoog 2:5b7d055dbc91 52 TODO
jensdehoog 2:5b7d055dbc91 53 ----
jensdehoog 2:5b7d055dbc91 54 This function:
jensdehoog 2:5b7d055dbc91 55 - turns the built-in LED on, so the user gets informed that the program has started with counting clicks
jensdehoog 2:5b7d055dbc91 56 - disables the button such that the debouncer is active
jensdehoog 2:5b7d055dbc91 57 - enables the button again after a certain amount of time
jensdehoog 2:5b7d055dbc91 58 (use interrupts with "button1_enabled_cb()" as callback.
jensdehoog 2:5b7d055dbc91 59 - counts the amount of clicks within a period of 1 second
jensdehoog 2:5b7d055dbc91 60 - informs the main loop that the button has been pressed
jensdehoog 2:5b7d055dbc91 61 - informs the main loop that the user is clicking the button.
jensdehoog 2:5b7d055dbc91 62 Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted.
jensdehoog 2:5b7d055dbc91 63 */
jensdehoog 0:fd29cd85e75e 64 void button1_onpressed_cb(void)
jensdehoog 0:fd29cd85e75e 65 {
vincentnys 3:c53f27bdcfb5 66 if(!button1_busy)
vincentnys 3:c53f27bdcfb5 67 {
vincentnys 3:c53f27bdcfb5 68 multiclick.attach( &button1_multiclick_reset_cb, 1 );
vincentnys 3:c53f27bdcfb5 69 }
vincentnys 3:c53f27bdcfb5 70 button1_busy = true;
vincentnys 3:c53f27bdcfb5 71 led1 = 1;
vincentnys 3:c53f27bdcfb5 72 if( button1_enabled )
vincentnys 3:c53f27bdcfb5 73 {
vincentnys 3:c53f27bdcfb5 74 button1_enabled = false;
vincentnys 3:c53f27bdcfb5 75 click_counter++;
vincentnys 3:c53f27bdcfb5 76 debounce.attach( &button1_enabled_cb, 0.08 ); // 80ms
vincentnys 3:c53f27bdcfb5 77 }
jensdehoog 0:fd29cd85e75e 78 }