Skip to content

Commit

Permalink
FF: Account for the fact that connected devices don't show up with ge…
Browse files Browse the repository at this point in the history
…t_xid_devices
  • Loading branch information
TEParsons committed Oct 3, 2024
1 parent b7b3bec commit 7872351
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions psychopy_cedrus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class BaseXidDevice(BaseDevice):
)
# all Cedrus devices have a product ID - subclasses should specify what this is
productId = None
# keep track of instances, as pyxid2 will not show active instances
instances = []

def __init__(self, index=0):
# give error if no device connected
Expand All @@ -42,6 +44,8 @@ def __init__(self, index=0):
# get xid device
self.index = index
self.xid = pyxid2.get_xid_device(index)
# store xid instance
BaseXidDevice.instances.append(self)
# nodes
self.nodes = []
# dict of responses by timestamp
Expand All @@ -50,6 +54,15 @@ def __init__(self, index=0):
self._lastTimerReset = None
self.resetTimer()

def __del__(self):
# remove instance from record on deletion
i = None
for i, obj in enumerate(self.instances):
if obj is self:
break
if i is not None:
self.instances.pop(i)

@classmethod
def resolve(cls, requested):
"""
Expand Down Expand Up @@ -109,6 +122,9 @@ def isSameDevice(self, other):
if isinstance(other, type(self)):
# if given another object, get index
index = other.index
elif isinstance(other, (BaseXidButtonGroup, BaseXidPhotodiodeGroup, BaseXidPhotodiodeGroup)):
# if given a child object, get its parent's index
index = other.parent.index
elif isinstance(other, dict) and "index" in other:
# if given a dict, get index from key
index = other['index']
Expand All @@ -130,6 +146,12 @@ def getAvailableDevices(cls):
'deviceName': profile.device_name,
'index': i,
})
# get_xid_devices only shows unconnected devices, so include connected ones here
for (i, inst) in enumerate(BaseXidDevice.instances):
devices.append({
'deviceName': inst.xid.device_name,
'index': inst.index,
})

return devices

Expand Down Expand Up @@ -229,8 +251,8 @@ def isSameDevice(self, other):
# if given another BaseXidPhotodiodeGroup, compare parent boxes
other = other.parent
elif isinstance(other, dict) and "pad" in other:
# if given a dict, make sure we have a `port` rather than a `pad`
other['port'] = other['pad']
# if given a dict, make sure we have an `index` rather than a `pad`
other['index'] = other.pop('pad')
# use parent's comparison method
return self.parent.isSameDevice(other)

Expand Down Expand Up @@ -337,8 +359,8 @@ def isSameDevice(self, other):
# if given another BaseXidButtonGroup, compare parent boxes
other = other.parent
elif isinstance(other, dict) and "pad" in other:
# if given a dict, make sure we have a `port` rather than a `pad`
other['port'] = other['pad']
# if given a dict, make sure we have an `index` rather than a `pad`
other['index'] = other.pop('pad')
# use parent's comparison method
return self.parent.isSameDevice(other)

Expand Down Expand Up @@ -434,8 +456,8 @@ def isSameDevice(self, other):
# if given another BaseXidVoiceKeyGroup, compare parent boxes
other = other.parent
elif isinstance(other, dict) and "pad" in other:
# if given a dict, make sure we have a `port` rather than a `pad`
other['port'] = other['pad']
# if given a dict, make sure we have an `index` rather than a `pad`
other['index'] = other.pop('pad')
# use parent's comparison method
return self.parent.isSameDevice(other)

Expand Down

0 comments on commit 7872351

Please sign in to comment.