AceTimeClock  1.0.3
Clock classes for Arduino 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 #include <stdint.h>
10 #include <AceTime.h>
11 #include "../hw/DS3231.h"
12 #include "../hw/HardwareDateTime.h"
13 #include "Clock.h"
14 
15 namespace ace_time {
16 namespace clock {
17 
23 template<typename T_WIREI>
24 class DS3231Clock : public Clock {
25  public:
27  explicit DS3231Clock(const T_WIREI& wireInterface) :
28  mDS3231(wireInterface)
29  {}
30 
32  void setup() {}
33 
34  acetime_t getNow() const override {
35  hw::HardwareDateTime hardwareDateTime;
36  mDS3231.readDateTime(&hardwareDateTime);
37  return toDateTime(hardwareDateTime).toEpochSeconds();
38  }
39 
40  void setNow(acetime_t epochSeconds) override {
41  if (epochSeconds == kInvalidSeconds) return;
42 
43  LocalDateTime now = LocalDateTime::forEpochSeconds(epochSeconds);
44  mDS3231.setDateTime(toHardwareDateTime(now));
45  }
46 
47  private:
52  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
53  return LocalDateTime::forComponents(
54  dt.year + LocalDate::kEpochYear, dt.month, dt.day,
55  dt.hour, dt.minute, dt.second);
56  }
57 
64  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
65  return hw::HardwareDateTime{(uint8_t) dt.yearTiny(), dt.month(),
66  dt.day(), dt.hour(), dt.minute(), dt.second(), dt.dayOfWeek()};
67  }
68 
69  private:
70  const hw::DS3231<T_WIREI> mDS3231;
71 };
72 
73 }
74 }
75 
76 #endif
ace_time::clock::DS3231Clock::getNow
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: DS3231Clock.h:34
ace_time::hw::HardwareDateTime::minute
uint8_t minute
[0, 59]
Definition: HardwareDateTime.h:36
ace_time::clock::DS3231Clock
An implementation of Clock that uses a DS3231 RTC chip.
Definition: DS3231Clock.h:24
ace_time::hw::HardwareDateTime::year
uint8_t year
[00, 99], year - 2000
Definition: HardwareDateTime.h:24
ace_time::hw::HardwareDateTime::day
uint8_t day
[1, 31]
Definition: HardwareDateTime.h:30
ace_time::clock::DS3231Clock::DS3231Clock
DS3231Clock(const T_WIREI &wireInterface)
Constructor.
Definition: DS3231Clock.h:27
ace_time::hw::HardwareDateTime::second
uint8_t second
[0, 59]
Definition: HardwareDateTime.h:39
ace_time::clock::Clock
Abstract base class for objects that provide and store time.
Definition: Clock.h:19
ace_time::clock::Clock::kInvalidSeconds
static const acetime_t kInvalidSeconds
Error value returned by getNow() and other methods when this object is not yet initialized.
Definition: Clock.h:25
ace_time::clock::DS3231Clock::setup
void setup()
Setup that currently does nothing.
Definition: DS3231Clock.h:32
ace_time::hw::HardwareDateTime::hour
uint8_t hour
[0, 23]
Definition: HardwareDateTime.h:33
ace_time::clock::DS3231Clock::setNow
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: DS3231Clock.h:40
ace_time::hw::HardwareDateTime::month
uint8_t month
[1, 12]
Definition: HardwareDateTime.h:27
ace_time::hw::HardwareDateTime
The date (year, month, day) and time (hour, minute, second) fields supported by the DS3231 RTC chip.
Definition: HardwareDateTime.h:19