Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions examples/03_multi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,19 @@

def main():
# First, discover connected devices
connected = pyspacemouse.get_connected_devices()
print(f"Found {len(connected)} device(s): {connected}")
devices = pyspacemouse.get_connected_devices()
paths = [path for path, name in devices]
names = [name for path, name in devices]
print(f"Found {len(names)} spacemouse device(s): {names}")

if len(connected) < 2:
if len(names) < 2:
print("This example requires 2 SpaceMouse devices connected.")
print("Tip: Use a 3Dconnexion Universal Receiver with device_index parameter")
return

# Open two devices using device_index
# device_index=0 is the first device, device_index=1 is the second
device_name = connected[0]

with pyspacemouse.open(device=device_name, device_index=0) as left_hand:
with pyspacemouse.open(device=device_name, device_index=1) as right_hand:
# Open two devices by path
with pyspacemouse.open_by_path(paths[0]) as left_hand:
with pyspacemouse.open_by_path(paths[1]) as right_hand:
print(f"Left hand: {left_hand.name}")
print(f"Right hand: {right_hand.name}")
print()
Expand All @@ -35,8 +34,8 @@ def main():
right = right_hand.read()

print(
f"L: x={left.x:+.2f} y={left.y:+.2f} z={left.z:+.2f} | "
f"R: x={right.x:+.2f} y={right.y:+.2f} z={right.z:+.2f}"
f"Left: x={left.x:+.2f} y={left.y:+.2f} z={left.z:+.2f} | "
f"Right: x={right.x:+.2f} y={right.y:+.2f} z={right.z:+.2f}"
)
time.sleep(0.02)

Expand Down
5 changes: 3 additions & 2 deletions examples/05_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ def main():
# 1. List connected SpaceMouse devices
print("Connected SpaceMouse devices:")
connected = pyspacemouse.get_connected_devices()
connected_names = [name for _, name in connected]
if connected:
for name in connected:
for name in connected_names:
print(f" ✓ {name}")
else:
print(" (none found)")
Expand All @@ -28,7 +29,7 @@ def main():
supported = pyspacemouse.get_supported_devices()
for name, vid, pid in supported:
# Check if this device type is connected
is_connected = name in connected
is_connected = name in connected_names
status = "✓" if is_connected else " "
print(f" [{status}] {name} (VID: {vid:#06x}, PID: {pid:#06x})")
print()
Expand Down
17 changes: 13 additions & 4 deletions examples/09_custom_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ def example_modify_existing():
if not connected:
print("No devices connected!")
return
if len(connected) > 1:
print("This example only works with one device connected.")
return

device_name = connected[0]
device = connected[0]
device_path, device_name = device
print(f"Using device: {device_name}")

# Get base spec and create modified version
base_spec = specs[device_name]
base_spec = specs[device_path]
print(f"Original mappings: y scale = {base_spec.mappings['y'].scale}")

# Create modified spec with inverted Y and Z (common for ROS)
Expand Down Expand Up @@ -66,14 +70,19 @@ def example_invert_rotations():
if not connected:
print("No devices connected!")
return
if len(connected) > 1:
print("This example only works with one device connected.")
return

device = connected[0]
device_path, device_name = device
specs = pyspacemouse.get_device_specs()
base_spec = specs[connected[0]]
base_spec = specs[device_name]

# Invert roll and yaw for right-handed coordinate system
fixed_spec = pyspacemouse.modify_device_info(
base_spec,
name=f"{connected[0]} (Fixed Rotations)",
name=f"{device_name} (Fixed Rotations)",
invert_axes=["roll", "yaw"],
)

Expand Down
14 changes: 7 additions & 7 deletions pyspacemouse/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
from .types import DeviceInfo, SpaceMouseState


def get_connected_devices() -> List[str]:
"""Return a list of the supported devices currently connected.
def get_connected_devices() -> List[Tuple[str, str]]:
"""Return the paths and names of the supported devices currently connected.

Returns:
List of device names that are both supported and connected.
List of tuples: (device_path, device_name).
Empty list if no supported devices are found.

Raises:
Expand All @@ -45,14 +45,14 @@ def get_connected_devices() -> List[str]:
) from e

device_specs = get_device_specs()
devices = []
devices_by_path = {}

for hid_device in hid.find():
for name, spec in device_specs.items():
if hid_device.vendor_id == spec.vendor_id and hid_device.product_id == spec.product_id:
devices.append(name)
devices_by_path[hid_device.path] = name

return devices
return list(devices_by_path.items())


def get_supported_devices() -> List[Tuple[str, int, int]]:
Expand Down Expand Up @@ -256,7 +256,7 @@ def open(
connected = get_connected_devices()
if not connected:
raise RuntimeError("No connected or supported devices found.")
device = connected[0]
device = connected[0][1]

if device not in device_specs:
raise ValueError(f"Unknown device: '{device}'. Available: {list(device_specs.keys())}")
Expand Down
4 changes: 2 additions & 2 deletions pyspacemouse/pyspacemouse_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def list_spacemouse_cli():
devices = pyspacemouse.get_connected_devices()
if devices:
print("Connected SpaceMouse devices:")
for device in devices:
print(f" - {device}")
for path, device in devices:
print(f" - {device} ({path})")
else:
print("No connected SpaceMouse devices found.")

Expand Down