K210系列:i2c方式使用mlx90640

本文档详细介绍了如何将树莓派的 MLX90640 库中的 i2c 初始化及读写接口适配到 MaixPy 操作系统。主要涉及替换 i2c 初始化代码以及修改 i2c 读写函数,以匹配 MaixPy 中的 API。通过替换原始代码中的 i2c_device 使用方式,采用 MaixPy 的 i2c.writeto_mem 和 i2c.readfrom_mem_into 方法,成功实现了兼容。
摘要由CSDN通过智能技术生成

这里使用了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手册

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值