Skip to content

Commit 52c1a06

Browse files
committed
Does not crash if invalid connection
1 parent a65228f commit 52c1a06

File tree

1 file changed

+23
-18
lines changed

1 file changed

+23
-18
lines changed

src/ephys_link/bindings/ump_binding.py

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,6 @@ def __init__(self) -> None:
3434
UMP.set_library_path(RESOURCES_DIRECTORY)
3535
self._ump: UMP = UMP.get_ump() # pyright: ignore [reportUnknownMemberType]
3636

37-
# Exit if no manipulators are connected.
38-
device_ids: list[str] = list(map(str, self._ump.list_devices()))
39-
if len(device_ids) == 0:
40-
msg = "No manipulators connected."
41-
raise RuntimeError(msg)
42-
43-
# Currently only supports using uMp-4 XOR uMp-3. Exit if both are connected.
44-
45-
# Use the first device as the reference for the number of axes.
46-
self.num_axes: int = self._get_device(device_ids[0]).n_axes()
47-
48-
if any(self._get_device(device_id).n_axes() != self.num_axes for device_id in device_ids): # pyright: ignore [reportUnknownArgumentType, reportUnknownMemberType]
49-
msg = "uMp-4 and uMp-3 cannot be used at the same time."
50-
raise RuntimeError(msg)
51-
5237
@staticmethod
5338
@override
5439
def get_display_name() -> str:
@@ -61,11 +46,31 @@ def get_cli_name() -> str:
6146

6247
@override
6348
async def get_manipulators(self) -> list[str]:
64-
return list(map(str, self._ump.list_devices()))
49+
device_ids = list(map(str, self._ump.list_devices()))
50+
51+
# Shortcut for empty device list.
52+
if len(device_ids) == 0:
53+
return []
54+
55+
first_device_axis_count = self._get_device(device_ids[0]).n_axes()
56+
57+
# Currently only supports using uMp-4 XOR uMp-3. Throw error if both are connected.
58+
if any(self._get_device(device_id).n_axes() != first_device_axis_count for device_id in device_ids): # pyright: ignore [reportUnknownArgumentType, reportUnknownMemberType]
59+
msg = "uMp-4 and uMp-3 cannot be used at the same time."
60+
raise RuntimeError(msg)
61+
62+
return device_ids
6563

6664
@override
6765
async def get_axes_count(self) -> int:
68-
return self.num_axes
66+
device_ids = await self.get_manipulators()
67+
68+
# If no manipulators are connected, return 0.
69+
if len(device_ids) == 0:
70+
return 0
71+
72+
# If multiple manipulators are connected, return the number of axes of the first one.
73+
return self._get_device(device_ids[0]).n_axes()
6974

7075
@override
7176
def get_dimensions(self) -> Vector4:
@@ -236,4 +241,4 @@ def _is_ump_3(self) -> bool:
236241
Returns:
237242
True if the device is uMp-3, False otherwise.
238243
"""
239-
return self.num_axes == self.UMP_3_NUM_AXES
244+
return self.get_axes_count() == self.UMP_3_NUM_AXES

0 commit comments

Comments
 (0)