From a519739ca62cc8cdfc7dc14f2d2c9c07a9fe736b Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 8 Feb 2019 14:02:17 -0500 Subject: [PATCH 1/3] Added debug to i2c_device. --- adafruit_bus_device/i2c_device.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 5dea936..b0bb65b 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -57,7 +57,7 @@ class I2CDevice: device.write(bytes_read) """ - def __init__(self, i2c, device_address): + def __init__(self, i2c, device_address, *, debug=False): """ Try to read a byte from an address, if you get an OSError it means the device is not there @@ -79,6 +79,7 @@ def __init__(self, i2c, device_address): self.i2c = i2c self.device_address = device_address + self._debug = debug def readinto(self, buf, **kwargs): """ @@ -94,6 +95,8 @@ def readinto(self, buf, **kwargs): :param int end: Index to write up to but not include """ self.i2c.readfrom_into(self.device_address, buf, **kwargs) + if self._debug: + print("i2c_device.readinto:", [hex(i) for i in buf]) def write(self, buf, **kwargs): """ @@ -110,6 +113,8 @@ def write(self, buf, **kwargs): :param bool stop: If true, output an I2C stop condition after the buffer is written """ self.i2c.writeto(self.device_address, buf, **kwargs) + if self._debug: + print("i2c_device.write:", [hex(i) for i in buf]) #pylint: disable-msg=too-many-arguments def write_then_readinto(self, out_buffer, in_buffer, *, @@ -147,10 +152,19 @@ def write_then_readinto(self, out_buffer, in_buffer, *, self.i2c.writeto_then_readfrom(self.device_address, out_buffer, in_buffer, out_start=out_start, out_end=out_end, in_start=in_start, in_end=in_end, stop=stop) + if self._debug: + print("i2c_device.writeto_then_readfrom.out_buffer:", [hex(i) for i in out_buffer]) + print("i2c_device.writeto_then_readfrom.in_buffer:", [hex(i) for i in in_buffer]) else: # If we don't have a special implementation, we can fake it with two calls self.write(out_buffer, start=out_start, end=out_end, stop=stop) + if self._debug: + print("i2c_device.write_then_readinto.write.out_buffer:", + [hex(i) for i in out_buffer]) self.readinto(in_buffer, start=in_start, end=in_end) + if self._debug: + print("i2c_device.write_then_readinto.readinto.in_buffer:", + [hex(i) for i in in_buffer]) #pylint: enable-msg=too-many-arguments From d8a7033119f39ee6a5dadcb8cfad2542d48a66e8 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 8 Feb 2019 14:39:27 -0500 Subject: [PATCH 2/3] Update to uses slices. --- adafruit_bus_device/i2c_device.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index b0bb65b..54d967a 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -153,18 +153,20 @@ def write_then_readinto(self, out_buffer, in_buffer, *, out_start=out_start, out_end=out_end, in_start=in_start, in_end=in_end, stop=stop) if self._debug: - print("i2c_device.writeto_then_readfrom.out_buffer:", [hex(i) for i in out_buffer]) - print("i2c_device.writeto_then_readfrom.in_buffer:", [hex(i) for i in in_buffer]) + print("i2c_device.writeto_then_readfrom.out_buffer:", + [hex(i) for i in out_buffer[out_start:out_end]]) + print("i2c_device.writeto_then_readfrom.in_buffer:", + [hex(i) for i in in_buffer[in_start:in_end]]) else: # If we don't have a special implementation, we can fake it with two calls self.write(out_buffer, start=out_start, end=out_end, stop=stop) if self._debug: print("i2c_device.write_then_readinto.write.out_buffer:", - [hex(i) for i in out_buffer]) + [hex(i) for i in out_buffer[out_start:out_end]]) self.readinto(in_buffer, start=in_start, end=in_end) if self._debug: print("i2c_device.write_then_readinto.readinto.in_buffer:", - [hex(i) for i in in_buffer]) + [hex(i) for i in in_buffer[in_start:in_end]]) #pylint: enable-msg=too-many-arguments From 7aa7496fc50031a254f9c48d7b7201cbb5737595 Mon Sep 17 00:00:00 2001 From: Kattni Rembor Date: Fri, 8 Feb 2019 15:27:33 -0500 Subject: [PATCH 3/3] Move write buffer debug print. --- adafruit_bus_device/i2c_device.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/adafruit_bus_device/i2c_device.py b/adafruit_bus_device/i2c_device.py index 54d967a..dcc1692 100644 --- a/adafruit_bus_device/i2c_device.py +++ b/adafruit_bus_device/i2c_device.py @@ -148,13 +148,14 @@ def write_then_readinto(self, out_buffer, in_buffer, *, if in_end is None: in_end = len(in_buffer) if hasattr(self.i2c, 'writeto_then_readfrom'): + if self._debug: + print("i2c_device.writeto_then_readfrom.out_buffer:", + [hex(i) for i in out_buffer[out_start:out_end]]) # In linux, at least, this is a special kernel function call self.i2c.writeto_then_readfrom(self.device_address, out_buffer, in_buffer, out_start=out_start, out_end=out_end, in_start=in_start, in_end=in_end, stop=stop) if self._debug: - print("i2c_device.writeto_then_readfrom.out_buffer:", - [hex(i) for i in out_buffer[out_start:out_end]]) print("i2c_device.writeto_then_readfrom.in_buffer:", [hex(i) for i in in_buffer[in_start:in_end]]) else: