1 , "" 20 . , , .
while(bcm2835_gpio_lev(PIN)){} waitButtonRelease().
#include <unistd.h>
#define DEBOUNCE_MILLISEC 20
void waitButtonRelease()
{
int debounce = 0 ;
while( debounce < DEBOUNCE_MILLISEC )
{
usleep(1000) ;
if( bcm2835_gpio_lev(PIN) )
{
debounce = 0 ;
}
else
{
debounce++ ;
}
}
}
You may also need to cancel button clicks as well as releases. This is done the same way, but assuming the opposite state:
void waitButtonPress()
{
int debounce = 0 ;
while( debounce < DEBOUNCE_MILLISEC )
{
usleep(1000) ;
if( !bcm2835_gpio_lev(PIN) )
{
debounce = 0 ;
}
else
{
debounce++ ;
}
}
}
Or perhaps one function to debut any state:
#include <stdbool.h>
void waitButton( bool state )
{
int debounce = 0 ;
while( debounce < DEBOUNCE_MILLISEC )
{
usleep(1000) ;
if( bcm2835_gpio_lev(PIN) == state )
{
debounce++ ;
}
else
{
debounce = 0 ;
}
}
}
Given this last function, your main while loop might look like this:
while(1)
{
waitButton( true )
printf("The button has been pressed\n");
waitButton( false ) ;
}
If you have access to a digital storage oscilloscope, you can directly check the switch signal to see what the switch bounce looks like. This can help you understand the problem, as well as adapt debugging to the characteristics of your particular switch.
source
share