Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ We have several packages which live in this repository. Changes are tracked sepa

### [defmt-next]

* [#940] `defmt-print`: Allow reading from a serial port
* [#935] Add note in book's setup chapter to clarify staticlib setup
* [#914] Add cargo-deny as a CI action to check crate security and licensing

Expand Down
1 change: 1 addition & 0 deletions print/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ defmt-decoder = { version = "=0.4.0", path = "../decoder" }
log = "0.4"
notify = "7"
tokio = { version = "1.38", features = ["full"] }
tokio-serial = "5.4"
18 changes: 18 additions & 0 deletions print/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
env,
path::{Path, PathBuf},
time::Duration,
};

use anyhow::anyhow;
Expand All @@ -20,6 +21,7 @@ use tokio::{
select,
sync::mpsc::Receiver,
};
use tokio_serial::{SerialPort, SerialPortBuilderExt, SerialStream};

/// Prints defmt-encoded logs to stdout
#[derive(Parser, Clone)]
Expand Down Expand Up @@ -65,11 +67,19 @@ enum Command {
#[arg(long, env = "RTT_PORT", default_value_t = 19021)]
port: u16,
},
Serial {
#[arg(long, env = "SERIAL_PORT", default_value = "/dev/ttyUSB0")]
path: PathBuf,

#[arg(long, env = "SERIAL_BAUD", default_value_t = 115200)]
baud: u32,
},
}

enum Source {
Stdin(Stdin),
Tcp(TcpStream),
Serial(SerialStream),
}

impl Source {
Expand All @@ -84,13 +94,20 @@ impl Source {
}
}

fn serial(path: PathBuf, baud: u32) -> anyhow::Result<Self> {
let mut ser = tokio_serial::new(path.to_string_lossy(), baud).open_native_async()?;
ser.set_timeout(Duration::from_millis(500))?;
Ok(Source::Serial(ser))
}

async fn read(&mut self, buf: &mut [u8]) -> anyhow::Result<(usize, bool)> {
match self {
Source::Stdin(stdin) => {
let n = stdin.read(buf).await?;
Ok((n, n == 0))
}
Source::Tcp(tcpstream) => Ok((tcpstream.read(buf).await?, false)),
Source::Serial(serial) => Ok((serial.read(buf).await?, false)),
}
}
}
Expand All @@ -109,6 +126,7 @@ async fn main() -> anyhow::Result<()> {
let mut source = match opts.command.clone() {
None | Some(Command::Stdin) => Source::stdin(),
Some(Command::Tcp { host, port }) => Source::tcp(host, port).await?,
Some(Command::Serial { path, baud }) => Source::serial(path, baud)?,
};

if opts.watch_elf {
Expand Down