-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathi2c_helpers.hh
62 lines (51 loc) · 1.32 KB
/
i2c_helpers.hh
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
#ifndef I2C_HELPERS_HH
#define I2C_HELPERS_HH
#include <yaal/requirements.hh>
#ifdef __YAAL__
#include <yaal/communication/i2c_hw.hh>
#ifndef __cpp_if_constexpr
#if __cplusplus >= 201703L
#define __cpp_if_constexpr 201606
#else
#define __cpp_if_constexpr 0
#endif
#endif
#if __cpp_if_constexpr >= 201606
#define IF_CONSTEXPR if constexpr
#else
#define IF_CONSTEXPR if
#endif
namespace i2c_helpers {
using i2c_err_f_t = void (*)(uint8_t addr, uint8_t arg_count);
template<typename ...Ts>
inline void I2C_INIT(Ts... args)
{
using yaal::I2c_HW;
I2c_HW.setup(args...);
}
namespace internal {
i2c_err_f_t err_func = nullptr;
}
inline void I2C_set_err_func(i2c_err_f_t f)
{
using internal::err_func;
err_func = f;
}
template<bool fail_fatal = true, typename ...Ts>
void I2C_WRITE(uint8_t addr, Ts... args)
{
using yaal::I2c_HW;
using internal::err_func;
bool err = I2c_HW.write(addr, args...);
IF_CONSTEXPR (fail_fatal && err && err_func)
err_func(addr, sizeof...(args));
}
inline uint8_t I2C_READ_ONE(uint8_t addr, uint8_t reg)
{
using yaal::I2c_HW;
I2c_HW.write<true, false>(addr, reg);
return I2c_HW.read(addr);
}
}
#endif
#endif