init
This commit is contained in:
6
examples/led_strip_rmt_ws2812/CMakeLists.txt
Normal file
6
examples/led_strip_rmt_ws2812/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(COMPONENTS main)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(led_strip_rmt_ws2812)
|
||||
31
examples/led_strip_rmt_ws2812/README.md
Normal file
31
examples/led_strip_rmt_ws2812/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# LED Strip Example (RMT backend + WS2812)
|
||||
|
||||
This example demonstrates how to blink the WS2812 LED using the [led_strip](https://components.espressif.com/component/espressif/led_strip) component.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with Espressif SoC
|
||||
* A USB cable for Power supply and programming
|
||||
* WS2812 LED strip
|
||||
|
||||
### Configure the Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`. Then assign the proper GPIO in the [source file](main/led_strip_rmt_ws2812_main.c). If your led strip has multiple LEDs, don't forget update the number.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT build flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
```text
|
||||
I (299) gpio: GPIO[8]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (309) example: Created LED strip object with RMT backend
|
||||
I (309) example: Start blinking LED strip
|
||||
```
|
||||
2
examples/led_strip_rmt_ws2812/main/CMakeLists.txt
Normal file
2
examples/led_strip_rmt_ws2812/main/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "led_strip_rmt_ws2812_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
3
examples/led_strip_rmt_ws2812/main/idf_component.yml
Normal file
3
examples/led_strip_rmt_ws2812/main/idf_component.yml
Normal file
@ -0,0 +1,3 @@
|
||||
dependencies:
|
||||
espressif/led_strip:
|
||||
version: ^3
|
||||
@ -0,0 +1,89 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "led_strip.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
// Set to 1 to use DMA for driving the LED strip, 0 otherwise
|
||||
// Please note the RMT DMA feature is only available on chips e.g. ESP32-S3/P4
|
||||
#define LED_STRIP_USE_DMA 0
|
||||
|
||||
#if LED_STRIP_USE_DMA
|
||||
// Numbers of the LED in the strip
|
||||
#define LED_STRIP_LED_COUNT 256
|
||||
#define LED_STRIP_MEMORY_BLOCK_WORDS 1024 // this determines the DMA block size
|
||||
#else
|
||||
// Numbers of the LED in the strip
|
||||
#define LED_STRIP_LED_COUNT 24
|
||||
#define LED_STRIP_MEMORY_BLOCK_WORDS 0 // let the driver choose a proper memory block size automatically
|
||||
#endif // LED_STRIP_USE_DMA
|
||||
|
||||
// GPIO assignment
|
||||
#define LED_STRIP_GPIO_PIN 2
|
||||
|
||||
// 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution)
|
||||
#define LED_STRIP_RMT_RES_HZ (10 * 1000 * 1000)
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
led_strip_handle_t configure_led(void)
|
||||
{
|
||||
// LED strip general initialization, according to your led board design
|
||||
led_strip_config_t strip_config = {
|
||||
.strip_gpio_num = LED_STRIP_GPIO_PIN, // The GPIO that connected to the LED strip's data line
|
||||
.max_leds = LED_STRIP_LED_COUNT, // The number of LEDs in the strip,
|
||||
.led_model = LED_MODEL_WS2812, // LED strip model
|
||||
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB, // The color order of the strip: GRB
|
||||
.flags = {
|
||||
.invert_out = false, // don't invert the output signal
|
||||
}
|
||||
};
|
||||
|
||||
// LED strip backend configuration: RMT
|
||||
led_strip_rmt_config_t rmt_config = {
|
||||
.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
|
||||
.mem_block_symbols = LED_STRIP_MEMORY_BLOCK_WORDS, // the memory block size used by the RMT channel
|
||||
.flags = {
|
||||
.with_dma = LED_STRIP_USE_DMA, // Using DMA can improve performance when driving more LEDs
|
||||
}
|
||||
};
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
led_strip_handle_t led_strip = configure_led();
|
||||
bool led_on_off = false;
|
||||
|
||||
ESP_LOGI(TAG, "Start blinking LED strip");
|
||||
while (1) {
|
||||
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_COUNT; i++) {
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, 5, 5, 5));
|
||||
}
|
||||
/* 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_on_off = !led_on_off;
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
}
|
||||
6
examples/led_strip_spi_ws2812/CMakeLists.txt
Normal file
6
examples/led_strip_spi_ws2812/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
cmake_minimum_required(VERSION 3.16)
|
||||
|
||||
set(COMPONENTS main)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(led_strip_spi_ws2812)
|
||||
31
examples/led_strip_spi_ws2812/README.md
Normal file
31
examples/led_strip_spi_ws2812/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# LED Strip Example (SPI backend + WS2812)
|
||||
|
||||
This example demonstrates how to blink the WS2812 LED using the [led_strip](https://components.espressif.com/component/espressif/led_strip) component.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with Espressif SoC
|
||||
* A USB cable for Power supply and programming
|
||||
* WS2812 LED strip
|
||||
|
||||
### Configure the Example
|
||||
|
||||
Before project configuration and build, be sure to set the correct chip target using `idf.py set-target <chip_name>`. Then assign the proper GPIO in the [source file](main/led_strip_spi_ws2812_main.c). If your led strip has multiple LEDs, don't forget update the number.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT build flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
```text
|
||||
I (299) gpio: GPIO[14]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
|
||||
I (309) example: Created LED strip object with SPI backend
|
||||
I (309) example: Start blinking LED strip
|
||||
```
|
||||
2
examples/led_strip_spi_ws2812/main/CMakeLists.txt
Normal file
2
examples/led_strip_spi_ws2812/main/CMakeLists.txt
Normal file
@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "led_strip_spi_ws2812_main.c"
|
||||
INCLUDE_DIRS ".")
|
||||
4
examples/led_strip_spi_ws2812/main/idf_component.yml
Normal file
4
examples/led_strip_spi_ws2812/main/idf_component.yml
Normal file
@ -0,0 +1,4 @@
|
||||
dependencies:
|
||||
espressif/led_strip:
|
||||
version: ^3
|
||||
idf: '>=5.1'
|
||||
@ -0,0 +1,81 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Unlicense OR CC0-1.0
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "led_strip.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
|
||||
// GPIO assignment
|
||||
#define LED_STRIP_GPIO_PIN 2
|
||||
// Numbers of the LED in the strip
|
||||
#define LED_STRIP_LED_COUNT 24
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
led_strip_handle_t configure_led(void)
|
||||
{
|
||||
// LED strip general initialization, according to your led board design
|
||||
led_strip_config_t strip_config = {
|
||||
.strip_gpio_num = LED_STRIP_GPIO_PIN, // The GPIO that connected to the LED strip's data line
|
||||
.max_leds = LED_STRIP_LED_COUNT, // The number of LEDs in the strip,
|
||||
.led_model = LED_MODEL_WS2812, // LED strip model
|
||||
// set the color order of the strip: GRB
|
||||
.color_component_format = {
|
||||
.format = {
|
||||
.r_pos = 1, // red is the second byte in the color data
|
||||
.g_pos = 0, // green is the first byte in the color data
|
||||
.b_pos = 2, // blue is the third byte in the color data
|
||||
.num_components = 3, // total 3 color components
|
||||
},
|
||||
},
|
||||
.flags = {
|
||||
.invert_out = false, // don't invert the output signal
|
||||
}
|
||||
};
|
||||
|
||||
// LED strip backend configuration: SPI
|
||||
led_strip_spi_config_t spi_config = {
|
||||
.clk_src = SPI_CLK_SRC_DEFAULT, // different clock source can lead to different power consumption
|
||||
.spi_bus = SPI2_HOST, // SPI bus ID
|
||||
.flags = {
|
||||
.with_dma = true, // Using DMA can improve performance and help drive more LEDs
|
||||
}
|
||||
};
|
||||
|
||||
// LED Strip object handle
|
||||
led_strip_handle_t led_strip;
|
||||
ESP_ERROR_CHECK(led_strip_new_spi_device(&strip_config, &spi_config, &led_strip));
|
||||
ESP_LOGI(TAG, "Created LED strip object with SPI backend");
|
||||
return led_strip;
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
led_strip_handle_t led_strip = configure_led();
|
||||
bool led_on_off = false;
|
||||
|
||||
ESP_LOGI(TAG, "Start blinking LED strip");
|
||||
while (1) {
|
||||
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_COUNT; i++) {
|
||||
ESP_ERROR_CHECK(led_strip_set_pixel(led_strip, i, 5, 5, 5));
|
||||
}
|
||||
/* 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_on_off = !led_on_off;
|
||||
vTaskDelay(pdMS_TO_TICKS(500));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user