Change interface to only pass i2c backend driver into class

This commit is contained in:
BlackMark 2020-05-16 19:52:22 +02:00
parent a946746960
commit 1388412d70

View File

@ -28,6 +28,8 @@ struct DateTime : Date, Time {
template <typename I2cDriver, bool SetDayOfWeek = true> template <typename I2cDriver, bool SetDayOfWeek = true>
class DS3231 { class DS3231 {
using i2c_t = i2c::I2c<I2cDriver>;
public: public:
static constexpr auto I2C_ADDRESS = 0x68; static constexpr auto I2C_ADDRESS = 0x68;
@ -50,7 +52,7 @@ class DS3231 {
static inline void init() static inline void init()
{ {
I2cDriver::init(); i2c_t::init();
} }
static auto getDate() static auto getDate()
@ -136,14 +138,14 @@ class DS3231 {
template <uint8_t Address, typename Register> template <uint8_t Address, typename Register>
static Register readRegisterHelper() static Register readRegisterHelper()
{ {
I2cDriver::template start<I2C_ADDRESS>(false); i2c_t::template start<I2C_ADDRESS>(false);
I2cDriver::write(Address); i2c_t::write(Address);
I2cDriver::stop(); i2c_t::stop();
Register reg; Register reg;
I2cDriver::template start<I2C_ADDRESS>(true); i2c_t::template start<I2C_ADDRESS>(true);
I2cDriver::template readBytes<sizeof(Register)>(reinterpret_cast<uint8_t *>(&reg)); i2c_t::template readBytes<sizeof(Register)>(reinterpret_cast<uint8_t *>(&reg));
I2cDriver::stop(); i2c_t::stop();
return reg; return reg;
} }
@ -197,10 +199,10 @@ class DS3231 {
static_assert(util::always_false_v<Register>, "Invalid register address"); static_assert(util::always_false_v<Register>, "Invalid register address");
} }
I2cDriver::template start<I2C_ADDRESS>(false); i2c_t::template start<I2C_ADDRESS>(false);
I2cDriver::write(Address + StartOffset); i2c_t::write(Address + StartOffset);
I2cDriver::template writeBytes<WRITE_SIZE>(reinterpret_cast<const uint8_t *>(&reg) + StartOffset); i2c_t::template writeBytes<WRITE_SIZE>(reinterpret_cast<const uint8_t *>(&reg) + StartOffset);
I2cDriver::stop(); i2c_t::stop();
} }
static uint8_t calcDayOfWeek(uint16_t year, uint8_t month, uint16_t day) static uint8_t calcDayOfWeek(uint16_t year, uint8_t month, uint16_t day)