vote controller implementatie van Jonas De Rynck
Fork of PGO6_VoteController_template by
debounce_button.cpp@2:5f2104244c27, 2017-10-30 (annotated)
- Committer:
- jonasdr
- Date:
- Mon Oct 30 16:43:58 2017 +0000
- Revision:
- 2:5f2104244c27
- Parent:
- 1:34e76c0cbe5a
Jonas De Rynck - embedded 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 | 1:34e76c0cbe5a | 4 | TODO |
jensdehoog | 1:34e76c0cbe5a | 5 | ---- |
jensdehoog | 1:34e76c0cbe5a | 6 | - A debouncer has to be present: the built-in buttons of the Nucleo aren't that good, so false positives have to be filtered out. |
jensdehoog | 1:34e76c0cbe5a | 7 | Find a method such that false calls of this function are going to be filtered out. |
jensdehoog | 1:34e76c0cbe5a | 8 | The main loop also needs to know when it can process the further instructions. |
jensdehoog | 1:34e76c0cbe5a | 9 | An acceptable time to disable further false positives is between 50ms and 100ms. |
jensdehoog | 1:34e76c0cbe5a | 10 | - The user needs to be able to click the button multiple times in 1 second. |
jensdehoog | 1:34e76c0cbe5a | 11 | The main loop needs to know how many times the user has pressed the button, such that |
jensdehoog | 1:34e76c0cbe5a | 12 | it can link different procedures to the different multiclicks. The main loop also needs |
jensdehoog | 1:34e76c0cbe5a | 13 | to know when this function is counting the clicks. Therefore, it has to wait until the clicks have been counted |
jensdehoog | 1:34e76c0cbe5a | 14 | before it can proceed. |
jensdehoog | 1:34e76c0cbe5a | 15 | - LED1 needs to be turned on while the function is counting the amount of clicks within 1 second. |
jensdehoog | 1:34e76c0cbe5a | 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 | 1:34e76c0cbe5a | 25 | someTimeout.attach(callback(&anotherFunction), 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 | 1:34e76c0cbe5a | 29 | - Use boolean flags to send information between different processes. |
jensdehoog | 1:34e76c0cbe5a | 30 | - In the header file are extra functions and variables that can help you developing these procedures. |
jensdehoog | 1:34e76c0cbe5a | 31 | You can add, change or remove these functions and variables at any time, as long as it is clear what you've done. |
jensdehoog | 0:fd29cd85e75e | 32 | */ |
jensdehoog | 0:fd29cd85e75e | 33 | |
jensdehoog | 0:fd29cd85e75e | 34 | /** |
jensdehoog | 1:34e76c0cbe5a | 35 | This function is called when the button has been pressed by the user. |
jensdehoog | 0:fd29cd85e75e | 36 | */ |
jonasdr | 2:5f2104244c27 | 37 | Timeout timeout; |
jonasdr | 2:5f2104244c27 | 38 | Timeout timeout2; |
jonasdr | 2:5f2104244c27 | 39 | DigitalOut busy_led(LED2); |
jonasdr | 2:5f2104244c27 | 40 | DigitalOut testled(LED1); |
jonasdr | 2:5f2104244c27 | 41 | volatile bool button1_enabled = true; |
jonasdr | 2:5f2104244c27 | 42 | volatile int multiclick_state = 0; |
jonasdr | 2:5f2104244c27 | 43 | volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button |
jonasdr | 2:5f2104244c27 | 44 | volatile int multiclick_state_mem = 0; |
jonasdr | 2:5f2104244c27 | 45 | volatile bool isready = false; |
jensdehoog | 0:fd29cd85e75e | 46 | |
jensdehoog | 0:fd29cd85e75e | 47 | void button1_onpressed_cb(void) |
jensdehoog | 0:fd29cd85e75e | 48 | { |
jonasdr | 2:5f2104244c27 | 49 | if (button1_enabled) |
jonasdr | 2:5f2104244c27 | 50 | { |
jonasdr | 2:5f2104244c27 | 51 | if(multiclick_state == 0) //eerste druk binnen een seconde |
jonasdr | 2:5f2104244c27 | 52 | { |
jonasdr | 2:5f2104244c27 | 53 | button1_busy = true; //knop is busy voor 1 sec |
jonasdr | 2:5f2104244c27 | 54 | busy_led = 1; |
jonasdr | 2:5f2104244c27 | 55 | timeout2.attach(callback(button1_multiclick_reset_cb), 1.0); |
jonasdr | 2:5f2104244c27 | 56 | } |
jonasdr | 2:5f2104244c27 | 57 | multiclick_state += 1; |
jonasdr | 2:5f2104244c27 | 58 | button1_enabled = false; |
jonasdr | 2:5f2104244c27 | 59 | timeout.attach(callback(&button1_enabled_cb), 0.075); |
jonasdr | 2:5f2104244c27 | 60 | } |
jonasdr | 2:5f2104244c27 | 61 | } |
jonasdr | 2:5f2104244c27 | 62 | |
jonasdr | 2:5f2104244c27 | 63 | void button1_multiclick_reset_cb(void) |
jonasdr | 2:5f2104244c27 | 64 | { |
jonasdr | 2:5f2104244c27 | 65 | isready = true; |
jonasdr | 2:5f2104244c27 | 66 | multiclick_state_mem = multiclick_state; // aantal kliks onthouden |
jonasdr | 2:5f2104244c27 | 67 | multiclick_state = 0; |
jonasdr | 2:5f2104244c27 | 68 | button1_busy = false; //button niet meer busy |
jonasdr | 2:5f2104244c27 | 69 | busy_led = 0; |
jonasdr | 2:5f2104244c27 | 70 | for(int i = 0; i<multiclick_state_mem; i++) |
jonasdr | 2:5f2104244c27 | 71 | { |
jonasdr | 2:5f2104244c27 | 72 | testing(); |
jonasdr | 2:5f2104244c27 | 73 | } |
jensdehoog | 0:fd29cd85e75e | 74 | |
jonasdr | 2:5f2104244c27 | 75 | } |
jonasdr | 2:5f2104244c27 | 76 | |
jonasdr | 2:5f2104244c27 | 77 | |
jonasdr | 2:5f2104244c27 | 78 | void button1_enabled_cb(void) |
jonasdr | 2:5f2104244c27 | 79 | { |
jonasdr | 2:5f2104244c27 | 80 | button1_enabled = true; |
jonasdr | 2:5f2104244c27 | 81 | } |
jonasdr | 2:5f2104244c27 | 82 | |
jonasdr | 2:5f2104244c27 | 83 | void testing(void) |
jonasdr | 2:5f2104244c27 | 84 | { |
jonasdr | 2:5f2104244c27 | 85 | testled = 1; |
jonasdr | 2:5f2104244c27 | 86 | wait(0.2); |
jonasdr | 2:5f2104244c27 | 87 | testled = 0; |
jonasdr | 2:5f2104244c27 | 88 | wait(0.2); |
jensdehoog | 0:fd29cd85e75e | 89 | } |