Aleksander Chernyavskiy
/
pgo6_aleksander_chernyavskiy
opdracht mqtt nucleo
Fork of PGO6_VoteController_template by
debounce_button.cpp@3:abfa79c5b7bd, 2018-10-11 (annotated)
- Committer:
- alex69
- Date:
- Thu Oct 11 15:52:51 2018 +0000
- Revision:
- 3:abfa79c5b7bd
- Parent:
- 2:5b7d055dbc91
Initial commit
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 | |
jensdehoog | 0:fd29cd85e75e | 3 | /** |
jensdehoog | 0:fd29cd85e75e | 4 | Some tips and tricks: |
jensdehoog | 0:fd29cd85e75e | 5 | - To use the built-in LED: |
jensdehoog | 0:fd29cd85e75e | 6 | DigitalOut led1(LED1); |
jensdehoog | 0:fd29cd85e75e | 7 | ... |
jensdehoog | 0:fd29cd85e75e | 8 | led1 = 1; |
jensdehoog | 0:fd29cd85e75e | 9 | - To delay the call of a function: |
jensdehoog | 0:fd29cd85e75e | 10 | Timeout someTimeout; |
jensdehoog | 0:fd29cd85e75e | 11 | ... |
jensdehoog | 2:5b7d055dbc91 | 12 | someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds |
jensdehoog | 0:fd29cd85e75e | 13 | - The variables that are used in interrupt callbacks have to be volatile, |
jensdehoog | 0:fd29cd85e75e | 14 | because these variables can change at any time. Therefore, the compiler is not |
jensdehoog | 0:fd29cd85e75e | 15 | going to make optimisations. |
jensdehoog | 0:fd29cd85e75e | 16 | */ |
jensdehoog | 0:fd29cd85e75e | 17 | |
alex69 | 3:abfa79c5b7bd | 18 | |
alex69 | 3:abfa79c5b7bd | 19 | // init the extern variables |
alex69 | 3:abfa79c5b7bd | 20 | volatile int multiclick_state = 0; |
alex69 | 3:abfa79c5b7bd | 21 | volatile int tempCount = 0; |
alex69 | 3:abfa79c5b7bd | 22 | volatile bool button1_busy = false; |
alex69 | 3:abfa79c5b7bd | 23 | volatile bool button1_pressed = false; |
alex69 | 3:abfa79c5b7bd | 24 | volatile bool button1_enabled = true; |
alex69 | 3:abfa79c5b7bd | 25 | DigitalOut myled(LED1); |
alex69 | 3:abfa79c5b7bd | 26 | Timeout enableButtonTimer; |
alex69 | 3:abfa79c5b7bd | 27 | Timeout resetButtonTimer; |
alex69 | 3:abfa79c5b7bd | 28 | |
jensdehoog | 2:5b7d055dbc91 | 29 | void button1_multiclick_reset_cb(void) { |
alex69 | 3:abfa79c5b7bd | 30 | //stores the amount of clicks in a variable which is read by the main loop. |
alex69 | 3:abfa79c5b7bd | 31 | multiclick_state = tempCount; |
alex69 | 3:abfa79c5b7bd | 32 | //resets the click counter which is used inside this file. |
alex69 | 3:abfa79c5b7bd | 33 | tempCount = 0; |
alex69 | 3:abfa79c5b7bd | 34 | //lowers a flag which tells the main loop that the user stopped pressing the button such that it can proceed its program. |
alex69 | 3:abfa79c5b7bd | 35 | button1_busy = false; |
alex69 | 3:abfa79c5b7bd | 36 | //turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. |
alex69 | 3:abfa79c5b7bd | 37 | myled = 0; |
jensdehoog | 2:5b7d055dbc91 | 38 | |
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 | { |
alex69 | 3:abfa79c5b7bd | 48 | button1_enabled = true; |
jensdehoog | 2:5b7d055dbc91 | 49 | } |
jensdehoog | 0:fd29cd85e75e | 50 | |
jensdehoog | 0:fd29cd85e75e | 51 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 52 | { |
alex69 | 3:abfa79c5b7bd | 53 | if(button1_enabled) { |
alex69 | 3:abfa79c5b7bd | 54 | //disables the button such that the debouncer is active |
alex69 | 3:abfa79c5b7bd | 55 | button1_enabled = false; |
alex69 | 3:abfa79c5b7bd | 56 | //enables the button again after a certain amount of time (use interrupts with "button1_enabled_cb()" as callback.) |
alex69 | 3:abfa79c5b7bd | 57 | enableButtonTimer.attach(callback(&button1_enabled_cb), 0.1); |
alex69 | 3:abfa79c5b7bd | 58 | //informs the main loop that the button has been pressed |
alex69 | 3:abfa79c5b7bd | 59 | button1_pressed = true; |
alex69 | 3:abfa79c5b7bd | 60 | //informs the main loop that the user is clicking the button. |
alex69 | 3:abfa79c5b7bd | 61 | //Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
alex69 | 3:abfa79c5b7bd | 62 | if(!button1_busy) { |
alex69 | 3:abfa79c5b7bd | 63 | //turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
alex69 | 3:abfa79c5b7bd | 64 | myled = 1; |
alex69 | 3:abfa79c5b7bd | 65 | button1_busy = true; |
alex69 | 3:abfa79c5b7bd | 66 | resetButtonTimer.attach(callback(&button1_multiclick_reset_cb), 1); |
alex69 | 3:abfa79c5b7bd | 67 | } |
alex69 | 3:abfa79c5b7bd | 68 | //counts the amount of clicks within a period of 1 second |
alex69 | 3:abfa79c5b7bd | 69 | tempCount++; |
alex69 | 3:abfa79c5b7bd | 70 | |
alex69 | 3:abfa79c5b7bd | 71 | } |
jensdehoog | 0:fd29cd85e75e | 72 | } |