-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathI2C.hpp
59 lines (51 loc) · 1.56 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
#pragma once
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <linux/i2c.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <array>
#include <cstring>
#include <stdexcept>
class I2C
{
public:
I2C(int instance, int slave_address) : m_slave_address{slave_address}
{
m_path_to_dev.replace(m_path_to_dev.find("{}"), 2, std::to_string(instance));
if ((m_fd = open(m_path_to_dev.data(), O_RDWR)) < 0)
{
throw std::invalid_argument("Failed to create i2c instance: invalid device provided");
};
if (ioctl(m_fd, I2C_SLAVE, m_slave_address) < 0)
{
throw std::invalid_argument("Failed to create i2c instance: invalid slave address provided");
}
}
virtual ~I2C()
{
close(m_fd);
}
template <size_t size> void write(std::array<uint8_t, size> buffer)
{
if (::write(m_fd, buffer.data(), buffer.size()) != 1)
{
std::string errmsg{"Failed to write to " + m_path_to_dev + ": " + std::strerror(errno)};
throw std::runtime_error(errmsg);
}
}
template <size_t size> std::array<uint8_t, size> read()
{
std::array<uint8_t, size> buffer{};
if (::read(m_fd, buffer.data(), buffer.size()) != buffer.size())
{
std::string errmsg{"Failed to read from " + m_path_to_dev + ": " + std::strerror(errno)};
throw std::runtime_error(errmsg);
}
return buffer;
}
private:
std::string m_path_to_dev{"/dev/i2c-{}"};
int m_fd{};
int m_slave_address{};
};