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