@@ -35,6 +35,7 @@ class I2CDevice:
35
35
36
36
:param ~busio.I2C i2c: The I2C bus the device is on
37
37
:param int device_address: The 7 bit device address
38
+ :param bool probe: Probe for the device upon object creation, default is true
38
39
39
40
.. note:: This class is **NOT** built into CircuitPython. See
40
41
:ref:`here for install instructions <bus_device_installation>`.
@@ -58,25 +59,9 @@ class I2CDevice:
58
59
"""
59
60
60
61
def __init__ (self , i2c , device_address , probe = True ):
61
- """
62
- Try to read a byte from an address,
63
- if you get an OSError it means the device is not there
64
- """
62
+
65
63
if probe :
66
- while not i2c .try_lock ():
67
- pass
68
- try :
69
- i2c .writeto (device_address , b'' )
70
- except OSError :
71
- # some OS's dont like writing an empty bytesting...
72
- # Retry by reading a byte
73
- try :
74
- result = bytearray (1 )
75
- i2c .readfrom_into (device_address , result )
76
- except OSError :
77
- raise ValueError ("No I2C device at address: %x" % device_address )
78
- finally :
79
- i2c .unlock ()
64
+ self .__probe_for_device ()
80
65
81
66
self .i2c = i2c
82
67
self .device_address = device_address
@@ -165,3 +150,24 @@ def __enter__(self):
165
150
def __exit__ (self , * exc ):
166
151
self .i2c .unlock ()
167
152
return False
153
+
154
+ def __probe_for_device (self ):
155
+ """
156
+ Try to read a byte from an address,
157
+ if you get an OSError it means the device is not there
158
+ or that the device does not support these means of probing
159
+ """
160
+ while not i2c .try_lock ():
161
+ pass
162
+ try :
163
+ i2c .writeto (device_address , b'' )
164
+ except OSError :
165
+ # some OS's dont like writing an empty bytesting...
166
+ # Retry by reading a byte
167
+ try :
168
+ result = bytearray (1 )
169
+ i2c .readfrom_into (device_address , result )
170
+ except OSError :
171
+ raise ValueError ("No I2C device at address: %x" % device_address )
172
+ finally :
173
+ i2c .unlock ()
0 commit comments