Skip to main content

SpaceMouseThread

Struct SpaceMouseThread 

Source
pub struct SpaceMouseThread { /* private fields */ }
Expand description

Cross-platform SpaceMouse HID capture thread.

Handle to a background thread, which continuously (re-)discovers a compatible device, reads its raw HID reports, and forwards the resulting SpaceMouseEvents to the callback given to SpaceMouseThread::new. The thread stops when the handle is dropped. Constructing one is a safe no-op on a machine without a compatible device connected. A device that is plugged in later, or unplugged and replugged, is picked up automatically. If the HID subsystem cannot be initialized, the thread ends immediately and reports nothing.

§Example

Print all events of a connected device until Enter is pressed:

use spacemouse::{SpaceMouseEvent, SpaceMouseThread};
use std::io;

fn on_event_callback(event: SpaceMouseEvent) {
  match event {
    SpaceMouseEvent::Connected => {
      println!("connected");
    }
    SpaceMouseEvent::Disconnected => {
      println!("disconnected");
    }
    SpaceMouseEvent::Translation { x, y, z } => {
      println!("translation: {x} {y} {z}");
    }
    SpaceMouseEvent::Rotation { x, y, z } => {
      println!("rotation: {x} {y} {z}");
    }
    _ => {}
  }
}

// Start the background thread, which automatically connects to the device.
let thread = SpaceMouseThread::new(on_event_callback);

println!("Listening for SpaceMouse events. Press Enter to quit.");
let _ = io::stdin().read_line(&mut String::new());

// Dropping the handle stops the background thread.
drop(thread);

Implementations§

Source§

impl SpaceMouseThread

Source

pub fn new<F>(on_event: F) -> Self
where F: Fn(SpaceMouseEvent) + Send + 'static,

Start capturing in a new background thread.

on_event is invoked on the background thread, not the caller’s thread, for every SpaceMouseEvent. Callers that need events on a specific thread are responsible for the hop themselves.

Waits between scans (while no compatible device is open) using the default scan interval. See SpaceMouseThread::with_rescan_interval for a configurable scan period.

Examples found in repository?
examples/threaded.rs (line 10)
9fn main() {
10  let _spacemouse = SpaceMouseThread::new(|event| println!("{event:?}"));
11  println!("Listening for SpaceMouse events. Press Enter to quit.");
12  let _ = io::stdin().read_line(&mut String::new());
13}
Source

pub fn with_rescan_interval<F>(rescan_interval: Duration, on_event: F) -> Self
where F: Fn(SpaceMouseEvent) + Send + 'static,

Like SpaceMouseThread::new, but waits rescan_interval between scans while no compatible device is open.

Shorter intervals pick up a newly connected device sooner, at the cost of enumerating the system’s HID devices more often. Values below 100 ms are raised to 100 ms.

Source

pub fn is_connected(&self) -> bool

Whether a compatible device is currently open.

Source

pub fn set_led(&self, enabled: bool)

Set whether the device’s LED is lit.

Applied immediately if a device is currently open, and reapplied whenever a device is (re)opened. Devices without an LED and write failures are silently ignored.

Trait Implementations§

Source§

impl Drop for SpaceMouseThread

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.