142 lines
4.5 KiB
C
142 lines
4.5 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";
|
|
|
|
/* Use event group Bit0 to record the current display state */
|
|
#define LCD_STATE_BIT BIT0
|
|
|
|
/* Display state recording */
|
|
EventGroupHandle_t lcd_state_event_group;
|
|
|
|
|
|
static void button_event_cb(void *arg, void *data)
|
|
{
|
|
ESP_LOGI(BUTTON_TAG, "Button event %d", (int)data);
|
|
|
|
/* Handle events based on input button events */
|
|
switch ((int)data)
|
|
{
|
|
case BUTTON_CMD_POWER_ON:
|
|
/* Respond to power button press event */
|
|
POWER_ON;
|
|
break;
|
|
case BUTTON_CMD_POWER_OFF:
|
|
ESP_LOGI(BUTTON_TAG, "LCD DoubleClick change");
|
|
POWER_OFF;
|
|
break;
|
|
case BUTTON_CMD_LCD_BACKLIGHT_CHANGE:
|
|
/* Power button single click event */
|
|
ESP_LOGI(BUTTON_TAG, "LCD backlight change");
|
|
/* Toggle backlight based on current backlight state */
|
|
if (xEventGroupGetBits(lcd_state_event_group) & LCD_STATE_BIT) // If the current backlight state is on, turn it off
|
|
{
|
|
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:
|
|
if (lv_scr_act()==ui_pageHome)
|
|
{ /* Start test button */
|
|
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);
|
|
}
|
|
break;
|
|
case BUTTON_CMD_LEFT_KEY:
|
|
if (lv_scr_act()==ui_pageHome)
|
|
{
|
|
lv_event_send(ui_pageHome_buttonMinus, LV_EVENT_CLICKED, NULL);
|
|
}
|
|
break;
|
|
case BUTTON_CMD_RIGHT_KEY:
|
|
if (lv_scr_act()==ui_pageHome)
|
|
{
|
|
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;
|
|
/* Modify the activation state to high level when the button is the power button */
|
|
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,
|
|
},
|
|
};
|
|
button_handle_t btn = iot_button_create(&btn_cfg);
|
|
assert(btn);
|
|
|
|
esp_err_t err = ESP_OK;
|
|
/* Register callback function for power button press */
|
|
if (button_num == BUTTON_PowerIn_IO_NUM)
|
|
{
|
|
/* 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);
|
|
}else{
|
|
/* Register callback functions for single clicks on the three buttons below */
|
|
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);
|
|
}
|
|
|
|
/* Initialize all pins */
|
|
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);
|
|
|
|
/* Initialize the display event group */
|
|
lcd_state_event_group = xEventGroupCreate();
|
|
|
|
/* Set the default state to backlight on */
|
|
xEventGroupSetBits(lcd_state_event_group, LCD_STATE_BIT);
|
|
|
|
/* Initialize the power control IO output here */
|
|
gpio_reset_pin(BUTTON_PowerOut_IO_NUM);
|
|
gpio_set_direction(BUTTON_PowerOut_IO_NUM, GPIO_MODE_OUTPUT);
|
|
}
|
|
|