Skip to content

Commit 8ee5166

Browse files
committed
Include this code as blocks instead
1 parent 4de6ab9 commit 8ee5166

File tree

2 files changed

+16
-36
lines changed

2 files changed

+16
-36
lines changed

book/en/src/by-example/delay.md

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,8 @@ This can be achieved by instantiating a monotonic timer (for implementations, se
99

1010
``` rust
1111
...
12-
#[init]
13-
fn init(cx: init::Context) -> (Shared, Local) {
14-
hprintln!("init");
15-
16-
let token = rtic_monotonics::create_systick_token!();
17-
Systick::start(cx.core.SYST, 12_000_000, token);
18-
...
12+
{{#include ../../../../rtic/examples/async-timeout.rs:init}}
13+
...
1914
```
2015

2116
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
6358

6459
Using the `select_biased` macro from the `futures` crate it may look like this:
6560

66-
``` rust
67-
// Call hal with short relative timeout using `select_biased`
68-
select_biased! {
69-
v = hal_get(1).fuse() => hprintln!("hal returned {}", v),
70-
_ = Systick::delay(200.millis()).fuse() => hprintln!("timeout", ), // this will finish first
71-
}
61+
``` rust,noplayground
62+
{{#include ../../../../rtic/examples/async-timeout.rs:select_biased}}
7263
```
7364

7465
Assuming the `hal_get` will take 450ms to finish, a short timeout of 200ms will expire before `hal_get` can complete.
@@ -80,36 +71,17 @@ Using `select_biased` any number of futures can be combined, so its very powerfu
8071
Rewriting the second example from above using `timeout_after` gives:
8172

8273
``` rust
83-
// Call hal with long relative timeout using monotonic `timeout_after`
84-
match Systick::timeout_after(1000.millis(), hal_get(1)).await {
85-
Ok(v) => hprintln!("hal returned {}", v),
86-
_ => hprintln!("timeout"),
87-
}
74+
{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at_basic}}
8875
```
8976

9077
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.
9178

9279
[fugit]: https://crates.io/crates/fugit
9380

9481
``` rust
95-
// get the current time instance
96-
let mut instant = Systick::now();
97-
98-
// do this 3 times
99-
for n in 0..3 {
100-
// absolute point in time without drift
101-
instant += 1000.millis();
102-
Systick::delay_until(instant).await;
103-
104-
// absolute point it time for timeout
105-
let timeout = instant + 500.millis();
106-
hprintln!("now is {:?}, timeout at {:?}", Systick::now(), timeout);
107-
108-
match Systick::timeout_at(timeout, hal_get(n)).await {
109-
Ok(v) => hprintln!("hal returned {} at time {:?}", v, Systick::now()),
110-
_ => hprintln!("timeout"),
111-
}
112-
}
82+
83+
{{#include ../../../../rtic/examples/async-timeout.rs:timeout_at}}
84+
11385
```
11486

11587
`let mut instant = Systick::now()` sets the starting time of execution.

rtic/examples/async-timeout.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@ mod app {
2323
#[local]
2424
struct Local {}
2525

26+
// ANCHOR: init
2627
#[init]
2728
fn init(cx: init::Context) -> (Shared, Local) {
2829
hprintln!("init");
2930

3031
let systick_token = rtic_monotonics::create_systick_token!();
3132
Systick::start(cx.core.SYST, 12_000_000, systick_token);
33+
// ANCHOR_END: init
3234

3335
foo::spawn().ok();
3436

@@ -37,6 +39,7 @@ mod app {
3739

3840
#[task]
3941
async fn foo(_cx: foo::Context) {
42+
// ANCHOR: select_biased
4043
// Call hal with short relative timeout using `select_biased`
4144
select_biased! {
4245
v = hal_get(1).fuse() => hprintln!("hal returned {}", v),
@@ -48,13 +51,17 @@ mod app {
4851
v = hal_get(1).fuse() => hprintln!("hal returned {}", v), // hal finish first
4952
_ = Systick::delay(1000.millis()).fuse() => hprintln!("timeout", ),
5053
}
54+
// ANCHOR_END: select_biased
5155

56+
// ANCHOR: timeout_after_basic
5257
// Call hal with long relative timeout using monotonic `timeout_after`
5358
match Systick::timeout_after(1000.millis(), hal_get(1)).await {
5459
Ok(v) => hprintln!("hal returned {}", v),
5560
_ => hprintln!("timeout"),
5661
}
62+
// ANCHOR_END: timeout_after_basic
5763

64+
// ANCHOR: timeout_at
5865
// get the current time instance
5966
let mut instant = Systick::now();
6067

@@ -73,6 +80,7 @@ mod app {
7380
_ => hprintln!("timeout"),
7481
}
7582
}
83+
// ANCHOR_END: timeout_at
7684

7785
debug::exit(debug::EXIT_SUCCESS);
7886
}

0 commit comments

Comments
 (0)