jamie wubben
/
PGO6_VoteController_template
jamie
Fork of PGO6_VoteController_template by
debounce_button.cpp@3:c77bed3482d3, 2018-10-11 (annotated)
- Committer:
- jamie_wubben
- Date:
- Thu Oct 11 15:51:58 2018 +0000
- Revision:
- 3:c77bed3482d3
- Parent:
- 2:5b7d055dbc91
Jamie
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 | |
jamie_wubben | 3:c77bed3482d3 | 3 | DigitalIn button(USER_BUTTON); |
jamie_wubben | 3:c77bed3482d3 | 4 | DigitalOut led1(LED1); |
jamie_wubben | 3:c77bed3482d3 | 5 | DigitalOut led2(LED2); |
jamie_wubben | 3:c77bed3482d3 | 6 | volatile bool button1_enabled = true; |
jamie_wubben | 3:c77bed3482d3 | 7 | volatile bool button1_buzy = false; |
jamie_wubben | 3:c77bed3482d3 | 8 | Timeout someTimeout; |
jamie_wubben | 3:c77bed3482d3 | 9 | Timeout timer; |
jamie_wubben | 3:c77bed3482d3 | 10 | int counter = 0; |
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 | */ |
jamie_wubben | 3:c77bed3482d3 | 36 | |
jamie_wubben | 3:c77bed3482d3 | 37 | void replay(){ |
jamie_wubben | 3:c77bed3482d3 | 38 | //replay the multiclick |
jamie_wubben | 3:c77bed3482d3 | 39 | for(int i=0;i<counter;i++){ |
jamie_wubben | 3:c77bed3482d3 | 40 | led2 = 1; |
jamie_wubben | 3:c77bed3482d3 | 41 | wait(0.2); |
jamie_wubben | 3:c77bed3482d3 | 42 | led2 =0; |
jamie_wubben | 3:c77bed3482d3 | 43 | wait(0.2); |
jamie_wubben | 3:c77bed3482d3 | 44 | } |
jensdehoog | 2:5b7d055dbc91 | 45 | } |
jensdehoog | 2:5b7d055dbc91 | 46 | |
jamie_wubben | 3:c77bed3482d3 | 47 | // This function enables the button again, such that unwanted clicks of the bouncing button get ignored. |
jensdehoog | 2:5b7d055dbc91 | 48 | void button1_enabled_cb(void) |
jensdehoog | 2:5b7d055dbc91 | 49 | { |
jamie_wubben | 3:c77bed3482d3 | 50 | button1_enabled = true; |
jamie_wubben | 3:c77bed3482d3 | 51 | led1 = 0; |
jamie_wubben | 3:c77bed3482d3 | 52 | replay(); |
jensdehoog | 2:5b7d055dbc91 | 53 | } |
jensdehoog | 0:fd29cd85e75e | 54 | |
jensdehoog | 2:5b7d055dbc91 | 55 | /** |
jensdehoog | 2:5b7d055dbc91 | 56 | TODO |
jensdehoog | 2:5b7d055dbc91 | 57 | ---- |
jensdehoog | 2:5b7d055dbc91 | 58 | This function: |
jensdehoog | 2:5b7d055dbc91 | 59 | - turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jensdehoog | 2:5b7d055dbc91 | 60 | - disables the button such that the debouncer is active |
jensdehoog | 2:5b7d055dbc91 | 61 | - enables the button again after a certain amount of time |
jensdehoog | 2:5b7d055dbc91 | 62 | (use interrupts with "button1_enabled_cb()" as callback. |
jensdehoog | 2:5b7d055dbc91 | 63 | - counts the amount of clicks within a period of 1 second |
jensdehoog | 2:5b7d055dbc91 | 64 | - informs the main loop that the button has been pressed |
jensdehoog | 2:5b7d055dbc91 | 65 | - informs the main loop that the user is clicking the button. |
jensdehoog | 2:5b7d055dbc91 | 66 | Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
jensdehoog | 2:5b7d055dbc91 | 67 | */ |
jamie_wubben | 3:c77bed3482d3 | 68 | |
jamie_wubben | 3:c77bed3482d3 | 69 | void button1_busy_enable(){ |
jamie_wubben | 3:c77bed3482d3 | 70 | button1_buzy = false; |
jamie_wubben | 3:c77bed3482d3 | 71 | } |
jensdehoog | 0:fd29cd85e75e | 72 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 73 | { |
jamie_wubben | 3:c77bed3482d3 | 74 | //button is pressed first time, turn led on |
jamie_wubben | 3:c77bed3482d3 | 75 | if(button1_enabled){ |
jamie_wubben | 3:c77bed3482d3 | 76 | counter = 0; |
jamie_wubben | 3:c77bed3482d3 | 77 | // turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jamie_wubben | 3:c77bed3482d3 | 78 | led1 = 1; |
jamie_wubben | 3:c77bed3482d3 | 79 | //disables the button such that the debouncer is active |
jamie_wubben | 3:c77bed3482d3 | 80 | button1_enabled = false; |
jamie_wubben | 3:c77bed3482d3 | 81 | button1_buzy = true; |
jamie_wubben | 3:c77bed3482d3 | 82 | timer.attach(callback(&button1_busy_enable),0.1); |
jamie_wubben | 3:c77bed3482d3 | 83 | // enables the button again after a certain amount of time (use interrupts with "button1_enabled_cb()" as callback. |
jamie_wubben | 3:c77bed3482d3 | 84 | someTimeout.attach(callback(&button1_enabled_cb), 5); |
jamie_wubben | 3:c77bed3482d3 | 85 | }else if(!button1_buzy){ |
jamie_wubben | 3:c77bed3482d3 | 86 | counter++; |
jamie_wubben | 3:c77bed3482d3 | 87 | button1_buzy = true; |
jamie_wubben | 3:c77bed3482d3 | 88 | timer.attach(callback(&button1_busy_enable),0.1); |
jamie_wubben | 3:c77bed3482d3 | 89 | } |
jensdehoog | 0:fd29cd85e75e | 90 | |
jensdehoog | 0:fd29cd85e75e | 91 | } |