-
Notifications
You must be signed in to change notification settings - Fork 1
/
I2CBus.cpp
59 lines (52 loc) · 1.2 KB
/
I2CBus.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "I2CBus.h"
#include <fcntl.h>
#include <linux/i2c-dev.h>
I2CBus::I2CBus(const char * deviceName)
{
fd = open(deviceName, O_RDWR);
if (fd == -1)
{
throw posix_error("Failed to open I2C device.");
}
}
I2CBus::~I2CBus()
{
close(fd);
}
void I2CBus::addressSet(uint8_t address)
{
int result = ioctl(fd, I2C_SLAVE, address);
if (result == -1)
{
throw posix_error("Failed to set address.");
}
}
void I2CBus::writeByte(uint8_t command, uint8_t data)
{
int result = i2c_smbus_write_byte_data(fd, command, data);
if (result == -1)
{
throw posix_error("Failed to write byte to I2C.");
}
}
uint8_t I2CBus::readByte(uint8_t command)
{
int result = i2c_smbus_read_byte_data(fd, command);
if (result == -1)
{
throw posix_error("Failed to read byte from I2C.");
}
return result;
}
int I2CBus::tryReadByte(uint8_t command)
{
return i2c_smbus_read_byte_data(fd, command);
}
void I2CBus::readBlock(uint8_t command, uint8_t size, uint8_t * data)
{
int result = i2c_smbus_read_i2c_block_data(fd, command, size, data);
if (result != size)
{
throw posix_error("Failed to read block from I2C.");
}
}