vsergeev / python-periphery

A pure Python 2/3 library for peripheral I/O (GPIO, LED, PWM, SPI, I2C, MMIO, Serial) in Linux.
MIT License
519 stars 139 forks source link

i2c: when reading, modify mutable buffer in-place; accept more types #67

Open jmuchemb opened 7 months ago

jmuchemb commented 7 months ago

First commit is important for performance because it avoids useless data copies when the caller provides a mutable buffer. Locally, I have an incomplete micropython-compat layer that I'd like to write as follows:

    def readfrom_into(self, addr, buf, stop=True):
        self.transfer(addr, [self.Message(buf, read=True)])

But without this PR, I'd have to do:

    def readfrom_into(self, addr, buf, stop=True):
        msg = self.Message(buf, read=True)
        self.transfer(addr, [msg])
        buf[:] = msg.data

(off-topic: I don't know yet what to with the stop=True parameter)

The next 2 commits are less important. They only relax some typing constraints, for free. I haven't checked my changes to i2c.pyi.