Skip to content

Optimization: remove read/write from SPI device and return SPI bus on context manager enter #2

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
Dec 13, 2016
Merged
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
34 changes: 1 addition & 33 deletions adafruit_bus_device/spi_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,45 +51,13 @@ def __init__(self, spi, chip_select, baudrate=100000, polarity=0, phase=0):
self.chip_select = chip_select
self.chip_select.switch_to_output(value=True)

def read(self, buffer, **kwargs):
"""
Read into ``buffer`` from the device. The number of bytes read will be the
length of ``buffer``.

If ``start`` or ``end`` is provided, then the buffer will be sliced
as if ``buffer[start:end]``. This will not cause an allocation like
``buffer[start:end]`` will so it saves memory.

:param bytearray buffer: buffer to write into
:param int start: Index to start writing at
:param int end: Index to write up to but not include
"""
self.spi.read(buffer, **kwargs)

def write(self, buffer, **kwargs):
"""
Write the bytes from ``buffer`` to the device. Transmits a stop bit if
``stop`` is set.

If ``start`` or ``end`` is provided, then the buffer will be sliced
as if ``buffer[start:end]``. This will not cause an allocation like
``buffer[start:end]`` will so it saves memory.

:param bytearray buffer: buffer containing the bytes to write
:param int start: Index to start writing from
: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.spi.write(buffer, **kwargs)

def __enter__(self):
while not self.spi.try_lock():
pass
self.spi.configure(baudrate=self.baudrate, polarity=self.polarity,
phase=self.phase)
self.chip_select.value = False
return self
return self.spi

def __exit__(self, *exc):
self.chip_select.value = True
Expand Down