@@ -5,12 +5,12 @@ Now we're going to take a brief look into delay abstractions provided by `embedd
55before combining this with the GPIO abstractions from the previous chapter in order to
66finally make an LED blink.
77
8- ` embedded-hal ` provides us with two abstractions to delay the execution of our program:
9- [ ` DelayUs ` ] and [ ` DelayMs ` ] . Both of them essentially work the exact same way except
10- that they accept different units for their delay function.
8+ ` embedded-hal ` provides us with an abstractions to delay the execution of our program:
9+ [ ` DelayNs ` ] . This abstraction provides three functions ` delay_ns ` , ` delay_us ` and ` delay_ms `
10+ that delays execution for nano, micro or mili seconds respectively. They essentially work
11+ the exact same way except that they accept different units for their delay function.
1112
12- [ `DelayUs` ] : https://docs.rs/embedded-hal/0.2.6/embedded_hal/blocking/delay/trait.DelayUs.html
13- [ `DelayMs` ] : https://docs.rs/embedded-hal/0.2.6/embedded_hal/blocking/delay/trait.DelayMs.html
13+ [ `DelayNs` ] : https://docs.rs/embedded-hal/1.0.0/embedded_hal/blocking/delay/trait.DelayNs.html
1414
1515Inside our MCU, several so-called "timers" exist. They can do various things regarding time for us,
1616including simply pausing the execution of our program for a fixed amount of time. A very
@@ -22,21 +22,25 @@ simple delay-based program that prints something every second might for example
2222#![no_std]
2323
2424use cortex_m_rt :: entry;
25- use rtt_target :: {rtt_init_print, rprintln};
25+ use embedded_hal :: delay :: DelayNS ;
26+ use rtt_target :: {
27+ rtt_init_print,
28+ rprintln,
29+ };
2630use panic_rtt_target as _;
2731use microbit :: board :: Board ;
2832use microbit :: hal :: timer :: Timer ;
29- use microbit :: hal :: prelude :: * ;
3033
3134#[entry]
3235fn main () -> ! {
3336 rtt_init_print! ();
34- let mut board = Board :: take (). unwrap ();
37+
38+ let board = Board :: take (). unwrap ();
3539
3640 let mut timer = Timer :: new (board . TIMER0 );
3741
3842 loop {
39- timer . delay_ms (1000 u16 );
43+ timer . delay_ms (1_000 u32 );
4044 rprintln! (" 1000 ms passed" );
4145 }
4246}
@@ -47,20 +51,20 @@ Note that we changed our panic implementation from `panic_halt` to
4751RTT lines from ` Cargo.toml ` and comment the ` panic-halt ` one out,
4852since Rust only allows one panic implementation at a time.
4953
50- In order to actually see the prints we have to change ` Embed.toml ` like this :
54+ In order to actually see the prints we have to change ` Embed.toml ` like shown on the marked lines ( ` <--- Here ` ) :
5155```
5256[default.general]
5357# chip = "nrf52833_xxAA" # uncomment this line for micro:bit V2
5458# chip = "nrf51822_xxAA" # uncomment this line for micro:bit V1
5559
5660[default.reset]
57- halt_afterwards = false
61+ halt_afterwards = false <--- Here
5862
5963[default.rtt]
60- enabled = true
64+ enabled = true <--- Here
6165
6266[default.gdb]
63- enabled = false
67+ enabled = false <--- Here
6468```
6569
6670And now after putting the code into ` src/main.rs ` and another quick ` cargo embed ` (again with the same flags you used before)
@@ -78,15 +82,22 @@ a mash-up of the one above and the one that turned an LED on in the last section
7882#![no_std]
7983
8084use cortex_m_rt :: entry;
81- use rtt_target :: {rtt_init_print, rprintln};
85+ use rtt_target :: {
86+ rtt_init_print,
87+ rprintln,
88+ };
8289use panic_rtt_target as _;
90+ use embedded_hal :: {
91+ delay :: DelayNS ,
92+ digital :: OutputPin ,
93+ };
8394use microbit :: board :: Board ;
8495use microbit :: hal :: timer :: Timer ;
85- use microbit :: hal :: prelude :: * ;
8696
8797#[entry]
8898fn main () -> ! {
8999 rtt_init_print! ();
100+
90101 let mut board = Board :: take (). unwrap ();
91102
92103 let mut timer = Timer :: new (board . TIMER0 );
@@ -98,6 +109,7 @@ fn main() -> ! {
98109 row1 . set_low (). unwrap ();
99110 rprintln! (" Dark!" );
100111 timer . delay_ms (1_000_u16 );
112+
101113 row1 . set_high (). unwrap ();
102114 rprintln! (" Light!" );
103115 timer . delay_ms (1_000_u16 );
0 commit comments