PGO6_VoteController (Astrid Vanneste)
Fork of PGO6_VoteController_template by
debounce_button.cpp@5:ba94770ce1c7, 2018-10-08 (annotated)
- Committer:
- AstridVanneste
- Date:
- Mon Oct 08 15:39:22 2018 +0000
- Revision:
- 5:ba94770ce1c7
- Parent:
- 4:08da93eb6014
mqtt
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 | |
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 | |
AstridVanneste | 5:ba94770ce1c7 | 9 | Timeout debounce_time; |
AstridVanneste | 5:ba94770ce1c7 | 10 | Timeout multiclick_time; |
AstridVanneste | 5:ba94770ce1c7 | 11 | |
jensdehoog | 0:fd29cd85e75e | 12 | /** |
jensdehoog | 0:fd29cd85e75e | 13 | Some tips and tricks: |
jensdehoog | 0:fd29cd85e75e | 14 | - To use the built-in LED: |
jensdehoog | 0:fd29cd85e75e | 15 | DigitalOut led1(LED1); |
jensdehoog | 0:fd29cd85e75e | 16 | ... |
jensdehoog | 0:fd29cd85e75e | 17 | led1 = 1; |
jensdehoog | 0:fd29cd85e75e | 18 | - To delay the call of a function: |
jensdehoog | 0:fd29cd85e75e | 19 | Timeout someTimeout; |
jensdehoog | 0:fd29cd85e75e | 20 | ... |
jensdehoog | 2:5b7d055dbc91 | 21 | someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds |
jensdehoog | 0:fd29cd85e75e | 22 | - The variables that are used in interrupt callbacks have to be volatile, |
jensdehoog | 0:fd29cd85e75e | 23 | because these variables can change at any time. Therefore, the compiler is not |
jensdehoog | 0:fd29cd85e75e | 24 | going to make optimisations. |
jensdehoog | 0:fd29cd85e75e | 25 | */ |
jensdehoog | 0:fd29cd85e75e | 26 | |
jensdehoog | 0:fd29cd85e75e | 27 | /** |
jensdehoog | 2:5b7d055dbc91 | 28 | TODO |
jensdehoog | 2:5b7d055dbc91 | 29 | ---- |
jensdehoog | 2:5b7d055dbc91 | 30 | This function: |
jensdehoog | 2:5b7d055dbc91 | 31 | - stores the amount of clicks in a variable which is read by the main loop. |
jensdehoog | 2:5b7d055dbc91 | 32 | - resets the click counter which is used inside this file. |
jensdehoog | 2:5b7d055dbc91 | 33 | - lowers a flag which tells the main loop that the user stopped pressing the button |
jensdehoog | 2:5b7d055dbc91 | 34 | such that it can proceed its program. |
jensdehoog | 2:5b7d055dbc91 | 35 | - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. |
jensdehoog | 2:5b7d055dbc91 | 36 | */ |
AstridVanneste | 3:376ac6744373 | 37 | void button1_multiclick_reset_cb(void) |
AstridVanneste | 3:376ac6744373 | 38 | { |
AstridVanneste | 3:376ac6744373 | 39 | button1_busy = false; |
AstridVanneste | 3:376ac6744373 | 40 | multiclick_state = internal_click_count; |
AstridVanneste | 5:ba94770ce1c7 | 41 | //printf("Clicks: %d\n", multiclick_state); |
AstridVanneste | 3:376ac6744373 | 42 | internal_click_count = 0; |
AstridVanneste | 4:08da93eb6014 | 43 | |
AstridVanneste | 4:08da93eb6014 | 44 | DigitalOut led1(LED1); |
AstridVanneste | 4:08da93eb6014 | 45 | led1 = 0; |
AstridVanneste | 5:ba94770ce1c7 | 46 | button1_pressed = true; |
jensdehoog | 2:5b7d055dbc91 | 47 | } |
jensdehoog | 2:5b7d055dbc91 | 48 | |
jensdehoog | 2:5b7d055dbc91 | 49 | /** |
jensdehoog | 2:5b7d055dbc91 | 50 | TODO |
jensdehoog | 2:5b7d055dbc91 | 51 | ---- |
jensdehoog | 2:5b7d055dbc91 | 52 | This function enables the button again, such that unwanted clicks of the bouncing button get ignored. |
jensdehoog | 0:fd29cd85e75e | 53 | */ |
jensdehoog | 2:5b7d055dbc91 | 54 | void button1_enabled_cb(void) |
jensdehoog | 2:5b7d055dbc91 | 55 | { |
AstridVanneste | 4:08da93eb6014 | 56 | button1_enabled = true; |
jensdehoog | 2:5b7d055dbc91 | 57 | } |
jensdehoog | 0:fd29cd85e75e | 58 | |
jensdehoog | 2:5b7d055dbc91 | 59 | /** |
jensdehoog | 2:5b7d055dbc91 | 60 | TODO |
jensdehoog | 2:5b7d055dbc91 | 61 | ---- |
jensdehoog | 2:5b7d055dbc91 | 62 | This function: |
jensdehoog | 2:5b7d055dbc91 | 63 | - turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jensdehoog | 2:5b7d055dbc91 | 64 | - disables the button such that the debouncer is active |
jensdehoog | 2:5b7d055dbc91 | 65 | - enables the button again after a certain amount of time |
jensdehoog | 2:5b7d055dbc91 | 66 | (use interrupts with "button1_enabled_cb()" as callback. |
jensdehoog | 2:5b7d055dbc91 | 67 | - counts the amount of clicks within a period of 1 second |
jensdehoog | 2:5b7d055dbc91 | 68 | - informs the main loop that the button has been pressed |
jensdehoog | 2:5b7d055dbc91 | 69 | - informs the main loop that the user is clicking the button. |
jensdehoog | 2:5b7d055dbc91 | 70 | Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
jensdehoog | 2:5b7d055dbc91 | 71 | */ |
jensdehoog | 0:fd29cd85e75e | 72 | void button1_onpressed_cb(void) |
AstridVanneste | 5:ba94770ce1c7 | 73 | { |
AstridVanneste | 3:376ac6744373 | 74 | if(button1_enabled) |
AstridVanneste | 3:376ac6744373 | 75 | { |
AstridVanneste | 5:ba94770ce1c7 | 76 | DigitalOut led1(LED1); |
AstridVanneste | 5:ba94770ce1c7 | 77 | led1 = 1; |
AstridVanneste | 3:376ac6744373 | 78 | button1_enabled = false; // button is not enabled => debouncing |
AstridVanneste | 3:376ac6744373 | 79 | |
AstridVanneste | 5:ba94770ce1c7 | 80 | debounce_time.attach(callback(&button1_enabled_cb), 0.1); |
AstridVanneste | 3:376ac6744373 | 81 | |
AstridVanneste | 3:376ac6744373 | 82 | internal_click_count++; |
AstridVanneste | 3:376ac6744373 | 83 | |
AstridVanneste | 3:376ac6744373 | 84 | if(!button1_busy) |
AstridVanneste | 3:376ac6744373 | 85 | { |
AstridVanneste | 3:376ac6744373 | 86 | button1_busy = true; |
AstridVanneste | 3:376ac6744373 | 87 | |
AstridVanneste | 5:ba94770ce1c7 | 88 | multiclick_time.attach(callback(&button1_multiclick_reset_cb), 1); |
AstridVanneste | 3:376ac6744373 | 89 | } |
AstridVanneste | 3:376ac6744373 | 90 | } |
AstridVanneste | 3:376ac6744373 | 91 | } |
AstridVanneste | 3:376ac6744373 | 92 | |
AstridVanneste | 3:376ac6744373 | 93 | void init_debouncer() |
AstridVanneste | 3:376ac6744373 | 94 | { |
AstridVanneste | 3:376ac6744373 | 95 | button1_pressed = false; |
AstridVanneste | 3:376ac6744373 | 96 | button1_enabled = true; |
AstridVanneste | 3:376ac6744373 | 97 | button1_busy = false; |
AstridVanneste | 3:376ac6744373 | 98 | multiclick_state = 0; |
AstridVanneste | 3:376ac6744373 | 99 | internal_click_count = 0; |
jensdehoog | 0:fd29cd85e75e | 100 | } |