AceTime  1.0
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(UNIX_HOST_DUINO)
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 
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
uint8_t minute() const
Return the minute.
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: DS3231.cpp:22
uint8_t day() const
Return the day of the month.
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:67
An implementation of Clock that uses a DS3231 RTC chip.
Definition: DS3231Clock.h:23
void setDateTime(const HardwareDateTime &dateTime) const
Set the DS3231 with the HardwareDateTime values.
Definition: DS3231.cpp:48
uint8_t month() const
Return the month with January=1, December=12.
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
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch 2000-01-01 00:00:00Z, after assuming that the date and time compon...
uint8_t hour() const
Return the hour.
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
uint8_t second() const
Return the second.
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:39
The date (year, month, day) and time (hour, minute, second) fields supported by the DS3231 RTC chip...
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: DS3231Clock.h:29
A class that reads and writes HardwareDateTime and HardwareTemperature from a DS3231 RTC chip...
Definition: DS3231.h:34
Base class for objects that provide and store time.
Definition: Clock.h:20
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:27
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: DS3231Clock.h:35