c++ - I2C-dev : Change write and read parameters

标签 c++ c raspberry-pi i2c

我目前正在研究 Raspberry Pi 3 和 PsoC 4 之间的 I2C。我正在使用 I2C-dev 库来处理 I2C 通信。至此I2C总线启动运行,读写功能已经正确实现。但是,我想让函数指向读写函数,因此 to 函数使用相同类型的参数(至少看起来像)我有以下主要代码调用 I

2Ctest-tools.cpp:

#include <unistd.h>             //Needed for I2C port
#include <fcntl.h>              //Needed for I2C port
#include <sys/ioctl.h>          //Needed for I2C port
#include <linux/i2c-dev.h>      //Needed for I2C port
#include <stdio.h>
#include "i2c.h"
#include "functions.h"


int main() {


        int addr = 0x49;
        int cmd = 2; //Write
        int length = 5;
        int file_i2c = 0;
        I2C myI2C;



        unsigned char buffer[5];

        buffer[0] = 0x01;
        buffer[1] = 0x02;
        buffer[2] = 0x20;
        buffer[3] = 0x00;
        buffer[4] = 0x17;

        i2c_init(&myI2C, cmd, addr, &file_i2c);
        i2c_exe(&myI2C, file_i2c, buffer, length);

    return 0;
}

如代码所示,我使用了一个名为 myI2C 的结构对象,它在两个函数 i2c_init、i2c_exe 中传递。 functions.cpp 的源代码如下:

#include <unistd.h>             //Needed for I2C port
#include <fcntl.h>              //Needed for I2C port
#include <sys/ioctl.h>          //Needed for I2C port
#include <linux/i2c-dev.h>      //Needed for I2C port
#include <stdio.h>
#include "i2c.h"
#include "functions.h"

int i2c_init(I2C *cthis, int cmd, int addr, int *ptrFile_i2c ) {
    char *filename = (char*)"/dev/i2c-1";
    if ((*ptrFile_i2c = open(filename, O_RDWR)) < 0)
    {
        //ERROR HANDLING: you can check errno to see what went wrong
        printf("Failed to open the i2c bus");
        return 0;
    }

    if (ioctl(*ptrFile_i2c, I2C_SLAVE, addr) < 0)
    {
        printf("Failed to acquire bus access and/or talk to slave.\n");
        //ERROR HANDLING; you can check errno to see what went wrong
        return 0;
    }

    switch(cmd) {
        case 1: 
            //cthis->WR = write;
            break;
        case 2:
            cthis->WR = read;
            break;
    }
    return 0;
}

int i2c_exe(I2C *cthis, int file_i2c, unsigned char *buffer, size_t length) {

    cthis->WR(file_i2c, buffer, length);
    return 0;
}

所以这里要注意的重要一点是,在函数 i2c_init im 中打开 varialbe cmd,它指示函数指针是指向写入函数还是读取函数。现在出现故障部分。函数指针在其自己的名为 i2c.h 的 .h 文件中声明,如下所示:

struct I2C {
    ssize_t (*WR)(int, const void   *, size_t);
};

如您所见,函数指针必须指向一个带有参数 (int, const void*, size_t) 的函数获取和错误,错误说:

functions.cpp: In function ‘int i2c_init(I2C*, int, int, int*)’: functions.cpp:30:14: error: invalid conversion from ‘ssize_t ()(int, void, size_t) {aka int ()(int, void, unsigned int)}’ to ‘ssize_t ()(int, const void, size_t) {aka int ()(int, const void, unsigned int)}’ [-fpermissive] cthis->WR = read;

我研究了这个错误并得出结论,这是因为读写函数以某种方式不采用相同的参数,这很奇怪,因为我在其中传递了相同的参数(int i2s_file,int buffer,int length)所以如果我将函数指针更改为

ssize_t (*WR)(int, void *, size_t);

该函数适用于读取,但不适用于写入。所以我的问题是:我可以通过更改 i2c-dev 库以某种方式更改写入或读取函数以采用相同的参数,还是我可以做其他任何事情来解决这个问题?

这是 i2c-dev 库的链接,我已经完全放弃理解 http://textuploader.com/5yioi

提前致谢

最佳答案

您可以将函数显式转换为正确的类型:

typedef ssize_t (*I2CCMD)(int, void *, size_t);

cthis->WR = (I2CCMD)write;

cthis->WR = (I2CCMD)read;

这应该可以消除错误。

关于c++ - I2C-dev : Change write and read parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981513/

相关文章:

c++ - 如何在没有 Z-fighting 的情况下在 OpenGL 中绘制一条线

c++ - 如何实现一个支持模板协变的泛型工厂?

c - exit(1) 没有给我 1 作为退出值?

python - 尝试使用树莓派控制8x8 LED矩阵而不使用max7219

linux - 使用 Raspbian Jessi Wheezy 和 Raspberry Pi2b 在启动时运行脚本

c++ - 打印给定编号的所有分区

c++ - Linux 性能监控,有什么方法可以监控每个线程?

Python - time.sleep 的替代品

c - 将字符串数组作为参数传递给 C 中的函数

c++ - (unsigned char)str 与 str + 0xff 或 (unsigned int)value 与 value + 0xffff 哪个是更快的首选方法?