aboutsummaryrefslogtreecommitdiff
path: root/docs/examples/basic
diff options
context:
space:
mode:
Diffstat (limited to 'docs/examples/basic')
-rw-r--r--docs/examples/basic/src/main.rs33
1 files 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 @@
3 3
4use defmt::*; 4use defmt::*;
5use embassy_executor::Spawner; 5use embassy_executor::Spawner;
6use embassy_nrf::gpio::{Level, Output, OutputDrive}; 6use embassy_nrf::Peri;
7use embassy_time::{Duration, Timer}; 7use embassy_nrf::gpio::{AnyPin, Input, Level, Output, OutputDrive, Pull};
8use {defmt_rtt as _, panic_probe as _}; // global logger 8use embassy_time::Timer;
9use {defmt_rtt as _, panic_probe as _};
9 10
11// Declare async tasks
10#[embassy_executor::task] 12#[embassy_executor::task]
11async fn blinker(mut led: Output<'static>, interval: Duration) { 13async fn blink(pin: Peri<'static, AnyPin>) {
14 let mut led = Output::new(pin, Level::Low, OutputDrive::Standard);
15
12 loop { 16 loop {
17 // Timekeeping is globally available, no need to mess with hardware timers.
13 led.set_high(); 18 led.set_high();
14 Timer::after(interval).await; 19 Timer::after_millis(150).await;
15 led.set_low(); 20 led.set_low();
16 Timer::after(interval).await; 21 Timer::after_millis(150).await;
17 } 22 }
18} 23}
19 24
25// Main is itself an async task as well.
20#[embassy_executor::main] 26#[embassy_executor::main]
21async fn main(spawner: Spawner) { 27async fn main(spawner: Spawner) {
28 // Initialize the embassy-nrf HAL.
22 let p = embassy_nrf::init(Default::default()); 29 let p = embassy_nrf::init(Default::default());
23 30
24 let led = Output::new(p.P0_13, Level::Low, OutputDrive::Standard); 31 // Spawned tasks run in the background, concurrently.
25 spawner.spawn(unwrap!(blinker(led, Duration::from_millis(300)))); 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 }
26} 43}