2025-04-18 18:59:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @file Flux_Button.c
|
|
|
|
|
|
* @brief 控制板按键源文件
|
|
|
|
|
|
*
|
|
|
|
|
|
* 实现按键初始化及响应功能
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author wang xiang en
|
|
|
|
|
|
* @date 2025-04-18
|
|
|
|
|
|
* @version 版本号
|
|
|
|
|
|
* @copyright 版权声明((C)2025, YUWELL MEDTECH Co.ltd)
|
|
|
|
|
|
*/
|
2025-03-08 14:45:49 +08:00
|
|
|
|
#include <stdio.h>
|
2025-04-18 18:59:53 +08:00
|
|
|
|
#include "Flux_Button.h"
|
2025-03-08 14:45:49 +08:00
|
|
|
|
#include "driver/gpio.h"
|
2025-04-18 18:59:53 +08:00
|
|
|
|
#include "Flux_power.h"
|
2025-03-13 09:04:45 +08:00
|
|
|
|
|
2025-03-08 14:45:49 +08:00
|
|
|
|
#include "ui.h"
|
|
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 日志打印标签 */
|
|
|
|
|
|
static const char *S_BUTTON_TAG = "tag_flux_button";
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 屏幕状态记录事件组 */
|
|
|
|
|
|
EventGroupHandle_t g_lcd_state_event_group;
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 按键事件处理函数
|
|
|
|
|
|
*
|
|
|
|
|
|
* 将按键触发事件绑定到此函数中,按键事件发生后执行该函数。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param[in] arg not used
|
|
|
|
|
|
* @param[in] data 按键事件枚举值
|
|
|
|
|
|
*/
|
2025-03-08 14:45:49 +08:00
|
|
|
|
static void button_event_cb(void *arg, void *data)
|
|
|
|
|
|
{
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 按键事件出发后发送日志信息到屏幕 */
|
|
|
|
|
|
ESP_LOGI(S_BUTTON_TAG, "Button event %d", (int)data);
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
|
|
|
|
|
switch ((int)data)
|
|
|
|
|
|
{
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 电源按键按下事件 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
case BUTTON_CMD_POWER_ON:
|
2025-03-10 10:42:45 +08:00
|
|
|
|
POWER_ON;
|
2025-03-08 14:45:49 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case BUTTON_CMD_POWER_OFF:
|
2025-03-10 10:42:45 +08:00
|
|
|
|
POWER_OFF;
|
2025-03-08 14:45:49 +08:00
|
|
|
|
break;
|
2025-03-13 09:04:45 +08:00
|
|
|
|
case BUTTON_CMD_LCD_BACKLIGHT_CHANGE:
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 单击电源键事件 */
|
|
|
|
|
|
ESP_LOGI(S_BUTTON_TAG, "LCD backlight change");
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Toggle backlight based on current backlight state */
|
2025-04-18 18:59:53 +08:00
|
|
|
|
if (xEventGroupGetBits(g_lcd_state_event_group) & LCD_STATE_BIT) // If the current backlight state is on, turn it off
|
2025-03-13 09:04:45 +08:00
|
|
|
|
{
|
|
|
|
|
|
LCD_BACKLIGHT_CLOSE;
|
2025-04-18 18:59:53 +08:00
|
|
|
|
xEventGroupClearBits(g_lcd_state_event_group, LCD_STATE_BIT);
|
2025-03-13 09:04:45 +08:00
|
|
|
|
}else{
|
|
|
|
|
|
LCD_BACKLIGHT_OPEN;
|
2025-04-18 18:59:53 +08:00
|
|
|
|
xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT);
|
2025-03-13 09:04:45 +08:00
|
|
|
|
}
|
|
|
|
|
|
break;
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 中间按键按下事件 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
case BUTTON_CMD_CENTER_KEY:
|
2025-03-13 15:13:27 +08:00
|
|
|
|
if (lv_scr_act()==ui_pageHome)
|
2025-03-15 09:37:07 +08:00
|
|
|
|
{ /* Start test button */
|
2025-03-13 15:13:27 +08:00
|
|
|
|
lv_event_send(ui_pageHome_buttonStartTest, LV_EVENT_CLICKED, NULL);
|
|
|
|
|
|
}else{
|
|
|
|
|
|
lv_scr_load_anim(ui_pageHome, LV_SCR_LOAD_ANIM_OUT_TOP, 400, 0, false);
|
|
|
|
|
|
}
|
2025-03-08 14:45:49 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case BUTTON_CMD_LEFT_KEY:
|
2025-03-13 15:13:27 +08:00
|
|
|
|
if (lv_scr_act()==ui_pageHome)
|
|
|
|
|
|
{
|
|
|
|
|
|
lv_event_send(ui_pageHome_buttonMinus, LV_EVENT_CLICKED, NULL);
|
|
|
|
|
|
}
|
2025-03-08 14:45:49 +08:00
|
|
|
|
break;
|
|
|
|
|
|
case BUTTON_CMD_RIGHT_KEY:
|
2025-03-13 15:13:27 +08:00
|
|
|
|
if (lv_scr_act()==ui_pageHome)
|
|
|
|
|
|
{
|
|
|
|
|
|
lv_event_send(ui_pageHome_buttonAdd, LV_EVENT_CLICKED, NULL);
|
|
|
|
|
|
}
|
2025-03-08 14:45:49 +08:00
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 单个按键初始化函数
|
|
|
|
|
|
*
|
|
|
|
|
|
* 根据按键编号,初始化对应的按键IO,并设置按键回调函数为button_event_cb
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param[in] button_num 按键编号
|
|
|
|
|
|
*/
|
|
|
|
|
|
static void single_button_init(uint32_t button_num)
|
2025-03-08 14:45:49 +08:00
|
|
|
|
{
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 根据引脚是否为电源键判断活动状态电平值 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
uint8_t activeLevel = BUTTON_ACTIVE_LEVEL;
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Modify the activation state to high level when the button is the power button */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
if (button_num == BUTTON_PowerIn_IO_NUM)
|
|
|
|
|
|
{
|
|
|
|
|
|
activeLevel = BUTTON_POWERIN_ACTIVE_LEVEL;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 按键配置结构体初始化 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
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);
|
|
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 注册按键事件处理函数 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
esp_err_t err = ESP_OK;
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Register callback function for power button press */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
if (button_num == BUTTON_PowerIn_IO_NUM)
|
|
|
|
|
|
{
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Turn off by double-clicking the power button */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, button_event_cb, (void *)BUTTON_CMD_POWER_OFF);
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Turn on by long-pressing the power button */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_CMD_POWER_ON);
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Single click on the power button to toggle the display backlight state */
|
2025-03-13 09:04:45 +08:00
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_LCD_BACKLIGHT_CHANGE);
|
2025-03-08 14:45:49 +08:00
|
|
|
|
}else{
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Register callback functions for single clicks on the three buttons below */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
switch (button_num)
|
|
|
|
|
|
{
|
|
|
|
|
|
case BUTTON_Center_IO_NUM:
|
|
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_CENTER_KEY);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BUTTON_Left_IO_NUM:
|
|
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_LEFT_KEY);
|
|
|
|
|
|
break;
|
|
|
|
|
|
case BUTTON_Right_IO_NUM:
|
|
|
|
|
|
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_RIGHT_KEY);
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
ESP_ERROR_CHECK(err);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* @brief 初始化所有按键
|
|
|
|
|
|
*
|
|
|
|
|
|
* 配置按键、电源管理IO引脚状态
|
|
|
|
|
|
*/
|
|
|
|
|
|
void flux_button_init(void)
|
2025-03-08 14:45:49 +08:00
|
|
|
|
{
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 初始化下方的左右及中间按键 */
|
|
|
|
|
|
single_button_init(BUTTON_Center_IO_NUM);
|
|
|
|
|
|
single_button_init(BUTTON_Left_IO_NUM);
|
|
|
|
|
|
single_button_init(BUTTON_Right_IO_NUM);
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 初始化电源按键 */
|
|
|
|
|
|
single_button_init(BUTTON_PowerIn_IO_NUM);
|
2025-03-08 14:45:49 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 初始化按键事件组 */
|
|
|
|
|
|
g_lcd_state_event_group = xEventGroupCreate();
|
2025-03-13 09:04:45 +08:00
|
|
|
|
|
2025-03-15 09:37:07 +08:00
|
|
|
|
/* Set the default state to backlight on */
|
2025-04-18 18:59:53 +08:00
|
|
|
|
xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT);
|
2025-03-13 09:04:45 +08:00
|
|
|
|
|
2025-04-18 18:59:53 +08:00
|
|
|
|
/* 初始化电源控制引脚 */
|
2025-03-08 14:45:49 +08:00
|
|
|
|
gpio_reset_pin(BUTTON_PowerOut_IO_NUM);
|
|
|
|
|
|
gpio_set_direction(BUTTON_PowerOut_IO_NUM, GPIO_MODE_OUTPUT);
|
|
|
|
|
|
}
|
|
|
|
|
|
|