Robert Wiewel a55c9d77ec Add RMT output channel support for all current ESP32 variants
- Add RMT output channel support for ESP32, ESP32-S2, ESP32-S3, ESP32-C3
- Add add RMT adapter for `smart-leds` crate
- Add example `hello_rgb` for ESP32-S2, ESP32-S3 and ESP32-C3 that either
  drives one LED at the pin where a LED is located on the official devkits
- Add example `hello_rgb` for ESP32 that is driving a 12-element RGB ring.
2022-05-17 15:56:25 +02:00

89 lines
2.5 KiB
Rust

//! RGB LED Demo
//!
//! This example drives an SK68XX RGB LED that is connected to the GPI48 pin.
//! A RGB LED is connected to that 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).
#![no_std]
#![no_main]
use esp32s3_hal::{
pac,
prelude::*,
pulse_control::ClockSource,
utils::{smartLedAdapter, SmartLedsAdapter},
Delay,
PulseControl,
RtcCntl,
Timer,
IO,
};
#[allow(unused_imports)]
use panic_halt as _;
use smart_leds::{
brightness,
gamma,
hsv::{hsv2rgb, Hsv},
SmartLedsWrite,
};
use xtensa_lx_rt::entry;
#[entry]
fn main() -> ! {
let mut peripherals = pac::Peripherals::take().unwrap();
let mut rtc_cntl = RtcCntl::new(peripherals.RTC_CNTL);
let mut timer0 = Timer::new(peripherals.TIMG0);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// Disable MWDT and RWDT (Watchdog) flash boot protection
timer0.disable();
rtc_cntl.set_wdt_global_enable(false);
// Configure RMT peripheral globally
let pulse = PulseControl::new(
peripherals.RMT,
&mut peripherals.SYSTEM,
ClockSource::APB,
0,
0,
0,
)
.unwrap();
// We use one of the RMT channels to instantiate a `SmartLedsAdapter` which can
// be used directly with all `smart_led` implementations
let mut led = <smartLedAdapter!(1)>::new(pulse.channel0, io.pins.gpio48);
// Initialize the Delay peripheral, and use it to toggle the LED state in a
// loop.
let mut delay = Delay::new();
let mut color = Hsv {
hue: 0,
sat: 255,
val: 255,
};
let mut data;
loop {
// Iterate over the rainbow!
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()), 10))
.unwrap();
delay.delay_ms(20u8);
}
}
}