From b7261984996725dd792b956eb25b66fb97b49386 Mon Sep 17 00:00:00 2001 From: Chris Osterwood Date: Mon, 25 Nov 2019 11:15:49 -0500 Subject: [PATCH 1/4] Adding ability to skip device probing upon object init. Some hardware devices do not respond to these probes, or could be in reset when software object is created --- adafruit_bus_device/i2c_device.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 069365a..3ff3035 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -57,25 +57,26 @@ class I2CDevice: device.write(bytes_read) """ - def __init__(self, i2c, device_address): + def __init__(self, i2c, device_address, probe=True): """ Try to read a byte from an address, if you get an OSError it means the device is not there """ - while not i2c.try_lock(): - pass - try: - i2c.writeto(device_address, b'') - except OSError: - # some OS's dont like writing an empty bytesting... - # Retry by reading a byte + if probe: + while not i2c.try_lock(): + pass try: - result = bytearray(1) - i2c.readfrom_into(device_address, result) + i2c.writeto(device_address, b'') except OSError: - raise ValueError("No I2C device at address: %x" % device_address) - finally: - i2c.unlock() + # some OS's dont like writing an empty bytesting... + # Retry by reading a byte + try: + result = bytearray(1) + i2c.readfrom_into(device_address, result) + except OSError: + raise ValueError("No I2C device at address: %x" % device_address) + finally: + i2c.unlock() self.i2c = i2c self.device_address = device_address From e9459c70f49c122475e2781eff34618b1b16f252 Mon Sep 17 00:00:00 2001 From: Chris Osterwood Date: Mon, 25 Nov 2019 21:33:33 -0500 Subject: [PATCH 2/4] Move probing logic into private method. Update comments and doc strings --- adafruit_bus_device/i2c_device.py | 42 ++++++++++++++++++------------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 3ff3035..b87a983 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -35,6 +35,7 @@ class I2CDevice: :param ~busio.I2C i2c: The I2C bus the device is on :param int device_address: The 7 bit device address + :param bool probe: Probe for the device upon object creation, default is true .. note:: This class is **NOT** built into CircuitPython. See :ref:`here for install instructions `. @@ -58,25 +59,9 @@ class I2CDevice: """ def __init__(self, i2c, device_address, probe=True): - """ - Try to read a byte from an address, - if you get an OSError it means the device is not there - """ + if probe: - while not i2c.try_lock(): - pass - try: - i2c.writeto(device_address, b'') - except OSError: - # some OS's dont like writing an empty bytesting... - # Retry by reading a byte - try: - result = bytearray(1) - i2c.readfrom_into(device_address, result) - except OSError: - raise ValueError("No I2C device at address: %x" % device_address) - finally: - i2c.unlock() + self.__probe_for_device() self.i2c = i2c self.device_address = device_address @@ -165,3 +150,24 @@ def __enter__(self): def __exit__(self, *exc): self.i2c.unlock() return False + + def __probe_for_device(self): + """ + Try to read a byte from an address, + if you get an OSError it means the device is not there + or that the device does not support these means of probing + """ + while not i2c.try_lock(): + pass + try: + i2c.writeto(device_address, b'') + except OSError: + # some OS's dont like writing an empty bytesting... + # Retry by reading a byte + try: + result = bytearray(1) + i2c.readfrom_into(device_address, result) + except OSError: + raise ValueError("No I2C device at address: %x" % device_address) + finally: + i2c.unlock() From 6635e403758398638a01b611acd7abec9ac7a238 Mon Sep 17 00:00:00 2001 From: Chris Osterwood Date: Mon, 25 Nov 2019 21:37:47 -0500 Subject: [PATCH 3/4] Fixed NameErrors caused by moving probing logic into private method --- adafruit_bus_device/i2c_device.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index b87a983..a73972a 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -60,12 +60,12 @@ class I2CDevice: def __init__(self, i2c, device_address, probe=True): - if probe: - self.__probe_for_device() - self.i2c = i2c self.device_address = device_address + if probe: + self.__probe_for_device() + def readinto(self, buf, **kwargs): """ Read into ``buf`` from the device. The number of bytes read will be the @@ -157,17 +157,17 @@ def __probe_for_device(self): if you get an OSError it means the device is not there or that the device does not support these means of probing """ - while not i2c.try_lock(): + while not self.i2c.try_lock(): pass try: - i2c.writeto(device_address, b'') + self.i2c.writeto(self.device_address, b'') except OSError: # some OS's dont like writing an empty bytesting... # Retry by reading a byte try: result = bytearray(1) - i2c.readfrom_into(device_address, result) + self.i2c.readfrom_into(self.device_address, result) except OSError: - raise ValueError("No I2C device at address: %x" % device_address) + raise ValueError("No I2C device at address: %x" % self.device_address) finally: - i2c.unlock() + self.i2c.unlock() From db586d272f62ef0d30566a63f86c27abdb4f093d Mon Sep 17 00:00:00 2001 From: Chris Osterwood Date: Mon, 25 Nov 2019 22:07:14 -0500 Subject: [PATCH 4/4] Whitespace change to make pylint happy --- adafruit_bus_device/i2c_device.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index a73972a..d72995d 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -154,8 +154,8 @@ def __exit__(self, *exc): def __probe_for_device(self): """ Try to read a byte from an address, - if you get an OSError it means the device is not there - or that the device does not support these means of probing + if you get an OSError it means the device is not there + or that the device does not support these means of probing """ while not self.i2c.try_lock(): pass