| { | ||
| "git": { | ||
| "sha1": "bbdce3d50704c0abf2ce04bc60f8aee6af254f6b" | ||
| } | ||
| } |
| /**************************************************************************** | ||
| Copyright (c) 2018 Roland Ruckerbauer All Rights Reserved. | ||
| This file is part of hidapi-rs, based on hidapi-rs by Osspial | ||
| ****************************************************************************/ | ||
| //! Opens the first hid device it can find, and reads data in a blocking fashion | ||
| //! from it in an endless loop. | ||
| extern crate failure; | ||
| extern crate hidapi; | ||
| use failure::Error; | ||
| use hidapi::HidApi; | ||
| fn main() { | ||
| fn run() -> Result<(), Error> { | ||
| let hidapi = HidApi::new()?; | ||
| let device_info = hidapi | ||
| .devices() | ||
| .iter() | ||
| .next() | ||
| .expect("No devices are available!") | ||
| .clone(); | ||
| println!("Opening device:\n {:#?}\n", device_info); | ||
| let device = device_info.open_device(&hidapi)?; | ||
| let mut buf = vec![0; 64]; | ||
| println!("Reading data from device ...\n"); | ||
| loop { | ||
| let len = device.read(&mut buf)?; | ||
| println!("{:?}", &buf[..len]); | ||
| } | ||
| Ok(()) | ||
| } | ||
| if let Err(e) = run() { | ||
| eprintln!("Error: {}", e); | ||
| } | ||
| } |
+2
-2
@@ -15,4 +15,4 @@ # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO | ||
| name = "hidapi" | ||
| version = "0.5.0" | ||
| authors = ["Roland Ruckerbauer <roland.rucky@gmail.com>", "Osspial <osspial@gmail.com>", "Artyom Pavlov <newpavlov@gmail.com>", "mberndt123"] | ||
| version = "0.5.1" | ||
| authors = ["Roland Ruckerbauer <roland.rucky@gmail.com>", "Osspial <osspial@gmail.com>", "Artyom Pavlov <newpavlov@gmail.com>", "mberndt123", "niklasad1"] | ||
| build = "build.rs" | ||
@@ -19,0 +19,0 @@ links = "hidapi" |
+1
-1
@@ -19,3 +19,3 @@ # hidapi [](https://travis-ci.org/ruabmbua/hidapi-rs) [](https://crates.io/crates/hidapi) [](https://github.com/Osspial/hidapi-rs/blob/master/LICENSE.txt) [](https://docs.rs/hidapi) | ||
| // Print out information about all connected devices | ||
| for device in &api.devices() { | ||
| for device in api.devices() { | ||
| println!("{:#?}", device); | ||
@@ -22,0 +22,0 @@ } |
+6
-7
@@ -52,2 +52,3 @@ // ************************************************************************** | ||
| use std::rc::Rc; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
@@ -64,9 +65,9 @@ pub use error::HidError; | ||
| fn acquire() -> HidResult<HidApiLock> { | ||
| if unsafe { !HID_API_LOCK } { | ||
| if HID_API_LOCK.compare_and_swap(false, true, Ordering::SeqCst) { | ||
| // Initialize the HID and prevent other HIDs from being created | ||
| unsafe { | ||
| if ffi::hid_init() == -1 { | ||
| HID_API_LOCK.store(false, Ordering::SeqCst); | ||
| return Err(HidError::InitializationError); | ||
| } | ||
| HID_API_LOCK = true; | ||
| Ok(HidApiLock) | ||
@@ -82,6 +83,4 @@ } | ||
| fn drop(&mut self) { | ||
| unsafe { | ||
| ffi::hid_exit(); | ||
| HID_API_LOCK = false; | ||
| } | ||
| unsafe { ffi::hid_exit(); } | ||
| HID_API_LOCK.store(false, Ordering::SeqCst); | ||
| } | ||
@@ -97,3 +96,3 @@ } | ||
| static mut HID_API_LOCK: bool = false; | ||
| static HID_API_LOCK: AtomicBool = AtomicBool::new(false); | ||
@@ -100,0 +99,0 @@ impl HidApi { |
Sorry, the diff of this file is not supported yet