diff --git a/src/commands/control.rs b/src/commands/control.rs index 8f90d23..3f71b8a 100644 --- a/src/commands/control.rs +++ b/src/commands/control.rs @@ -160,22 +160,26 @@ impl Command for OptEnd { } /// Set Power, from pow3v3, pow5v fn -#[derive(Debug)] +#[derive(clap::Subcommand, PartialEq, Clone, Copy, Debug)] pub enum SetPower { - Enable3V3, - Disable3V3, - Enable5V, - Disable5V, + /// Enable 3.3V output + Enable3v3, + /// Disable 3.3V output + Disable3v3, + /// Enable 5V output + Enable5v, + /// Disable 5V output + Disable5v, } impl Command for SetPower { type Response = (); const COMMAND_ID: u8 = 0x0d; fn payload(&self) -> Vec { match self { - SetPower::Enable3V3 => vec![0x09], - SetPower::Disable3V3 => vec![0x0A], - SetPower::Enable5V => vec![0x0B], - SetPower::Disable5V => vec![0x0C], + SetPower::Enable3v3 => vec![0x09], + SetPower::Disable3v3 => vec![0x0A], + SetPower::Enable5v => vec![0x0B], + SetPower::Disable5v => vec![0x0C], } } } diff --git a/src/main.rs b/src/main.rs index 922d51b..b5cdb0d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -148,14 +148,11 @@ enum Commands { }, /// List probes List {}, - /// Enable 3.3V output - Enable3v3 {}, - /// Disable 3.3V output - Disable3v3 {}, - /// Enable 5V output - Enable5v {}, - /// Disable 5V output - Disable5v {}, + /// Enable or disable power output + SetPower { + #[command(subcommand)] + cmd: commands::control::SetPower, + }, /// SDI virtual serial port, #[command(subcommand)] SdiPrint(SdiPrint), @@ -212,17 +209,8 @@ fn main() -> Result<()> { Some(Commands::List {}) => { WchLink::list_probes()?; } - Some(Commands::Enable3v3 {}) => { - WchLink::set_3v3_output_enabled(device_index, true)?; - } - Some(Commands::Disable3v3 {}) => { - WchLink::set_3v3_output_enabled(device_index, false)?; - } - Some(Commands::Enable5v {}) => { - WchLink::set_5v_output_enabled(device_index, true)?; - } - Some(Commands::Disable5v {}) => { - WchLink::set_5v_output_enabled(device_index, false)?; + Some(Commands::SetPower { cmd }) => { + WchLink::set_power_output_enabled(device_index, cmd)?; } Some(Commands::Erase { method }) if method != EraseMode::Default => { diff --git a/src/probe.rs b/src/probe.rs index 3b996b9..06f2043 100644 --- a/src/probe.rs +++ b/src/probe.rs @@ -173,7 +173,7 @@ impl WchLink { Ok(()) } - pub fn set_3v3_output_enabled(nth: usize, enable: bool) -> Result<()> { + pub fn set_power_output_enabled(nth: usize, cmd: commands::control::SetPower) -> Result<()> { let mut probe = Self::open_nth(nth)?; if !probe.info.variant.support_power_funcs() { @@ -182,32 +182,13 @@ impl WchLink { )); } - if enable { - log::info!("Enable 3.3V Output"); - probe.send_command(commands::control::SetPower::Enable3V3)?; - } else { - log::info!("Disable 3.3V Output"); - probe.send_command(commands::control::SetPower::Disable3V3)?; - } - - Ok(()) - } - - pub fn set_5v_output_enabled(nth: usize, enable: bool) -> Result<()> { - let mut probe = Self::open_nth(nth)?; + probe.send_command(cmd)?; - if !probe.info.variant.support_power_funcs() { - return Err(Error::Custom( - "Probe doesn't support power control".to_string(), - )); - } - - if enable { - log::info!("Enable 5V Output"); - probe.send_command(commands::control::SetPower::Enable5V)?; - } else { - log::info!("Disable 5V Output"); - probe.send_command(commands::control::SetPower::Disable5V)?; + match cmd { + commands::control::SetPower::Enable3v3 => log::info!("Enable 3.3V Output"), + commands::control::SetPower::Disable3v3 => log::info!("Disable 3.3V Output"), + commands::control::SetPower::Enable5v => log::info!("Enable 5V Output"), + commands::control::SetPower::Disable5v => log::info!("Disable 5V Output"), } Ok(())