The following example just register one button connected to port B7, the button is sample every 10ms and needs at least 5 samples to report an event. Two tasks have been used, one in charge of polling the button main function every 10ms and a second one to rea is it was a press to toggle a led connected to pin C0
#include "bsp.h"
#include "scheduler.h"
#include "buttons.h"
KeyboardType Buttons;
ButtonType ButtonBuffer[ 1 ];
uint8_t ButtonSW7;
/**
* @brief Task1_Init
*
* This function initializes the GPIO port B pin 7 as input where the button is connected
* Also it initializes the button handler with the number of buttons to handle,
* the number of samples to detect an activation and the buffer to store the buttons structure.
*/
void Task1_Init( void )
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_7;
HAL_GPIO_Init( GPIOB, &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 );
}
/**
* @brief Task1_Run
*
* This function poll the button handler to detect the button status every 10ms
*/
void Task1_Run( void )
{
Buttons_MainFunction( &Buttons );
}
/**
* @brief Task2_Init
*
* This function initializes the GPIO port C pin 0 as output where the LED is connected
*/
void Task2_Init( void )
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Pin = GPIO_PIN_0;
HAL_GPIO_Init( GPIOC, &GPIO_InitStruct );
}
/**
* @brief Task2_Run
*
* This function toggles the LED connected to port C pin 0 when the button is pressed
*/
void Task2_Run( void )
{
/* Check if the button is pressed */
if( Buttons_GetEvent( &Buttons, ButtonSW7 ) == BTN_PRESSED )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
}
}
/**
* @brief **Application entry point**
*
* Ptovide the proper description for function main according to your own
* implementation
*
* @retval None
*/
int main( void )
{
SchedulerType Scheduler;
TaskType TaskBuffer[ 2 ];
TimerType TimerBuffer[ 1 ];
HAL_Init( );
__HAL_RCC_GPIOB_CLK_ENABLE( );
__HAL_RCC_GPIOC_CLK_ENABLE( );
/*set up scheduler to run up to 3 task with a 10ms tick */
Scheduler_Init( &Scheduler, 10, 2, TaskBuffer, 1, TimerBuffer );
/*Register the two task in our application */
Scheduler_RegisterTask( &Scheduler, Task1_Init, Task1_Run, 10u );
Scheduler_RegisterTask( &Scheduler, Task2_Init, Task2_Run, 50u );
/*start the scheduler */
Scheduler_MainFunction( &Scheduler );
return 0u;
}