【发布时间】:2016-08-27 03:55:24
【问题描述】:
我目前正在研究 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 中,我打开了 varialbe cmd,它指示函数指针是指向写入函数还是读取函数。现在出现了失败部分。函数指针在其自己的名为 i2c.h 的 .h 文件中声明,如下所示:
struct I2C {
ssize_t (*WR)(int, const void *, size_t);
};
如您所见,函数指针必须指向带有参数(int、const void*、size_t)的函数,当函数指向写入函数但指向读取函数时,这就像一个魅力获取和错误,错误说:
functions.cpp:在函数‘int i2c_init(I2C*, int, int, int*)’中: functions.cpp:30:14: 错误: '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 = 读取;
我研究了错误并得出结论,这是因为读写函数不知何故不采用相同的参数,这很奇怪,因为我在其中传递了相同的参数(int i2s_file,int buffer,int length)所以如果我将函数指针更改为
ssize_t (*WR)(int, void *, size_t);
该函数适用于读取,但不适用于写入。所以我的问题是:我可以通过更改 i2c-dev 库以某种方式更改写入或读取函数以采用相同的参数,或者我可以做些什么来解决这个问题?
这是 i2c-dev 库的链接,我完全放弃理解 http://textuploader.com/5yioi
多谢了
【问题讨论】:
标签: c++ c raspberry-pi i2c