// phidget-rs/examples/voltage_in_ratio.rs // // Copyright (c) 2023, Frank Pagliughi // Copyright (c) 2024 Jorge Guerra and Riley Hernandez // // This file is an example application for the 'phidget-rs' library. // // Licensed under the MIT license: // // This file may not be copied, modified, or distributed except according // to those terms. // //! Rust Phidget example application to read voltage ratio input values. //! //! A number of sensors have a simple, linear, formula to get a value in //! the form: //! //! ```text //! value = (voltage_ratio - offset) * gain //! ``` //! On a real application the offset and gain can be determined via a calibration procedure //! (https://www.phidgets.com/docs/Calibrating_Load_Cells) //! For this example, assume an offset of 2.5 and a gain (scaling factor) of 16 resulting //! in a weight in grams. To see the output in grams, run this: //! //! ```text //! $ voltage_in_ratio -o 2.5 -g 16.0 //! ``` //! //! The input channel can be selected by choosing the serial number of a device //! and channel number for the input. //! use clap::{arg, value_parser, ArgAction}; use phidget::{devices::VoltageRatioInput, Phidget}; use std::{thread, time::Duration}; // The open/connect timeout // const TIMEOUT: Duration = phidget::TIMEOUT_DEFAULT; const TIMEOUT: Duration = phidget::TIMEOUT_DEFAULT; // The package version is used as the app version const VERSION: &str = env!("CARGO_PKG_VERSION"); // -------------------------------------------------------------------------- fn main() -> anyhow::Result<()> { let opts = clap::Command::new("bridge_in") .version(VERSION) .author(env!("CARGO_PKG_AUTHORS")) .about("Phidget Voltage (Analog) 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!(-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(); println!("Opening Phidget bridge input device..."); let mut vin: VoltageRatioInput = VoltageRatioInput::new(); //Some other device selection filters.. if let Some(&num) = opts.get_one::("serial") { vin.set_serial_number(num)?; } if let Some(&chan) = opts.get_one::("channel") { vin.set_channel(chan)?; } let offset = *opts.get_one::("offset").unwrap(); let gain = *opts.get_one::("gain").unwrap(); vin.open_wait(TIMEOUT)?; 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); })?; // ^C handler wakes up the main thread ctrlc::set_handler({ let thr = thread::current(); move || { println!("\nExiting..."); thr.unpark(); } }) .expect("Error setting Ctrl-C handler"); // // // Block until a ^C wakes us up to exit. thread::park(); Ok(()) }