Skip to content

Commit ff6874e

Browse files
committed
Add caching for the default I2C bus
If no keyword arguments are provided, then the default bus is requested. If this happens multiple times (eg. the user has multiple devices on the default bus), then it's faster to do this caching.
1 parent da2846f commit ff6874e

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

qwiic_i2c/__init__.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,12 @@
8484
except:
8585
pass
8686

87+
_default_driver = None
88+
8789
#-------------------------------------------------
8890
# Exported method to get the I2C driver for the execution plaform.
8991
#
9092
# If no driver is found, a None value is returned
91-
9293
def getI2CDriver(*args, **argk):
9394
"""
9495
.. function:: getI2CDriver()
@@ -105,14 +106,25 @@ def getI2CDriver(*args, **argk):
105106
>>> myData = i2cDriver.readByte(0x73, 0x34)
106107
"""
107108

109+
# If no parameters are provided, return the default driver if we have it
110+
global _default_driver
111+
if len(argk) == 0 and _default_driver != None:
112+
return _default_driver
113+
114+
# Loop through all the drivers to find the one for this platform
108115
for driverClass in _drivers:
109-
110-
# Does this class/driverd support this platform?
111116
if driverClass.isPlatform():
112-
113-
# Yes - return the driver object
114-
return driverClass(*args, **argk)
115-
117+
# Found it!
118+
driver = driverClass(*args, **argk)
119+
120+
# If no parameters are provided, set this as the default driver
121+
if len(argk) == 0:
122+
_default_driver = driver
123+
124+
# And return it
125+
return driver
126+
127+
# If we get here, we didn't find a driver for this platform
116128
return None
117129

118130
def get_i2c_driver(*args, **argk):

0 commit comments

Comments
 (0)