PGO6_VoteController (Astrid Vanneste)

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
AstridVanneste
Date:
Mon Oct 08 12:56:29 2018 +0000
Revision:
3:376ac6744373
Parent:
2:5b7d055dbc91
Child:
4:08da93eb6014
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jensdehoog 0:fd29cd85e75e 1 #include "debounce_button.h"
jensdehoog 0:fd29cd85e75e 2
AstridVanneste 3:376ac6744373 3 volatile bool button1_pressed; // Used in the main loop
AstridVanneste 3:376ac6744373 4 volatile bool button1_enabled; // Used for debouncing
AstridVanneste 3:376ac6744373 5 volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop
AstridVanneste 3:376ac6744373 6 volatile bool button1_busy; // Informs the mainloop that the user is clicking the button => busy multiclick
AstridVanneste 3:376ac6744373 7 volatile int internal_click_count; // Counts how many clicks occured in the time slot
AstridVanneste 3:376ac6744373 8
jensdehoog 0:fd29cd85e75e 9 /**
jensdehoog 0:fd29cd85e75e 10 Some tips and tricks:
jensdehoog 0:fd29cd85e75e 11 - To use the built-in LED:
jensdehoog 0:fd29cd85e75e 12 DigitalOut led1(LED1);
jensdehoog 0:fd29cd85e75e 13 ...
jensdehoog 0:fd29cd85e75e 14 led1 = 1;
jensdehoog 0:fd29cd85e75e 15 - To delay the call of a function:
jensdehoog 0:fd29cd85e75e 16 Timeout someTimeout;
jensdehoog 0:fd29cd85e75e 17 ...
jensdehoog 2:5b7d055dbc91 18 someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds
jensdehoog 0:fd29cd85e75e 19 - The variables that are used in interrupt callbacks have to be volatile,
jensdehoog 0:fd29cd85e75e 20 because these variables can change at any time. Therefore, the compiler is not
jensdehoog 0:fd29cd85e75e 21 going to make optimisations.
jensdehoog 0:fd29cd85e75e 22 */
jensdehoog 0:fd29cd85e75e 23
jensdehoog 0:fd29cd85e75e 24 /**
jensdehoog 2:5b7d055dbc91 25 TODO
jensdehoog 2:5b7d055dbc91 26 ----
jensdehoog 2:5b7d055dbc91 27 This function:
jensdehoog 2:5b7d055dbc91 28 - stores the amount of clicks in a variable which is read by the main loop.
jensdehoog 2:5b7d055dbc91 29 - resets the click counter which is used inside this file.
jensdehoog 2:5b7d055dbc91 30 - lowers a flag which tells the main loop that the user stopped pressing the button
jensdehoog 2:5b7d055dbc91 31 such that it can proceed its program.
jensdehoog 2:5b7d055dbc91 32 - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks.
jensdehoog 2:5b7d055dbc91 33 */
AstridVanneste 3:376ac6744373 34 void button1_multiclick_reset_cb(void)
AstridVanneste 3:376ac6744373 35 {
AstridVanneste 3:376ac6744373 36 button1_busy = false;
AstridVanneste 3:376ac6744373 37 multiclick_state = internal_click_count;
AstridVanneste 3:376ac6744373 38 internal_click_count = 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 {
jensdehoog 2:5b7d055dbc91 48
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 {
AstridVanneste 3:376ac6744373 66 DigitalOut led1(LED1);
AstridVanneste 3:376ac6744373 67 led1 = 1;
AstridVanneste 3:376ac6744373 68
AstridVanneste 3:376ac6744373 69 if(button1_enabled)
AstridVanneste 3:376ac6744373 70 {
AstridVanneste 3:376ac6744373 71 button1_enabled = false; // button is not enabled => debouncing
AstridVanneste 3:376ac6744373 72
AstridVanneste 3:376ac6744373 73 Timeout debounce_time;
AstridVanneste 3:376ac6744373 74 debounce_time.attach(callback(button1_enabled_cb), 0.5);
AstridVanneste 3:376ac6744373 75
AstridVanneste 3:376ac6744373 76 internal_click_count++;
AstridVanneste 3:376ac6744373 77
AstridVanneste 3:376ac6744373 78 if(!button1_busy)
AstridVanneste 3:376ac6744373 79 {
AstridVanneste 3:376ac6744373 80 button1_pressed = true;
AstridVanneste 3:376ac6744373 81 button1_busy = true;
AstridVanneste 3:376ac6744373 82
AstridVanneste 3:376ac6744373 83 Timeout multiclick_time;
AstridVanneste 3:376ac6744373 84 multiclick_time.attach(callback(button1_multiclick_reset_cb), 1);
AstridVanneste 3:376ac6744373 85 }
AstridVanneste 3:376ac6744373 86 }
AstridVanneste 3:376ac6744373 87 }
AstridVanneste 3:376ac6744373 88
AstridVanneste 3:376ac6744373 89 void init_debouncer()
AstridVanneste 3:376ac6744373 90 {
AstridVanneste 3:376ac6744373 91 button1_pressed = false;
AstridVanneste 3:376ac6744373 92 button1_enabled = true;
AstridVanneste 3:376ac6744373 93 button1_busy = false;
AstridVanneste 3:376ac6744373 94 multiclick_state = 0;
AstridVanneste 3:376ac6744373 95 internal_click_count = 0;
jensdehoog 0:fd29cd85e75e 96
jensdehoog 0:fd29cd85e75e 97 }