use clap::{arg, value_parser, ArgAction};
use phidget::{devices::DigitalInput, Phidget};
use std::{thread, time::Duration};
const TIMEOUT: Duration = phidget::TIMEOUT_DEFAULT;
const VERSION: &str = env!("CARGO_PKG_VERSION");
fn main() -> anyhow::Result<()> {
let opts = clap::Command::new("digital_in")
.version(VERSION)
.author(env!("CARGO_PKG_AUTHORS"))
.about("Phidget Digital 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))
.get_matches();
let use_hub = opts.get_flag("hub");
println!("Opening Phidget digital input device...");
let mut digin = DigitalInput::new();
digin.set_is_hub_port_device(use_hub)?;
if let Some(&port) = opts.get_one::<i32>("port") {
digin.set_hub_port(port)?;
}
if let Some(&num) = opts.get_one::<i32>("serial") {
digin.set_serial_number(num)?;
}
if let Some(&chan) = opts.get_one::<i32>("channel") {
digin.set_channel(chan)?;
}
digin.open_wait(TIMEOUT)?;
if use_hub {
let port = digin.hub_port()?;
println!("Opened on hub port: {}", port);
}
let s = digin.state()?;
println!("Digital: {}", s);
digin.set_on_state_change_handler(|_, s: u8| {
println!("State: {}", s);
})?;
ctrlc::set_handler({
let thr = thread::current();
move || {
println!("\nExiting...");
thr.unpark();
}
})
.expect("Error setting Ctrl-C handler");
thread::park();
Ok(())
}