diff options
| author | Dario Nieuwenhuis <[email protected]> | 2021-01-01 23:04:18 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2021-01-01 23:04:18 +0100 |
| commit | 396d7650caa2dda1d4e04202b7dd7ea75ad1d8d6 (patch) | |
| tree | 24889619497eda1ebbf5f0863883945876e815ee | |
| parent | 20d3dc87f95f844e747a7e93036ce9ddac369081 (diff) | |
Add some comments to uart example.
| -rw-r--r-- | embassy-nrf-examples/src/bin/uart.rs | 27 |
1 files changed, 18 insertions, 9 deletions
diff --git a/embassy-nrf-examples/src/bin/uart.rs b/embassy-nrf-examples/src/bin/uart.rs index 107936686..883dadde2 100644 --- a/embassy-nrf-examples/src/bin/uart.rs +++ b/embassy-nrf-examples/src/bin/uart.rs | |||
| @@ -27,17 +27,26 @@ async fn run(mut uart: uarte::Uarte<pac::UARTE0>) { | |||
| 27 | uart.send(&buf).await; | 27 | uart.send(&buf).await; |
| 28 | info!("wrote hello in uart!"); | 28 | info!("wrote hello in uart!"); |
| 29 | 29 | ||
| 30 | info!("reading..."); | ||
| 31 | loop { | 30 | loop { |
| 32 | let received = match select( | 31 | info!("reading..."); |
| 33 | uart.receive(&mut buf), | 32 | |
| 34 | Timer::after(Duration::from_millis(10)), | 33 | // `receive()` doesn't return until the buffer has been completely filled with |
| 35 | ) | 34 | // incoming data, which in this case is 8 bytes. |
| 36 | .await | 35 | // |
| 37 | { | 36 | // This example shows how to use `select` to run an uart receive concurrently with a |
| 37 | // 1 second timer, effectively adding a timeout to the receive operation. | ||
| 38 | let recv_fut = uart.receive(&mut buf); | ||
| 39 | let timer_fut = Timer::after(Duration::from_millis(1000)); | ||
| 40 | let received = match select(recv_fut, timer_fut).await { | ||
| 41 | // recv_fut completed first, so we've received `buf_len` bytes. | ||
| 38 | Either::Left((buf, _)) => buf, | 42 | Either::Left((buf, _)) => buf, |
| 39 | Either::Right((_, read)) => { | 43 | // timer_fut completed first. `select` gives us back the future that didn't complete, which |
| 40 | let (buf, n) = read.stop().await; | 44 | // is `recv_fut` in this case, so we can do further stuff with it. |
| 45 | // | ||
| 46 | // The recv_fut would stop the uart read automatically when dropped. However, we want to know how | ||
| 47 | // many bytes have been received, so we have to "gracefully stop" it with `.stop()`. | ||
| 48 | Either::Right((_, recv_fut)) => { | ||
| 49 | let (buf, n) = recv_fut.stop().await; | ||
| 41 | &buf[..n] | 50 | &buf[..n] |
| 42 | } | 51 | } |
| 43 | }; | 52 | }; |
