From 46a8b1e19d39231dc5c61e92714146f074f8403a Mon Sep 17 00:00:00 2001 From: Federico Lolli <federico.lolli@skywarder.eu> Date: Tue, 16 Apr 2024 12:29:45 +0200 Subject: [PATCH] Added CloseAll command --- src/lib.rs | 10 +++++++++- src/serial.rs | 5 +++++ src/types.rs | 2 ++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index cae93d4..ee10f14 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,6 +78,7 @@ fn serialbridge(lhs: Lhs, rhs: Rhs) -> rustmex::Result<()> { match mode { Mode::Open => open_serial(args)?, Mode::Close => close_serial(args)?, + Mode::CloseAll => close_all_serials(args)?, Mode::Read => read_from_serial(out, args)?, Mode::Write => write_to_serial(args)?, } @@ -121,7 +122,7 @@ fn open_serial(args: Args<'_>) -> SResult<()> { Ok(()) } -/// Close the serial port if it is open (didn't expect any arguments) +/// Close the serial port specified by the first argument, if it is open fn close_serial(args: Args<'_>) -> SResult<()> { args.assert_params_max_len(1)?; let port = get_port_name(1, &args)?; @@ -130,6 +131,13 @@ fn close_serial(args: Args<'_>) -> SResult<()> { Ok(()) } +/// Close all serial ports that are open (didn't expect any arguments) +fn close_all_serials(args: Args<'_>) -> SResult<()> { + args.assert_params_max_len(0)?; + SERIAL.write().close_all(); + Ok(()) +} + /// Read `n` doubles from the serial port and return them as a vector fn read_from_serial(mut outputs: Output<'_>, args: Args<'_>) -> SResult<()> { args.assert_params_max_len(2)?; diff --git a/src/serial.rs b/src/serial.rs index f6e107f..4011d74 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -56,6 +56,11 @@ impl SerialManager { } } + /// Close all serial that are open + pub fn close_all(&mut self) { + self.serial_map.write().clear(); + } + /// Read `n` bytes from the serial port pub fn read_n_bytes(&self, name: &str, n: usize) -> SResult<Vec<u8>> { let map = self.serial_map.read(); diff --git a/src/types.rs b/src/types.rs index 97fca4f..e177f8d 100644 --- a/src/types.rs +++ b/src/types.rs @@ -110,6 +110,7 @@ impl IntoMatlabType for Vec<f32> { pub enum Mode { Open, Close, + CloseAll, Read, Write, } @@ -121,6 +122,7 @@ impl FromStr for Mode { match s { "Open" => Ok(Mode::Open), "Close" => Ok(Mode::Close), + "CloseAll" => Ok(Mode::CloseAll), "Read" => Ok(Mode::Read), "Write" => Ok(Mode::Write), _ => Err(Error::InvalidMode), -- GitLab