AceTime  1.7.1
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 #include <stdint.h>
10 #include "../hw/DS3231.h"
11 #include "../hw/HardwareDateTime.h"
12 #include "../LocalDateTime.h"
13 #include "Clock.h"
14 
15 namespace ace_time {
16 namespace clock {
17 
21 class DS3231Clock: public Clock {
22  public:
23  explicit DS3231Clock() {}
24 
25  void setup() {}
26 
27  acetime_t getNow() const override {
28  hw::HardwareDateTime hardwareDateTime;
29  mDS3231.readDateTime(&hardwareDateTime);
30  return toDateTime(hardwareDateTime).toEpochSeconds();
31  }
32 
33  void setNow(acetime_t epochSeconds) override {
34  if (epochSeconds == kInvalidSeconds) return;
35 
37  mDS3231.setDateTime(toHardwareDateTime(now));
38  }
39 
40  private:
45  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
47  dt.year + LocalDate::kEpochYear, dt.month, dt.day,
48  dt.hour, dt.minute, dt.second);
49  }
50 
57  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
58  return hw::HardwareDateTime{(uint8_t) dt.yearTiny(), dt.month(),
59  dt.day(), dt.hour(), dt.minute(), dt.second(), dt.dayOfWeek()};
60  }
61 
62  const hw::DS3231 mDS3231;
63 };
64 
65 }
66 }
67 
68 #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:27
ace_time::clock::DS3231Clock
An implementation of Clock that uses a DS3231 RTC chip.
Definition: DS3231Clock.h:21
ace_time::hw::DS3231::setDateTime
void setDateTime(const HardwareDateTime &dateTime) const
Set the DS3231 with the HardwareDateTime values.
Definition: DS3231.cpp:45
ace_time::clock::DS3231Clock::setNow
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: DS3231Clock.h:33
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:19
ace_time::LocalDateTime::minute
uint8_t minute() const
Return the minute.
Definition: LocalDateTime.h:207
ace_time::clock::Clock
Abstract 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::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:26
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