From 8ff497e9bc2d9fa7402bc6f060ebc8909ad97a32 Mon Sep 17 00:00:00 2001 From: xoviat Date: Wed, 29 Oct 2025 17:17:39 -0500 Subject: doc: use frontpage example ref #4776 --- docs/examples/basic/src/main.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'docs') diff --git a/docs/examples/basic/src/main.rs b/docs/examples/basic/src/main.rs index 6e274bacb..42797612c 100644 --- a/docs/examples/basic/src/main.rs +++ b/docs/examples/basic/src/main.rs @@ -3,24 +3,41 @@ use defmt::*; use embassy_executor::Spawner; -use embassy_nrf::gpio::{Level, Output, OutputDrive}; -use embassy_time::{Duration, Timer}; -use {defmt_rtt as _, panic_probe as _}; // global logger +use embassy_nrf::Peri; +use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull}; +use embassy_time::Timer; +use {defmt_rtt as _, panic_probe as _}; +// Declare async tasks #[embassy_executor::task] -async fn blinker(mut led: Output<'static>, interval: Duration) { +async fn blink(pin: Peri<'static, AnyPin>) { + let mut led = Output::new(pin, Level::Low, OutputDrive::Standard); + loop { + // Timekeeping is globally available, no need to mess with hardware timers. led.set_high(); - Timer::after(interval).await; + Timer::after_millis(150).await; led.set_low(); - Timer::after(interval).await; + Timer::after_millis(150).await; } } +// Main is itself an async task as well. #[embassy_executor::main] async fn main(spawner: Spawner) { + // Initialize the embassy-nrf HAL. let p = embassy_nrf::init(Default::default()); - let led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); - spawner.spawn(unwrap!(blinker(led, Duration::from_millis(300)))); + // Spawned tasks run in the background, concurrently. + spawner.spawn(blink(p.P0_13.into()).unwrap()); + + let mut button = Input::new(p.P0_11, Pull::Up); + loop { + // Asynchronously wait for GPIO events, allowing other tasks + // to run, or the core to sleep. + button.wait_for_low().await; + info!("Button pressed!"); + button.wait_for_high().await; + info!("Button released!"); + } } -- cgit