Niels Balemans
/
PGO6_VoteController_Niels_Balemans_
vote controller
Fork of PGO6_VoteController_template by
debounce_button.cpp@4:836cd4df41ba, 2018-10-11 (annotated)
- Committer:
- niels_balemans
- Date:
- Thu Oct 11 16:08:43 2018 +0000
- Revision:
- 4:836cd4df41ba
- Parent:
- 3:062b94b59b1b
vote controller
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 | |
niels_balemans | 3:062b94b59b1b | 18 | volatile bool button1_pressed = false; |
niels_balemans | 3:062b94b59b1b | 19 | volatile bool button1_enabled = true; |
niels_balemans | 3:062b94b59b1b | 20 | volatile bool button1_busy = false; |
niels_balemans | 3:062b94b59b1b | 21 | volatile int multiclick_state = 0; |
niels_balemans | 3:062b94b59b1b | 22 | int counter = 0; |
niels_balemans | 3:062b94b59b1b | 23 | DigitalOut myled(LED1); |
niels_balemans | 3:062b94b59b1b | 24 | Timeout debouncer_delay; |
niels_balemans | 3:062b94b59b1b | 25 | Timeout busy_delay; |
niels_balemans | 3:062b94b59b1b | 26 | DigitalOut led2(LED2); |
niels_balemans | 3:062b94b59b1b | 27 | |
niels_balemans | 3:062b94b59b1b | 28 | void showPresses() { |
niels_balemans | 3:062b94b59b1b | 29 | for(int i = 0; i < multiclick_state; i++) { |
niels_balemans | 3:062b94b59b1b | 30 | led2 = 1; |
niels_balemans | 3:062b94b59b1b | 31 | wait(1); |
niels_balemans | 3:062b94b59b1b | 32 | led2 = 0; |
niels_balemans | 3:062b94b59b1b | 33 | wait(1); |
niels_balemans | 3:062b94b59b1b | 34 | } |
niels_balemans | 3:062b94b59b1b | 35 | } |
niels_balemans | 3:062b94b59b1b | 36 | |
jensdehoog | 0:fd29cd85e75e | 37 | /** |
jensdehoog | 2:5b7d055dbc91 | 38 | TODO |
jensdehoog | 2:5b7d055dbc91 | 39 | ---- |
jensdehoog | 2:5b7d055dbc91 | 40 | This function: |
jensdehoog | 2:5b7d055dbc91 | 41 | - stores the amount of clicks in a variable which is read by the main loop. |
jensdehoog | 2:5b7d055dbc91 | 42 | - resets the click counter which is used inside this file. |
jensdehoog | 2:5b7d055dbc91 | 43 | - lowers a flag which tells the main loop that the user stopped pressing the button |
jensdehoog | 2:5b7d055dbc91 | 44 | such that it can proceed its program. |
jensdehoog | 2:5b7d055dbc91 | 45 | - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. |
jensdehoog | 2:5b7d055dbc91 | 46 | */ |
jensdehoog | 2:5b7d055dbc91 | 47 | void button1_multiclick_reset_cb(void) { |
niels_balemans | 3:062b94b59b1b | 48 | multiclick_state = counter; |
niels_balemans | 3:062b94b59b1b | 49 | counter = 0; |
niels_balemans | 3:062b94b59b1b | 50 | button1_busy = false; |
niels_balemans | 3:062b94b59b1b | 51 | myled = 0; |
niels_balemans | 3:062b94b59b1b | 52 | //showPresses(); |
jensdehoog | 2:5b7d055dbc91 | 53 | } |
jensdehoog | 2:5b7d055dbc91 | 54 | |
jensdehoog | 2:5b7d055dbc91 | 55 | /** |
jensdehoog | 2:5b7d055dbc91 | 56 | TODO |
jensdehoog | 2:5b7d055dbc91 | 57 | ---- |
jensdehoog | 2:5b7d055dbc91 | 58 | This function enables the button again, such that unwanted clicks of the bouncing button get ignored. |
jensdehoog | 0:fd29cd85e75e | 59 | */ |
niels_balemans | 3:062b94b59b1b | 60 | void button1_enabled_cb(void) { |
niels_balemans | 3:062b94b59b1b | 61 | button1_enabled = true; |
jensdehoog | 2:5b7d055dbc91 | 62 | } |
jensdehoog | 0:fd29cd85e75e | 63 | |
jensdehoog | 2:5b7d055dbc91 | 64 | /** |
jensdehoog | 2:5b7d055dbc91 | 65 | TODO |
jensdehoog | 2:5b7d055dbc91 | 66 | ---- |
jensdehoog | 2:5b7d055dbc91 | 67 | This function: |
jensdehoog | 2:5b7d055dbc91 | 68 | - turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jensdehoog | 2:5b7d055dbc91 | 69 | - disables the button such that the debouncer is active |
jensdehoog | 2:5b7d055dbc91 | 70 | - enables the button again after a certain amount of time |
jensdehoog | 2:5b7d055dbc91 | 71 | (use interrupts with "button1_enabled_cb()" as callback. |
jensdehoog | 2:5b7d055dbc91 | 72 | - counts the amount of clicks within a period of 1 second |
jensdehoog | 2:5b7d055dbc91 | 73 | - informs the main loop that the button has been pressed |
jensdehoog | 2:5b7d055dbc91 | 74 | - informs the main loop that the user is clicking the button. |
jensdehoog | 2:5b7d055dbc91 | 75 | Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
jensdehoog | 2:5b7d055dbc91 | 76 | */ |
jensdehoog | 0:fd29cd85e75e | 77 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 78 | { |
niels_balemans | 3:062b94b59b1b | 79 | if (button1_enabled) { // Disabled while the button is bouncing |
niels_balemans | 3:062b94b59b1b | 80 | button1_enabled = false; |
niels_balemans | 3:062b94b59b1b | 81 | button1_pressed = true; // To be read by the main loop |
niels_balemans | 3:062b94b59b1b | 82 | debouncer_delay.attach(callback(button1_enabled_cb), 0.05); // Debounce time 50 ms |
niels_balemans | 3:062b94b59b1b | 83 | counter++; |
niels_balemans | 3:062b94b59b1b | 84 | if(!button1_busy) { |
niels_balemans | 3:062b94b59b1b | 85 | myled = 1; |
niels_balemans | 3:062b94b59b1b | 86 | button1_busy = true; |
niels_balemans | 3:062b94b59b1b | 87 | busy_delay.attach(callback(button1_multiclick_reset_cb), 1); |
niels_balemans | 3:062b94b59b1b | 88 | } |
niels_balemans | 3:062b94b59b1b | 89 | } |
jensdehoog | 0:fd29cd85e75e | 90 | } |