-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathi2c.cpp
More file actions
157 lines (146 loc) · 3.93 KB
/
Copy pathi2c.cpp
File metadata and controls
157 lines (146 loc) · 3.93 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <fcntl.h>
#include "smbus.h"
#include <linux/i2c.h>
#include <linux/i2c-dev.h>
#include <unistd.h>
#include "i2c.h"
#include <regex>
#include <filesystem>
I2c::I2c(const char * deviceName)
{
fd = open(deviceName, O_RDWR);
if (fd == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to open I2c device!";
#else
std::cout<<"Failed to open I2c device!"<<std::endl;
#endif
}
}
I2c::~I2c()
{
close(fd);
}
void I2c::setup(const char * deviceName)
{
fd = open(deviceName, O_RDWR);
if (fd == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to open I2c device!";
#else
std::cout<<"Failed to open I2c device!"<<std::endl;
#endif
}
}
int I2c::addressSet(uint8_t address)
{
int result = ioctl(fd, I2C_SLAVE, address);
if (result == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to set address.";
#else
std::cout<<"Failed to set address."<<std::endl;
#endif
return -1;
}
return 1;
}
int I2c::write(uint8_t command)
{
int result = i2c_smbus_write_byte(fd, command);
if (result == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to write byte to I2c.";
#else
std::cout<<"Failed to write byte to I2c."<<std::endl;
#endif
return -1;
}
return 1;
}
int I2c::writeByte(uint8_t command, uint8_t data)
{
int result = i2c_smbus_write_byte_data(fd, command, data);
if (result == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to write byte to I2c.";
#else
std::cout<<"Failed to write byte to I2c."<<std::endl;
#endif
return -1;
}
return 1;
}
int I2c::writeBlockData(uint8_t command, uint8_t size, __u8 * data)
{
int result = i2c_smbus_write_i2c_block_data(fd, command, size, data);
if (result == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to write block data byte to I2c.";
#else
std::cout<<"Failed to write block data byte to I2c."<<std::endl;
#endif
return -1;
}
return 1;
}
uint16_t I2c::readByte(uint8_t command)
{
int result = i2c_smbus_read_byte_data(fd, command);
if (result == -1)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to read byte from I2c.";
#else
std::cout<<"Failed to read byte from I2c."<<std::endl;
#endif
}
return result;
}
uint16_t I2c::tryReadByte(uint8_t command)
{
return i2c_smbus_read_byte_data(fd, command);
}
uint16_t I2c::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)
{
#ifndef COMPILE_WITHOUT_OPENFRAMEWORKS
ofLog()<<"Failed to read block from I2c.";
#else
std::cout<<"Failed to read block from I2c."<<std::endl;
#endif
}
return result;
}
std::vector<std::pair<std::string, uint8_t>> I2c::getDevices(uint8_t start /*=0*/, uint8_t end /*128*/)
{
std::vector<std::pair<std::string, uint8_t>> i2cDevices;
std::regex i2cPattern("/dev/i2c-\\d+");
for (const auto& entry : std::filesystem::directory_iterator("/dev")) {
std::string path = entry.path().string();
if (std::regex_match(path, i2cPattern)) {
int f = open(path.c_str(), O_RDWR);
if(f == -1) continue;
for(uint8_t address = 0; address <= end; ++address) {
int result = ioctl(f, I2C_SLAVE, address);
if(result == 0) {
int res = i2c_smbus_read_byte(f);
if (res >0)
{
i2cDevices.push_back(std::make_pair(path, address));
}
}
}
close(f);
}
}
return i2cDevices;
}