Skip to content

Commit 369dffd

Browse files
authored
Replace our naive monitor with espmonitor (#148)
* Remove unnecessary if block * Use espmonitor as our monitor output
1 parent 481b8cd commit 369dffd

File tree

8 files changed

+167
-91
lines changed

8 files changed

+167
-91
lines changed

Cargo.lock

Lines changed: 139 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cargo-espflash/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn flash(
169169
}
170170

171171
if opts.flash_opts.monitor {
172-
monitor(flasher.into_serial()).into_diagnostic()?;
172+
monitor(flasher.into_serial(), &elf_data).into_diagnostic()?;
173173
}
174174

175175
Ok(())

espflash/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ crossterm = "0.23"
4646
directories-next = "2.0"
4747
dialoguer = "0.10"
4848
serde-hex = "0.1"
49+
espmonitor = "0.7.0"

espflash/src/cli/line_endings.rs

Lines changed: 0 additions & 57 deletions
This file was deleted.

espflash/src/cli/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use crate::{
2424
pub mod config;
2525
pub mod monitor;
2626

27-
mod line_endings;
2827
mod serial;
2928

3029
#[derive(Parser)]

espflash/src/cli/monitor.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ use crossterm::{
88
event::{poll, read, Event, KeyCode, KeyEvent, KeyModifiers},
99
terminal::{disable_raw_mode, enable_raw_mode},
1010
};
11+
use espmonitor::{handle_serial, load_bin_context, SerialState};
1112
use miette::{IntoDiagnostic, Result};
1213
use serialport::SerialPort;
1314

14-
use super::line_endings::normalized;
15-
1615
/// Converts key events from crossterm into appropriate character/escape
1716
/// sequences which are then sent over the serial connection.
1817
///
@@ -83,7 +82,7 @@ impl Drop for RawModeGuard {
8382
}
8483
}
8584

86-
pub fn monitor(mut serial: Box<dyn SerialPort>) -> serialport::Result<()> {
85+
pub fn monitor(mut serial: Box<dyn SerialPort>, elf: &[u8]) -> serialport::Result<()> {
8786
println!("Commands:");
8887
println!(" CTRL+R Reset chip");
8988
println!(" CTRL+C Exit");
@@ -99,7 +98,11 @@ pub fn monitor(mut serial: Box<dyn SerialPort>) -> serialport::Result<()> {
9998
let stdout = stdout();
10099
let mut stdout = stdout.lock();
101100

102-
let mut buff = [0; 128];
101+
let symbols = load_bin_context(elf).ok();
102+
103+
let mut serial_state = SerialState::new(symbols);
104+
105+
let mut buff = [0; 1024];
103106
loop {
104107
let read_count = match serial.read(&mut buff) {
105108
Ok(count) => Ok(count),
@@ -109,11 +112,7 @@ pub fn monitor(mut serial: Box<dyn SerialPort>) -> serialport::Result<()> {
109112
}?;
110113

111114
if read_count > 0 {
112-
let data: Vec<u8> = normalized(buff[0..read_count].iter().copied()).collect();
113-
let data = String::from_utf8_lossy(&data);
114-
115-
stdout.write_all(data.as_bytes()).ok();
116-
stdout.flush()?;
115+
handle_serial(&mut serial_state, &buff[0..read_count], &mut stdout)?;
117116
}
118117

119118
if poll(Duration::from_secs(0))? {

0 commit comments

Comments
 (0)