6 releases

0.2.0 Oct 22, 2024
0.1.4 May 30, 2024
0.1.3 Mar 10, 2024
0.1.1 Apr 20, 2023

#250 in Embedded development

Download history 22/week @ 2024-09-23 149/week @ 2024-10-21 7/week @ 2024-10-28 4/week @ 2024-11-04

160 downloads per month

MIT license

765KB
23K SLoC

phidget-rs

A safe Rust wrapper around the phidgets22 library for interacting with Phidget sensors and actuators.

This is currently an early-stage project to wrap the Phidget API. It is intended to be a production-quality crate for use in real industrial settings.

Note that the authors only have a limited number of sensors available. Pull Requests are glady accepted for any additional types of Phidgets to add to the library, or let us know if you have a device that you would like to see supported and are willing to test and validate.

Minimum Supported Rust Version (MSRV)

v1.73

This package uses Rust Edition 2021, requiring an MSRV of 1.73. Although it may build and work with slightly older versions of the compiler, this is the oldest version being tested and maintained by the developers.


lib.rs:

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();

Dependencies

~0–7.5MB
~44K SLoC