RMT入门WS2812(基于ESP-IDF)

主要参考资料:
ESP32使用外设RMT控制WS2812灯条: https://blog.csdn.net/tian_milk/article/details/123585610
红外遥控 (RMT)链接: https://docs.espressif.com/projects/esp-idf/zh_CN/latest/esp32/api-reference/peripherals/rmt.html#

代码

1.1 RMT初始化

对RMT的初始化需要设置 rmt_config_t 中的相关参数,根据官方的说明:
在这里插入图片描述
填好相关参数后,需要调用函数 rmt_config()​​​​​​ 以使初始化参数有效和函数 rmt_driver_install() 安装驱动。使用方法:

// 使用默认参数填入rmt_config_t结构体
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL);
// 将时钟设置为40MHz
config.clk_div = 2;
 
// 调用两个函数完成初始化
ESP_ERROR_CHECK(rmt_config(&config));
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));

1.2 WS2812初始化

对WS2812进行初始化:

// install ws2812 driver 安装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");
}
// Clear LED strip (turn off all LEDs)关闭所有ws2812
ESP_ERROR_CHECK(strip->clear(strip, 100));

根据 led_strip_config_t 的定义,该结构体的第一个参数为WS2812灯的数量,第二个参数为RMT的通道值。

/**
* @brief LED Strip Configuration Type
*
*/
typedef struct {
    uint32_t max_leds;   /*!< Maximum LEDs in a single strip */
    led_strip_dev_t dev; /*!< LED strip device (e.g. RMT channel, PWM channel, etc) */
} led_strip_config_t;

1.3 点亮WS2812

led_strip_s 结构体使用了指向函数的指针来模拟面向对象的使用方法,将几个函数放在了结构体中。

使用 led_strip_s 结构体中的 set_pixel 函数设置各个灯珠的8位RGB值。使用该函数逐一设置每个灯珠的值,灯珠的位置从0开始计数,每调用一次设置一个灯珠。

设置好灯珠颜色后,调用 refresh 函数将颜色更新到灯条上。
下面是头文件里的描述。

/**
* @brief Set RGB for a specific pixel 设置特定灯珠的RGB值
*
* @param strip: LED strip WS2812的结构体
* @param index: index of pixel to set 需要设置的灯的位置下标,从0开始
* @param red: red part of color 红色的值
* @param green: green part of color 绿色的值
* @param blue: blue part of color 蓝色的值
*
* @return
*      - ESP_OK: Set RGB for a specific pixel successfully
*      - ESP_ERR_INVALID_ARG: Set RGB for a specific pixel failed because of invalid parameters
*      - ESP_FAIL: Set RGB for a specific pixel failed because other error occurred
*/
esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);
 
/**
* @brief Refresh memory colors to LEDs 用内存中的颜色更新LEDs
*
* @param strip: LED strip WS2812的结构体
* @param timeout_ms: timeout value for refreshing task 更新任务超时的值
*
* @return
*      - ESP_OK: Refresh successfully
*      - ESP_ERR_TIMEOUT: Refresh failed because of timeout
*      - ESP_FAIL: Refresh failed because some other error occurred
*
* @note:
*      After updating the LED colors in the memory, a following invocation of this API is needed to flush colors to strip.
*      在更新内存中的LED颜色值后,需要调用该API将颜色更新到灯条上。
*/
esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);

使用方法:

uint32_t Green=100, Red=100, Blue=100;
for(int j=0; j < LED_Number; j ++){
    // 设置ws2812的RGB的值
    ESP_ERROR_CHECK(strip->set_pixel(strip, j, Red, Green, Blue));
}
 
// 给WS2812发送RGB的值
ESP_ERROR_CHECK(strip->refresh(strip, 100));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值