Skip to content

Commit

Permalink
Add a port_name() function to kaleidoscope_focus::Focus
Browse files Browse the repository at this point in the history
The function returns the name of the serial port Focus is connected to, if
available. Also updated the command-line tools to make use of this information,
and display the port name on the progress indicator.

Signed-off-by: Gergely Nagy <[email protected]>
  • Loading branch information
algernon committed Oct 23, 2022
1 parent 3fbaed4 commit f4a126c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

_No changes yet._
### Added
- Added a `port_name()` function to `kaleidoscope_focus::Focus`, which returns
the name of the serial port it is connected to, if available.
- Updated the command-line tools to make use of it, and display the port name on
the progress indicator.

## [0.1.0] - 2022-10-23

Expand Down
16 changes: 14 additions & 2 deletions kaleidoscope-focus-cli/src/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ impl Cli {
}

pub fn send(&mut self, command: &str, args: &[String]) -> Result<()> {
self.progress.set_prefix(format!(
"sending `{}` (to {}): ",
&command,
&self.conn.port_name().unwrap()
));

let reply = self.conn.flush()?.request(command, Some(args))?;
self.progress.finish_and_clear();

Expand All @@ -96,7 +102,10 @@ impl Cli {
}

pub fn backup(&mut self) -> Result<()> {
self.progress.set_prefix("backing up: ");
self.progress.set_prefix(format!(
"backing up (from {}): ",
&self.conn.port_name().unwrap()
));

let reply = self.conn.flush()?.command("backup")?;

Expand Down Expand Up @@ -171,7 +180,10 @@ impl Cli {
let backup: BackupData =
serde_json::from_reader(io::stdin()).expect("Unable to parse the backup");

self.progress.set_prefix("restoring: ");
self.progress.set_prefix(format!(
"restoring (to {}): ",
&self.conn.port_name().unwrap()
));

for k in &backup.restore {
self.progress.set_message(k.clone());
Expand Down
14 changes: 14 additions & 0 deletions kaleidoscope-focus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,20 @@ impl Focus {
Ok(self)
}

/// Return the port name - if known - of the connected device.
///
/// ```no_run
/// # use kaleidoscope_focus::Focus;
/// # fn main() -> Result<(), std::io::Error> {
/// let mut conn = Focus::create("/dev/ttyACM0").open()?;
/// assert_eq!(conn.port_name(), Some("/dev/ttyACM0".to_string()));
/// # Ok(())
/// # }
/// ```
pub fn port_name(&self) -> Option<String> {
self.port.name()
}

/// Find supported devices, and return the paths to their ports.
///
/// Iterates over available USB serial ports, and keeps only those that belong
Expand Down

0 comments on commit f4a126c

Please sign in to comment.