Skip to content

Commit eb09647

Browse files
authored
Merge pull request #4806 from xoviat/frontpage
doc: use frontpage example
2 parents 5bcf5d3 + d7023ed commit eb09647

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

docs/examples/basic/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
authors = ["Dario Nieuwenhuis <[email protected]>"]
3-
edition = "2018"
3+
edition = "2024"
44
name = "embassy-basic-example"
55
version = "0.1.0"
66
license = "MIT OR Apache-2.0"

docs/examples/basic/src/main.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,41 @@
33

44
use defmt::*;
55
use embassy_executor::Spawner;
6-
use embassy_nrf::gpio::{Level, Output, OutputDrive};
7-
use embassy_time::{Duration, Timer};
8-
use {defmt_rtt as _, panic_probe as _}; // global logger
6+
use embassy_nrf::Peri;
7+
use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull};
8+
use embassy_time::Timer;
9+
use {defmt_rtt as _, panic_probe as _};
910

11+
// Declare async tasks
1012
#[embassy_executor::task]
11-
async fn blinker(mut led: Output<'static>, interval: Duration) {
13+
async fn blink(pin: Peri<'static, AnyPin>) {
14+
let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
15+
1216
loop {
17+
// Timekeeping is globally available, no need to mess with hardware timers.
1318
led.set_high();
14-
Timer::after(interval).await;
19+
Timer::after_millis(150).await;
1520
led.set_low();
16-
Timer::after(interval).await;
21+
Timer::after_millis(150).await;
1722
}
1823
}
1924

25+
// Main is itself an async task as well.
2026
#[embassy_executor::main]
2127
async fn main(spawner: Spawner) {
28+
// Initialize the embassy-nrf HAL.
2229
let p = embassy_nrf::init(Default::default());
2330

24-
let led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard);
25-
spawner.spawn(unwrap!(blinker(led, Duration::from_millis(300))));
31+
// Spawned tasks run in the background, concurrently.
32+
spawner.spawn(blink(p.P0_13.into()).unwrap());
33+
34+
let mut button = Input::new(p.P0_11, Pull::Up);
35+
loop {
36+
// Asynchronously wait for GPIO events, allowing other tasks
37+
// to run, or the core to sleep.
38+
button.wait_for_low().await;
39+
info!("Button pressed!");
40+
button.wait_for_high().await;
41+
info!("Button released!");
42+
}
2643
}

0 commit comments

Comments
 (0)