Skip to content

Commit 2a2a026

Browse files
peterpeter
authored andcommitted
update API for listing connected spacemouse to better handle multiple different types of spacemouse
1 parent 17e5296 commit 2a2a026

File tree

6 files changed

+41
-32
lines changed

6 files changed

+41
-32
lines changed

examples/03_multi_device.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,19 @@
1111

1212
def main():
1313
# First, discover connected devices
14-
connected = pyspacemouse.get_connected_devices()
15-
print(f"Found {len(connected)} device(s): {connected}")
14+
devices = pyspacemouse.get_connected_spacemice()
15+
paths = [path for path, name in devices]
16+
names = [name for path, name in devices]
17+
print(f"Found {len(names)} spacemouse device(s): {names}")
1618

17-
if len(connected) < 2:
19+
if len(names) < 2:
1820
print("This example requires 2 SpaceMouse devices connected.")
1921
print("Tip: Use a 3Dconnexion Universal Receiver with device_index parameter")
2022
return
2123

22-
# Open two devices using device_index
23-
# device_index=0 is the first device, device_index=1 is the second
24-
device_name = connected[0]
25-
26-
with pyspacemouse.open(device=device_name, device_index=0) as left_hand:
27-
with pyspacemouse.open(device=device_name, device_index=1) as right_hand:
24+
# Open two devices by path
25+
with pyspacemouse.open_by_path(paths[0]) as left_hand:
26+
with pyspacemouse.open_by_path(paths[1]) as right_hand:
2827
print(f"Left hand: {left_hand.name}")
2928
print(f"Right hand: {right_hand.name}")
3029
print()
@@ -35,8 +34,8 @@ def main():
3534
right = right_hand.read()
3635

3736
print(
38-
f"L: x={left.x:+.2f} y={left.y:+.2f} z={left.z:+.2f} | "
39-
f"R: x={right.x:+.2f} y={right.y:+.2f} z={right.z:+.2f}"
37+
f"Left: x={left.x:+.2f} y={left.y:+.2f} z={left.z:+.2f} | "
38+
f"Right: x={right.x:+.2f} y={right.y:+.2f} z={right.z:+.2f}"
4039
)
4140
time.sleep(0.02)
4241

examples/05_discovery.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ def main():
1515

1616
# 1. List connected SpaceMouse devices
1717
print("Connected SpaceMouse devices:")
18-
connected = pyspacemouse.get_connected_devices()
18+
connected = pyspacemouse.get_connected_spacemice()
19+
connected_names = [name for _, name in connected]
1920
if connected:
20-
for name in connected:
21+
for name in connected_names:
2122
print(f" ✓ {name}")
2223
else:
2324
print(" (none found)")
@@ -28,7 +29,7 @@ def main():
2829
supported = pyspacemouse.get_supported_devices()
2930
for name, vid, pid in supported:
3031
# Check if this device type is connected
31-
is_connected = name in connected
32+
is_connected = name in connected_names
3233
status = "✓" if is_connected else " "
3334
print(f" [{status}] {name} (VID: {vid:#06x}, PID: {pid:#06x})")
3435
print()

examples/09_custom_config.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,20 @@ def example_modify_existing():
2323
print(f"Available devices: {list(specs.keys())}")
2424

2525
# Get connected devices
26-
connected = pyspacemouse.get_connected_devices()
26+
connected = pyspacemouse.get_connected_spacemice()
2727
if not connected:
2828
print("No devices connected!")
2929
return
30+
if len(connected) > 1:
31+
print("This example only works with one device connected.")
32+
return
3033

31-
device_name = connected[0]
34+
device = connected[0]
35+
device_path, device_name = device
3236
print(f"Using device: {device_name}")
3337

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

3842
# Create modified spec with inverted Y and Z (common for ROS)
@@ -62,18 +66,23 @@ def example_invert_rotations():
6266
print("Example 2: Fix rotation conventions")
6367
print("=" * 60)
6468

65-
connected = pyspacemouse.get_connected_devices()
69+
connected = pyspacemouse.get_connected_spacemice()
6670
if not connected:
6771
print("No devices connected!")
6872
return
73+
if len(connected) > 1:
74+
print("This example only works with one device connected.")
75+
return
6976

77+
device = connected[0]
78+
device_path, device_name = device
7079
specs = pyspacemouse.get_device_specs()
71-
base_spec = specs[connected[0]]
80+
base_spec = specs[device_name]
7281

7382
# Invert roll and yaw for right-handed coordinate system
7483
fixed_spec = pyspacemouse.modify_device_info(
7584
base_spec,
76-
name=f"{connected[0]} (Fixed Rotations)",
85+
name=f"{device_name} (Fixed Rotations)",
7786
invert_axes=["roll", "yaw"],
7887
)
7988

pyspacemouse/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
# Public API
3838
from .api import (
3939
get_all_hid_devices,
40-
get_connected_devices,
40+
get_connected_spacemice,
4141
get_supported_devices,
4242
open,
4343
open_by_path,
@@ -95,7 +95,7 @@
9595
"SpaceMouseDevice",
9696
# API
9797
"get_all_hid_devices",
98-
"get_connected_devices",
98+
"get_connected_spacemice",
9999
"get_supported_devices",
100100
"open",
101101
"open_by_path",

pyspacemouse/api.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
from .types import DeviceInfo, SpaceMouseState
2828

2929

30-
def get_connected_devices() -> List[str]:
31-
"""Return a list of the supported devices currently connected.
30+
def get_connected_spacemice() -> List[Tuple[str, str]]:
31+
"""Return the paths and names of the supported devices currently connected.
3232
3333
Returns:
34-
List of device names that are both supported and connected.
34+
Tuple of two lists: (device_paths, device_names)
3535
Empty list if no supported devices are found.
3636
3737
Raises:
@@ -45,14 +45,14 @@ def get_connected_devices() -> List[str]:
4545
) from e
4646

4747
device_specs = get_device_specs()
48-
devices = []
48+
devices_by_path = {}
4949

5050
for hid_device in hid.find():
5151
for name, spec in device_specs.items():
5252
if hid_device.vendor_id == spec.vendor_id and hid_device.product_id == spec.product_id:
53-
devices.append(name)
53+
devices_by_path[hid_device.path] = name
5454

55-
return devices
55+
return list(devices_by_path.items())
5656

5757

5858
def get_supported_devices() -> List[Tuple[str, int, int]]:
@@ -253,10 +253,10 @@ def open(
253253

254254
# Auto-detect device if not specified
255255
if device is None:
256-
connected = get_connected_devices()
256+
connected = get_connected_spacemice()
257257
if not connected:
258258
raise RuntimeError("No connected or supported devices found.")
259-
device = connected[0]
259+
device = connected[0][1]
260260

261261
if device not in device_specs:
262262
raise ValueError(f"Unknown device: '{device}'. Available: {list(device_specs.keys())}")

pyspacemouse/pyspacemouse_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ def print_version_cli():
1313

1414
def list_spacemouse_cli():
1515
"""List connected SpaceMouse devices."""
16-
devices = pyspacemouse.get_connected_devices()
16+
devices = pyspacemouse.get_connected_spacemice()
1717
if devices:
1818
print("Connected SpaceMouse devices:")
19-
for device in devices:
19+
for _, device in devices:
2020
print(f" - {device}")
2121
else:
2222
print("No connected SpaceMouse devices found.")

0 commit comments

Comments
 (0)