新增按键组件

This commit is contained in:
ipason 2025-11-28 02:38:40 +00:00
parent 3d35d5bad2
commit e0cdcc1542
5 changed files with 236 additions and 1 deletions

View File

@ -0,0 +1,20 @@
#
set(FLUX_BUTTON_SRCS "flux_button.c")
#
set(FLUX_BUTTON_REQUIRES
espressif__button
esp_driver_gpio
lvgl
freertos
)
#
set(FLUX_BUTTON_INCLUDE_DIRS ".")
#
idf_component_register(
SRCS ${FLUX_BUTTON_SRCS}
REQUIRES ${FLUX_BUTTON_REQUIRES}
INCLUDE_DIRS ${FLUX_BUTTON_INCLUDE_DIRS}
)

View File

@ -0,0 +1,123 @@
/**
* @file FluxButton.c
* @brief
*
*
*
* @author wang xiang en
* @date 2025-04-18
* @version
* @copyright (C)2025, YUWELL MEDTECH Co.ltd
*/
#include <stdio.h>
#include "flux_button.h"
#include "driver/gpio.h"
/* 日志打印标签 */
static const char *S_BUTTON_TAG = "tag_flux_button";
/* 屏幕状态记录事件组 */
EventGroupHandle_t g_lcd_state_event_group;
/**
* @brief
*
*
*
* @param[in] arg not used
* @param[in] data
*/
static void button_event_cb(void *arg, void *data)
{
/* 按键事件出发后发送日志信息到屏幕 */
ESP_LOGI(S_BUTTON_TAG, "Button event %d", (int)data);
switch ((int)data)
{
/* 电源按键按下事件 */
case BUTTON_CMD_POWER_ON:
POWER_ON;
break;
case BUTTON_CMD_POWER_OFF:
POWER_OFF;
break;
case BUTTON_CMD_LCD_BACKLIGHT_CHANGE:
#if USE_LCD_BACKLIGHT_CLOSE
/* 单击电源键事件 */
ESP_LOGI(S_BUTTON_TAG, "LCD backlight change");
/* Toggle backlight based on current backlight state */
if (xEventGroupGetBits(g_lcd_state_event_group) & LCD_STATE_BIT) // If the current backlight state is on, turn it off
{
LCD_BACKLIGHT_CLOSE;
xEventGroupClearBits(g_lcd_state_event_group, LCD_STATE_BIT);
}else{
LCD_BACKLIGHT_OPEN;
xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT);
}
#endif
break;
default:
break;
}
}
/**
* @brief
*
* IObutton_event_cb
*
* @param[in] button_num
*/
static void single_button_init(uint32_t button_num)
{
/* 根据引脚是否为电源键判断活动状态电平值 */
uint8_t activeLevel = BUTTON_POWERIN_ACTIVE_LEVEL;
/* 按键配置结构体初始化 */
button_config_t btn_cfg = {
.type = BUTTON_TYPE_GPIO,
.gpio_button_config = {
.gpio_num = button_num,
.active_level = activeLevel,
},
};
button_handle_t btn = iot_button_create(&btn_cfg);
assert(btn);
/* 注册按键事件处理函数 */
esp_err_t err = ESP_OK;
/* Turn off by double-clicking the power button */
err |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, button_event_cb, (void *)BUTTON_CMD_POWER_OFF);
/* Turn on by long-pressing the power button */
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_CMD_POWER_ON);
/* Single click on the power button to toggle the display backlight state */
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_LCD_BACKLIGHT_CHANGE);
ESP_ERROR_CHECK(err);
}
/**
* @brief
*
* IO引脚状态
*/
void flux_button_init(void)
{
/* 初始化电源按键 */
single_button_init(BUTTON_PowerIn_IO_NUM);
/* 初始化按键事件组 */
g_lcd_state_event_group = xEventGroupCreate();
/* Set the default state to backlight on */
xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT);
/* 初始化电源控制引脚 */
gpio_reset_pin(BUTTON_PowerOut_IO_NUM);
gpio_set_direction(BUTTON_PowerOut_IO_NUM, GPIO_MODE_OUTPUT);
}

View File

@ -0,0 +1,91 @@
/**
* @file FluxButton.h
* @brief
*
* IO编号
*
* @author wang xiang en
* @date 2025-04-18
* @version
* @copyright (C)2025, YUWELL MEDTECH Co.ltd
*/
#ifndef _FLUX_BUTTON_H
#define _FLUX_BUTTON_H
#ifdef __cplusplus
extern "C" {
#endif
/* Include external button component header files */
#include "iot_button.h"
#include "esp_log.h"
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
/* 定义电源输入IO编号 */
#define BUTTON_PowerIn_IO_NUM GPIO_NUM_10
#define BUTTON_PowerOut_IO_NUM GPIO_NUM_11
/* 定义电源输出电平 */
#define POWER_OFF_OUT_LEVEL (0)
#define POWER_ON_OUT_LEVEL (1)
/* 定义按键电平 */
#define BUTTON_ACTIVE_LEVEL (0)
#define BUTTON_POWERIN_ACTIVE_LEVEL (1)
/* 定义LCD状态位 */
#define LCD_STATE_BIT BIT0
/* 定义按键支持休眠 */
#define CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE (0)
#define USE_LCD_BACKLIGHT_CLOSE (0)
/**
* @brief
*
*
*/
enum button_cmd_t
{
BUTTON_CMD_POWER_ON,
BUTTON_CMD_POWER_OFF,
BUTTON_CMD_LCD_BACKLIGHT_CHANGE,
};
/**
* @brief
*
* LDO芯片电源输出
*/
#define POWER_OFF gpio_set_level(BUTTON_PowerOut_IO_NUM, POWER_OFF_OUT_LEVEL);
/**
* @brief
*
* 使LDO芯片电源输出
*/
#define POWER_ON gpio_set_level(BUTTON_PowerOut_IO_NUM, POWER_ON_OUT_LEVEL);
/**
* @brief
*
*
*
*/
void flux_button_init(void);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /*_FLUX_BUTTON_H*/

View File

@ -6,6 +6,7 @@ set(MAIN_REQUIRES
lvgl lvgl
esp_driver_ledc esp_driver_ledc
esp_driver_gpio esp_driver_gpio
FluxButton
) )
# #

View File

@ -17,7 +17,7 @@
#include "driver/ledc.h" #include "driver/ledc.h"
#include "lvgl.h" #include "lvgl.h"
#include "flux_button.h"
// /* Include UI initialization header file */ // /* Include UI initialization header file */
// #include "FluxDisplayPort.h" // #include "FluxDisplayPort.h"