-
Notifications
You must be signed in to change notification settings - Fork 0
/
I2C.hpp
59 lines (58 loc) · 1.41 KB
/
I2C.hpp
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
#ifndef I2C_H_
#define I2C_H_
#include "IO.hpp"
#include "sys/ioctl.h"
#include "linux/i2c-dev.h"
#include "cstring"
#include "fcntl.h"
#include "unistd.h"
class I2C : public IO
{
public:
I2C(const std::string& path,const std::string& adress):m_path(path)
{
if(adress=="0x76"||adress=="0x77")
{
m_adress=static_cast<uint8_t>(std::stoi(adress,0,16));
m_interfaceName="I2C";
}
else
{
std::cout<<"Adress must be 0x76 or 0x77"<<std::endl;
}
};
virtual int8_t read(uint8_t reg_addr,uint8_t *data, uint16_t len)
{
::write(m_fd, ®_addr,1);
::read(m_fd, data, len);
return 0;
}
virtual int8_t write(uint8_t reg_addr,uint8_t *data, uint16_t len)
{
int8_t *buf;
buf =static_cast<int8_t*>(malloc(len +1));
buf[0] = reg_addr;
memcpy(buf +1, data, len);
::write(m_fd, buf, len +1);
free(buf);
return 0;
}
int8_t connect()
{
if((m_fd = open(m_path.c_str(),O_RDWR)) < 0)
{
printf("Failed to open the i2c bus %s",m_path.c_str());
exit(1);
}
if(ioctl(m_fd,I2C_SLAVE,m_adress) < 0)
{
printf("Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
}
private:
int m_fd;
std::string m_path{""};
uint8_t m_adress{0x76};
};
#endif