Skip to content

Commit

Permalink
Merge pull request #51 from zfi/demo
Browse files Browse the repository at this point in the history
Add code to detect FTDI driver state for Windows.
  • Loading branch information
zfi authored Feb 15, 2017
2 parents b071aba + 2ccd88f commit cffe447
Showing 1 changed file with 47 additions and 9 deletions.
56 changes: 47 additions & 9 deletions BlocklyHardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand All @@ -29,23 +31,25 @@ 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


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)
Expand All @@ -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)
Expand Down Expand Up @@ -91,4 +95,38 @@ def __is_module_loaded_macos():
return 0
except CalledProcessError:
__module_logger.warning('No FTDI modules detected.')
return 1
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

0 comments on commit cffe447

Please sign in to comment.