#![no_std]
#![no_main]
use esp32_hal::{
clock::ClockControl,
gpio::IO,
ledc::{
channel::{self, ChannelIFace},
timer::{self, TimerIFace},
HighSpeed,
LEDC,
},
peripherals::Peripherals,
prelude::*,
};
use esp_backtrace as _;
#[entry]
fn main() -> ! {
let 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);
let led = io.pins.gpio4.into_push_pull_output();
let ledc = LEDC::new(peripherals.LEDC, &clocks);
let mut hstimer0 = ledc.get_timer::<HighSpeed>(timer::Number::Timer0);
hstimer0
.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::HSClockSource::APBClk,
frequency: 24u32.kHz(),
})
.unwrap();
let mut channel0 = ledc.get_channel(channel::Number::Channel0, led);
channel0
.configure(channel::config::Config {
timer: &hstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();
channel0.start_duty_fade(0, 100, 2000).expect_err(
"Fading from 0% to 100%, at 24kHz and 5-bit resolution, over 2 seconds, should fail",
);
loop {
channel0.start_duty_fade(0, 100, 1000).unwrap();
while channel0.is_duty_fade_running() {}
channel0.start_duty_fade(100, 0, 1000).unwrap();
while channel0.is_duty_fade_running() {}
}
}