#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/rmt.h"
#include "led_strip.h"
static const char *TAG = "example";
#define RMT_TX_CHANNEL RMT_CHANNEL_0
#define EXAMPLE_CHASE_SPEED_MS (10)
uint8_t RGB_dir = 0; // ws2812灯的位置
void app_main(void)
{
uint32_t red = 0;
uint32_t green = 255;
uint32_t blue = 255;
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL); //配置rmt_config_t结构体,初始化引脚和rmt通道
config.clk_div = 2; //将时钟设置为40MHZ
rmt_config(&config); //初始化
rmt_driver_install(config.channel, 0, 0); //安装驱动
//安装WS2812驱动
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(CONFIG_EXAMPLE_STRIP_LED_NUMBER, (led_strip_dev_t)config.channel);
led_strip_t *strip = led_strip_new_rmt_ws2812(&strip_config);
if (!strip)
{
ESP_LOGE(TAG, "install WS2812 driver failed");
}
strip->clear(strip, 100); //清楚ws2812灯
while (true)
{
strip->set_pixel(strip, RGB_dir, red, green, blue); //设置RGB值 第二个参数RGB_dir表示让第几个灯亮
//将RGB值更新到灯上
strip->refresh(strip, 100);
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
}
}
// hsv转rgb函数
void led_strip_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b)
{
h %= 360; // h -> [0,360]
uint32_t rgb_max = v * 2.55f;
uint32_t rgb_min = rgb_max * (100 - s) / 100.0f;
uint32_t i = h / 60;
uint32_t diff = h % 60;
// RGB adjustment amount by hue
uint32_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
switch (i)
{
case 0:
*r = rgb_max;
*g = rgb_min + rgb_adj;
*b = rgb_min;
break;
case 1:
*r = rgb_max - rgb_adj;
*g = rgb_max;
*b = rgb_min;
break;
case 2:
*r = rgb_min;
*g = rgb_max;
*b = rgb_min + rgb_adj;
break;
case 3:
*r = rgb_min;
*g = rgb_max - rgb_adj;
*b = rgb_max;
break;
case 4:
*r = rgb_min + rgb_adj;
*g = rgb_min;
*b = rgb_max;
break;
default:
*r = rgb_max;
*g = rgb_min;
*b = rgb_max - rgb_adj;
break;
}
}
【奈茶君】ESP32S3 IDF RMT驱动WS2812
于 2022-09-18 13:57:52 首次发布