Skip to content

Commit 2eb0124

Browse files
committed
Improve I2C driver importing
Store module and class names of all supported platforms in lists Loop through each platform and attempt to import each, and append to driver list
1 parent aa2ab9c commit 2eb0124

File tree

1 file changed

+15
-29
lines changed

1 file changed

+15
-29
lines changed

qwiic_i2c/__init__.py

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -65,36 +65,22 @@
6565
# Drivers and driver baseclass
6666
from .i2c_driver import I2CDriver
6767

68+
# All supported platform module and class names
69+
sub_modules = ["linux_i2c", "circuitpy_i2c", "micropython_i2c"]
70+
class_names = ["LinuxI2C", "CircuitPythonI2C", "MicroPythonI2C"]
71+
72+
# List of platform drivers found on this system
6873
_drivers = []
69-
# foo = ["LinuxI2C", "CircuitPythonI2C", "MicroPythonI2C"]
70-
71-
# for driver in foo:
72-
# try:
73-
# print("Importing:", driver)
74-
# submodule = __import__(driver)
75-
# print("Submodule:", submodule)
76-
# _drivers.append(submodule)
77-
# except:
78-
# print("Could not import:", driver)
79-
# pass
80-
81-
try:
82-
from .linux_i2c import LinuxI2C
83-
_drivers.append(LinuxI2C)
84-
except:
85-
pass
86-
87-
try:
88-
from .circuitpy_i2c import CircuitPythonI2C
89-
_drivers.append(CircuitPythonI2C)
90-
except:
91-
pass
92-
93-
try:
94-
from .micropython_i2c import MicroPythonI2C
95-
_drivers.append(MicroPythonI2C)
96-
except:
97-
pass
74+
75+
# Loop through all supported platform drivers, see if they're included, and
76+
# append them to the driver list if so
77+
for i in range(len(sub_modules)):
78+
try:
79+
full_name = "qwiic_i2c." + sub_modules[i]
80+
sub_module = __import__(full_name, None, None, [None])
81+
_drivers.append(getattr(sub_module, class_names[i]))
82+
except:
83+
pass
9884

9985
_theDriver = None
10086

0 commit comments

Comments
 (0)