pub struct HidApi { /* private fields */ }Expand description
hidapi context.
The hidapi C library is lazily initialized when creating the first instance,
and never deinitialized. Therefore, it is allowed to create multiple HidApi
instances.
Each instance has its own device list cache.
Implementations§
Source§impl HidApi
impl HidApi
Sourcepub fn new() -> HidResult<Self>
pub fn new() -> HidResult<Self>
Create a new hidapi context.
Will also initialize the currently available device list if device discovery has not already been disabled.
Sourcepub fn disable_device_discovery()
pub fn disable_device_discovery()
Disable device discovery on context creation.
This may be necessary on Android, where access to USB device enumeration is limited.
§Panics
Panics if an hidapi context has already been initialized with device discovery.
Avoid using this in library code, as it is an inherently global operation.
This function is intended to be called by code that knows the environment it is running in. Usually this means application code either directly, or through another abstraction.
Sourcepub fn new_without_enumerate() -> HidResult<Self>
👎Deprecated: Please use only HidApi::new() in library code. Application code should disable device discovery explicitly.
pub fn new_without_enumerate() -> HidResult<Self>
HidApi::new() in library code. Application code should disable device discovery explicitly.Create a new hidapi context, after disabling discovery. Please avoid using this function in library code, because it forces all instances of HidApi to disable device discovery.
See HidApi::disable_device_discovery().
§Panics
Panics if an hidapi context has already been initialized with device discovery.
Sourcepub fn refresh_devices(&mut self) -> HidResult<()>
pub fn refresh_devices(&mut self) -> HidResult<()>
Refresh devices list and information about them (to access them use
device_list() method)
Identical to reset_devices() followed by add_devices(0, 0).
Sourcepub fn reset_devices(&mut self) -> HidResult<()>
pub fn reset_devices(&mut self) -> HidResult<()>
Reset devices list. Intended to be used with the add_devices method.
Sourcepub fn add_devices(&mut self, vid: u16, pid: u16) -> HidResult<()>
pub fn add_devices(&mut self, vid: u16, pid: u16) -> HidResult<()>
Indexes devices that match the given VID and PID filters. 0 indicates no filter.
Sourcepub fn device_list(&self) -> impl Iterator<Item = &DeviceInfo>
pub fn device_list(&self) -> impl Iterator<Item = &DeviceInfo>
Returns iterator containing information about attached HID devices
that have been indexed, either by refresh_devices or add_devices.
Sourcepub fn open(&self, vid: u16, pid: u16) -> HidResult<HidDevice>
pub fn open(&self, vid: u16, pid: u16) -> HidResult<HidDevice>
Open a HID device using a Vendor ID (VID) and Product ID (PID).
When multiple devices with the same vid and pid are available, then the first one found in the internal device list will be used. There are however no guarantees, which device this will be.
Sourcepub fn open_serial(&self, vid: u16, pid: u16, sn: &str) -> HidResult<HidDevice>
pub fn open_serial(&self, vid: u16, pid: u16, sn: &str) -> HidResult<HidDevice>
Open a HID device using a Vendor ID (VID), Product ID (PID) and a serial number.
Sourcepub fn open_path(&self, device_path: &CStr) -> HidResult<HidDevice>
pub fn open_path(&self, device_path: &CStr) -> HidResult<HidDevice>
The path name be determined by inspecting the device list available with HidApi::device_list.
Alternatively a platform-specific path name can be used (eg: /dev/hidraw0 on Linux).
Sourcepub fn check_error(&self) -> HidResult<HidError>
👎Deprecated since 2.2.3: use the return values from the other methodsAvailable on hidapi only.
pub fn check_error(&self) -> HidResult<HidError>
hidapi only.Get the last non-device specific error, which happened in the underlying hidapi C library.
To get the last device specific error, use HidDevice::check_error.
The Ok() variant of the result will contain a HidError::HidApiError.
When Err() is returned, then acquiring the error string from the hidapi C
library failed. The contained HidError is the cause, why no error could
be fetched.