Skip to content

Commit

Permalink
ENH: Detect when FTDI driver is missing and raise an informative error
Browse files Browse the repository at this point in the history
  • Loading branch information
TEParsons committed Nov 21, 2024
1 parent fc66fe4 commit ddcb49c
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion psychopy_cedrus/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

from psychopy.hardware.manager import deviceManager, DeviceManager, ManagedDeviceError
from psychopy import logging, layout, __version__ as ppyVersion
import pyxid2
import time
from packaging.version import Version
# voicekey is only available from 2015.1.0 onwards, so import with a safe fallback
Expand All @@ -14,6 +13,14 @@
from psychopy.hardware.base import BaseResponseDevice as BaseVoiceKeyGroup, BaseResponse as VoiceKeyResponse


# check whether FTDI driver is installed
hasDriver = False
try:
import ftd2xx
hasDriver = True
except FileNotFoundError:
pass

class BaseXidDevice(BaseDevice):
"""
Base class for all Cedrus XID devices.
Expand All @@ -36,6 +43,15 @@ class BaseXidDevice(BaseDevice):
productId = None

def __init__(self, index=0):
# error if there's no ftdi driver
if hasDriver:
import pyxid2
else:
raise ModuleNotFoundError(
"Could not connect to Cedrus device as your computer is missing a necessary "
"hardware driver. You should be able to find the correct driver for your operating "
"system here: https://ftdichip.com/drivers/vcp-drivers/"
)
# give error if no device connected
if not len(self.getAvailableDevices()):
raise ConnectionError("No Cedrus device is connected.")
Expand Down Expand Up @@ -123,6 +139,11 @@ def isSameDevice(self, other):

@classmethod
def getAvailableDevices(cls):
if hasDriver:
import pyxid2
else:
# if missing FTDI driver, return blank rather than erroring
return []
# list devices
devices = []
# iterate through profiles of all serial port devices
Expand Down

0 comments on commit ddcb49c

Please sign in to comment.