MCPcopy Create free account
hub / github.com/brainflow-dev/brainflow / BoardControllerDLL

Class BoardControllerDLL

python_package/brainflow/board_shim.py:159–592  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

157
158
159class BoardControllerDLL(object):
160 __instance = None
161
162 @classmethod
163 def get_instance(cls):
164 if cls.__instance is None:
165 cls.__instance = cls()
166 return cls.__instance
167
168 def __init__(self):
169 if platform.system() == 'Windows':
170 if struct.calcsize("P") * 8 == 64:
171 dll_path = 'lib\\BoardController.dll'
172 else:
173 dll_path = 'lib\\BoardController32.dll'
174 elif platform.system() == 'Darwin':
175 dll_path = 'lib/libBoardController.dylib'
176 else:
177 dll_path = 'lib/libBoardController.so'
178 try:
179 resource = files(__name__).joinpath(dll_path)
180 full_path = str(resource)
181 except (TypeError, AttributeError):
182 # Fallback for:
183 # 1. Python < 3.9 (importlib.resources.files not available)
184 # 2. NixOS/packaging edge cases where importlib.resources may not work
185 import pkg_resources
186 full_path = pkg_resources.resource_filename(__name__, dll_path)
187 if os.path.isfile(full_path):
188 dir_path = os.path.abspath(os.path.dirname(full_path))
189 # for python we load dll by direct path but this dll may depend on other dlls and they will not be found!
190 # to solve it we can load all of them before loading the main one or change PATH\LD_LIBRARY_PATH env var.
191 # env variable looks better, since it can be done only once for all dependencies
192 # for python 3.8 PATH env var doesnt work anymore
193 try:
194 os.add_dll_directory(dir_path)
195 except (AttributeError, FileNotFoundError, OSError):
196 # Best effort only: this may be unavailable or fail on some
197 # platforms/runtime configurations; PATH/LD_LIBRARY_PATH is
198 # updated below as a fallback.
199 pass
200 if platform.system() == 'Windows':
201 os.environ['PATH'] = dir_path + os.pathsep + os.environ.get('PATH', '')
202 else:
203 os.environ['LD_LIBRARY_PATH'] = dir_path + os.pathsep + os.environ.get('LD_LIBRARY_PATH', '')
204 self.lib = ctypes.cdll.LoadLibrary(full_path)
205 else:
206 raise FileNotFoundError(
207 'Dynamic library %s is missed, did you forget to compile brainflow before installation of python package?' % full_path)
208
209 self.prepare_session = self.lib.prepare_session
210 self.prepare_session.restype = ctypes.c_int
211 self.prepare_session.argtypes = [
212 ctypes.c_int,
213 ctypes.c_char_p
214 ]
215
216 self.is_prepared = self.lib.is_prepared

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected