Fix partial writing to only write to a range instead of until the end
This commit is contained in:
parent
cd5317db5b
commit
a65b30f9df
22
ds3231.hpp
22
ds3231.hpp
@ -93,12 +93,13 @@ class DS3231 {
|
||||
timeReg.setYear(date.year);
|
||||
timeReg.setMonth(date.month);
|
||||
timeReg.setDate(date.day);
|
||||
if constexpr (SetDayOfWeek) {
|
||||
if constexpr (SetDayOfWeek)
|
||||
timeReg.setDay(calcDayOfWeek(date.year, date.month, date.day) + 1);
|
||||
}
|
||||
|
||||
constexpr auto DATE_OFFSET = offsetof(detail::TimeReg, day);
|
||||
writePartialRegister<TIME_REG_ADDR, DATE_OFFSET>(timeReg);
|
||||
constexpr auto DATE_START_OFFSET = offsetof(detail::TimeReg, day);
|
||||
constexpr auto DATE_END_OFFSET = offsetof(detail::TimeReg, year);
|
||||
|
||||
writePartialRegister<TIME_REG_ADDR, DATE_START_OFFSET, DATE_END_OFFSET>(timeReg);
|
||||
}
|
||||
static void setTime(const Time &time) {}
|
||||
static void setDateTime(const DateTime &dateTime) {}
|
||||
@ -140,10 +141,15 @@ class DS3231 {
|
||||
}
|
||||
}
|
||||
|
||||
template <uint8_t Address, uint8_t Offset, typename Register>
|
||||
template <uint8_t Address, uint8_t StartOffset, uint8_t EndOffset, typename Register>
|
||||
static void writePartialRegister(const Register ®)
|
||||
{
|
||||
static_assert(Offset < sizeof(Register), "Offset out of bounds");
|
||||
static_assert(StartOffset <= EndOffset, "Invalid offset range");
|
||||
static_assert(StartOffset < sizeof(Register), "Start offset out of bounds");
|
||||
static_assert(EndOffset < sizeof(Register), "End offset out of bounds");
|
||||
|
||||
constexpr auto WRITE_SIZE = EndOffset + 1 - StartOffset;
|
||||
static_assert(StartOffset + WRITE_SIZE <= sizeof(Register), "Writing out of bounds");
|
||||
|
||||
if constexpr (Address == TIME_REG_ADDR) {
|
||||
static_assert(type::is_same_v<Register, detail::TimeReg>, "Invalid register type");
|
||||
@ -164,8 +170,8 @@ class DS3231 {
|
||||
}
|
||||
|
||||
I2cDriver::template start<I2C_ADDRESS>(false);
|
||||
I2cDriver::write(Address + Offset);
|
||||
I2cDriver::template writeBytes<sizeof(Register) - Offset>(reinterpret_cast<const uint8_t *>(®) + Offset);
|
||||
I2cDriver::write(Address + StartOffset);
|
||||
I2cDriver::template writeBytes<WRITE_SIZE>(reinterpret_cast<const uint8_t *>(®) + StartOffset);
|
||||
I2cDriver::stop();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user