From 2ccd88f9195876d832a6b85ca5a9492ce305cdaf Mon Sep 17 00:00:00 2001 From: Jim Ewald Date: Wed, 15 Feb 2017 11:23:28 -0800 Subject: [PATCH] Add code to detect FTDI driver state for Windows. --- BlocklyHardware.py | 56 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 47 insertions(+), 9 deletions(-) diff --git a/BlocklyHardware.py b/BlocklyHardware.py index 26be7e0..f1ce93e 100644 --- a/BlocklyHardware.py +++ b/BlocklyHardware.py @@ -3,15 +3,17 @@ from sys import platform from subprocess import Popen, PIPE, check_output, CalledProcessError -# Define platform constants +# Platform constants +PLATFORM_LINUX = 'linux2' PLATFORM_MACOS = 'darwin' -PLATFORM_UBUNTU = 'linux2' +PLATFORM_WINDOWS = 'win32' # Define error constants FTDI_DRIVER_NOT_INSTALLED = 201 FTDI_DRIVER_NOT_LOADED = 202 PLATFORM_UNKNOWN = 2 + # Enable logging __module_logger = logging.getLogger('blockly') @@ -29,8 +31,10 @@ def init(): def is_module_installed(): if platform == PLATFORM_MACOS: return __is_module_installed_macos() - elif platform == PLATFORM_UBUNTU: - return __is_module_installed_ubuntu() + elif platform == PLATFORM_LINUX: + return __is_module_installed_linux() + elif platform == PLATFORM_WINDOWS: + return __is_module_installed_windows() else: return PLATFORM_UNKNOWN @@ -38,14 +42,14 @@ def is_module_installed(): def is_module_loaded(): if platform == PLATFORM_MACOS: return __is_module_loaded_macos() - elif platform == PLATFORM_UBUNTU: - return __is_module_loaded_ubuntu() + elif platform == PLATFORM_LINUX: + return __is_module_loaded_linux() else: return PLATFORM_UNKNOWN # Ubuntu implementation -def __is_module_installed_ubuntu(): +def __is_module_installed_linux(): try: process = Popen(['cat', '/proc/modules'], stdout=PIPE, stderr=PIPE) output = check_output(('grep', 'ftdi'), stdin=process.stdout) @@ -57,7 +61,7 @@ def __is_module_installed_ubuntu(): return FTDI_DRIVER_NOT_INSTALLED -def __is_module_loaded_ubuntu(): +def __is_module_loaded_linux(): try: process = Popen(['dmesg', '-H', '-x'], stdout=PIPE, stderr=PIPE) output = check_output(('grep', 'ftdi'), stdin=process.stdout) @@ -91,4 +95,38 @@ def __is_module_loaded_macos(): return 0 except CalledProcessError: __module_logger.warning('No FTDI modules detected.') - return 1 \ No newline at end of file + return 1 + + +# Windows implementation +def __is_module_installed_windows(): + try: + out, err = Popen('driverquery.exe -v', stdout=PIPE).communicate() + position = out.find('FT') + if position > 0: + __module_logger.debug('FTDI module load state') + __module_logger.debug(out[position:position+91]) + return 0 + else: + __module_logger.warning('No FTDI modules installed.') + return FTDI_DRIVER_NOT_INSTALLED + except CalledProcessError: + __module_logger.warning('No FTDI modules detected.') + return FTDI_DRIVER_NOT_INSTALLED + + +def __is_module_loaded_windows(): + try: + out, err = Popen('driverquery.exe -v', stdout=PIPE).communicate() + position = out.find('FT') + if position > 0: + __module_logger.debug('FTDI module load state') + __module_logger.debug(out[position+84:position+91]) + return 0 + else: + __module_logger.warning('No FTDI modules loaded.') + return FTDI_DRIVER_NOT_INSTALLED + except CalledProcessError: + __module_logger.warning('No FTDI modules detected.') + return FTDI_DRIVER_NOT_INSTALLED +