cat-in-136 / ws2812-esp32-rmt-driver

WS2812 driver using ESP32 RMT for Rust
MIT License
40 stars 22 forks source link

LED not turning on #52

Open weiying-chen opened 3 months ago

weiying-chen commented 3 months ago

I tried the crate like this with my ESP32-C3-Zero:

use anyhow::Result;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::log::EspLogger;
use esp_idf_sys::{self as _};
use smart_leds_trait::{SmartLedsWrite, RGB8};
use ws2812_esp32_rmt_driver::Ws2812Esp32Rmt;

fn main() -> Result<()> {
    esp_idf_svc::sys::link_patches();
    EspLogger::initialize_default();

    let peripherals = Peripherals::take()?;
    let led_pin = peripherals.pins.gpio10; // Assuming GPIO10 is used for the LED data pin

    println!("Initializing WS2812 LED strip");

    let channel = peripherals.rmt.channel0;
    let mut ws2812 = Ws2812Esp32Rmt::new(channel, led_pin).unwrap();

    // Define a single LED color
    let color = RGB8 { r: 255, g: 0, b: 0 }; // Red color

    loop {
        // Write the color to the LED strip
        ws2812.write([color].iter().cloned()).unwrap();

        // Add a delay to keep the color on for a while
        FreeRtos::delay_ms(1000);

        // Turn off the LED
        ws2812
            .write([RGB8 { r: 0, g: 0, b: 0 }].iter().cloned())
            .unwrap();

        // Add a delay before the next loop iteration
        FreeRtos::delay_ms(1000);
    }
}

The code compiles, but the LED is not turning on.

Note this is my cargo file:

[package]
name = "heavy-cloud"
version = "0.1.0"
authors = ["Wei-ying Chen <weiying.alex.chen@gmail.com>"]
edition = "2021"
resolver = "2"
rust-version = "1.71"

[profile.release]
opt-level = "s"

[profile.dev]
debug = true # Symbols are nice, and they don't increase the size on Flash
opt-level = "z"

[features]
default = ["std", "embassy", "esp-idf-svc/native"]

pio = ["esp-idf-svc/pio"]
std = ["alloc", "esp-idf-svc/binstart", "esp-idf-svc/std"]
alloc = ["esp-idf-svc/alloc"]
nightly = ["esp-idf-svc/nightly"]
experimental = ["esp-idf-svc/experimental"]
# embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/critical-section", "esp-idf-svc/embassy-time-driver"]
embassy = ["esp-idf-svc/embassy-sync", "esp-idf-svc/embassy-time-driver"]

[dependencies]
log = { version = "0.4", default-features = false }
esp-idf-sys = "0.34"
esp-idf-hal = "0.43"
esp-idf-svc = { version = "0.48.1", default-features = false }
embedded-svc = "0.27.1"
anyhow = "1.0.80"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
signalo_filters = "0.6.0"
smart-leds-trait = "0.3.0"
smart-leds = "0.3.0"
ws2812-esp32-rmt-driver = { version = "0.8.0", features = ["smart-leds-trait"] }

[build-dependencies]
embuild = "0.31.3"
weiying-chen commented 3 months ago

I also tried this:

use anyhow::Result;
use esp_idf_hal::delay::FreeRtos;
use esp_idf_hal::peripherals::Peripherals;
use esp_idf_svc::log::EspLogger;
use esp_idf_sys::{self as _};
use log::info;
use smart_leds_trait::{SmartLedsWrite, RGB8};
use ws2812_esp32_rmt_driver::Ws2812Esp32Rmt;

fn main() -> Result<()> {
    esp_idf_svc::sys::link_patches();
    EspLogger::initialize_default();

    let peripherals = Peripherals::take()?;
    let led_pin = peripherals.pins.gpio10; // Assuming GPIO10 is used for the LED data pin

    println!("Initializing WS2812 LED strip");

    let channel = peripherals.rmt.channel0;
    let mut ws2812 = Ws2812Esp32Rmt::new(channel, led_pin).unwrap();

    // Define an array of LED colors
    let mut leds = vec![RGB8 { r: 0, g: 0, b: 0 }; 8]; // 8 LEDs initialized to off

    loop {
        info!("Turn ON");
        // Set the color of all LEDs to red
        for led in leds.iter_mut() {
            *led = RGB8 { r: 255, g: 0, b: 0 };
        }
        ws2812.write(leds.iter().cloned()).unwrap();

        // Add a delay to keep the color on for a while
        FreeRtos::delay_ms(1000);

        info!("Turn OFF");
        // Turn off all LEDs
        for led in leds.iter_mut() {
            *led = RGB8 { r: 0, g: 0, b: 0 };
        }
        ws2812.write(leds.iter().cloned()).unwrap();

        // Add a delay before the next loop iteration
        FreeRtos::delay_ms(1000);
    }
}

Same: No errors. But the LED doesn't turn on.

W (335) rmt(legacy): legacy driver is deprecated, please migrate to `driver/rmt_tx.h` and/or `driver/rmt_rx.h`
W (345) timer_group: legacy driver is deprecated, please migrate to `driver/gptimer.h`
I (354) sleep: Configure to isolate all GPIO pins in sleep state
I (361) sleep: Enable automatic switching of GPIO sleep configuration
I (368) app_start: Starting scheduler on CPU0
I (373) main_task: Started on CPU0
I (373) main_task: Calling app_main()
Initializing WS2812 LED strip
I (383) heavy_cloud: Turn ON
I (1383) heavy_cloud: Turn OFF

Ah, I found out it works with an external WS2812. So it doesn't work with the built-in WS2812 of the ESP32-C3-Zero for some reason.