Skip to content
Merged
Changes from 1 commit
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
Next Next commit
feat: ✨ Add hello_rgb example
  • Loading branch information
SergioGasquez committed Jun 9, 2023
commit 02c7333d2de94ee5cf4628c20649bfc39532c4c7
103 changes: 103 additions & 0 deletions esp32h2-hal/examples/hello_rgb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
//! //! 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::*,
pulse_control::ClockSource,
timer::TimerGroup,
Delay,
PulseControl,
Rtc,
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 mut system = peripherals.PCR.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();

// Disable the watchdog timers. For the ESP32-H2, this includes the Super WDT,
// and the TIMG WDTs.
let mut rtc = Rtc::new(peripherals.LP_CLKRST);
let timer_group0 = TimerGroup::new(
peripherals.TIMG0,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt0 = timer_group0.wdt;
let timer_group1 = TimerGroup::new(
peripherals.TIMG1,
&clocks,
&mut system.peripheral_clock_control,
);
let mut wdt1 = timer_group1.wdt;

// Disable watchdog timers
rtc.swd.disable();
rtc.rwdt.disable();
wdt0.disable();
wdt1.disable();

// Configure RMT peripheral globally
let pulse = PulseControl::new(
peripherals.RMT,
&mut system.peripheral_clock_control,
ClockSource::RTC20M,
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 io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let mut led = <smartLedAdapter!(1)>::new(pulse.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);
}
}
}