Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of PGO6_VoteController_template by
debounce_button.cpp
00001 #include "debounce_button.h" 00002 00003 /** 00004 TODO 00005 ---- 00006 - 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. 00007 Find a method such that false calls of this function are going to be filtered out. 00008 The main loop also needs to know when it can process the further instructions. 00009 An acceptable time to disable further false positives is between 50ms and 100ms. 00010 - The user needs to be able to click the button multiple times in 1 second. 00011 The main loop needs to know how many times the user has pressed the button, such that 00012 it can link different procedures to the different multiclicks. The main loop also needs 00013 to know when this function is counting the clicks. Therefore, it has to wait until the clicks have been counted 00014 before it can proceed. 00015 - LED1 needs to be turned on while the function is counting the amount of clicks within 1 second. 00016 00017 Some tips and tricks: 00018 - To use the built-in LED: 00019 DigitalOut led1(LED1); 00020 ... 00021 led1 = 1; 00022 - To delay the call of a function: 00023 Timeout someTimeout; 00024 ... 00025 someTimeout.attach(callback(&anotherFunction), 0.5) with 0.5 as 500 milliseconds 00026 - The variables that are used in interrupt callbacks have to be volatile, 00027 because these variables can change at any time. Therefore, the compiler is not 00028 going to make optimisations. 00029 - Use boolean flags to send information between different processes. 00030 - In the header file are extra functions and variables that can help you developing these procedures. 00031 You can add, change or remove these functions and variables at any time, as long as it is clear what you've done. 00032 */ 00033 00034 /** 00035 This function is called when the button has been pressed by the user. 00036 */ 00037 00038 Timeout timeout1; 00039 Timeout timeout2; 00040 DigitalOut busy_led(LED1); 00041 volatile bool button1_pressed=false; // Used in the main loop 00042 volatile bool button1_enabled=true; // Used for debouncing 00043 volatile int multiclick_state=0; // Counts how many clicks occured in the time slot, used in main loop 00044 volatile int last_multiclick_state=0; // Counts how many clicks occured in the previous timeslot 00045 volatile bool button1_busy = false; // Informs the mainloop that the user is clicking the button 00046 volatile bool result_ready=false; 00047 00048 void button1_onpressed_cb(void) 00049 { 00050 if(button1_enabled) 00051 { 00052 if(multiclick_state == 0) //first press this second 00053 { 00054 button1_busy=true; 00055 busy_led = 1; 00056 timeout1.attach(callback(&button1_multiclick_reset_cb), 1.0); 00057 } 00058 multiclick_state += 1; 00059 button1_enabled=false; 00060 timeout2.attach(callback(&button1_enabled_cb), 0.075); 00061 } 00062 } 00063 00064 /** 00065 Resets the amount of clicks, but stores this value for the usage in the main loop 00066 */ 00067 void button1_multiclick_reset_cb() 00068 { 00069 last_multiclick_state = multiclick_state; 00070 multiclick_state = 0; 00071 button1_busy=false; 00072 result_ready=true; 00073 busy_led=0; 00074 } 00075 00076 /** 00077 Enables the button again after a timeout, used for debouncing the button 00078 */ 00079 void button1_enabled_cb() 00080 { 00081 button1_enabled=true; 00082 }
Generated on Wed Jul 13 2022 07:17:10 by
