Files
ESPDocs/public/exemplos/zh-CN/esp-iot-solution.c
T

73 lines
2.0 KiB
C

/*
* 面向 ESP32 的 ESP-IoT-Solution 示例
* 智能按键初始化,包含回调和显示屏/传感器驱动
*
* 依赖(idf_component.yml):
* espressif/button: "^3.3.0"
* espressif/led_indicator: "^1.2.0"
*/
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "iot_button.h"
#include "led_indicator.h"
static const char *TAG = "iot_solution_app";
#define BUTTON_GPIO_NUM 0 // 原生 BOOT 按键
#define LED_INDICATOR_GPIO 2 // 指示 LED
static led_indicator_handle_t led_handle = NULL;
/* 按键单击回调 */
static void button_single_click_cb(void *arg, void *data)
{
ESP_LOGI(TAG, "Evento: Clique Único detectado!");
led_indicator_start(led_handle, BLINK_DOUBLE_FAST);
}
/* 长按回调(> 3 秒) */
static void button_long_press_cb(void *arg, void *data)
{
ESP_LOGW(TAG, "Event: long press! Entering factory reset mode.");
led_indicator_start(led_handle, BLINK_BREATHE_FAST);
}
void app_main(void)
{
ESP_LOGI(TAG, "Starting ESP-IoT-Solution Component Suite");
// 1. LED Indicator 配置
led_indicator_gpio_config_t led_cfg = {
.is_active_level_high = true,
.gpio_num = LED_INDICATOR_GPIO,
};
led_handle = led_indicator_create(&led_cfg);
// 2. 按键配置,自动进行硬件/软件消抖
button_config_t btn_cfg = {
.type = BUTTON_TYPE_GPIO,
.long_press_time = 3000,
.short_press_time = 180,
.gpio_button_config = {
.gpio_num = BUTTON_GPIO_NUM,
.active_level = 0,
},
};
button_handle_t btn_handle = iot_button_create(&btn_cfg);
assert(btn_handle != NULL);
// 为不同手势注册回调
iot_button_register_cb(btn_handle, BUTTON_SINGLE_CLICK, button_single_click_cb, NULL);
iot_button_register_cb(btn_handle, BUTTON_LONG_PRESS_START, button_long_press_cb, NULL);
ESP_LOGI(TAG, "Drivers initialized! Press the button on GPIO 0.");
while (1) {
vTaskDelay(pdMS_TO_TICKS(1000));
}
}