#![no_std]
#![no_main]
#![deny(clippy::mem_forget)]
use embassy_executor::Spawner;
use esp_hal::{
clock::CpuClock,
gpio::{Level, Output, OutputConfig},
ledc::{
channel::{self, ChannelIFace},
timer::{self, TimerIFace},
LSGlobalClkSource, Ledc, LowSpeed,
},
time::Rate,
timer::systimer::SystemTimer,
};
use esp_hal::ledc::channel::ChannelHW;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
esp_bootloader_esp_idf::esp_app_desc!();
#[esp_hal_embassy::main]
async fn main(spawner: Spawner) {
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
let timer0 = SystemTimer::new(peripherals.SYSTIMER);
esp_hal_embassy::init(timer0.alarm0);
let led = Output::new(peripherals.GPIO8, Level::High, OutputConfig::default());
let mut ledc = Ledc::new(peripherals.LEDC);
ledc.set_global_slow_clock(LSGlobalClkSource::APBClk);
let mut lstimer0 = ledc.timer::<LowSpeed>(timer::Number::Timer0);
lstimer0.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::LSClockSource::APBClk,
frequency: Rate::from_khz(24),
});
let mut channel0 = ledc.channel(channel::Number::Channel0, led);
let _= channel0.configure(channel::config::Config {
timer: &lstimer0,
duty_pct: 10,
pin_config: channel::config::PinConfig::PushPull,
});
// 定义七彩颜色序列 (使用PWM占空比表示)
let colors = [
(100, 0, 0), // 红
(100, 50, 0), // 橙
(100, 100, 0), // 黄
(0, 100, 0), // 绿
(0, 0, 100), // 蓝
(50, 0, 100), // 靛
(100, 0, 100), // 紫
];
loop {
// let _ =channel0.start_duty_fade(0, 100, 1000);
// while channel0.is_duty_fade_running() {}
// let _ =channel0.start_duty_fade(100, 0, 1000);
// while channel0.is_duty_fade_running() {}
// for &(r, g, b) in colors.iter() {
// 如果是单色LED,我们只能控制亮度变化
// 这里我们使用最亮的颜色分量作为单色LED的控制值
let brightness = 0.max(0).max(100);
let _ =channel0.start_duty_fade(0, brightness, 1000);
while channel0.is_duty_fade_running() {}
embassy_time::Timer::after(embassy_time::Duration::from_millis(500)).await;
let _ =channel0.start_duty_fade(brightness, 0, 1000);
while channel0.is_duty_fade_running() {}
embassy_time::Timer::after(embassy_time::Duration::from_millis(500)).await;
// // 淡入到新颜色
// let _ = channel0.start_duty_fade(0, brightness, 1000);
// while channel0.is_duty_fade_running() {}
//
// // 保持当前颜色一段时间
// embassy_time::Timer::after(embassy_time::Duration::from_millis(500)).await;
//
// // 淡出
// let _ = channel0.start_duty_fade(brightness, 0, 1000);
// while channel0.is_duty_fade_running_hw() {}
// }
}
}