Kjell Kleyn
/
PGO6_VoteController_template
Vote Controller IOT
Fork of PGO6_VoteController_template by
debounce_button.cpp@3:b2c9de2f45c7, 2017-11-06 (annotated)
- Committer:
- kjellkleyn
- Date:
- Mon Nov 06 14:18:21 2017 +0000
- Revision:
- 3:b2c9de2f45c7
- Parent:
- 2:5b7d055dbc91
Vote Controller einde laatste les
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jensdehoog | 0:fd29cd85e75e | 1 | #include "debounce_button.h" |
kjellkleyn | 3:b2c9de2f45c7 | 2 | DigitalOut led1(LED1); |
kjellkleyn | 3:b2c9de2f45c7 | 3 | Timeout second; |
kjellkleyn | 3:b2c9de2f45c7 | 4 | Timeout debounce; |
jensdehoog | 0:fd29cd85e75e | 5 | /** |
jensdehoog | 0:fd29cd85e75e | 6 | Some tips and tricks: |
jensdehoog | 0:fd29cd85e75e | 7 | - To use the built-in LED: |
jensdehoog | 0:fd29cd85e75e | 8 | DigitalOut led1(LED1); |
jensdehoog | 0:fd29cd85e75e | 9 | ... |
jensdehoog | 0:fd29cd85e75e | 10 | led1 = 1; |
jensdehoog | 0:fd29cd85e75e | 11 | - To delay the call of a function: |
jensdehoog | 0:fd29cd85e75e | 12 | Timeout someTimeout; |
jensdehoog | 0:fd29cd85e75e | 13 | ... |
jensdehoog | 2:5b7d055dbc91 | 14 | someTimeout.attach(callback(&someFunction), 0.5) with 0.5 as 500 milliseconds |
jensdehoog | 0:fd29cd85e75e | 15 | - The variables that are used in interrupt callbacks have to be volatile, |
jensdehoog | 0:fd29cd85e75e | 16 | because these variables can change at any time. Therefore, the compiler is not |
jensdehoog | 0:fd29cd85e75e | 17 | going to make optimisations. |
jensdehoog | 0:fd29cd85e75e | 18 | */ |
jensdehoog | 0:fd29cd85e75e | 19 | |
jensdehoog | 0:fd29cd85e75e | 20 | /** |
jensdehoog | 2:5b7d055dbc91 | 21 | TODO |
jensdehoog | 2:5b7d055dbc91 | 22 | ---- |
jensdehoog | 2:5b7d055dbc91 | 23 | This function: |
jensdehoog | 2:5b7d055dbc91 | 24 | - stores the amount of clicks in a variable which is read by the main loop. |
jensdehoog | 2:5b7d055dbc91 | 25 | - resets the click counter which is used inside this file. |
jensdehoog | 2:5b7d055dbc91 | 26 | - lowers a flag which tells the main loop that the user stopped pressing the button |
jensdehoog | 2:5b7d055dbc91 | 27 | such that it can proceed its program. |
jensdehoog | 2:5b7d055dbc91 | 28 | - turns the built-in LED off. Therefore, the user gets informed that the program stopped counting the clicks. |
jensdehoog | 2:5b7d055dbc91 | 29 | */ |
jensdehoog | 2:5b7d055dbc91 | 30 | void button1_multiclick_reset_cb(void) { |
jensdehoog | 2:5b7d055dbc91 | 31 | |
jensdehoog | 2:5b7d055dbc91 | 32 | } |
jensdehoog | 2:5b7d055dbc91 | 33 | |
jensdehoog | 2:5b7d055dbc91 | 34 | /** |
jensdehoog | 2:5b7d055dbc91 | 35 | TODO |
jensdehoog | 2:5b7d055dbc91 | 36 | ---- |
jensdehoog | 2:5b7d055dbc91 | 37 | This function enables the button again, such that unwanted clicks of the bouncing button get ignored. |
jensdehoog | 0:fd29cd85e75e | 38 | */ |
jensdehoog | 2:5b7d055dbc91 | 39 | void button1_enabled_cb(void) |
jensdehoog | 2:5b7d055dbc91 | 40 | { |
kjellkleyn | 3:b2c9de2f45c7 | 41 | button1_enabled = true; |
jensdehoog | 2:5b7d055dbc91 | 42 | } |
jensdehoog | 0:fd29cd85e75e | 43 | |
jensdehoog | 2:5b7d055dbc91 | 44 | /** |
jensdehoog | 2:5b7d055dbc91 | 45 | TODO |
jensdehoog | 2:5b7d055dbc91 | 46 | ---- |
jensdehoog | 2:5b7d055dbc91 | 47 | This function: |
jensdehoog | 2:5b7d055dbc91 | 48 | - turns the built-in LED on, so the user gets informed that the program has started with counting clicks |
jensdehoog | 2:5b7d055dbc91 | 49 | - disables the button such that the debouncer is active |
jensdehoog | 2:5b7d055dbc91 | 50 | - enables the button again after a certain amount of time |
jensdehoog | 2:5b7d055dbc91 | 51 | (use interrupts with "button1_enabled_cb()" as callback. |
jensdehoog | 2:5b7d055dbc91 | 52 | - counts the amount of clicks within a period of 1 second |
jensdehoog | 2:5b7d055dbc91 | 53 | - informs the main loop that the button has been pressed |
jensdehoog | 2:5b7d055dbc91 | 54 | - informs the main loop that the user is clicking the button. |
jensdehoog | 2:5b7d055dbc91 | 55 | Therefore, this main loop cannot continue its procedure until the clicks within 1 second have been counted. |
jensdehoog | 2:5b7d055dbc91 | 56 | */ |
jensdehoog | 0:fd29cd85e75e | 57 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 58 | { |
kjellkleyn | 3:b2c9de2f45c7 | 59 | if(button1_enabled){ |
kjellkleyn | 3:b2c9de2f45c7 | 60 | button1_enabled = false; |
kjellkleyn | 3:b2c9de2f45c7 | 61 | debounce.attach(&button1_enabled_cb,0.08); |
kjellkleyn | 3:b2c9de2f45c7 | 62 | |
kjellkleyn | 3:b2c9de2f45c7 | 63 | if(countSecond == false){ |
kjellkleyn | 3:b2c9de2f45c7 | 64 | led1 = 1; |
kjellkleyn | 3:b2c9de2f45c7 | 65 | multiclick_state = 1; |
kjellkleyn | 3:b2c9de2f45c7 | 66 | countSecond = true; |
kjellkleyn | 3:b2c9de2f45c7 | 67 | } |
kjellkleyn | 3:b2c9de2f45c7 | 68 | else{ |
kjellkleyn | 3:b2c9de2f45c7 | 69 | multiclick_state++; |
kjellkleyn | 3:b2c9de2f45c7 | 70 | } |
kjellkleyn | 3:b2c9de2f45c7 | 71 | second.attach(&second_ended_cd,1); |
kjellkleyn | 3:b2c9de2f45c7 | 72 | |
kjellkleyn | 3:b2c9de2f45c7 | 73 | } |
kjellkleyn | 3:b2c9de2f45c7 | 74 | } |
jensdehoog | 0:fd29cd85e75e | 75 | |
kjellkleyn | 3:b2c9de2f45c7 | 76 | void second_ended_cd(void) |
kjellkleyn | 3:b2c9de2f45c7 | 77 | { |
kjellkleyn | 3:b2c9de2f45c7 | 78 | led1 = 0; |
kjellkleyn | 3:b2c9de2f45c7 | 79 | countSecond = false; |
kjellkleyn | 3:b2c9de2f45c7 | 80 | readyClick =true; |
jensdehoog | 0:fd29cd85e75e | 81 | } |