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(-) 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 From d7023ed3d1bea7929be8a6cb25597a734936276e Mon Sep 17 00:00:00 2001 From: xoviat Date: Wed, 29 Oct 2025 17:29:16 -0500 Subject: fix edition --- docs/examples/basic/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/examples/basic/Cargo.toml b/docs/examples/basic/Cargo.toml index b90180853..fadda102b 100644 --- a/docs/examples/basic/Cargo.toml +++ b/docs/examples/basic/Cargo.toml @@ -1,6 +1,6 @@ [package] authors = ["Dario Nieuwenhuis "] -edition = "2018" +edition = "2024" name = "embassy-basic-example" version = "0.1.0" license = "MIT OR Apache-2.0" -- cgit