From a7d11f8898e3232140e0c43ba77856de9be7a64d Mon Sep 17 00:00:00 2001 From: Tony DiCola Date: Mon, 12 Dec 2016 00:19:40 -0800 Subject: [PATCH] Optimization: remove read/write from SPI device and return SPI bus on context manager enter. --- adafruit_bus_device/spi_device.py | 34 +------------------------------ 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/adafruit_bus_device/spi_device.py b/adafruit_bus_device/spi_device.py index 02047f7..5d6d807 100644 --- a/adafruit_bus_device/spi_device.py +++ b/adafruit_bus_device/spi_device.py @@ -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