Crate phidget

Source
Expand description

Safe Rust bindings to the phidget22 library.

§Basic usage

This example shows how to access a simple Digital Input, connected to the first available channel of a Vint HUB. See the examples directory for more thorough code snippets.

use phidget::{DigitalOutput, Phidget};

// Create a handle to a Digital Output device
let mut out = DigitalOutput::new();
// Before opening the device, set its VINT hub port
out.set_is_hub_port_device(true).unwrap();
out.set_hub_port(0).unwrap();

// Start connection. Make sure to handle the result
// to check the device is available
out.open_wait_default().unwrap();

// Control the output device
loop {
    println!("Turn on LED");
    out.set_state(1).unwrap();
    std::thread::sleep(Duration::from_secs(3));

    println!("Turn off LED");
    out.set_state(0).unwrap();
    std::thread::sleep(Duration::from_secs(3));
}

§Callbacks

In order to activate an output phidget device depending on the state of other sensors, for instance by turning on an LED whenever another sensor detects something, you need to set a callback listening for sensor value changes, and keep a valid handle to the output device to set its state.

The problem is, Phidget callbacks do run in a different thread. A Phidget handle can already be sent to a different thread, as it implements Send, but it doesn’t implement Sync. Hence, if you desire to access the same handle from different callbacks, it has to be wrapped in a Sync container, such as a Mutex.

    let mut button = DigitalInput::new();
    // Open the digital output where
    // a LED is connected to.
    // In this example, it is initialized
    // and wrapped in a Mutex
    let led = Mutex::new({
        let mut tmp = DigitalOutput::new();
        tmp.set_channel(1).unwrap();
        tmp.open_wait_default().unwrap();
        tmp
    });

    // Make the button alternate the LED state
    button.set_on_state_change_handler(move |_, s: u8| {
        let lock = led.lock().unwrap();
        match s {
            // Access the device inside the Mutex and change its state
            0 => lock.set_state(0).unwrap(),
            _ => lock.set_state(1).unwrap()
        }
    }).unwrap();

Re-exports§

pub use crate::phidget::AttachCallback;
pub use crate::phidget::DetachCallback;
pub use crate::phidget::GenericPhidget;
pub use crate::phidget::Phidget;
pub use crate::phidget::PhidgetFilter;
pub use crate::phidget::PhidgetInfo;
pub use crate::phidget::PhidgetRef;
pub use crate::net::ServerType;
pub use crate::devices::current_input::CurrentInput;
pub use crate::devices::digital_input::DigitalInput;
pub use crate::devices::digital_output::DigitalOutput;
pub use crate::devices::hub::Hub;
pub use crate::devices::humidity_sensor::HumiditySensor;
pub use crate::devices::pressure_sensor::PressureSensor;
pub use crate::devices::temperature_sensor::TemperatureSensor;
pub use crate::devices::voltage_input::VoltageInput;
pub use crate::devices::voltage_output::VoltageOutput;
pub use crate::devices::voltage_ratio_input::VoltageRatioInput;
pub use crate::manager::ManagerAttachCallback;
pub use crate::manager::ManagerDetachCallback;
pub use crate::manager::PhidgetManager;
pub use phidget_sys as ffi;
pub use crate::errors::*;
pub use crate::types::*;

Modules§

devices
Module containing all implemented devices
errors
The error types for the crate The error return type for the library.
manager
this is the PhidgetManager struct. It allows discovery of the connected phidgets and provides a way to handle connect/disconnect event.
net
Network API Phidget network API
phidget
The main Phidget trait Phidget trait and PhidgetRef implementation.
types
The enumerated types for the crate Base types for the Phidgets library.

Constants§

PHIDGET_CHANNEL_ANY
PHIDGET_HUBPORTSPEED_AUTO
PHIDGET_HUBPORT_ANY
PHIDGET_SERIALNUMBER_ANY
PHIDGET_TIMEOUT_DEFAULT
PHIDGET_TIMEOUT_INFINITE
TIMEOUT_DEFAULT
The default timeout for the library
TIMEOUT_INFINITE
An infinite timeout (wait forever)

Functions§

library_version
The the full version of the phidget22 library as a string. This is something like, “Phidget22 - Version 1.14 - Built Mar 31 2023 22:44:59”
library_version_number
Gets just the version number of the phidget22 library as a string. This is something like, “1.14”
reset_library
Closes all channels, and stops all threads. The library is reset to a newly loaded state. All channel handles have been freed.