// GPIO assignment #define LED_STRIP_BLINK_GPIO 4 // Numbers of the LED in the strip #define LED_STRIP_LED_NUMBERS 1 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) #define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)
led_strip_handle_tconfigure_led(void) { // LED strip general initialization, according to your led board design led_strip_config_t strip_config = { .strip_gpio_num = LED_STRIP_BLINK_GPIO, // The GPIO that connected to the LED strip's data line .max_leds = LED_STRIP_LED_NUMBERS, // The number of LEDs in the strip, .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip .led_model = LED_MODEL_WS2812, // LED strip model .flags.invert_out = false, // whether to invert the output signal };
// LED strip backend configuration: RMT led_strip_rmt_config_t rmt_config = { #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) .rmt_channel = 0, #else .clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption .resolution_hz = LED_STRIP_RMT_RES_HZ, // RMT counter clock frequency .flags.with_dma = false, // DMA feature is available on ESP target like ESP32-S3 #endif };
// LED Strip object handle led_strip_handle_t led_strip; ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); ESP_LOGI(TAG, "Created LED strip object with RMT backend"); return led_strip; }
while (1) { ESP_LOGI(TAG,"ws2812 test"); if (led_on_off) { /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */ for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) { ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, red, green, blue)); } /* Refresh the strip to send data */ ESP_ERROR_CHECK(led_strip_refresh(led_strip)); ESP_LOGI(TAG, "LED ON!"); } else { /* Set all LED off to clear all pixels */ ESP_ERROR_CHECK(led_strip_clear(led_strip)); ESP_LOGI(TAG, "LED OFF!"); }
led_strip_handle_tconfigure_led(void) { // LED strip general initialization, according to your led board design led_strip_config_t strip_config = { .strip_gpio_num = LED_STRIP_BLINK_GPIO, // The GPIO that connected to the LED strip's data line .max_leds = LED_STRIP_LED_NUMBERS, // The number of LEDs in the strip, .led_pixel_format = LED_PIXEL_FORMAT_GRB, // Pixel format of your LED strip .led_model = LED_MODEL_WS2812, // LED strip model .flags.invert_out = false, // whether to invert the output signal };
// LED strip backend configuration: RMT led_strip_rmt_config_t rmt_config = { #if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 0, 0) .rmt_channel = 0, #else .clk_src = RMT_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption .resolution_hz = LED_STRIP_RMT_RES_HZ, // RMT counter clock frequency .flags.with_dma = false, // DMA feature is available on ESP target like ESP32-S3 #endif };
// LED Strip object handle led_strip_handle_t led_strip; ESP_ERROR_CHECK(led_strip_new_rmt_device(&strip_config, &rmt_config, &led_strip)); ESP_LOGI(TAG, "Created LED strip object with RMT backend"); return led_strip; }
3.2 亮度控制
对于数字 LED,我们可以通过调整发送到 LED 的 RGB 值来控制亮度。降低 RGB 值将降低亮度。
将设置好的RGB值传递给led_strip_set_pixel()函数 led_strip_set_pixel 函数通常用于控制 LED 灯带中单个 LED 的颜色。这个函数是 ESP-IDF 中 LED 控制库的一部分,用于设置指定 LED 灯的颜色。 led_strip_flush 函数来更新 LED 灯带,以显示新颜色。 led_strip_clear 函数来清除led灯带,以关闭灯光
while (1) { ESP_LOGI(TAG,"ws2812 test"); if (led_on_off) { /* Set the LED pixel using RGB from 0 (0%) to 255 (100%) for each color */ for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) { ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, red, green, blue)); } /* Refresh the strip to send data */ ESP_ERROR_CHECK(led_strip_refresh(led_strip)); ESP_LOGI(TAG, "LED ON!"); } else { /* Set all LED off to clear all pixels */ ESP_ERROR_CHECK(led_strip_clear(led_strip)); ESP_LOGI(TAG, "LED OFF!"); }