Robrecht Daems

Dependencies:   MQTT

Fork of PGO6_VoteController_template by Jens de hoog

Committer:
Robrecht
Date:
Mon Oct 30 16:44:56 2017 +0000
Revision:
2:b47f54fdea91
Parent:
1:34e76c0cbe5a
initial commit

Who changed what in which revision?

UserRevisionLine numberNew 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 */
jensdehoog 0:fd29cd85e75e 37
Robrecht 2:b47f54fdea91 38 Timeout timeout1;
Robrecht 2:b47f54fdea91 39 Timeout timeout2;
Robrecht 2:b47f54fdea91 40 DigitalOut busy_led(LED1);
Robrecht 2:b47f54fdea91 41 volatile bool button1_pressed=false; // Used in the main loop
Robrecht 2:b47f54fdea91 42 volatile bool button1_enabled=true; // Used for debouncing
Robrecht 2:b47f54fdea91 43 volatile int multiclick_state=0; // Counts how many clicks occured in the time slot, used in main loop
Robrecht 2:b47f54fdea91 44 volatile int last_multiclick_state=0; // Counts how many clicks occured in the previous timeslot
Robrecht 2:b47f54fdea91 45 volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button
Robrecht 2:b47f54fdea91 46 volatile bool result_ready=false;
Robrecht 2:b47f54fdea91 47
jensdehoog 0:fd29cd85e75e 48 void button1_onpressed_cb(void)
jensdehoog 0:fd29cd85e75e 49 {
Robrecht 2:b47f54fdea91 50 if(button1_enabled)
Robrecht 2:b47f54fdea91 51 {
Robrecht 2:b47f54fdea91 52 if(multiclick_state == 0) //first press this second
Robrecht 2:b47f54fdea91 53 {
Robrecht 2:b47f54fdea91 54 button1_busy=true;
Robrecht 2:b47f54fdea91 55 busy_led = 1;
Robrecht 2:b47f54fdea91 56 timeout1.attach(callback(&button1_multiclick_reset_cb), 1.0);
Robrecht 2:b47f54fdea91 57 }
Robrecht 2:b47f54fdea91 58 multiclick_state += 1;
Robrecht 2:b47f54fdea91 59 button1_enabled=false;
Robrecht 2:b47f54fdea91 60 timeout2.attach(callback(&button1_enabled_cb), 0.075);
Robrecht 2:b47f54fdea91 61 }
Robrecht 2:b47f54fdea91 62 }
Robrecht 2:b47f54fdea91 63
Robrecht 2:b47f54fdea91 64 /**
Robrecht 2:b47f54fdea91 65 Resets the amount of clicks, but stores this value for the usage in the main loop
Robrecht 2:b47f54fdea91 66 */
Robrecht 2:b47f54fdea91 67 void button1_multiclick_reset_cb()
Robrecht 2:b47f54fdea91 68 {
Robrecht 2:b47f54fdea91 69 last_multiclick_state = multiclick_state;
Robrecht 2:b47f54fdea91 70 multiclick_state = 0;
Robrecht 2:b47f54fdea91 71 button1_busy=false;
Robrecht 2:b47f54fdea91 72 result_ready=true;
Robrecht 2:b47f54fdea91 73 busy_led=0;
Robrecht 2:b47f54fdea91 74 }
Robrecht 2:b47f54fdea91 75
Robrecht 2:b47f54fdea91 76 /**
Robrecht 2:b47f54fdea91 77 Enables the button again after a timeout, used for debouncing the button
Robrecht 2:b47f54fdea91 78 */
Robrecht 2:b47f54fdea91 79 void button1_enabled_cb()
Robrecht 2:b47f54fdea91 80 {
Robrecht 2:b47f54fdea91 81 button1_enabled=true;
Robrecht 2:b47f54fdea91 82 }