use clap::{arg, value_parser, ArgAction};
use phidget::{devices::VoltageOutput, Phidget};
use std::thread;
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() -> anyhow::Result<()> {
let opts = clap::Command::new("voltage_out")
.version(VERSION)
.author(env!("CARGO_PKG_AUTHORS"))
.about("Phidget Voltage (Analog) Output Example")
.disable_help_flag(true)
.arg(
arg!(--help "Print help information")
.short('?')
.action(ArgAction::Help),
)
.arg(
arg!(-s --serial [serial_num] "Specify the serial number of the device to open")
.value_parser(value_parser!(i32)),
)
.arg(
arg!(-c --channel [chan] "Specify the channel number of the device to open")
.value_parser(value_parser!(i32)),
)
.arg(
arg!(-p --port [port] "Use a specific port on a VINT hub directly")
.value_parser(value_parser!(i32)),
)
.arg(
arg!(-o --offset [offset] "The offset for reading [val = gain * (volts - offset)]")
.default_value("0.0")
.value_parser(value_parser!(f64)),
)
.arg(
arg!(-g --gain [gain] "The gain for the reading [val = gain * (volts - offset)]")
.default_value("1.0")
.value_parser(value_parser!(f64)),
)
.arg(arg!(<val> "The analog value to output").value_parser(value_parser!(f64)))
.get_matches();
let val = *opts.get_one::<f64>("val").unwrap();
println!("Opening Phidget voltage output device...");
let mut vout = VoltageOutput::new();
if let Some(&port) = opts.get_one::<i32>("port") {
vout.set_hub_port(port)?;
}
if let Some(&num) = opts.get_one::<i32>("serial") {
vout.set_serial_number(num)?;
}
if let Some(&chan) = opts.get_one::<i32>("channel") {
vout.set_channel(chan)?;
}
let offset = *opts.get_one::<f64>("offset").unwrap();
let gain = *opts.get_one::<f64>("gain").unwrap();
vout.open_wait_default()?;
let v = val / gain + offset;
println!("{:.4}", v);
vout.set_voltage(v)?;
ctrlc::set_handler({
let thr = thread::current();
move || {
println!("\nExiting...");
thr.unpark();
}
})
.expect("Error setting Ctrl-C handler");
thread::park();
Ok(())
}