smart-home/ziyuan/simple_led_test.cpp
2026-02-26 09:16:34 +08:00

79 lines
2.4 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 简单LED测试程序 - 独立运行
#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#define TAG "SIMPLE_LED_TEST"
extern "C" void app_main() {
ESP_LOGI(TAG, "简单LED测试程序启动");
// 测试引脚数组
gpio_num_t test_pins[] = {
GPIO_NUM_42, GPIO_NUM_41, GPIO_NUM_40, GPIO_NUM_38,
GPIO_NUM_35, GPIO_NUM_34, GPIO_NUM_33, GPIO_NUM_32, GPIO_NUM_31
};
const char* pin_names[] = {
"GPIO42", "GPIO41", "GPIO40", "GPIO38",
"GPIO35", "GPIO34", "GPIO33", "GPIO32", "GPIO31"
};
int num_pins = sizeof(test_pins) / sizeof(test_pins[0]);
// 初始化所有测试引脚
for (int i = 0; i < num_pins; i++) {
gpio_config_t io_conf = {};
io_conf.intr_type = GPIO_INTR_DISABLE;
io_conf.mode = GPIO_MODE_OUTPUT;
io_conf.pin_bit_mask = (1ULL << test_pins[i]);
io_conf.pull_down_en = GPIO_PULLDOWN_DISABLE;
io_conf.pull_up_en = GPIO_PULLUP_DISABLE;
esp_err_t ret = gpio_config(&io_conf);
if (ret == ESP_OK) {
gpio_set_level(test_pins[i], 0);
ESP_LOGI(TAG, "✅ %s 初始化成功", pin_names[i]);
} else {
ESP_LOGE(TAG, "❌ %s 初始化失败: %s", pin_names[i], esp_err_to_name(ret));
}
vTaskDelay(pdMS_TO_TICKS(100)); // 每个引脚初始化后延时
}
ESP_LOGI(TAG, "开始LED测试循环...");
int current_pin = 0;
while (true) {
// 关闭所有LED
for (int i = 0; i < num_pins; i++) {
gpio_set_level(test_pins[i], 0);
}
// 点亮当前LED
ESP_LOGI(TAG, "🔴 点亮 %s (引脚%d) - 持续3秒",
pin_names[current_pin], test_pins[current_pin]);
gpio_set_level(test_pins[current_pin], 1);
// 等待3秒
vTaskDelay(pdMS_TO_TICKS(3000));
// 关闭当前LED
gpio_set_level(test_pins[current_pin], 0);
ESP_LOGI(TAG, "⚫ 关闭 %s", pin_names[current_pin]);
// 移到下一个引脚
current_pin = (current_pin + 1) % num_pins;
// 间隔1秒
vTaskDelay(pdMS_TO_TICKS(1000));
// 每轮循环后暂停
if (current_pin == 0) {
ESP_LOGI(TAG, "⏸️ 一轮测试完成等待3秒后开始下一轮...");
vTaskDelay(pdMS_TO_TICKS(3000));
}
}
}