Skip to content

Commit f2bc310

Browse files
authored
Merge pull request #16 from sparkfun/v1.0.0
V1.0.0
2 parents f167548 + f96fc52 commit f2bc310

File tree

6 files changed

+454
-146
lines changed

6 files changed

+454
-146
lines changed

qwiic_i2c/__init__.py

Lines changed: 69 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,13 @@
8484
except:
8585
pass
8686

87-
_theDriver = None
87+
_default_driver = None
8888

8989
#-------------------------------------------------
9090
# Exported method to get the I2C driver for the execution plaform.
9191
#
9292
# If no driver is found, a None value is returned
93-
94-
def getI2CDriver():
93+
def getI2CDriver(*args, **argk):
9594
"""
9695
.. function:: getI2CDriver()
9796
@@ -107,28 +106,48 @@ def getI2CDriver():
107106
>>> myData = i2cDriver.readByte(0x73, 0x34)
108107
"""
109108

110-
global _theDriver
111-
112-
if _theDriver != None:
113-
return _theDriver
114-
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
115113

116-
114+
# Loop through all the drivers to find the one for this platform
117115
for driverClass in _drivers:
118-
119-
# Does this class/driverd support this platform?
120116
if driverClass.isPlatform():
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
128+
return None
121129

122-
_theDriver = driverClass()
123-
# Yes - return the driver object
124-
return _theDriver
130+
def get_i2c_driver(*args, **argk):
131+
"""
132+
.. function:: get_i2c_driver()
125133
126-
return None
134+
Returns the qwiic I2C driver object for current platform.
135+
136+
:return: A qwiic I2C driver object for the current platform.
137+
:rtype: object
138+
139+
:example:
140+
141+
>>> import qwiic_i2c
142+
>>> i2cDriver = qwiic_i2c.get_i2c_driver()
143+
>>> myData = i2cDriver.readByte(0x73, 0x34)
144+
"""
145+
return getI2CDriver(*args, **argk)
127146

128147
#-------------------------------------------------
129148
# Method to determine if a particular device (at the provided address)
130149
# is connected to the bus.
131-
def isDeviceConnected(devAddress):
150+
def isDeviceConnected(devAddress, *args, **argk):
132151
"""
133152
.. function:: isDeviceConnected()
134153
@@ -141,22 +160,43 @@ def isDeviceConnected(devAddress):
141160
:rtype: bool
142161
143162
"""
144-
i2c = getI2CDriver()
163+
i2c = getI2CDriver(*args, **argk)
145164

146165
if not i2c:
147166
print("Unable to load the I2C driver for this device")
148167
return False
149168

150-
isConnected = False
151-
try:
152-
# Try to write a byte to the device, command 0x0
153-
# If it throws an I/O error - the device isn't connected
154-
with i2c as i2cDriver:
155-
i2cDriver.writeCommand(devAddress, 0x0)
156-
157-
isConnected = True
158-
except Exception as ee:
159-
print("Error connecting to Device: %X, %s" % (devAddress, ee))
160-
pass
169+
return i2c.isDeviceConnected(devAddress)
161170

162-
return isConnected
171+
def is_device_connected(devAddress, *args, **argk):
172+
"""
173+
.. function:: is_device_connected()
174+
175+
Function to determine if a particular device (at the provided address)
176+
is connected to the bus.
177+
178+
:param devAddress: The I2C address of the device to check
179+
180+
:return: True if the device is connected, otherwise False.
181+
:rtype: bool
182+
183+
"""
184+
return isDeviceConnected(devAddress, *args, **argk)
185+
186+
#-------------------------------------------------
187+
# Method to determine if a particular device (at the provided address)
188+
# is connected to the bus.
189+
def ping(devAddress, *args, **argk):
190+
"""
191+
.. function:: ping()
192+
193+
Function to determine if a particular device (at the provided address)
194+
is connected to the bus.
195+
196+
:param devAddress: The I2C address of the device to check
197+
198+
:return: True if the device is connected, otherwise False.
199+
:rtype: bool
200+
201+
"""
202+
return isDeviceConnected(devAddress, *args, **argk)

0 commit comments

Comments
 (0)