-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2c.cpp
51 lines (42 loc) · 850 Bytes
/
i2c.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
#include <iostream>
#include <iomanip>
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <linux/i2c-dev.h>
#include "i2c.h"
using namespace std;
I2C::I2C(uint8_t address)
: address(address)
{
file = open("/dev/i2c-1", O_RDWR);
if (file < 0) {
perror("open");
exit(1);
}
if (ioctl(file, I2C_SLAVE, address) < 0) {
perror("ioctl");
exit(1);
}
}
I2C::~I2C()
{
close(file);
}
uint16_t I2C::read(uint8_t addr)
{
cout << __PRETTY_FUNCTION__ << ": " << (int)addr << endl;
int32_t res = i2c_smbus_read_word_data(file, addr);
if (res < 0) {
perror("i2c_smbus_read_word_data");
exit(1);
}
return res & 0xffff;
}
void I2C::write(uint8_t addr, uint16_t data)
{
}