AceTime  1.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.
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
ace_time::LocalDateTime::dayOfWeek
uint8_t dayOfWeek() const
Return the day of the week, Monday=1, Sunday=7 (per ISO 8601).
Definition: LocalDateTime.h:219
ace_time::LocalDateTime::hour
uint8_t hour() const
Return the hour.
Definition: LocalDateTime.h:201
ace_time::LocalDateTime
Class that holds the date-time as the components (year, month, day, hour, minute, second) without reg...
Definition: LocalDateTime.h:30
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:29
ace_time::clock::DS3231Clock
An implementation of Clock that uses a DS3231 RTC chip.
Definition: DS3231Clock.h:23
ace_time::hw::DS3231::setDateTime
void setDateTime(const HardwareDateTime &dateTime) const
Set the DS3231 with the HardwareDateTime values.
Definition: DS3231.cpp:48
ace_time::clock::DS3231Clock::setNow
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: DS3231Clock.h:35
ace_time::LocalDateTime::forComponents
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:43
ace_time::LocalDateTime::month
uint8_t month() const
Return the month with January=1, December=12.
Definition: LocalDateTime.h:189
ace_time::LocalDateTime::second
uint8_t second() const
Return the second.
Definition: LocalDateTime.h:213
ace_time::hw::DS3231::readDateTime
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: DS3231.cpp:22
ace_time::LocalDateTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalDateTime.h:207
ace_time::clock::Clock
Base class for objects that provide and store time.
Definition: Clock.h:20
ace_time::LocalDateTime::toEpochSeconds
acetime_t toEpochSeconds() const
Return seconds since AceTime epoch 2000-01-01 00:00:00Z, after assuming that the date and time compon...
Definition: LocalDateTime.h:246
ace_time::LocalDate::kEpochYear
static const int16_t kEpochYear
Base year of epoch.
Definition: LocalDate.h:39
ace_time::LocalDateTime::yearTiny
int8_t yearTiny() const
Return the single-byte year offset from year 2000.
Definition: LocalDateTime.h:179
ace_time::LocalDateTime::day
uint8_t day() const
Return the day of the month.
Definition: LocalDateTime.h:195
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
ace_time::LocalDateTime::forEpochSeconds
static LocalDateTime forEpochSeconds(acetime_t epochSeconds)
Factory method.
Definition: LocalDateTime.h:70