182 lines
5.2 KiB
C
182 lines
5.2 KiB
C
#include <stdio.h>
|
|
#include "FluxButton.h"
|
|
#include "driver/gpio.h"
|
|
#include "FluxPower.h"
|
|
|
|
|
|
#include "ui.h"
|
|
|
|
static const char *BUTTON_TAG = "button_test";
|
|
|
|
/*使用事件组Bit0进行当前显示屏状态记录*/
|
|
#define LCD_STATE_BIT BIT0
|
|
|
|
/*显示屏状态记录*/
|
|
EventGroupHandle_t lcd_state_event_group;
|
|
|
|
|
|
static void button_event_cb(void *arg, void *data)
|
|
{
|
|
ESP_LOGI(BUTTON_TAG, "Button event %d", (int)data);
|
|
|
|
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
|
|
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
|
|
if (cause != ESP_SLEEP_WAKEUP_UNDEFINED) {
|
|
ESP_LOGI(BUTTON_TAG, "Wake up from light sleep, reason %d", cause);
|
|
}
|
|
#endif
|
|
|
|
|
|
/*根据输入按键事件进行事件处理*/
|
|
switch ((int)data)
|
|
{
|
|
case BUTTON_CMD_POWER_ON:
|
|
/*响应电源键按下事件*/
|
|
POWER_ON;
|
|
break;
|
|
case BUTTON_CMD_POWER_OFF:
|
|
ESP_LOGI(BUTTON_TAG, "LCD DoubleClick change");
|
|
POWER_OFF;
|
|
break;
|
|
case BUTTON_CMD_LCD_BACKLIGHT_CHANGE:
|
|
/*电源键单击事件*/
|
|
ESP_LOGI(BUTTON_TAG, "LCD backlight change");
|
|
/*根据当前背光状态进行背光开关*/
|
|
if (xEventGroupGetBits(lcd_state_event_group) & LCD_STATE_BIT) //如果当前背光状态为开启,则关闭
|
|
{
|
|
LCD_BACKLIGHT_CLOSE;
|
|
xEventGroupClearBits(lcd_state_event_group, LCD_STATE_BIT);
|
|
|
|
}else{
|
|
LCD_BACKLIGHT_OPEN;
|
|
xEventGroupSetBits(lcd_state_event_group, LCD_STATE_BIT);
|
|
}
|
|
|
|
break;
|
|
|
|
case BUTTON_CMD_CENTER_KEY:
|
|
/*开始测试按键*/
|
|
lv_event_send(ui_pageHome_buttonStartTest, LV_EVENT_CLICKED, NULL);
|
|
|
|
break;
|
|
case BUTTON_CMD_LEFT_KEY:
|
|
lv_event_send(ui_pageHome_buttonMinus, LV_EVENT_CLICKED, NULL);
|
|
break;
|
|
case BUTTON_CMD_RIGHT_KEY:
|
|
lv_event_send(ui_pageHome_buttonAdd, LV_EVENT_CLICKED, NULL);
|
|
break;
|
|
}
|
|
}
|
|
|
|
void button_init_func(uint32_t button_num)
|
|
{
|
|
uint8_t activeLevel = BUTTON_ACTIVE_LEVEL;
|
|
/*当按键为电源按键时修改激活状态为高电平*/
|
|
if (button_num == BUTTON_PowerIn_IO_NUM)
|
|
{
|
|
activeLevel = BUTTON_POWERIN_ACTIVE_LEVEL;
|
|
}
|
|
|
|
|
|
button_config_t btn_cfg = {
|
|
.type = BUTTON_TYPE_GPIO,
|
|
.gpio_button_config = {
|
|
.gpio_num = button_num,
|
|
.active_level = activeLevel,
|
|
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
|
|
.enable_power_save = true,
|
|
#endif
|
|
},
|
|
};
|
|
button_handle_t btn = iot_button_create(&btn_cfg);
|
|
assert(btn);
|
|
|
|
esp_err_t err = ESP_OK;
|
|
/*为电源键按下注册回调函数*/
|
|
if (button_num == BUTTON_PowerIn_IO_NUM)
|
|
{
|
|
/*双击电源键键进行关机*/
|
|
err |= iot_button_register_cb(btn, BUTTON_DOUBLE_CLICK, button_event_cb, (void *)BUTTON_CMD_POWER_OFF);
|
|
/*长按电源键进行开机*/
|
|
err |= iot_button_register_cb(btn, BUTTON_LONG_PRESS_HOLD, button_event_cb, (void *)BUTTON_CMD_POWER_ON);
|
|
/* 单击电源键进行显示屏背光状态切换 */
|
|
err |= iot_button_register_cb(btn, BUTTON_SINGLE_CLICK, button_event_cb, (void *)BUTTON_CMD_LCD_BACKLIGHT_CHANGE);
|
|
}else{
|
|
/*为下方三个按键 单击 注册回调函数*/
|
|
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);
|
|
}
|
|
|
|
/*初始化所有引脚*/
|
|
void button_init(void)
|
|
{
|
|
button_init_func(BUTTON_Center_IO_NUM);
|
|
button_init_func(BUTTON_Left_IO_NUM);
|
|
button_init_func(BUTTON_Right_IO_NUM);
|
|
|
|
button_init_func(BUTTON_PowerIn_IO_NUM);
|
|
|
|
/*初始化显示屏事件组*/
|
|
lcd_state_event_group = xEventGroupCreate();
|
|
|
|
/*设置默认状态为背光开启*/
|
|
xEventGroupSetBits(lcd_state_event_group, LCD_STATE_BIT);
|
|
|
|
/*在此处初始化电源控制IO输出*/
|
|
gpio_reset_pin(BUTTON_PowerOut_IO_NUM);
|
|
gpio_set_direction(BUTTON_PowerOut_IO_NUM, GPIO_MODE_OUTPUT);
|
|
}
|
|
|
|
|
|
#if CONFIG_GPIO_BUTTON_SUPPORT_POWER_SAVE
|
|
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
|
|
void power_save_init(void)
|
|
{
|
|
esp_pm_config_t pm_config = {
|
|
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
|
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
|
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
|
.light_sleep_enable = true
|
|
#endif
|
|
};
|
|
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
|
}
|
|
#else
|
|
void power_save_init(void)
|
|
{
|
|
#if CONFIG_IDF_TARGET_ESP32
|
|
esp_pm_config_esp32_t pm_config = {
|
|
#elif CONFIG_IDF_TARGET_ESP32S2
|
|
esp_pm_config_esp32s2_t pm_config = {
|
|
#elif CONFIG_IDF_TARGET_ESP32C3
|
|
esp_pm_config_esp32c3_t pm_config = {
|
|
#elif CONFIG_IDF_TARGET_ESP32S3
|
|
esp_pm_config_esp32s3_t pm_config = {
|
|
#elif CONFIG_IDF_TARGET_ESP32C2
|
|
esp_pm_config_esp32c2_t pm_config = {
|
|
#endif
|
|
.max_freq_mhz = CONFIG_EXAMPLE_MAX_CPU_FREQ_MHZ,
|
|
.min_freq_mhz = CONFIG_EXAMPLE_MIN_CPU_FREQ_MHZ,
|
|
#if CONFIG_FREERTOS_USE_TICKLESS_IDLE
|
|
.light_sleep_enable = true
|
|
#endif
|
|
};
|
|
ESP_ERROR_CHECK(esp_pm_configure(&pm_config));
|
|
}
|
|
#endif
|
|
#endif
|