增加button支持
This commit is contained in:
parent
89a006ec80
commit
e674769b97
51
main/main.c
51
main/main.c
@ -41,11 +41,12 @@
|
|||||||
#include "esp_flash.h"
|
#include "esp_flash.h"
|
||||||
#include "esp_lcd_st7735.h"
|
#include "esp_lcd_st7735.h"
|
||||||
|
|
||||||
#include "lvgl.h"
|
#include "button_gpio.h"
|
||||||
|
#include "iot_button.h"
|
||||||
|
|
||||||
static const char *TAG = "main";
|
static const char *TAG = "main";
|
||||||
|
|
||||||
|
#define BUTTON_ACTIVE_LEVEL 0
|
||||||
|
|
||||||
uint8_t* load_html2Ram(wifi_html_t html)
|
uint8_t* load_html2Ram(wifi_html_t html)
|
||||||
{
|
{
|
||||||
@ -62,6 +63,51 @@ uint8_t* load_html2Ram(wifi_html_t html)
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define BUTTON_COUNT 3 // 假设有 3 个按钮
|
||||||
|
static button_handle_t buttons[BUTTON_COUNT]; // 存储按钮句柄
|
||||||
|
static const uint32_t button_pins[BUTTON_COUNT] = {GPIO_NUM_0, GPIO_NUM_40, GPIO_NUM_41}; // 按钮 GPIO
|
||||||
|
|
||||||
|
|
||||||
|
void button_event_cb(void *arg, void *usr_data)
|
||||||
|
{
|
||||||
|
|
||||||
|
iot_button_print_event((button_handle_t)arg);
|
||||||
|
|
||||||
|
printf("Button %ld\n", btn_idx);
|
||||||
|
uint32_t btn_idx = (uint32_t)usr_data; // 获取按钮索引
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void button_init_all()
|
||||||
|
{
|
||||||
|
for (int i = 0; i < BUTTON_COUNT; i++) {
|
||||||
|
button_config_t btn_cfg;
|
||||||
|
memset(&btn_cfg, 0, sizeof(button_config_t));
|
||||||
|
|
||||||
|
button_gpio_config_t gpio_cfg = {
|
||||||
|
.gpio_num = button_pins[i],
|
||||||
|
.active_level = BUTTON_ACTIVE_LEVEL,
|
||||||
|
.enable_power_save = true,
|
||||||
|
};
|
||||||
|
|
||||||
|
esp_err_t ret = iot_button_new_gpio_device(&btn_cfg, &gpio_cfg, &buttons[i]);
|
||||||
|
assert(ret == ESP_OK);
|
||||||
|
|
||||||
|
// 绑定事件回调,并传递按钮索引
|
||||||
|
ret = iot_button_register_cb(buttons[i], BUTTON_PRESS_DOWN, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_UP, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_REPEAT, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_SINGLE_CLICK, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_DOUBLE_CLICK, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_START, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_HOLD, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_LONG_PRESS_UP, NULL, button_event_cb, (void *)i);
|
||||||
|
ret |= iot_button_register_cb(buttons[i], BUTTON_PRESS_END, NULL, button_event_cb, (void *)i);
|
||||||
|
|
||||||
|
ESP_ERROR_CHECK(ret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
extern uint8_t* home_html_data;
|
extern uint8_t* home_html_data;
|
||||||
stream_data_t wifi_interface_data;
|
stream_data_t wifi_interface_data;
|
||||||
stream_data_t uart_interface_data;
|
stream_data_t uart_interface_data;
|
||||||
@ -101,6 +147,7 @@ void app_main(void)
|
|||||||
// .pull_up_en = 1
|
// .pull_up_en = 1
|
||||||
// };
|
// };
|
||||||
// gpio_config(&input_config);
|
// gpio_config(&input_config);
|
||||||
|
button_init_all();
|
||||||
|
|
||||||
#if CONFIG_IDF_TARGET_ESP32C3
|
#if CONFIG_IDF_TARGET_ESP32C3
|
||||||
usb_serial2Uart(NULL);
|
usb_serial2Uart(NULL);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user