use anyhow::Result;
use esp_idf_svc::{
hal::{
gpio::{InterruptType, PinDriver, Pull},
peripherals::Peripherals,
task::notification::Notification,
},
sys::esp_random,
};
use rgb_led::{RGB8, WS2812RMT};
use std::num::NonZeroU32;
fn main() -> Result<()> {
esp_idf_svc::sys::link_patches();
let peripherals = Peripherals::take()?;
let mut led = WS2812RMT::new(peripherals.pins.gpio2, peripherals.rmt.channel0)?;
// Configures the button
let mut button = PinDriver::input(peripherals.pins.gpio9)?;
button.set_pull(Pull::Up)?;
button.set_interrupt_type(InterruptType::PosEdge)?;
// Configures the notification
let notification = Notification::new();
let notifier = notification.notifier();
// Subscribe and create the callback
// Safety: make sure the `Notification` object is not dropped while the subscription is active
unsafe {
button.subscribe(move || {
notifier.notify_and_yield(NonZeroU32::new(1).unwrap());
})?;
}
println!("Hello world!");
loop {
// Enable interrupt and wait for new notificaton
button.enable_interrupt()?;
notification.wait(esp_idf_svc::hal::delay::BLOCK);
println!("Button pressed!");
// Generates random rgb values and sets them in the led.
random_light(&mut led);
}
}
#[allow(unused)]
fn random_light(led: &mut WS2812RMT) {
let mut color = RGB8::new(0, 0, 0);
unsafe {
let r = esp_random() as u8;
let g = esp_random() as u8;
let b = esp_random() as u8;
color = RGB8::new(r, g, b);
}
led.set_pixel(color).unwrap();
}
esp:0
esp:1
esp:2
esp:3
esp:4
esp:5
esp:6
esp:7
esp:9
esp:10
esp:18
esp:19
esp:20
esp:21
esp:RST
esp:3V3
esp:NC1
esp:GND
esp:8.1
esp:8.2
esp:NC2
esp:NC3
esp:NC4
esp:NC5
esp:NC6
esp:5V
esp:EN
esp:BAT+
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r