Compare commits
No commits in common. "remove-stop-kwarg2" and "master" have entirely different histories.
remove-sto
...
master
1 changed files with 25 additions and 13 deletions
|
|
@ -62,6 +62,7 @@ class I2CDevice:
|
||||||
def __init__(self, i2c, device_address, probe=True):
|
def __init__(self, i2c, device_address, probe=True):
|
||||||
|
|
||||||
self.i2c = i2c
|
self.i2c = i2c
|
||||||
|
self._has_write_read = hasattr(self.i2c, "writeto_then_readfrom")
|
||||||
self.device_address = device_address
|
self.device_address = device_address
|
||||||
|
|
||||||
if probe:
|
if probe:
|
||||||
|
|
@ -84,10 +85,10 @@ class I2CDevice:
|
||||||
end = len(buf)
|
end = len(buf)
|
||||||
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
|
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)
|
||||||
|
|
||||||
def write(self, buf, *, start=0, end=None):
|
def write(self, buf, *, start=0, end=None, stop=True):
|
||||||
"""
|
"""
|
||||||
Write the bytes from ``buffer`` to the device, then transmit a stop
|
Write the bytes from ``buffer`` to the device. Transmits a stop bit if
|
||||||
bit.
|
``stop`` is set.
|
||||||
|
|
||||||
If ``start`` or ``end`` is provided, then the buffer will be sliced
|
If ``start`` or ``end`` is provided, then the buffer will be sliced
|
||||||
as if ``buffer[start:end]``. This will not cause an allocation like
|
as if ``buffer[start:end]``. This will not cause an allocation like
|
||||||
|
|
@ -96,10 +97,11 @@ class I2CDevice:
|
||||||
:param bytearray buffer: buffer containing the bytes to write
|
:param bytearray buffer: buffer containing the bytes to write
|
||||||
:param int start: Index to start writing from
|
:param int start: Index to start writing from
|
||||||
:param int end: Index to read up to but not include; if None, use ``len(buf)``
|
:param int end: Index to read up to but not include; if None, use ``len(buf)``
|
||||||
|
:param bool stop: If true, output an I2C stop condition after the buffer is written
|
||||||
"""
|
"""
|
||||||
if end is None:
|
if end is None:
|
||||||
end = len(buf)
|
end = len(buf)
|
||||||
self.i2c.writeto(self.device_address, buf, start=start, end=end)
|
self.i2c.writeto(self.device_address, buf, start=start, end=end, stop=stop)
|
||||||
|
|
||||||
# pylint: disable-msg=too-many-arguments
|
# pylint: disable-msg=too-many-arguments
|
||||||
def write_then_readinto(
|
def write_then_readinto(
|
||||||
|
|
@ -111,6 +113,7 @@ class I2CDevice:
|
||||||
out_end=None,
|
out_end=None,
|
||||||
in_start=0,
|
in_start=0,
|
||||||
in_end=None,
|
in_end=None,
|
||||||
|
stop=False
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Write the bytes from ``out_buffer`` to the device, then immediately
|
Write the bytes from ``out_buffer`` to the device, then immediately
|
||||||
|
|
@ -133,12 +136,16 @@ class I2CDevice:
|
||||||
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
|
:param int out_end: Index to read up to but not include; if None, use ``len(out_buffer)``
|
||||||
:param int in_start: Index to start writing at
|
:param int in_start: Index to start writing at
|
||||||
:param int in_end: Index to write up to but not include; if None, use ``len(in_buffer)``
|
:param int in_end: Index to write up to but not include; if None, use ``len(in_buffer)``
|
||||||
|
:param bool stop: Deprecated
|
||||||
"""
|
"""
|
||||||
if out_end is None:
|
if out_end is None:
|
||||||
out_end = len(out_buffer)
|
out_end = len(out_buffer)
|
||||||
if in_end is None:
|
if in_end is None:
|
||||||
in_end = len(in_buffer)
|
in_end = len(in_buffer)
|
||||||
|
if stop:
|
||||||
|
raise ValueError("Stop must be False. Use writeto instead.")
|
||||||
|
if self._has_write_read:
|
||||||
|
# In linux, at least, this is a special kernel function call
|
||||||
self.i2c.writeto_then_readfrom(
|
self.i2c.writeto_then_readfrom(
|
||||||
self.device_address,
|
self.device_address,
|
||||||
out_buffer,
|
out_buffer,
|
||||||
|
|
@ -149,6 +156,11 @@ class I2CDevice:
|
||||||
in_end=in_end,
|
in_end=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=False)
|
||||||
|
self.readinto(in_buffer, start=in_start, end=in_end)
|
||||||
|
|
||||||
# pylint: enable-msg=too-many-arguments
|
# pylint: enable-msg=too-many-arguments
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue