这里使用了maixpy操作系统,其实使用micorpython也行
具体思路就是以树莓派的MLX90640库为基础,主要将底层对应的i2c读写接口以及i2c初始化程序替换
i2c初始化程序替换
初始化部分的替换很简单,将
self.i2c_device = I2CDevice(i2c_bus, address)
替换为
self.i2c = I2C(I2C.I2C0, freq=1000000, scl=9, sda=10)
self.addr=address
即可
i2c读写接口替换
原代码片段
def _I2CWriteWord(self, writeAddress, data):
cmd = bytearray(4)
cmd[0] = writeAddress >> 8
cmd[1] = writeAddress & 0x00FF
cmd[2] = data >> 8
cmd[3] = data & 0x00FF
dataCheck = [0]
with self.i2c_device as i2c:
i2c.write(cmd)
# print("Wrote:", [hex(i) for i in cmd])
time.sleep(0.001)
self._I2CReadWords(writeAddress, dataCheck)
# print("dataCheck: 0x%x" % dataCheck[0])
# if (dataCheck != data):
# return -2
def _I2CReadWords(self, addr, buffer, *, end=None):
# stamp = time.monotonic()
if end is None:
remainingWords = len(buffer)
else:
remainingWords = end
offset = 0
addrbuf = bytearray(2)
inbuf = bytearray(2 * I2C_READ_LEN)
with self.i2c_device as i2c:
while remainingWords:
addrbuf[0] = addr >> 8 # MSB
addrbuf[1] = addr & 0xFF # LSB
read_words = min(remainingWords, I2C_READ_LEN)
i2c.write_then_readinto(
addrbuf, inbuf, in_end=read_words * 2
) # in bytes
# print("-> ", [hex(i) for i in addrbuf])
outwords = struct.unpack(
">" + "H" * read_words, inbuf[0 : read_words * 2]
)
# print("<- (", read_words, ")", [hex(i) for i in outwords])
for i, w in enumerate(outwords):
buffer[offset + i] = w
offset += read_words
remainingWords -= read_words
addr += read_words
需要替换的代码分别是两个函数中的读与写的部分,也就是会在这里报错,因为maixpy中的i2c的api不是这么写的,因此只需要找到相同功能的api进行替换就可以了
with self.i2c_device as i2c:
i2c.write(cmd)
......
i2c.write_then_readinto(
addrbuf, inbuf, in_end=read_words * 2
) # in bytes
在找到原代码的api说明后,经过比较,将以上代码替换为即可
self.i2c.writeto_mem(self.addr,writeAddress,data.to_bytes(2,'big'),mem_size=16)
......
self.i2c.readfrom_mem_into(self.addr, addr, inbuf, mem_size=16)
替换后的代码如下,将之替换到原程序那里就可以(为了适配maixpy的i2c初始化的api,做了少许修改,建议直接复制替换)
def _I2CWriteWord(self, writeAddress, data):
cmd = bytearray(4)
cmd[0] = writeAddress >> 8
cmd[1] = writeAddress & 0x00FF
cmd[2] = data >> 8
cmd[3] = data & 0x00FF
dataCheck = [0]
#with self.i2c_device as i2c:
#i2c.write(cmd)
#self.i2c.writeto(self.addr, cmd)
self.i2c.writeto_mem(self.addr,writeAddress,data.to_bytes(2,'big'),mem_size=16)
# print("Wrote:", [hex(i) for i in cmd])
time.sleep(0.001)
self._I2CReadWords(writeAddress, dataCheck)
# print("dataCheck: 0x%x" % dataCheck[0])
# if (dataCheck != data):
# return -2
def _I2CReadWords(self, addr, buffer, *, end=None):
# stamp = time.monotonic()
if end is None:
remainingWords = len(buffer)
else:
remainingWords = end
offset = 0
addrbuf = bytearray(2)
inbuf = bytearray(2 * I2C_READ_LEN)
while remainingWords:
addrbuf[0] = addr >> 8 # MSB
addrbuf[1] = addr & 0xFF # LSB
read_words = min(remainingWords, I2C_READ_LEN)
#i2c.write_then_readinto(
#addrbuf, inbuf, in_end=read_words * 2
#) # in bytes int.from_bytes(data,'big')
self.i2c.readfrom_mem_into(self.addr, addr, inbuf, mem_size=16)
# print("-> ", [hex(i) for i in addrbuf])
outwords = struct.unpack(
">" + "H" * read_words, inbuf[0 : read_words * 2]
)
# print("<- (", read_words, ")", [hex(i) for i in outwords])
for i, w in enumerate(outwords):
buffer[offset + i] = w
offset += read_words
remainingWords -= read_words
addr += read_words
参考资料:
1.https://github.com/adafruit/Adafruit_CircuitPython_MLX90640
树莓派的库,不用自己进行数据的解析之类的了
2.https://circuitpython.readthedocs.io/projects/busdevice/en/latest/api.html
替换i2c读写方式时参考的原代码手册
3.https://cn.maixpy.sipeed.com/zh/libs/machine/i2c.html?h=i2c
maixpy的i2c的api手册