Last active
June 2, 2022 12:06
-
-
Save kashimAstro/c90d5faf925220c43d17 to your computer and use it in GitHub Desktop.
i2c Multiple Byte read and write c++
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <unistd.h> | |
#include <fcntl.h> | |
#include <sys/ioctl.h> | |
#include <linux/i2c-dev.h> | |
class i2cReadWrite { | |
public: | |
int file_i2c; | |
unsigned char buffer[60]; | |
int length=2; | |
void setup(char * id){//"/dev/i2c-1" | |
char *filename = id; | |
if ((file_i2c = open(filename, O_RDWR)) < 0) { | |
printf("Failed to open the i2c bus"); | |
return; | |
} | |
int addr = 0x5a; | |
if (ioctl(file_i2c, I2C_SLAVE, addr) < 0) { | |
printf("Failed to acquire bus access and/or talk to slave.\n"); | |
return; | |
} | |
} | |
string readi2cData(){ | |
string res; | |
if (read(file_i2c, buffer, length) != length) { | |
printf("Failed to read from the i2c bus.\n"); | |
} | |
else { | |
printf("Data read: %s\n", buffer); | |
} | |
return res; | |
} | |
void writeBytes(){ | |
buffer[0] = 0x01; | |
buffer[1] = 0x02; | |
if (write(file_i2c, buffer, length) != length) { | |
printf("Failed to write to the i2c bus.\n"); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment