|
| 1 | +//! Asynchronous RGB LED Demo |
| 2 | +//! |
| 3 | +//! This example drives an SK68XX RGB LED, which is connected to a pin on the |
| 4 | +//! official DevKits. |
| 5 | +//! |
| 6 | +//! The demo will leverage the [`smart_leds`](https://crates.io/crates/smart-leds) |
| 7 | +//! crate functionality to circle through the HSV hue color space (with |
| 8 | +//! saturation and value both at 255). Additionally, we apply a gamma correction |
| 9 | +//! and limit the brightness to 10 (out of 255). |
| 10 | +//! |
| 11 | +//! The following wiring is assumed for ESP32: |
| 12 | +//! - LED => GPIO33 |
| 13 | +//! The following wiring is assumed for ESP32C3: |
| 14 | +//! - LED => GPIO8 |
| 15 | +//! The following wiring is assumed for ESP32C6, ESP32H2: |
| 16 | +//! - LED => GPIO8 |
| 17 | +//! The following wiring is assumed for ESP32S2: |
| 18 | +//! - LED => GPIO18 |
| 19 | +//! The following wiring is assumed for ESP32S3: |
| 20 | +//! - LED => GPIO48 |
| 21 | +
|
| 22 | +#![no_std] |
| 23 | +#![no_main] |
| 24 | + |
| 25 | +use esp_backtrace as _; |
| 26 | +use esp_hal::{rmt::Rmt, time::Rate, timer::timg::TimerGroup, Config}; |
| 27 | +use esp_hal_smartled::{buffer_size_async, SmartLedsAdapterAsync}; |
| 28 | +use smart_leds::{ |
| 29 | + brightness, gamma, |
| 30 | + hsv::{hsv2rgb, Hsv}, |
| 31 | + SmartLedsWriteAsync, |
| 32 | +}; |
| 33 | +use embassy_executor::Spawner; |
| 34 | +use embassy_time::{Duration, Timer}; |
| 35 | + |
| 36 | +#[esp_hal_embassy::main] |
| 37 | +async fn main(_spawner: Spawner) -> ! { |
| 38 | + let peripherals = esp_hal::init(Config::default()); |
| 39 | + let timg0 = TimerGroup::new(peripherals.TIMG0); |
| 40 | + esp_hal_embassy::init(timg0.timer0); |
| 41 | + |
| 42 | + // Each devkit uses a unique GPIO for the RGB LED, so in order to support |
| 43 | + // all chips we must unfortunately use `#[cfg]`s: |
| 44 | + cfg_if::cfg_if! { |
| 45 | + if #[cfg(feature = "esp32")] { |
| 46 | + let led_pin = peripherals.GPIO33; |
| 47 | + } else if #[cfg(feature = "esp32c3")] { |
| 48 | + let led_pin = peripherals.GPIO8; |
| 49 | + } else if #[cfg(any(feature = "esp32c6", feature = "esp32h2"))] { |
| 50 | + let led_pin = peripherals.GPIO8; |
| 51 | + } else if #[cfg(feature = "esp32s2")] { |
| 52 | + let led_pin = peripherals.GPIO18; |
| 53 | + } else if #[cfg(feature = "esp32s3")] { |
| 54 | + let led_pin = peripherals.GPIO48; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // Configure RMT peripheral globally |
| 59 | + cfg_if::cfg_if! { |
| 60 | + if #[cfg(feature = "esp32h2")] { |
| 61 | + let freq = Rate::from_mhz(32); |
| 62 | + } else { |
| 63 | + let freq = Rate::from_mhz(80); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + let rmt = Rmt::new(peripherals.RMT, freq).unwrap().into_async(); |
| 68 | + |
| 69 | + // We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can |
| 70 | + // be used directly with all `smart_led` implementations |
| 71 | + let rmt_buffer: [u32; buffer_size_async(1)] = [0; buffer_size_async(1)]; |
| 72 | + let mut led = SmartLedsAdapterAsync::new(rmt.channel0, led_pin, rmt_buffer); |
| 73 | + |
| 74 | + let mut color = Hsv { |
| 75 | + hue: 0, |
| 76 | + sat: 255, |
| 77 | + val: 255, |
| 78 | + }; |
| 79 | + let mut data; |
| 80 | + |
| 81 | + loop { |
| 82 | + for hue in 0..=255 { |
| 83 | + color.hue = hue; |
| 84 | + // Convert from the HSV color space (where we can easily transition from one |
| 85 | + // color to the other) to the RGB color space that we can then send to the LED |
| 86 | + data = [hsv2rgb(color)]; |
| 87 | + // When sending to the LED, we do a gamma correction first (see smart_leds |
| 88 | + // documentation for details) and then limit the brightness to 10 out of 255 so |
| 89 | + // that the output it's not too bright. |
| 90 | + led.write(brightness(gamma(data.iter().cloned()), 20)).await.unwrap(); |
| 91 | + Timer::after(Duration::from_millis(10)).await; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
0 commit comments