1 """I2C Class for Binho Nova"""
3 from adafruit_blinka.microcontroller.nova import Connection
7 """Custom I2C Class for Binho Nova"""
9 WHR_PAYLOAD_MAX_LENGTH = 1024
11 def __init__(self, *, frequency=400000):
12 self._nova = Connection.getInstance()
13 self._nova.setNumericalBase(10)
14 self._nova.setOperationMode(0, "I2C")
15 self._nova.setPullUpStateI2C(0, "EN")
16 self._nova.setClockI2C(0, frequency)
18 self._novaCMDVer = "0"
19 if hasattr(self._nova, "getCommandVer"):
20 response = self._nova.getCommandVer().split(" ")
21 if response[0] != "-NG":
22 self._novaCMDVer = response[1]
25 """Close Nova on delete"""
29 """Perform an I2C Device Scan"""
32 for i in range(8, 121):
33 result = self._nova.scanAddrI2C(0, i << 1)
35 resp = result.split(" ")
42 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
43 """Write data from the buffer to an address"""
44 end = end if end else len(buffer)
46 if int(self._novaCMDVer) >= 1:
47 chunks, rest = divmod(end - start, self.WHR_PAYLOAD_MAX_LENGTH)
48 for i in range(chunks):
49 chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH
50 chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
51 self._nova.writeToReadFromI2C(
56 chunk_end - chunk_start,
57 buffer[chunk_start:chunk_end],
60 self._nova.writeToReadFromI2C(
61 0, address << 1, stop, readBytes, rest, buffer[-1 * rest :]
64 self._nova.startI2C(0, address << 1)
66 for i in range(start, end):
67 self._nova.writeByteI2C(0, buffer[i])
72 self._nova.endI2C(0, True)
74 # pylint: disable=unused-argument
75 def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
76 """Read data from an address and into the buffer"""
77 end = end if end else len(buffer)
78 result = self._nova.readBytesI2C(0, address << 1, len(buffer[start:end]))
81 resp = result.split(" ")
83 for i in range(len(buffer[start:end])):
84 buffer[start + i] = int(resp[2 + i])
87 "Received error response from Binho Nova, result = " + result
90 def writeto_then_readfrom(
102 """Write data from buffer_out to an address and then
103 read data from an address and into buffer_in
105 out_end = out_end if out_end else len(buffer_out)
106 in_end = in_end if in_end else len(buffer_in)
107 if int(self._novaCMDVer) >= 1:
108 totalIn = in_end - in_start
109 totalOut = out_end - out_start
111 if totalOut > totalIn:
112 totalBytes = totalOut
113 chunks, rest = divmod(totalBytes, self.WHR_PAYLOAD_MAX_LENGTH)
117 for i in range(chunks):
118 # calculate the number of bytes to be written and read
119 numInBytes = self.WHR_PAYLOAD_MAX_LENGTH
120 if totalIn < self.WHR_PAYLOAD_MAX_LENGTH:
122 numOutBytes = self.WHR_PAYLOAD_MAX_LENGTH
123 if totalOut < self.WHR_PAYLOAD_MAX_LENGTH:
124 numOutBytes = totalOut
126 # setup the buffer out chunk offset
129 chunk_start = out_start + i * self.WHR_PAYLOAD_MAX_LENGTH
130 chunk_end = chunk_start + numOutBytes
131 buffer = buffer_out[chunk_start:chunk_end]
133 result = self._nova.writeToReadFromI2C(
134 0, address << 1, stop, numInBytes, numOutBytes, buffer
136 totalIn -= numInBytes
137 totalOut -= numOutBytes
141 resp = result.split(" ")
144 for j in range(numInBytes):
146 in_start + i * self.WHR_PAYLOAD_MAX_LENGTH + j
147 ] = int(resp[j * 2] + resp[j * 2 + 1], 16)
150 "Received error response from Binho Nova, result = " + result
153 self._nova.startI2C(0, address << 1)
155 for i in range(out_start, out_end):
156 self._nova.writeByteI2C(0, buffer_out[i])
161 self._nova.endI2C(0, True)
163 result = self._nova.readBytesI2C(
164 0, address << 1, len(buffer_in[in_start:in_end])
168 resp = result.split(" ")
170 for i in range(len(buffer_in[in_start:in_end])):
171 buffer_in[in_start + i] = int(resp[2 + i])
174 "Received error response from Binho Nova, result = " + result
178 # pylint: enable=unused-argument