Move the LED strip example to C++ and split the driver into a WS2812 class (ws2812.h/.cpp). The class takes the GPIO and LED count in its constructor and provides set(), fill(), write(), setBrightness() and a static hsv2rgb(). The RMT encoder is now internal to ws2812.cpp. Also adds a top-level Makefile wrapping idf.py for Linux, macOS and Windows, and a .gitignore for build output. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
43 lines
1.3 KiB
C++
43 lines
1.3 KiB
C++
/*
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
|
*
|
|
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
|
*/
|
|
#include <cstdint>
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include "esp_log.h"
|
|
#include "driver/gpio.h"
|
|
#include "ws2812.h"
|
|
|
|
#define RMT_LED_STRIP_GPIO_NUM GPIO_NUM_10
|
|
#define EXAMPLE_LED_NUMBERS 119
|
|
#define EXAMPLE_CHASE_SPEED_MS 1000
|
|
|
|
static const char *TAG = "example";
|
|
|
|
extern "C" void app_main(void)
|
|
{
|
|
ESP_LOGI(TAG, "Create LED strip");
|
|
WS2812 strip(RMT_LED_STRIP_GPIO_NUM, EXAMPLE_LED_NUMBERS);
|
|
|
|
ESP_LOGI(TAG, "Start LED rainbow chase");
|
|
uint16_t start_rgb = 0;
|
|
strip.setBrightness(16);
|
|
while (true) {
|
|
for (size_t i = 0; i < 3; i++) {
|
|
for (size_t j = i; j < strip.size(); j += 3) {
|
|
// Build RGB pixels
|
|
uint32_t hue = (j * 360 / strip.size() + start_rgb) % 360;
|
|
uint32_t red = 0, green = 0, blue = 0;
|
|
WS2812::hsv2rgb(hue, 100, 100, &red, &green, &blue);
|
|
strip.set(j, red, green, blue);
|
|
}
|
|
// Flush RGB values to LEDs
|
|
ESP_ERROR_CHECK(strip.write());
|
|
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
|
|
}
|
|
start_rgb += 60;
|
|
}
|
|
}
|