Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: More informative errors if TPad can't be found #14

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Changes from all 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
36 changes: 28 additions & 8 deletions psychopy_bbtk/tpad.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@
from psychopy.hardware.voicekey import BaseVoiceKeyGroup, VoiceKeyResponse
except ImportError:
from psychopy.hardware.base import BaseResponseDevice as BaseVoiceKeyGroup, BaseResponse as VoiceKeyResponse

# DeviceNotFoundError is only available from 2025.1.0 onwards, so import with a safe fallback
try:
from psychopy.hardware.exceptions import DeviceNotConnectedError
except ImportError:
class DeviceNotConnectedError(ConnectionError):
def __init__(self, msg, deviceClass=None, context=None, *args):
ConnectionError.__init__(self, msg)

# check whether FTDI driver is installed
hasDriver = False
Expand Down Expand Up @@ -458,9 +464,28 @@ def __init__(
"hardware driver. You should be able to find the correct driver for your operating "
"system here: https://ftdichip.com/drivers/vcp-drivers/"
)
# get port if not given
# get ports with a TPad connected
possiblePorts = self._detectComPort()
# error if there are none
if not possiblePorts:
raise DeviceNotConnectedError(
(
"Could not find any connected TPad. Try checking the USB cable or restarting "
"your PC."
),
deviceClass=TPad
)
# if no port given, take first valid one
if port is None:
port = self._detectComPort()[0]
port = possiblePorts[0]
# if port doesn't have a TPad on, error
if port not in possiblePorts:
raise DeviceNotConnectedError(
(
"Could not find a TPad on {port}, but did find TPad(s) on: {possiblePorts}.",
).format(port=port, possiblePorts=possiblePorts),
deviceClass=TPad
)
# initial value for last timer reset
self._lastTimerReset = logging.defaultClock._timeAtLastReset
# dict of responses by timestamp
Expand Down Expand Up @@ -663,11 +688,6 @@ def hasUnfinishedMessage(self):
def _detectComPort():
# find available devices
available = TPad.getAvailableDevices()
# error if there are none
if not available:
raise ConnectionError(
"Could not find any TPad."
)
# get all available ports
return [profile['port'] for profile in available]

Expand Down
Loading