![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Vadim Gouskov 15-10-18 Vote Controller Version 1
Fork of PGO6_VoteController_template by
debounce_button.cpp@3:e234aaf2a634, 2018-10-15 (annotated)
- Committer:
- VadimGouskov
- Date:
- Mon Oct 15 15:53:31 2018 +0000
- Revision:
- 3:e234aaf2a634
- Parent:
- 2:5b7d055dbc91
15-10-17 VoteController Version 1
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 | |
VadimGouskov | 3:e234aaf2a634 | 3 | volatile bool button1_pressed; // Used in the main loop |
VadimGouskov | 3:e234aaf2a634 | 4 | volatile bool button1_enabled; // Used for debouncing |
VadimGouskov | 3:e234aaf2a634 | 5 | volatile int multiclick_state; // Counts how many clicks occured in the time slot, used in main loop |
VadimGouskov | 3:e234aaf2a634 | 6 | volatile bool button1_busy; // Informs the mainloop that the user is clicking the button |
VadimGouskov | 3:e234aaf2a634 | 7 | DigitalOut status_led(LED1); |
VadimGouskov | 3:e234aaf2a634 | 8 | |
VadimGouskov | 3:e234aaf2a634 | 9 | //debugging |
VadimGouskov | 3:e234aaf2a634 | 10 | volatile bool debounced_flag; // Used in the main loop |
VadimGouskov | 3:e234aaf2a634 | 11 | volatile bool done_counting; // Used in the main loop |
VadimGouskov | 3:e234aaf2a634 | 12 | |
VadimGouskov | 3:e234aaf2a634 | 13 | Timeout button_enabled_to; |
VadimGouskov | 3:e234aaf2a634 | 14 | Timeout multiclick_reset_to; |
VadimGouskov | 3:e234aaf2a634 | 15 | |
jensdehoog | 0:fd29cd85e75e | 16 | /** |
jensdehoog | 0:fd29cd85e75e | 17 | Some tips and tricks: |
jensdehoog | 0:fd29cd85e75e | 18 | - To use the built-in LED: |
jensdehoog | 0:fd29cd85e75e | 19 | DigitalOut led1(LED1); |
jensdehoog | 0:fd29cd85e75e | 20 | ... |
jensdehoog | 0:fd29cd85e75e | 21 | led1 = 1; |
jensdehoog | 0:fd29cd85e75e | 22 | - To delay the call of a function: |
jensdehoog | 0:fd29cd85e75e | 23 | Timeout someTimeout; |
jensdehoog | 0:fd29cd85e75e | 24 | ... |
jensdehoog | 2:5b7d055dbc91 | 25 | someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds |
jensdehoog | 0:fd29cd85e75e | 26 | - The variables that are used in interrupt callbacks have to be volatile, |
jensdehoog | 0:fd29cd85e75e | 27 | because these variables can change at any time. Therefore, the compiler is not |
jensdehoog | 0:fd29cd85e75e | 28 | going to make optimisations. |
jensdehoog | 0:fd29cd85e75e | 29 | */ |
jensdehoog | 0:fd29cd85e75e | 30 | |
jensdehoog | 0:fd29cd85e75e | 31 | /** |
jensdehoog | 2:5b7d055dbc91 | 32 | TODO |
jensdehoog | 2:5b7d055dbc91 | 33 | ---- |
jensdehoog | 2:5b7d055dbc91 | 34 | This function: |
jensdehoog | 2:5b7d055dbc91 | 35 | - stores the amount of clicks in a variable which is read by the main loop. |
jensdehoog | 2:5b7d055dbc91 | 36 | - resets the click counter which is used inside this file. |
jensdehoog | 2:5b7d055dbc91 | 37 | - lowers a flag which tells the main loop that the user stopped pressing the button |
jensdehoog | 2:5b7d055dbc91 | 38 | such that it can proceed its program. |
jensdehoog | 2:5b7d055dbc91 | 39 | - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. |
jensdehoog | 2:5b7d055dbc91 | 40 | */ |
jensdehoog | 2:5b7d055dbc91 | 41 | void button1_multiclick_reset_cb(void) { |
VadimGouskov | 3:e234aaf2a634 | 42 | button1_busy = false; |
VadimGouskov | 3:e234aaf2a634 | 43 | done_counting = true; |
VadimGouskov | 3:e234aaf2a634 | 44 | // status_led = 0; |
jensdehoog | 2:5b7d055dbc91 | 45 | } |
jensdehoog | 2:5b7d055dbc91 | 46 | |
jensdehoog | 2:5b7d055dbc91 | 47 | /** |
jensdehoog | 2:5b7d055dbc91 | 48 | TODO |
jensdehoog | 2:5b7d055dbc91 | 49 | ---- |
jensdehoog | 2:5b7d055dbc91 | 50 | This function enables the button again, such that unwanted clicks of the bouncing button get ignored. |
jensdehoog | 0:fd29cd85e75e | 51 | */ |
jensdehoog | 2:5b7d055dbc91 | 52 | void button1_enabled_cb(void) |
jensdehoog | 2:5b7d055dbc91 | 53 | { |
VadimGouskov | 3:e234aaf2a634 | 54 | button1_enabled = true; |
VadimGouskov | 3:e234aaf2a634 | 55 | // printf("button_enabled"); |
jensdehoog | 2:5b7d055dbc91 | 56 | } |
jensdehoog | 0:fd29cd85e75e | 57 | |
jensdehoog | 2:5b7d055dbc91 | 58 | /** |
jensdehoog | 2:5b7d055dbc91 | 59 | TODO |
jensdehoog | 2:5b7d055dbc91 | 60 | ---- |
jensdehoog | 2:5b7d055dbc91 | 61 | This function: |
jensdehoog | 2:5b7d055dbc91 | 62 | - turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jensdehoog | 2:5b7d055dbc91 | 63 | - disables the button such that the debouncer is active |
jensdehoog | 2:5b7d055dbc91 | 64 | - enables the button again after a certain amount of time |
jensdehoog | 2:5b7d055dbc91 | 65 | (use interrupts with "button1_enabled_cb()" as callback. |
jensdehoog | 2:5b7d055dbc91 | 66 | - counts the amount of clicks within a period of 1 second |
jensdehoog | 2:5b7d055dbc91 | 67 | - informs the main loop that the button has been pressed |
jensdehoog | 2:5b7d055dbc91 | 68 | - informs the main loop that the user is clicking the button. |
jensdehoog | 2:5b7d055dbc91 | 69 | Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
jensdehoog | 2:5b7d055dbc91 | 70 | */ |
jensdehoog | 0:fd29cd85e75e | 71 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 72 | { |
VadimGouskov | 3:e234aaf2a634 | 73 | // printf("button_pressed"); |
VadimGouskov | 3:e234aaf2a634 | 74 | if(button1_enabled){ |
VadimGouskov | 3:e234aaf2a634 | 75 | debounced_flag = true; |
VadimGouskov | 3:e234aaf2a634 | 76 | |
VadimGouskov | 3:e234aaf2a634 | 77 | // status_led = 1; |
VadimGouskov | 3:e234aaf2a634 | 78 | button1_enabled = false; |
VadimGouskov | 3:e234aaf2a634 | 79 | button_enabled_to.attach(callback(&button1_enabled_cb), 0.1); |
VadimGouskov | 3:e234aaf2a634 | 80 | multiclick_state += 1; |
VadimGouskov | 3:e234aaf2a634 | 81 | if (multiclick_state == 1){ |
VadimGouskov | 3:e234aaf2a634 | 82 | // clicked first time |
VadimGouskov | 3:e234aaf2a634 | 83 | multiclick_reset_to.attach(callback(&button1_multiclick_reset_cb), 1.0); // call this 1 sec after the first click |
VadimGouskov | 3:e234aaf2a634 | 84 | button1_busy = true; |
VadimGouskov | 3:e234aaf2a634 | 85 | } |
VadimGouskov | 3:e234aaf2a634 | 86 | // printf("button_pressed_debounced"); |
VadimGouskov | 3:e234aaf2a634 | 87 | } |
jensdehoog | 0:fd29cd85e75e | 88 | } |