Simple and similar to previous example but this time with two buttons, one of them toggle a le when is pressed and the other toggle another one when is release
#include "bsp.h"
#include "scheduler.h"
#include "buttons.h"
KeyboardType Buttons;
ButtonType ButtonBuffer[ 1 ];
uint8_t ButtonSW5, 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_5 | 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, 2, 5, ButtonBuffer );
/* Register the buttons in the button handler in pin 5 and 7 port B, the button
reports a logic 0 when are pressed*/
ButtonSW5 = Buttons_Register( &Buttons, 5, 1, 0 );
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 and 1 as output where the LEDs are 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 | GPIO_PIN_1;
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
* and toggles the LED connected to port C pin 1 when the button is released
*/
void Task2_Run( void )
{
/* Check if the button is pressed */
if( Buttons_GetEvent( &Buttons, ButtonSW7 ) == BTN_PRESSED )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_0 );
}
/* Check if the button is released */
if( Buttons_GetEvent( &Buttons, ButtonSW5 ) == BTN_RELEASED )
{
HAL_GPIO_TogglePin( GPIOC, GPIO_PIN_1 );
}
}
/**
* @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 2 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;
}