aboutsummaryrefslogtreecommitdiff
path: root/book/en/src
diff options
context:
space:
mode:
authordatdenkikniet <jcdra1@gmail.com>2023-04-23 15:33:29 +0200
committerdatdenkikniet <jcdra1@gmail.com>2023-05-11 19:20:58 +0200
commit0807aa548c865d12746ce17aa1c17f7968b139a9 (patch)
treed45c8c2df8df43f0c66c80fb89a5b5d00a556ef0 /book/en/src
parente51146a98cc7c83ea574d13b4b5d8e7ceeeb004b (diff)
Include this code as blocks instead
Diffstat (limited to 'book/en/src')
-rw-r--r--book/en/src/by-example/delay.md44
1 files changed, 8 insertions, 36 deletions
diff --git a/book/en/src/by-example/delay.md b/book/en/src/by-example/delay.md
index 893ead9..479fd42 100644
--- a/book/en/src/by-example/delay.md
+++ b/book/en/src/by-example/delay.md
@@ -9,13 +9,8 @@ This can be achieved by instantiating a monotonic timer (for implementations, se
``` rust
...
-#[init]
-fn init(cx: init::Context) -> (Shared, Local) {
- hprintln!("init");
-
- let token = rtic_monotonics::create_systick_token!();
- Systick::start(cx.core.SYST, 12_000_000, token);
- ...
+{{#include ../../../../rtic/examples/async-timeout.rs:init}}
+ ...
```
A *software* task can `await` the delay to expire:
@@ -63,12 +58,8 @@ A common use case is transactions with an associated timeout. In the examples sh
Using the `select_biased` macro from the `futures` crate it may look like this:
-``` rust
-// Call hal with short relative timeout using `select_biased`
-select_biased! {
- v = hal_get(1).fuse() => hprintln!("hal returned {}", v),
- _ = Systick::delay(200.millis()).fuse() => hprintln!("timeout", ), // this will finish first
-}
+``` rust,noplayground
+{{#include ../../../../rtic/examples/async-timeout.rs:select_biased}}
```
Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will expire before `hal_get` can complete.
@@ -80,11 +71,7 @@ Using `select_biased` any number of futures can be combined, so its very powerfu
Rewriting the second example from above using `timeout_after` gives:
``` rust
-// Call hal with long relative timeout using monotonic `timeout_after`
-match Systick::timeout_after(1000.millis(), hal_get(1)).await {
- Ok(v) => hprintln!("hal returned {}", v),
- _ => hprintln!("timeout"),
-}
+{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at_basic}}
```
In cases where you want exact control over time without drift we can use exact points in time using `Instant`, and spans of time using `Duration`. Operations on the `Instant` and `Duration` types come from the [`fugit`] crate.
@@ -92,24 +79,9 @@ In cases where you want exact control over time without drift we can use exact p
[fugit]: https://crates.io/crates/fugit
``` rust
-// get the current time instance
-let mut instant = Systick::now();
-
-// do this 3 times
-for n in 0..3 {
- // absolute point in time without drift
- instant += 1000.millis();
- Systick::delay_until(instant).await;
-
- // absolute point it time for timeout
- let timeout = instant + 500.millis();
- hprintln!("now is {:?}, timeout at {:?}", Systick::now(), timeout);
-
- match Systick::timeout_at(timeout, hal_get(n)).await {
- Ok(v) => hprintln!("hal returned {} at time {:?}", v, Systick::now()),
- _ => hprintln!("timeout"),
- }
-}
+
+{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at}}
+
```
`let mut instant = Systick::now()` sets the starting time of execution.