embassy/docs/modules/ROOT/pages/delaying_a_task.adoc
Barnaby Walters a76dd2d70f Moved content from the wiki to the docs
New: delaying_a_task.adoc, copied as-is from the wiki and placed in the
navigation until we have a better place for it (or remove/replace it)

index: Tweaked the structure, added some content from the wiki, and made
some general copy edits to improve clarity.

getting_started.adoc: Corrected various out-of-date information, added
troubleshooting tips from the wiki, added some new information, various
other small edits.

basic_application.adoc: Corrected out-of-date information, various clarifications
and edits.

After these changes, IMO most of the content on the github wiki is no longer
necessary and can be removed for clarity. The few sections I didn‘t integrate
or copy over were either out of date or unfinished.
2023-12-05 01:01:50 +01:00

28 lines
No EOL
1 KiB
Text

= Delaying a Task
In an embedded program, delaying a task is one of the most common actions taken. In an event loop, delays will need to be inserted to ensure
that other tasks have a chance to run before the next iteration of the loop is called, if no other I/O is performed. Embassy provides an abstraction
to delay the current task for a specified interval of time.
Timing is serviced by the `embassy::time::Timer` struct, which provides two timing methods.
`Timer::at` creates a future that completes at the specified `Instant`, relative to the system boot time.
`Timer::after` creates a future that completes after the specified `Duration`, relative to when the future was created.
An example of a delay is provided as follows:
[,rust]
----
use embassy::executor::{task, Executor};
use embassy::time::{Duration, Timer};
#[task]
/// Task that ticks periodically
async fn tick_periodic() -> ! {
loop {
rprintln!("tick!");
// async sleep primitive, suspends the task for 500ms.
Timer::after(Duration::from_millis(500)).await;
}
}
----