AceTime  0.3
Date and time classes for Arduino that support timezones from the TZ Database, and a system clock that can synchronize from an NTP server or an RTC chip.
DS3231TimeKeeper.h
1 #ifndef ACE_TIME_DS3231_TIME_KEEPER_H
2 #define ACE_TIME_DS3231_TIME_KEEPER_H
3 
4 #if defined(ARDUINO)
5 
6 #include <stdint.h>
7 #include "../hw/DS3231.h"
8 #include "../hw/HardwareDateTime.h"
9 #include "../LocalDateTime.h"
10 #include "TimeKeeper.h"
11 
12 namespace ace_time {
13 namespace clock {
14 
18 class DS3231TimeKeeper: public TimeKeeper {
19  public:
20  explicit DS3231TimeKeeper() {}
21 
22  void setup() {}
23 
24  acetime_t getNow() const override {
25  hw::HardwareDateTime hardwareDateTime;
26  mDS3231.readDateTime(&hardwareDateTime);
27  return toDateTime(hardwareDateTime).toEpochSeconds();
28  }
29 
30  void setNow(acetime_t epochSeconds) override {
31  if (epochSeconds == kInvalidSeconds) return;
32 
33  LocalDateTime now = LocalDateTime::forEpochSeconds(epochSeconds);
34  mDS3231.setDateTime(toHardwareDateTime(now));
35  }
36 
37  private:
42  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
44  dt.year + LocalDate::kEpochYear, dt.month, dt.day,
45  dt.hour, dt.minute, dt.second);
46  }
47 
54  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
55  return hw::HardwareDateTime{(uint8_t) dt.yearTiny(), dt.month(),
56  dt.day(), dt.hour(), dt.minute(), dt.second(), dt.dayOfWeek()};
57  }
58 
59  const hw::DS3231 mDS3231;
60 };
61 
62 }
63 }
64 
65 #endif
66 
67 #endif
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:53
static LocalDateTime forComponents(int16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
Factory method using separated date and time components.
Definition: LocalDateTime.h:26
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:33