The following code shows how to sample the buttons main functions using a period of tie of 10ms, the with another one we can poll when the button reports an event every 50ms, because we need 5 samples to determine if the button was pressed or released
#include "bsp.h"
#include "buttons.h"
/**
* @brief **Application entry point**
*
* Ptovide the proper description for function main according to your own
* implementation
*
* @retval None
*/
int main( void )
{
KeyboardType Buttons;
ButtonType ButtonBuffer[ 1 ];
uint8_t ButtonSW7;
HAL_Init( );
__HAL_RCC_GPIOB_CLK_ENABLE( );
__HAL_RCC_GPIOC_CLK_ENABLE( );
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
/* Initialize the button pin as input */
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init( GPIOB, &GPIO_InitStruct );
/* Initialize the LED pin as output */
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pin = GPIO_PIN_0;
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
/* Initialize the button handler with one button and 5 samples
to detect an activation */
Buttons_Init( &Buttons, 1, 5, ButtonBuffer );
/* Register the button in the button handler in pin 7 port B, the button
report a logic 0 when is pressed*/
ButtonSW7 = Buttons_Register( &Buttons, 7, 1, 0 );
uint32_t tick_10ms = HAL_GetTick(); /*get the current tick count for process 1*/
uint32_t tick_50ms = HAL_GetTick(); /*get the current tick count for process 2*/
/*if difference between current tick and last process tick is equal or bigger to 10ms*/
if( (HAL_GetTick() - tick_10ms) >= 10 )
{
tick_10ms = HAL_GetTick();/*get the current tick again*/
Buttons_MainFunction( &Buttons ); /*run process 1*/
}
/*if difference between current tick and last process tick is equal or bigger to 50ms*/
if( (HAL_GetTick() - tick_50ms) >= 50 )
{
tick_50ms = HAL_GetTick();/*get the current tick again*/
/* Check if the button is pressed */
if( Buttons_GetEvent( &Buttons, ButtonSW7 ) == BTN_PRESSED )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
}
}
return 0u;
}