Skip to content

Non blocking smart led #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions esp-hal-smartled/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

## 0.15.0

### Added

- New `SmartLedsAdapterAsync` which is an asynchronous, non-blocking version of the driver.

## 0.14.0

## 0.13.1

### Added

### Changed
Expand Down
19 changes: 11 additions & 8 deletions esp-hal-smartled/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "esp-hal-smartled"
version = "0.14.0"
version = "0.15.0"
edition = "2021"
rust-version = "1.84.0"
description = "RMT peripheral adapter for smart LEDs"
Expand All @@ -15,7 +15,7 @@ targets = ["riscv32imac-unknown-none-elf"]
defmt = { version = "0.3.10", optional = true }
document-features = "0.2.10"
esp-hal = { version = "1.0.0-beta.0", features = ["unstable"] }
smart-leds-trait = "0.3.0"
smart-leds-trait = "0.3.1"

[dev-dependencies]
cfg-if = "1.0.0"
Expand All @@ -24,6 +24,9 @@ esp-backtrace = { version = "0.15.0", features = [
"panic-handler",
"println",
] }
esp-hal-embassy = "0.7"
embassy-executor = "0.7.0"
embassy-time = "0.4.0"
esp-println = "0.13.0"
smart-leds = "0.4.0"

Expand All @@ -33,14 +36,14 @@ defmt = ["dep:defmt", "esp-hal/defmt"]

#! ### Chip Support Feature Flags
## Target the ESP32.
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32"]
esp32 = ["esp-backtrace/esp32", "esp-hal/esp32", "esp-println/esp32", "esp-hal-embassy/esp32"]
## Target the ESP32-C3.
esp32c3 = ["esp-backtrace/esp32c3", "esp-hal/esp32c3", "esp-println/esp32c3"]
esp32c3 = ["esp-backtrace/esp32c3", "esp-hal/esp32c3", "esp-println/esp32c3", "esp-hal-embassy/esp32c3"]
## Target the ESP32-C6.
esp32c6 = ["esp-backtrace/esp32c6", "esp-hal/esp32c6", "esp-println/esp32c6"]
esp32c6 = ["esp-backtrace/esp32c6", "esp-hal/esp32c6", "esp-println/esp32c6", "esp-hal-embassy/esp32c6"]
## Target the ESP32-H2.
esp32h2 = ["esp-backtrace/esp32h2", "esp-hal/esp32h2", "esp-println/esp32h2"]
esp32h2 = ["esp-backtrace/esp32h2", "esp-hal/esp32h2", "esp-println/esp32h2", "esp-hal-embassy/esp32h2"]
## Target the ESP32-S2.
esp32s2 = ["esp-backtrace/esp32s2", "esp-hal/esp32s2", "esp-println/esp32s2"]
esp32s2 = ["esp-backtrace/esp32s2", "esp-hal/esp32s2", "esp-println/esp32s2", "esp-hal-embassy/esp32s2"]
## Target the ESP32-S3.
esp32s3 = ["esp-backtrace/esp32s3", "esp-hal/esp32s3", "esp-println/esp32s3"]
esp32s3 = ["esp-backtrace/esp32s3", "esp-hal/esp32s3", "esp-println/esp32s3", "esp-hal-embassy/esp32s3"]
4 changes: 2 additions & 2 deletions esp-hal-smartled/examples/hello_rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

use esp_backtrace as _;
use esp_hal::{delay::Delay, main, rmt::Rmt, time::Rate};
use esp_hal_smartled::{smartLedBuffer, SmartLedsAdapter};
use esp_hal_smartled::{smart_led_buffer, SmartLedsAdapter};
use smart_leds::{
brightness, gamma,
hsv::{hsv2rgb, Hsv},
Expand Down Expand Up @@ -64,7 +64,7 @@ fn main() -> ! {

// We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can
// be used directly with all `smart_led` implementations
let rmt_buffer = smartLedBuffer!(1);
let rmt_buffer = smart_led_buffer!(1);
let mut led = SmartLedsAdapter::new(rmt.channel0, led_pin, rmt_buffer);

let delay = Delay::new();
Expand Down
94 changes: 94 additions & 0 deletions esp-hal-smartled/examples/hello_rgb_async.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//! Asynchronous RGB LED Demo
//!
//! This example drives an SK68XX RGB LED, which is connected to a pin on the
//! official DevKits.
//!
//! The demo will leverage the [`smart_leds`](https://crates.io/crates/smart-leds)
//! crate functionality to circle through the HSV hue color space (with
//! saturation and value both at 255). Additionally, we apply a gamma correction
//! and limit the brightness to 10 (out of 255).
//!
//! The following wiring is assumed for ESP32:
//! - LED => GPIO33
//! The following wiring is assumed for ESP32C3:
//! - LED => GPIO8
//! The following wiring is assumed for ESP32C6, ESP32H2:
//! - LED => GPIO8
//! The following wiring is assumed for ESP32S2:
//! - LED => GPIO18
//! The following wiring is assumed for ESP32S3:
//! - LED => GPIO48

#![no_std]
#![no_main]

use esp_backtrace as _;
use esp_hal::{rmt::Rmt, time::Rate, timer::timg::TimerGroup, Config};
use esp_hal_smartled::{buffer_size_async, SmartLedsAdapterAsync};
use smart_leds::{
brightness, gamma,
hsv::{hsv2rgb, Hsv},
SmartLedsWriteAsync,
};
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};

#[esp_hal_embassy::main]
async fn main(_spawner: Spawner) -> ! {
let peripherals = esp_hal::init(Config::default());
let timg0 = TimerGroup::new(peripherals.TIMG0);
esp_hal_embassy::init(timg0.timer0);

// Each devkit uses a unique GPIO for the RGB LED, so in order to support
// all chips we must unfortunately use `#[cfg]`s:
cfg_if::cfg_if! {
if #[cfg(feature = "esp32")] {
let led_pin = peripherals.GPIO33;
} else if #[cfg(feature = "esp32c3")] {
let led_pin = peripherals.GPIO8;
} else if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] {
let led_pin = peripherals.GPIO8;
} else if #[cfg(feature = "esp32s2")] {
let led_pin = peripherals.GPIO18;
} else if #[cfg(feature = "esp32s3")] {
let led_pin = peripherals.GPIO48;
}
}

// Configure RMT peripheral globally
cfg_if::cfg_if! {
if #[cfg(feature = "esp32h2")] {
let freq = Rate::from_mhz(32);
} else {
let freq = Rate::from_mhz(80);
}
}

let rmt = Rmt::new(peripherals.RMT, freq).unwrap().into_async();

// We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can
// be used directly with all `smart_led` implementations
let rmt_buffer: [u32; buffer_size_async(1)] = [0; buffer_size_async(1)];
let mut led = SmartLedsAdapterAsync::new(rmt.channel0, led_pin, rmt_buffer);

let mut color = Hsv {
hue: 0,
sat: 255,
val: 255,
};
let mut data;

loop {
for hue in 0..=255 {
color.hue = hue;
// Convert from the HSV color space (where we can easily transition from one
// color to the other) to the RGB color space that we can then send to the LED
data = [hsv2rgb(color)];
// When sending to the LED, we do a gamma correction first (see smart_leds
// documentation for details) and then limit the brightness to 10 out of 255 so
// that the output it's not too bright.
led.write(brightness(gamma(data.iter().cloned()), 20)).await.unwrap();
Timer::after(Duration::from_millis(10)).await;
}
}
}
Loading