esp-hal/examples/src/bin/embassy_wait.rs
Jesse Braham 68a4fb29ed
[1/3] Timer abstraction: refactor systimer and timer modules into a common timer module (#1527)
* Refactor `systimer` and `timer` modules into a common `timer` module

* Update `CHANGELOG.md`

* Rebase and update new example
2024-05-02 13:27:33 +00:00

48 lines
1.5 KiB
Rust

//! embassy wait
//!
//! This is an example of asynchronously `Wait`ing for a pin state to change.
//% CHIPS: esp32 esp32c2 esp32c3 esp32c6 esp32h2 esp32s2 esp32s3
//% FEATURES: async embassy embassy-executor-thread embassy-time-timg0 embassy-generic-timers
#![no_std]
#![no_main]
#![feature(type_alias_impl_trait)]
use embassy_executor::Spawner;
use embassy_time::{Duration, Timer};
use esp_backtrace as _;
use esp_hal::{
clock::ClockControl,
embassy::{self},
gpio::Io,
peripherals::Peripherals,
prelude::*,
system::SystemControl,
timer::timg::TimerGroup,
};
#[main]
async fn main(_spawner: Spawner) {
esp_println::println!("Init!");
let peripherals = Peripherals::take();
let system = SystemControl::new(peripherals.SYSTEM);
let clocks = ClockControl::boot_defaults(system.clock_control).freeze();
let timg0 = TimerGroup::new_async(peripherals.TIMG0, &clocks);
embassy::init(&clocks, timg0);
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
#[cfg(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3"))]
let mut input = io.pins.gpio0.into_pull_down_input();
#[cfg(not(any(feature = "esp32", feature = "esp32s2", feature = "esp32s3")))]
let mut input = io.pins.gpio9.into_pull_down_input();
loop {
esp_println::println!("Waiting...");
input.wait_for_rising_edge().await;
esp_println::println!("Ping!");
Timer::after(Duration::from_millis(100)).await;
}
}