HAL STM32通过multi_button库处理按键事件
- 📍作者:
0x1abin
的multi_button库:https://github.com/0x1abin/MultiButton
📘MultiButton简介
MultiButton 是一个小巧简单易用的事件驱动型按键驱动模块,可无限量扩展按键,按键事件的回调异步处理方式可以简化你的程序结构,去除冗余的按键处理硬编码,让你的按键业务逻辑更清晰。
- 🔖 该库驱动代码纯C语言实现,可以移植到任意需要使用的地方。
🔑使用方法
- 先申请一个按键结构
struct Button button1;
- 初始化按键对象,绑定按键的GPIO电平读取接口read_button_pin() ,后一个参数设置有效触发电平。
button_init(&button1, read_button_pin, 0, 0);//绑定按键,读取按键引脚状态,有效触发电平、按键ID
- 注册按键事件
button_attach(&button1, SINGLE_CLICK, Callback_SINGLE_CLICK_Handler);
button_attach(&button1, DOUBLE_CLICK, Callback_DOUBLE_Click_Handler);
…
4. 启动按键
button_start(&button1);
- 设置一个5ms间隔的定时器循环调用后台处理函数
while(1) {
...
if(timer_ticks == 5) {
timer_ticks = 0;
button_ticks();
}
}
🛠按键引脚配置
- 🌿通过
STM32CubeMX
,将按键引脚配置成上拉,那么有效触发电平就需要设置为0
.
📗通过按键事件查询,获取按键状态
- 🌿在滴答定时器中断函数(
SysTick_Handler
)中添加button_tick();
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
char buf[128];//自定义缓冲区大小
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}
void button_tick(void)
{
static uint8_t tickstart = 0;
tickstart++;
if(tickstart < 5)
return;
tickstart = 0;
button_ticks();
}
//按键状态读取接口 按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id) {
case 1:
return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
case 2:
return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
default:
return 0;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t TimerUART;
static PressEvent btn1_event_val;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
printf("Main_Fosc:%dHz \r\n", Main_Fosc);
TimerUART = HAL_GetTick();
usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
button_init(&btn1, Read_Button_GPIO, 0, 1);
button_start(&btn1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if((HAL_GetTick() - TimerUART) > 1000) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
TimerUART = HAL_GetTick();
}
if(btn1_event_val != get_button_event(&btn1)) {
btn1_event_val = get_button_event(&btn1);
if(btn1_event_val == PRESS_DOWN) {
// printf("STM32F427 KEY1 PRESS_DOWN \r\n");
usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
} else if(btn1_event_val == PRESS_UP) {
// printf("STM32F427 KEY1 PRESS_UP \r\n");
usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
} else if(btn1_event_val == LONG_PRESS_HOLD) {
// printf("STM32F427 LONG_PRESS_HOLD\r\n"/);
usb_printf("STM32F427 LONG_PRESS_HOLD\r\n");
}
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 384;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 8;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while(1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
📓通过注册按键事件,获取按键状态
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
void BTN1_PRESS_DOWN_Handler(void* btn);
void BTN1_PRESS_UP_Handler(void* btn);
void BTN1_SINGLE_Click_Handler(void* btn);
void BTN1_DOUBLE_Click_Handler(void* btn);
void BTN1_LONG_RRESS_START_Handler(void* btn);
void BTN1_LONG_PRESS_HOLD_Handler(void* btn);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
char buf[128];//自定义缓冲区大小
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}
void button_tick(void)
{
static uint8_t tickstart = 0;
tickstart++;
if(tickstart < 5)
return;
tickstart = 0;
button_ticks();
}
//按键状态读取接口 按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id) {
case 1:
return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
case 2:
return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
default:
return 0;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
uint32_t TimerUART;
// static PressEvent btn1_event_val;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
printf("Main_Fosc:%dHz \r\n", Main_Fosc);
TimerUART = HAL_GetTick();
usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
button_init(&btn1, Read_Button_GPIO, 0, 1);
// button_attach(&btn1, PRESS_DOWN, BTN1_PRESS_DOWN_Handler); //按下
// button_attach(&btn1, PRESS_UP, BTN1_PRESS_UP_Handler); //按起
// button_attach(&btn1, PRESS_REPEAT, BTN1_PRESS_REPEAT_Handler);
button_attach(&btn1, SINGLE_CLICK, BTN1_SINGLE_Click_Handler); //单击
button_attach(&btn1, DOUBLE_CLICK, BTN1_DOUBLE_Click_Handler); //双击
button_attach(&btn1, LONG_PRESS_START, BTN1_LONG_RRESS_START_Handler); //长按开始
// button_attach(&btn1, LONG_PRESS_HOLD, BTN1_LONG_PRESS_HOLD_Handler);//一直长按状态
button_start(&btn1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if((HAL_GetTick() - TimerUART) > 1000) {
HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
TimerUART = HAL_GetTick();
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 384;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 8;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void BTN1_PRESS_DOWN_Handler(void* btn)
{
// printf("STM32F427 KEY1 PRESS_DOWN \r\n");
usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
}
void BTN1_PRESS_UP_Handler(void* btn)
{
// printf("STM32F427 KEY1 PRESS_UP \r\n");
usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
// printf("STM32F427 KEY1 SINGLE_Click \r\n");
usb_printf("STM32F427 KEY1 SINGLE_Click \r\n");
}
void BTN1_DOUBLE_Click_Handler(void* btn)
{
// printf("STM32F427 KEY1 DOUBLE_Click \r\n");
usb_printf("STM32F427 KEY1 DOUBLE_Click \r\n");
}
void BTN1_LONG_RRESS_START_Handler(void* btn)
{
// printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
usb_printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
// printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
usb_printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while(1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
📝通过注册按键事件,统一查询获取按键状态
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2024 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usart.h"
#include "usb_device.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
#include <stdarg.h>
#include "usbd_cdc_if.h"
#include "multi_button.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
struct Button btn1;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
//void BTN1_PRESS_DOWN_Handler(void* btn);
//void BTN1_PRESS_UP_Handler(void* btn);
//void BTN1_SINGLE_Click_Handler(void* btn);
//void BTN1_DOUBLE_Click_Handler(void* btn);
//void BTN1_LONG_RRESS_START_Handler(void* btn);
//void BTN1_LONG_PRESS_HOLD_Handler(void* btn);
void button_callback(void *btn);//
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void usb_printf(const char *fmt, ...)
{
char buf[128];//自定义缓冲区大小
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
CDC_Transmit_FS((uint8_t *)buf, strlen(buf));
}
void button_tick(void)
{
static uint8_t tickstart = 0;
tickstart++;
if(tickstart < 5)
return;
tickstart = 0;
button_ticks();
}
//按键状态读取接口 按键输入模式 ReadInputDataBit
uint8_t Read_Button_GPIO(uint8_t button_id)
{
// you can share the GPIO read function with multiple Buttons
switch(button_id) {
case 1:
return HAL_GPIO_ReadPin(KEY1_GPIO_Port, KEY1_Pin);
case 2:
return HAL_GPIO_ReadPin(KEY2_GPIO_Port, KEY2_Pin);
default:
return 0;
}
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
// uint32_t TimerUART;
// static PressEvent btn1_event_val;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART1_UART_Init();
MX_USB_DEVICE_Init();
/* USER CODE BEGIN 2 */
uint32_t Main_Fosc = HAL_RCC_GetSysClockFreq();
printf("Main_Fosc:%dHz \r\n", Main_Fosc);
// TimerUART = HAL_GetTick();
usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
button_init(&btn1, Read_Button_GPIO, 0, 1);
// button_attach(&btn1, PRESS_DOWN, button_callback); //按下
// button_attach(&btn1, PRESS_UP, button_callback); //按起
// button_attach(&btn1, PRESS_REPEAT,button_callback);
button_attach(&btn1, SINGLE_CLICK, button_callback); //单击
button_attach(&btn1, DOUBLE_CLICK, button_callback); //双击
button_attach(&btn1, LONG_PRESS_START, button_callback); //长按开始
// button_attach(&btn1, LONG_PRESS_HOLD, button_callback);//一直长按状态
button_start(&btn1);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while(1) {
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// if((HAL_GetTick() - TimerUART) > 1000) {
// HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin | LED2_Pin | LED3_Pin); //翻转电平,LED翻转
// printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
// usb_printf("STM32F427 SysClockFreq:%d \r\n", Main_Fosc);
// TimerUART = HAL_GetTick();
// }
/*
if(btn1_event_val != get_button_event(&btn1)) {
btn1_event_val = get_button_event(&btn1);
if(btn1_event_val == PRESS_DOWN) {
// printf("STM32F427 KEY1 PRESS_DOWN \r\n");
usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
} else if(btn1_event_val == PRESS_UP) {
// printf("STM32F427 KEY1 PRESS_UP \r\n");
usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
} else if(btn1_event_val == LONG_PRESS_HOLD) {
// printf("STM32F427 LONG_PRESS_HOLD\r\n"/);
usb_printf("STM32F427 LONG_PRESS_HOLD\r\n");
}
}
*/
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 16;
RCC_OscInitStruct.PLL.PLLN = 384;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;
RCC_OscInitStruct.PLL.PLLQ = 8;
if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) {
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK
| RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
if(HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) {
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
void BTN1_PRESS_DOWN_Handler(void* btn)
{
// printf("STM32F427 KEY1 PRESS_DOWN \r\n");
usb_printf("STM32F427 KEY1 PRESS_DOWN \r\n");
}
void BTN1_PRESS_UP_Handler(void* btn)
{
// printf("STM32F427 KEY1 PRESS_UP \r\n");
usb_printf("STM32F427 KEY1 PRESS_UP \r\n");
}
void BTN1_SINGLE_Click_Handler(void* btn)
{
// printf("STM32F427 KEY1 SINGLE_Click \r\n");
usb_printf("STM32F427 KEY1 SINGLE_Click \r\n");
}
void BTN1_DOUBLE_Click_Handler(void* btn)
{
// printf("STM32F427 KEY1 DOUBLE_Click \r\n");
usb_printf("STM32F427 KEY1 DOUBLE_Click \r\n");
}
void BTN1_LONG_RRESS_START_Handler(void* btn)
{
// printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
usb_printf("STM32F427 KEY1 LONG_RRESS_START \r\n");
}
void BTN1_LONG_PRESS_HOLD_Handler(void* btn)
{
// printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
usb_printf("STM32F427 KEY1 LONG_PRESS_HOLD \r\n");
}
void button_callback(void *btn)
{
static PressEvent btn1_event_val;
// uint32_t btn_event_val;
// btn_event_val = get_button_event((struct Button *)btn);
// btn_event_val = get_button_event(&btn1);
if(btn1_event_val != get_button_event(&btn1)) {
btn1_event_val = get_button_event(&btn1);
switch(btn1_event_val)
{
case PRESS_DOWN:
printf("---> KEY1 press down! <---\r\n");
usb_printf("---> KEY1 press down! <---\r\n");
break;
case PRESS_UP:
printf("***> KEY1 press up! <***\r\n");
usb_printf("---> KEY1 press up! <---\r\n");
break;
case PRESS_REPEAT:
printf("---> KEY1 press repeat! <---\r\n");
break;
case SINGLE_CLICK:
printf("---> KEY1 single click! <---\r\n");
usb_printf("---> KEY1 single click! <---\r\n");
break;
case DOUBLE_CLICK:
printf("***> KEY1 double click! <***\r\n");
usb_printf("---> KEY1 double click! <---\r\n");
break;
case LONG_PRESS_START:
printf("---> KEY1 long press start! <---\r\n");
usb_printf("---> KEY1 long press start! <---\r\n");
break;
case LONG_PRESS_HOLD:
printf("***> KEY1 long press hold! <***\r\n");
usb_printf("---> KEY1 press down! <---\r\n");
break;
default:
break;
}
}
}
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while(1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
📚相关测试工程
链接:https://pan.baidu.com/s/1DRqSkKnj6Kznh3izOLrlWQ?pwd=awf4
提取码:awf4
链接:https://pan.baidu.com/s/1rviDB1MHQTQZkIBr590wXQ?pwd=11tu
提取码:11tu
链接:https://pan.baidu.com/s/1GkQE7OZJnBn8t6gNXMahEw?pwd=vhw5
提取码:vhw5