X-Git-Url: https://git.ayoreis.com/Adafruit_Blinka-hackapet.git/blobdiff_plain/f73c450270e34245d0f5c1a62fbca103bfec99ba..b671d2cb302d23f0cb457d16bf1d2300de16590e:/src/adafruit_blinka/microcontroller/nova/i2c.py diff --git a/src/adafruit_blinka/microcontroller/nova/i2c.py b/src/adafruit_blinka/microcontroller/nova/i2c.py index 5c1ecb1..4bbde4a 100644 --- a/src/adafruit_blinka/microcontroller/nova/i2c.py +++ b/src/adafruit_blinka/microcontroller/nova/i2c.py @@ -1,3 +1,6 @@ +# SPDX-FileCopyrightText: 2021 Melissa LeBlanc-Williams for Adafruit Industries +# +# SPDX-License-Identifier: MIT """I2C Class for Binho Nova""" from adafruit_blinka.microcontroller.nova import Connection @@ -5,6 +8,7 @@ from adafruit_blinka.microcontroller.nova import Connection class I2C: """Custom I2C Class for Binho Nova""" + WHR_PAYLOAD_MAX_LENGTH = 1024 def __init__(self, *, frequency=400000): @@ -47,21 +51,20 @@ class I2C: for i in range(chunks): chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH - self._nova.writeToReadFromI2C(0, - address<<1, - stop, - readBytes, - chunk_end-chunk_start, - buffer[chunk_start:chunk_end]) + self._nova.writeToReadFromI2C( + 0, + address << 1, + stop, + readBytes, + chunk_end - chunk_start, + buffer[chunk_start:chunk_end], + ) if rest: - self._nova.writeToReadFromI2C(0, - address<<1, - stop, - readBytes, - rest, - buffer[-1*rest:]) + self._nova.writeToReadFromI2C( + 0, address << 1, stop, readBytes, rest, buffer[-1 * rest :] + ) else: - self._nova.startI2C(0, address<<1) + self._nova.startI2C(0, address << 1) for i in range(start, end): self._nova.writeByteI2C(0, buffer[i]) @@ -87,17 +90,18 @@ class I2C: "Received error response from Binho Nova, result = " + result ) + # pylint: disable=too-many-locals,too-many-branches def writeto_then_readfrom( - self, - address, - buffer_out, - buffer_in, - *, - out_start=0, - out_end=None, - in_start=0, - in_end=None, - stop=False + self, + address, + buffer_out, + buffer_in, + *, + out_start=0, + out_end=None, + in_start=0, + in_end=None, + stop=False, ): """Write data from buffer_out to an address and then read data from an address and into buffer_in @@ -124,18 +128,15 @@ class I2C: numOutBytes = totalOut # setup the buffer out chunk offset - buffer = '0' + buffer = "0" if numOutBytes > 0: chunk_start = out_start + i * self.WHR_PAYLOAD_MAX_LENGTH chunk_end = chunk_start + numOutBytes buffer = buffer_out[chunk_start:chunk_end] - result = self._nova.writeToReadFromI2C(0, - address<<1, - stop, - numInBytes, - numOutBytes, - buffer) + result = self._nova.writeToReadFromI2C( + 0, address << 1, stop, numInBytes, numOutBytes, buffer + ) totalIn -= numInBytes totalOut -= numOutBytes @@ -145,13 +146,15 @@ class I2C: resp = resp[2] for j in range(numInBytes): - buffer_in[in_start+i*self.WHR_PAYLOAD_MAX_LENGTH+j] = \ - int(resp[j*2]+resp[j*2+1], 16) + buffer_in[ + in_start + i * self.WHR_PAYLOAD_MAX_LENGTH + j + ] = int(resp[j * 2] + resp[j * 2 + 1], 16) else: - raise RuntimeError("Received error response from Binho Nova, result = " + \ - result) + raise RuntimeError( + "Received error response from Binho Nova, result = " + result + ) else: - self._nova.startI2C(0, address<<1) + self._nova.startI2C(0, address << 1) for i in range(out_start, out_end): self._nova.writeByteI2C(0, buffer_out[i]) @@ -161,15 +164,19 @@ class I2C: else: self._nova.endI2C(0, True) - result = self._nova.readBytesI2C(0, address<<1, len(buffer_in[in_start:in_end])) + result = self._nova.readBytesI2C( + 0, address << 1, len(buffer_in[in_start:in_end]) + ) if result != "-NG": resp = result.split(" ") for i in range(len(buffer_in[in_start:in_end])): - buffer_in[in_start+i] = int(resp[2+i]) + buffer_in[in_start + i] = int(resp[2 + i]) else: raise RuntimeError( "Received error response from Binho Nova, result = " + result ) -# pylint: enable=unused-argument + + +# pylint: enable=unused-argument,too-many-locals,too-many-branches