AceTime  0.6
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.
DS3231Clock.h
1 /*
2  * MIT License
3  * Copyright (c) 2018 Brian T. Park
4  */
5 
6 #ifndef ACE_TIME_DS3231_CLOCK_H
7 #define ACE_TIME_DS3231_CLOCK_H
8 
9 #if defined(ARDUINO)
10 
11 #include <stdint.h>
12 #include "../hw/DS3231.h"
13 #include "../hw/HardwareDateTime.h"
14 #include "../LocalDateTime.h"
15 #include "Clock.h"
16 
17 namespace ace_time {
18 namespace clock {
19 
23 class DS3231Clock: public Clock {
24  public:
25  explicit DS3231Clock() {}
26 
27  void setup() {}
28 
29  acetime_t getNow() const override {
30  hw::HardwareDateTime hardwareDateTime;
31  mDS3231.readDateTime(&hardwareDateTime);
32  return toDateTime(hardwareDateTime).toEpochSeconds();
33  }
34 
35  void setNow(acetime_t epochSeconds) override {
36  if (epochSeconds == kInvalidSeconds) return;
37 
38  LocalDateTime now = LocalDateTime::forEpochSeconds(epochSeconds);
39  mDS3231.setDateTime(toHardwareDateTime(now));
40  }
41 
42  private:
47  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
49  dt.year + LocalDate::kEpochYear, dt.month, dt.day,
50  dt.hour, dt.minute, dt.second);
51  }
52 
59  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
60  return hw::HardwareDateTime{(uint8_t) dt.yearTiny(), dt.month(),
61  dt.day(), dt.hour(), dt.minute(), dt.second(), dt.dayOfWeek()};
62  }
63 
64  const hw::DS3231 mDS3231;
65 };
66 
67 }
68 }
69 
70 #endif
71 
72 #endif
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:67
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:40
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:39