界面实现,然后称重还有问题,灯改为手动控制
This commit is contained in:
@@ -2,7 +2,38 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void ws2812_start_task(void);
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
|
||||
// 预设颜色枚举
|
||||
typedef enum {
|
||||
WS2812_COLOR_RED = 0,
|
||||
WS2812_COLOR_GREEN,
|
||||
WS2812_COLOR_BLUE,
|
||||
WS2812_COLOR_YELLOW,
|
||||
WS2812_COLOR_CYAN,
|
||||
WS2812_COLOR_MAGENTA,
|
||||
WS2812_COLOR_WHITE,
|
||||
WS2812_COLOR_ORANGE,
|
||||
WS2812_COLOR_PURPLE,
|
||||
WS2812_COLOR_PINK,
|
||||
WS2812_COLOR_LIME,
|
||||
WS2812_COLOR_SKYBLUE,
|
||||
WS2812_COLOR_OFF,
|
||||
WS2812_COLOR_MAX
|
||||
} ws2812_color_t;
|
||||
|
||||
// 初始化 LED(调用一次)
|
||||
esp_err_t ws2812_init(void);
|
||||
|
||||
// 设置 RGB 颜色(0-255)
|
||||
esp_err_t ws2812_set_color(uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
// 使用预设颜色快速设置
|
||||
esp_err_t ws2812_set_preset_color(ws2812_color_t color);
|
||||
|
||||
// 熄灭 LED
|
||||
esp_err_t ws2812_off(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <string.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -10,6 +12,55 @@ extern "C" {
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
// 提前声明 ws2812_set_color,避免隐式声明
|
||||
esp_err_t ws2812_set_color(uint8_t r, uint8_t g, uint8_t b);
|
||||
|
||||
// 预设颜色枚举
|
||||
typedef enum {
|
||||
WS2812_COLOR_RED = 0,
|
||||
WS2812_COLOR_GREEN,
|
||||
WS2812_COLOR_BLUE,
|
||||
WS2812_COLOR_YELLOW,
|
||||
WS2812_COLOR_CYAN,
|
||||
WS2812_COLOR_MAGENTA,
|
||||
WS2812_COLOR_WHITE,
|
||||
WS2812_COLOR_ORANGE,
|
||||
WS2812_COLOR_PURPLE,
|
||||
WS2812_COLOR_PINK,
|
||||
WS2812_COLOR_LIME,
|
||||
WS2812_COLOR_SKYBLUE,
|
||||
WS2812_COLOR_OFF,
|
||||
WS2812_COLOR_MAX
|
||||
} ws2812_color_t;
|
||||
|
||||
// 预设颜色表(GRB 顺序)
|
||||
static const uint8_t ws2812_color_table[WS2812_COLOR_MAX][3] = {
|
||||
{0, 50, 0}, // RED (G,R,B)
|
||||
{50, 0, 0}, // GREEN
|
||||
{0, 0, 50}, // BLUE
|
||||
{50, 50, 0}, // YELLOW
|
||||
{50, 0, 50}, // CYAN
|
||||
{0, 50, 50}, // MAGENTA
|
||||
{50, 50, 50}, // WHITE
|
||||
{25, 50, 0}, // ORANGE
|
||||
{25, 0, 50}, // PURPLE
|
||||
{0, 25, 50}, // PINK
|
||||
{50, 25, 0}, // LIME
|
||||
{50, 0, 25}, // SKYBLUE
|
||||
{0, 0, 0}, // OFF
|
||||
};
|
||||
|
||||
// 快捷设置预设颜色接口
|
||||
esp_err_t ws2812_set_preset_color(ws2812_color_t color)
|
||||
{
|
||||
if (color < 0 || color >= WS2812_COLOR_MAX) {
|
||||
return ESP_ERR_INVALID_ARG;
|
||||
}
|
||||
const uint8_t *grb = ws2812_color_table[color];
|
||||
// 注意 GRB 顺序
|
||||
return ws2812_set_color(grb[1], grb[0], grb[2]);
|
||||
}
|
||||
|
||||
static const char *TAG = "led_strip";
|
||||
|
||||
// --- 针对 ESP32-C3 和单颗 LED 的配置 ---
|
||||
@@ -30,8 +81,14 @@ static const char *TAG = "led_strip";
|
||||
// 内存块大小:单颗 LED 需要的数据非常少,设为 0 让驱动自动分配最合适的内存
|
||||
#define LED_STRIP_MEMORY_BLOCK_WORDS 0
|
||||
|
||||
led_strip_handle_t configure_led(void)
|
||||
|
||||
static led_strip_handle_t s_led_strip = NULL;
|
||||
|
||||
esp_err_t ws2812_init(void)
|
||||
{
|
||||
if (s_led_strip) {
|
||||
return ESP_OK; // 已初始化
|
||||
}
|
||||
// 1. LED 灯带通用配置
|
||||
led_strip_config_t strip_config = {
|
||||
.strip_gpio_num = LED_STRIP_GPIO_PIN, // GPIO 0
|
||||
@@ -54,64 +111,45 @@ led_strip_handle_t configure_led(void)
|
||||
};
|
||||
|
||||
// 3. 创建 LED 灯带对象
|
||||
led_strip_handle_t led_strip;
|
||||
// 初始化 RMT 设备
|
||||
esp_err_t ret = led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip);
|
||||
esp_err_t ret = led_strip_new_rmt_device(&strip_config, &rmt_config, &s_led_strip);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to create LED strip object: %s", esp_err_to_name(ret));
|
||||
while (1); // 初始化失败则卡死
|
||||
s_led_strip = NULL;
|
||||
return ret;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Created LED strip object with RMT backend on GPIO %d", LED_STRIP_GPIO_PIN);
|
||||
return led_strip;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void ws2812_blink_task(void *pvParameters)
|
||||
esp_err_t ws2812_set_color(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
(void)pvParameters;
|
||||
|
||||
// 初始化 LED 灯带
|
||||
led_strip_handle_t led_strip = configure_led();
|
||||
uint8_t r = 0, g = 0, b = 0;
|
||||
int color_state = 0; // 0:红, 1:绿, 2:蓝
|
||||
|
||||
ESP_LOGI(TAG, "Start RGB cycling");
|
||||
|
||||
while (1) {
|
||||
// 根据状态设置颜色
|
||||
if (color_state == 0) {
|
||||
r = 50; g = 0; b = 0; // 红色
|
||||
ESP_LOGI(TAG, "Color: RED");
|
||||
} else if (color_state == 1) {
|
||||
r = 0; g = 50; b = 0; // 绿色
|
||||
ESP_LOGI(TAG, "Color: GREEN");
|
||||
} else {
|
||||
r = 0; g = 0; b = 50; // 蓝色
|
||||
ESP_LOGI(TAG, "Color: BLUE");
|
||||
}
|
||||
|
||||
// 设置像素 (索引0, R, G, B)
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, 0, r, g, b));
|
||||
|
||||
// 刷新显示
|
||||
ESP_ERROR_CHECK(led_strip_refresh(led_strip));
|
||||
|
||||
// 更新状态,0->1->2->0...
|
||||
color_state = (color_state + 1) % 3;
|
||||
|
||||
// 延时 500ms
|
||||
vTaskDelay(pdMS_TO_TICKS(1600));
|
||||
if (!s_led_strip) {
|
||||
ESP_LOGE(TAG, "LED strip not initialized");
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
esp_err_t ret = led_strip_set_pixel(s_led_strip, 0, r, g, b);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to set pixel: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
ret = led_strip_refresh(s_led_strip);
|
||||
if (ret != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to refresh strip: %s", esp_err_to_name(ret));
|
||||
return ret;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void ws2812_start_task(void)
|
||||
// 熄灭 LED(快捷接口)
|
||||
esp_err_t ws2812_off(void)
|
||||
{
|
||||
BaseType_t ret = xTaskCreate(ws2812_blink_task, "ws2812_blink_task", 4096, NULL, 5, NULL);
|
||||
if (ret != pdPASS) {
|
||||
ESP_LOGE(TAG, "Failed to create ws2812 blink task");
|
||||
}
|
||||
return ws2812_set_color(0, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user