33from sys import platform
44from subprocess import Popen , PIPE , check_output , CalledProcessError
55
6- # Define platform constants
6+ # Platform constants
7+ PLATFORM_LINUX = 'linux2'
78PLATFORM_MACOS = 'darwin'
8- PLATFORM_UBUNTU = 'linux2 '
9+ PLATFORM_WINDOWS = 'win32 '
910
1011# Define error constants
1112FTDI_DRIVER_NOT_INSTALLED = 201
1213FTDI_DRIVER_NOT_LOADED = 202
1314PLATFORM_UNKNOWN = 2
1415
16+
1517# Enable logging
1618__module_logger = logging .getLogger ('blockly' )
1719
@@ -29,23 +31,25 @@ def init():
2931def is_module_installed ():
3032 if platform == PLATFORM_MACOS :
3133 return __is_module_installed_macos ()
32- elif platform == PLATFORM_UBUNTU :
33- return __is_module_installed_ubuntu ()
34+ elif platform == PLATFORM_LINUX :
35+ return __is_module_installed_linux ()
36+ elif platform == PLATFORM_WINDOWS :
37+ return __is_module_installed_windows ()
3438 else :
3539 return PLATFORM_UNKNOWN
3640
3741
3842def is_module_loaded ():
3943 if platform == PLATFORM_MACOS :
4044 return __is_module_loaded_macos ()
41- elif platform == PLATFORM_UBUNTU :
42- return __is_module_loaded_ubuntu ()
45+ elif platform == PLATFORM_LINUX :
46+ return __is_module_loaded_linux ()
4347 else :
4448 return PLATFORM_UNKNOWN
4549
4650
4751# Ubuntu implementation
48- def __is_module_installed_ubuntu ():
52+ def __is_module_installed_linux ():
4953 try :
5054 process = Popen (['cat' , '/proc/modules' ], stdout = PIPE , stderr = PIPE )
5155 output = check_output (('grep' , 'ftdi' ), stdin = process .stdout )
@@ -57,7 +61,7 @@ def __is_module_installed_ubuntu():
5761 return FTDI_DRIVER_NOT_INSTALLED
5862
5963
60- def __is_module_loaded_ubuntu ():
64+ def __is_module_loaded_linux ():
6165 try :
6266 process = Popen (['dmesg' , '-H' , '-x' ], stdout = PIPE , stderr = PIPE )
6367 output = check_output (('grep' , 'ftdi' ), stdin = process .stdout )
@@ -91,4 +95,38 @@ def __is_module_loaded_macos():
9195 return 0
9296 except CalledProcessError :
9397 __module_logger .warning ('No FTDI modules detected.' )
94- return 1
98+ return 1
99+
100+
101+ # Windows implementation
102+ def __is_module_installed_windows ():
103+ try :
104+ out , err = Popen ('driverquery.exe -v' , stdout = PIPE ).communicate ()
105+ position = out .find ('FT' )
106+ if position > 0 :
107+ __module_logger .debug ('FTDI module load state' )
108+ __module_logger .debug (out [position :position + 91 ])
109+ return 0
110+ else :
111+ __module_logger .warning ('No FTDI modules installed.' )
112+ return FTDI_DRIVER_NOT_INSTALLED
113+ except CalledProcessError :
114+ __module_logger .warning ('No FTDI modules detected.' )
115+ return FTDI_DRIVER_NOT_INSTALLED
116+
117+
118+ def __is_module_loaded_windows ():
119+ try :
120+ out , err = Popen ('driverquery.exe -v' , stdout = PIPE ).communicate ()
121+ position = out .find ('FT' )
122+ if position > 0 :
123+ __module_logger .debug ('FTDI module load state' )
124+ __module_logger .debug (out [position + 84 :position + 91 ])
125+ return 0
126+ else :
127+ __module_logger .warning ('No FTDI modules loaded.' )
128+ return FTDI_DRIVER_NOT_INSTALLED
129+ except CalledProcessError :
130+ __module_logger .warning ('No FTDI modules detected.' )
131+ return FTDI_DRIVER_NOT_INSTALLED
132+
0 commit comments