1 """I2C Class for Binho Nova"""
3 from adafruit_blinka.microcontroller.nova import Connection
7 """Custom I2C Class for Binho Nova"""
8 WHR_PAYLOAD_MAX_LENGTH = 1024
10 def __init__(self, *, frequency=400000):
11 self._nova = Connection.getInstance()
12 self._nova.setNumericalBase(10)
13 self._nova.setOperationMode(0, "I2C")
14 self._nova.setPullUpStateI2C(0, "EN")
15 self._nova.setClockI2C(0, frequency)
17 self._novaCMDVer = "0"
18 if hasattr(self._nova, "getCommandVer"):
19 response = self._nova.getCommandVer().split(" ")
20 if response[0] != "-NG":
21 self._novaCMDVer = response[1]
24 """Close Nova on delete"""
28 """Perform an I2C Device Scan"""
31 for i in range(8, 121):
32 result = self._nova.scanAddrI2C(0, i << 1)
34 resp = result.split(" ")
41 def writeto(self, address, buffer, *, start=0, end=None, stop=True):
42 """Write data from the buffer to an address"""
43 end = end if end else len(buffer)
45 if int(self._novaCMDVer) >= 1:
46 chunks, rest = divmod(end - start, self.WHR_PAYLOAD_MAX_LENGTH)
47 for i in range(chunks):
48 chunk_start = start + i * self.WHR_PAYLOAD_MAX_LENGTH
49 chunk_end = chunk_start + self.WHR_PAYLOAD_MAX_LENGTH
50 self._nova.writeToReadFromI2C(0,
54 chunk_end-chunk_start,
55 buffer[chunk_start:chunk_end])
57 self._nova.writeToReadFromI2C(0,
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(0,
139 totalIn -= numInBytes
140 totalOut -= numOutBytes
144 resp = result.split(" ")
147 for j in range(numInBytes):
148 buffer_in[in_start+i*self.WHR_PAYLOAD_MAX_LENGTH+j] = \
149 int(resp[j*2]+resp[j*2+1], 16)
151 raise RuntimeError("Received error response from Binho Nova, result = " + \
154 self._nova.startI2C(0, address<<1)
156 for i in range(out_start, out_end):
157 self._nova.writeByteI2C(0, buffer_out[i])
162 self._nova.endI2C(0, True)
164 result = self._nova.readBytesI2C(0, address<<1, len(buffer_in[in_start:in_end]))
167 resp = result.split(" ")
169 for i in range(len(buffer_in[in_start:in_end])):
170 buffer_in[in_start+i] = int(resp[2+i])
173 "Received error response from Binho Nova, result = " + result
175 # pylint: enable=unused-argument