蜂鸣器+RGB修复

This commit is contained in:
kirl committed 2026-08-01 15:03:02 +08:00
1 parent fef37b36e9
commit 351e22e123
7 files changed
+205 -1

No files matched your search

+10
View File
@@ -14,6 +14,7 @@
#include "esp_log.h"
#include "driver/rmt_tx.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include <stdlib.h>
static const char *TAG = "RGB";
@@ -145,17 +146,24 @@ static esp_err_t rmt_new_led_strip_encoder(rmt_encoder_handle_t *ret_encoder)
static rmt_channel_handle_t s_led_chan = NULL;
static rmt_encoder_handle_t s_led_encoder = NULL;
static SemaphoreHandle_t s_lock = NULL; /* 串行化 RMT 访问 */
/* ── 内部: 发送 GRB 数据 ── */
static void rgb_transmit(uint8_t g, uint8_t r, uint8_t b)
{
if (!s_led_chan || !s_lock) return;
uint8_t grb[3] = { g, r, b };
/* main 循环与 Cycle 执行 (_wait_flash) 会从不同任务调用本函数,
必须互斥, 否则并发操作同一 RMT 通道会导致绿灯丢失甚至复位 */
xSemaphoreTake(s_lock, portMAX_DELAY);
rmt_transmit_config_t tx_config = { .loop_count = 0 };
ESP_ERROR_CHECK(rmt_transmit(s_led_chan, s_led_encoder,
grb, sizeof(grb), &tx_config));
ESP_ERROR_CHECK(rmt_tx_wait_all_done(s_led_chan, portMAX_DELAY));
xSemaphoreGive(s_lock);
}
/* ── 公开接口 ── */
@@ -167,6 +175,8 @@ void rgb_init(int gpio)
return;
}
s_lock = xSemaphoreCreateMutex();
rmt_tx_channel_config_t tx_chan_config = {
.clk_src = RMT_CLK_SRC_DEFAULT,
.gpio_num = gpio,