Skip to content

Commit f0e4b5c

Browse files
committed
refactor
1 parent def6f55 commit f0e4b5c

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

tutorials/p7-uart/src/main.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use esp_idf_hal::{
77
};
88
use esp_idf_sys as _;
99

10+
const CR: u8 = 13;
11+
1012
fn main() {
1113
esp_idf_sys::link_patches();
1214

@@ -25,20 +27,21 @@ fn main() {
2527
)
2628
.unwrap();
2729

28-
let mut uart_buf: Vec<u8> = Vec::new();
30+
let mut cli_buf: Vec<u8> = Vec::new();
2931

3032
loop {
3133
let mut buf: [u8; 10] = [0; 10];
3234
match uart.read(&mut buf, NON_BLOCK) {
33-
Ok(x) => {
34-
if x > 0 {
35-
uart_buf.push(buf[0]);
36-
if uart_buf[uart_buf.len() - 1] == 13 {
37-
match uart.write(&uart_buf) {
38-
Ok(_) => println!("{:?} written", buf),
35+
Ok(bytes_read) => {
36+
if bytes_read > 0 {
37+
let b = buf[0];
38+
cli_buf.push(b);
39+
if b == CR {
40+
match uart.write(&cli_buf) {
41+
Ok(_) => println!("{:?} written", cli_buf),
3942
Err(_) => {}
4043
}
41-
uart_buf.clear();
44+
cli_buf.clear();
4245
}
4346
}
4447
}

0 commit comments

Comments
 (0)