use clap::{arg, value_parser, ArgAction};
use phidget::{devices::VoltageRatioInput, Phidget};
use std::{thread, time::Duration};
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() -> anyhow::Result<()> {
let opts = clap::Command::new("voltage_ratio_in")
.version(VERSION)
.author(env!("CARGO_PKG_AUTHORS"))
.about("Phidget Voltage Ratio Input 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!(-h --hub "Use a hub VINT input port directly").action(ArgAction::SetTrue))
.arg(
arg!(-o --offset [offset] "The offset for reading [val = gain * (volt_ratio - offset)]")
.default_value("0.0")
.value_parser(value_parser!(f64)),
)
.arg(
arg!(-g --gain [gain] "The gain for the reading [val = gain * (volts_ratio - offset)]")
.default_value("1.0")
.value_parser(value_parser!(f64)),
)
.get_matches();
let use_hub = opts.get_flag("hub");
println!("Opening Phidget bridge input device...");
let mut vin = VoltageRatioInput::new();
vin.set_is_hub_port_device(use_hub)?;
if let Some(&port) = opts.get_one::<i32>("port") {
vin.set_hub_port(port)?;
}
if let Some(&num) = opts.get_one::<i32>("serial") {
vin.set_serial_number(num)?;
}
if let Some(&chan) = opts.get_one::<i32>("channel") {
vin.set_channel(chan)?;
}
let offset = *opts.get_one::<f64>("offset").unwrap();
let gain = *opts.get_one::<f64>("gain").unwrap();
vin.open_wait_default()?;
if use_hub {
let port = vin.hub_port()?;
println!("Opened on hub port: {}", port);
}
let min_interval = vin.min_data_interval().unwrap();
vin.set_data_interval(min_interval)?;
println!("This device features a 2-3 second calibration procedure once first opened...");
thread::sleep(Duration::from_millis(2000));
println!("Calibration procedure complete");
let v = vin.voltage_ratio()?;
let val = (v - offset) * gain;
println!("{:.4}", val);
vin.set_on_voltage_ratio_change_handler(move |_, v| {
let val = (v - offset) * gain;
println!("{:.4}", val);
})?;
ctrlc::set_handler({
let thr = thread::current();
move || {
println!("\nExiting...");
thr.unpark();
}
})
.expect("Error setting Ctrl-C handler");
thread::park();
Ok(())
}