mirror of
https://github.com/esp-rs/esp-hal.git
synced 2025-10-02 14:44:42 +00:00

* Unify the system peripheral Whilst the PCR, SYSTEM and DPORT peripherals are different, we currently use them all in the same way. This PR unifies the peripheral name in the hal to `SYSTEM`. The idea is that they all do the same sort of thing, so we can collect them under the same name, and later down the line we can being to expose differences under an extended API. The benifits to this are imo quite big, the examples now are all identical, which makes things easier for esp-wifi, and paves a path towards the multichip hal. Why not do this in the PAC? Imo the pac should be as close to the hardware as possible, and the HAL is where we should abstractions such as this. * changelog
65 lines
2.2 KiB
Rust
65 lines
2.2 KiB
Rust
//! //! RGB LED Demo
|
|
//!
|
|
//! This example drives an SK68XX RGB LED that is connected to the GPIO8 pin.
|
|
//! A RGB LED is connected to that pin on the ESP32-H2-DevKitM-1 and board.
|
|
//!
|
|
//! 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 esp32h2_hal::{clock::ClockControl, peripherals, prelude::*, rmt::Rmt, Delay, IO};
|
|
use esp_backtrace as _;
|
|
use esp_hal_smartled::{smartLedAdapter, SmartLedsAdapter};
|
|
use smart_leds::{
|
|
brightness,
|
|
gamma,
|
|
hsv::{hsv2rgb, Hsv},
|
|
SmartLedsWrite,
|
|
};
|
|
|
|
#[entry]
|
|
fn main() -> ! {
|
|
let peripherals = peripherals::Peripherals::take();
|
|
let system = peripherals.SYSTEM.split();
|
|
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
|
|
|
|
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
|
|
|
|
// Configure RMT peripheral globally
|
|
let rmt = Rmt::new(peripherals.RMT, 80u32.MHz(), &clocks).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!(0, 1)>::new(rmt.channel0, io.pins.gpio8);
|
|
|
|
// Initialize the Delay peripheral, and use it to toggle the LED state in a
|
|
// loop.
|
|
let mut delay = Delay::new(&clocks);
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|