Skip to content

avoid allocations by avoiding **kwargs #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions adafruit_bus_device/i2c_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ class I2CDevice:
def __init__(self, i2c, device_address, probe=True):

self.i2c = i2c
self._has_write_read = hasattr(self.i2c, 'writeto_then_readfrom')
self.device_address = device_address

if probe:
self.__probe_for_device()

def readinto(self, buf, **kwargs):
def readinto(self, buf, *, start=0, end=None):
"""
Read into ``buf`` from the device. The number of bytes read will be the
length of ``buf``.
Expand All @@ -79,9 +80,9 @@ def readinto(self, buf, **kwargs):
:param int start: Index to start writing at
:param int end: Index to write up to but not include
"""
self.i2c.readfrom_into(self.device_address, buf, **kwargs)
self.i2c.readfrom_into(self.device_address, buf, start=start, end=end)

def write(self, buf, **kwargs):
def write(self, buf, *, start=0, end=None, stop=True):
"""
Write the bytes from ``buffer`` to the device. Transmits a stop bit if
``stop`` is set.
Expand All @@ -95,7 +96,7 @@ def write(self, buf, **kwargs):
:param int end: Index to read up to but not include
:param bool stop: If true, output an I2C stop condition after the buffer is written
"""
self.i2c.writeto(self.device_address, buf, **kwargs)
self.i2c.writeto(self.device_address, buf, start=start, end=end, stop=stop)

#pylint: disable-msg=too-many-arguments
def write_then_readinto(self, out_buffer, in_buffer, *,
Expand Down Expand Up @@ -129,7 +130,7 @@ def write_then_readinto(self, out_buffer, in_buffer, *,
in_end = len(in_buffer)
if stop:
raise ValueError("Stop must be False. Use writeto instead.")
if hasattr(self.i2c, 'writeto_then_readfrom'):
if self._has_write_read:
# 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,
Expand All @@ -147,7 +148,7 @@ def __enter__(self):
pass
return self

def __exit__(self, *exc):
def __exit__(self, exc_type, exc_val, exc_tb):
self.i2c.unlock()
return False

Expand Down
2 changes: 1 addition & 1 deletion adafruit_bus_device/spi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __enter__(self):
self.chip_select.value = False
return self.spi

def __exit__(self, *exc):
def __exit__(self, exc_type, exc_val, exc_tb):
if self.chip_select:
self.chip_select.value = True
if self.extra_clocks > 0:
Expand Down