#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Executor;
use embassy_time::{Duration, Timer};
use embassy_futures::select::{Either, select};
use embedded_hal_async::digital::Wait;
use esp32c3_hal::{
clock::ClockControl,
embassy,
gpio::{Gpio9, Gpio2, Input, PullDown, Output, PushPull},
peripherals::Peripherals,
prelude::*,
timer::TimerGroup,
rmt::Rmt,
Rtc,
IO,
};
use esp_backtrace as _;
use static_cell::StaticCell;
use esp_hal_smartled::{smartLedAdapter, SmartLedsAdapter};
use smart_leds::{
brightness,
gamma,
hsv::{hsv2rgb, Hsv},
SmartLedsWrite,
RGB8
};
static EXECUTOR: StaticCell<Executor> = StaticCell::new();
#[entry]
fn main() -> ! {
let peripherals = Peripherals::take();
let mut system = peripherals.SYSTEM.split();
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let mut rtc = Rtc::new(peripherals.RTC_CNTL);
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();
embassy::init(&clocks, timer_group0.timer0);
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
// GPIO 9 as input
let input = io.pins.gpio9.into_pull_down_input();
// Configure RMT peripheral globally
let rmt = Rmt::new(
peripherals.RMT,
80u32.MHz(),
&mut system.peripheral_clock_control,
&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.gpio2);
let output = Led::new(led);
// Async requires the GPIO interrupt to wake futures
esp32c3_hal::interrupt::enable(
esp32c3_hal::peripherals::Interrupt::GPIO,
esp32c3_hal::interrupt::Priority::Priority1,
)
.unwrap();
let executor = EXECUTOR.init(Executor::new());
executor.run(|spawner| {
spawner.spawn(task(input, output)).ok();
});
}
#[embassy_executor::task]
async fn task(mut input: Gpio9<Input<PullDown>>, mut output: Led) {
loop {
input.wait_for_any_edge().await;
Timer::after(Duration::from_millis(50)).await; // small debounce, let the pin settle - boot button bounces a lot
esp_println::println!("Button pressed!");
if input.is_low().unwrap() {
output.set_high().unwrap()
} else {
output.set_low().unwrap();
}
}
}
pub struct Led {
inner: SmartLedsAdapter<esp32c3_hal::rmt::Channel0<0>, 0, 25>
}
impl Led {
pub fn new(inner: SmartLedsAdapter<esp32c3_hal::rmt::Channel0<0>, 0, 25>) -> Self {
Self {
inner
}
}
pub fn set_high(&mut self) -> Result<(), ()> {
self.inner.write([RGB8::new(255, 0, 0)].iter().cloned()).unwrap();
Ok(())
}
pub fn set_low(&mut self) -> Result<(), ()> {
self.inner.write([RGB8::new(0, 0, 0)].iter().cloned()).unwrap();
Ok(())
}
}