安信可ESP-12K模组学习心得 ③ 安信可 ESP-12K 驱动 WS2812B 灯板,实现五彩斑斓的灯光效果;

一 前言

     今天为大家带来的是 安信可 ESP-12K 驱动WS2812B 灯板;


二 材料

  • 安信可 ESP-12K 开发板 x1
  • WS2812B 灯板一个,
  • 杜邦线若干
  • 接线图:
ESP-12KWS2812B
5V5V
GndGnd
GPIO45Din

在这里插入图片描述


三 效果

    头文件定义好对应的GPIO以及要驱动灯板的LED数量:

#define WS2812B_RMT_LED_NUMBER 24  //24个WS2812B灯珠
#define WS2812B_RMT_LED_GPIO 45  //对应的是 GPIO45

3.1 三色循环

  //三色循环
  uint8_t nums = 2;
  while (nums--)
  {
    ws2812_ret->set_rgb(ws2812_ret, 100, 255, 0, 0);
    vTaskDelay((1000 / portTICK_RATE_MS));
    ws2812_ret->set_rgb(ws2812_ret, 100, 0, 255, 0);
    vTaskDelay((1000 / portTICK_RATE_MS));
    ws2812_ret->set_rgb(ws2812_ret, 100, 0, 0, 255);
    vTaskDelay((1000 / portTICK_RATE_MS));
  }

3.2 逐渐亮起

/**
 * @description: 一个一个逐渐亮起
 * @param {type} 
 * @return: 
 */
void TaskWS2812OneByOne(void *p)
{
  uint8_t nums = 5;

  while (nums--)
  {
    for (int j = 0; j < WS2812B_RMT_LED_NUMBER; j++)
    {
      // Build RGB values
      hue = j * 360 / WS2812B_RMT_LED_NUMBER + start_rgb;
      led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
      // Write RGB values to strip driver
      ESP_ERROR_CHECK(ws2812_ret->set_pixel(ws2812_ret, j, red, green, blue));
      ESP_ERROR_CHECK(ws2812_ret->refresh(ws2812_ret, 100));
      vTaskDelay(pdMS_TO_TICKS(500));
    }

    ws2812_ret->clear(ws2812_ret, 50);
    vTaskDelay(pdMS_TO_TICKS(1000));
  }
  xTaskCreate(TaskWS2812Ranbow, "TaskWS2812Ranbow", 1024 * 2, NULL, 8, NULL);
  vTaskDelete(NULL);
}

3.3 彩虹效果

/**
 * @description: 彩虹效果
 * @param {type} 
 * @return: 
 */
void TaskWS2812Ranbow(void *p)
{

  while (true)
  {
    for (int i = 0; i < 3; i++)
    {
      for (int j = i; j < 24; j += 3)
      {
        // Build RGB values
        hue = j * 360 / 24 + start_rgb;
        led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
        // Write RGB values to strip driver
        ESP_ERROR_CHECK(ws2812_ret->set_pixel(ws2812_ret, j, red, green, blue));
      }

      // Flush RGB values to LEDs
      ESP_ERROR_CHECK(ws2812_ret->refresh(ws2812_ret, 100));
      vTaskDelay(pdMS_TO_TICKS(10));
      ws2812_ret->clear(ws2812_ret, 50);
      vTaskDelay(pdMS_TO_TICKS(10));
    }
    start_rgb += 60;
  }
}