aboutsummaryrefslogtreecommitdiff
path: root/examples/nrf/src
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-05-27 16:03:19 +0000
committerGitHub <[email protected]>2022-05-27 16:03:19 +0000
commit82e873d920c843d458c23dbd1b3e631006a29d0b (patch)
treef67133f80abd90c1db9dcff4895c35fc3f42c860 /examples/nrf/src
parente10fc2bada1c59420431f09a35f7aa09a5b45623 (diff)
parent9772645718ae245c13d3905b2e70db8cb85443f6 (diff)
Merge #783
783: Reimplement BufRead for BufferedUart r=Dirbaio a=chemicstry The `AsyncBufRead` implementation for `BufferedUart` was removed in https://github.com/embassy-rs/embassy/pull/752, this PR reimplements it from `embedded-io`. This allows reading `BufferedUart` without copying slices. Co-authored-by: chemicstry <[email protected]>
Diffstat (limited to 'examples/nrf/src')
-rw-r--r--examples/nrf/src/bin/buffered_uart.rs20
1 files changed, 5 insertions, 15 deletions
diff --git a/examples/nrf/src/bin/buffered_uart.rs b/examples/nrf/src/bin/buffered_uart.rs
index a64c5821b..782c39499 100644
--- a/examples/nrf/src/bin/buffered_uart.rs
+++ b/examples/nrf/src/bin/buffered_uart.rs
@@ -6,7 +6,7 @@ use defmt::*;
6use embassy::executor::Spawner; 6use embassy::executor::Spawner;
7use embassy_nrf::buffered_uarte::State; 7use embassy_nrf::buffered_uarte::State;
8use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals}; 8use embassy_nrf::{buffered_uarte::BufferedUarte, interrupt, uarte, Peripherals};
9use embedded_io::asynch::{Read, Write}; 9use embedded_io::asynch::{BufRead, Write};
10use futures::pin_mut; 10use futures::pin_mut;
11 11
12use defmt_rtt as _; // global logger 12use defmt_rtt as _; // global logger
@@ -46,23 +46,13 @@ async fn main(_spawner: Spawner, p: Peripherals) {
46 unwrap!(u.write_all(b"Hello!\r\n").await); 46 unwrap!(u.write_all(b"Hello!\r\n").await);
47 info!("wrote hello in uart!"); 47 info!("wrote hello in uart!");
48 48
49 // Simple demo, reading 8-char chunks and echoing them back reversed.
50 loop { 49 loop {
51 info!("reading..."); 50 info!("reading...");
52 let mut buf = [0u8; 8]; 51 let buf = unwrap!(u.fill_buf().await);
53 unwrap!(u.read_exact(&mut buf).await);
54 info!("read done, got {}", buf); 52 info!("read done, got {}", buf);
55 53
56 // Reverse buf 54 // Read bytes have to be explicitly consumed, otherwise fill_buf() will return them again
57 for i in 0..4 { 55 let n = buf.len();
58 buf.swap(i, 7 - i); 56 u.consume(n);
59 }
60
61 info!("writing...");
62 unwrap!(u.write_all(&buf).await);
63 info!("write done");
64
65 // Wait until the bytes are actually finished being transmitted
66 unwrap!(u.flush().await);
67 } 57 }
68} 58}