FluxDC/components/FluxButton/Flux_Button.c

171 lines
5.4 KiB
C
Raw Normal View History

/**
* @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>
#include "Flux_Button.h"
2025-03-08 14:45:49 +08:00
#include "driver/gpio.h"
#include "Flux_power.h"
2025-03-08 14:45:49 +08:00
#include "ui.h"
/* 日志打印标签 */
static const char *S_BUTTON_TAG = "tag_flux_button";
2025-03-08 14:45:49 +08:00
/* 屏幕状态记录事件组 */
EventGroupHandle_t g_lcd_state_event_group;
2025-03-08 14:45:49 +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)
{
/* 按键事件出发后发送日志信息到屏幕 */
ESP_LOGI(S_BUTTON_TAG, "Button event %d", (int)data);
2025-03-08 14:45:49 +08:00
switch ((int)data)
{
/* 电源按键按下事件 */
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;
case BUTTON_CMD_LCD_BACKLIGHT_CHANGE:
2025-04-27 14:46:47 +08:00
#ifdef USE_LCD_BACKLIGHT_CLOSE
/* 单击电源键事件 */
ESP_LOGI(S_BUTTON_TAG, "LCD backlight change");
2025-03-15 09:37:07 +08:00
/* 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);
}
2025-04-27 14:46:47 +08:00
#endif
break;
/* 中间按键按下事件 */
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;
}
}
/**
* @brief
*
* IObutton_event_cb
*
* @param[in] button_num
*/
static void single_button_init(uint32_t button_num)
2025-03-08 14:45:49 +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-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-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 */
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);
}
/**
* @brief
*
* IO引脚状态
*/
void flux_button_init(void)
2025-03-08 14:45:49 +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
/* 初始化电源按键 */
single_button_init(BUTTON_PowerIn_IO_NUM);
2025-03-08 14:45:49 +08:00
/* 初始化按键事件组 */
g_lcd_state_event_group = xEventGroupCreate();
2025-03-15 09:37:07 +08:00
/* Set the default state to backlight on */
xEventGroupSetBits(g_lcd_state_event_group, LCD_STATE_BIT);
/* 初始化电源控制引脚 */
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);
}