ruabmbua / hidapi-rs

Rust bindings for the hidapi C library
MIT License
167 stars 80 forks source link

Open specific interface of device #101

Closed LuggaFiech closed 1 year ago

LuggaFiech commented 1 year ago

Is there any way of opening a specific interface of a composite device?

ruabmbua commented 1 year ago

Yes, the trick is that each device is enumerated multiple times for each interface / usage page etc...

Here is a modified example of logitech_gprox which opens a specific interface:

/****************************************************************************
    Copyright (c) 2022 ruabmbua All Rights Reserved.
****************************************************************************/

//! Sets the sidechannel volume of the logitech gpro x headset

extern crate hidapi;

use hidapi::HidApi;
use std::error::Error;

fn main() -> Result<(), Box<dyn Error>> {
    let vol = std::env::args()
        .nth(1)
        .map(|arg| arg.parse::<u8>())
        .ok_or("missing sidechannel volume arg")??
        .min(100);

    let api = HidApi::new()?;

    // let dev = api.open(0x046d, 0x0aaa)?;

    let Some(devinfo) = api
        .device_list()
        .filter(|d| d.vendor_id() == 0x046d && d.product_id() == 0x0aaa && d.interface_number() == 3)
        .next()
    else {
        return Err("device not found".into());
    };

    let dev = devinfo.open_device(&api)?;

    println!("Setting sidechannel volume to {}", vol);

    dev.write(&[0x11, 0xff, 0x05, 0x1c, vol])?;

    Ok(())
}