AceTimeClock  1.2.0
Clock classes for Arduino that can synchronize from an NTP server or an RTC chip
StmRtcClock.h
1 /*
2  * MIT License
3  * Copyright (c) 2020 Brian T. Park, Anatoli Arkhipenko
4  *
5  * Requires https://github.com/stm32duino/STM32RTC
6  */
7 
8 #ifndef ACE_TIME_STM_RTC_CLOCK_H
9 #define ACE_TIME_STM_RTC_CLOCK_H
10 
11 #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
12 
13 #include <stdint.h>
14 #include <AceTime.h> // LocalDateTime
15 #include "../hw/StmRtc.h"
16 #include "../hw/HardwareDateTime.h"
17 #include "Clock.h"
18 
19 namespace ace_time {
20 namespace clock {
21 
28 class StmRtcClock: public Clock {
29  public:
31  explicit StmRtcClock() = default;
32 
34  void setup() {}
35 
36  acetime_t getNow() const override {
37  hw::HardwareDateTime hardwareDateTime;
38  mStmRtc.readDateTime(&hardwareDateTime);
39  return toDateTime(hardwareDateTime).toEpochSeconds();
40  }
41 
42  void setNow(acetime_t epochSeconds) override {
43  if (epochSeconds == kInvalidSeconds) return;
44 
45  LocalDateTime now = LocalDateTime::forEpochSeconds(epochSeconds);
46  mStmRtc.setDateTime(toHardwareDateTime(now));
47  }
48 
50  bool isTimeSet() const {
51  return mStmRtc.isTimeSet();
52  }
53 
54  private:
59  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
60  return LocalDateTime::forComponents(
62  dt.hour, dt.minute, dt.second);
63  }
64 
70  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
71  return hw::HardwareDateTime{
72  (uint8_t) (dt.year() - hw::HardwareDateTime::kBaseYear),
73  dt.month(),
74  dt.day(),
75  dt.hour(),
76  dt.minute(),
77  dt.second(),
78  dt.dayOfWeek()};
79  }
80 
81  const hw::StmRtc mStmRtc;
82 };
83 
84 } // hw
85 } // ace_time
86 
87 #endif // #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
88 #endif // #ifndef ACE_TIME_STM_RTC_CLOCK_H
Abstract base class for objects that provide and store time.
Definition: Clock.h:19
static const acetime_t kInvalidSeconds
Error value returned by getNow() and other methods when this object is not yet initialized.
Definition: Clock.h:25
An implementation of Clock that uses an STM32 RTC chip using the STM32RTC library.
Definition: StmRtcClock.h:28
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtcClock.h:50
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: StmRtcClock.h:42
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: StmRtcClock.h:36
void setup()
Setup does nothing.
Definition: StmRtcClock.h:34
StmRtcClock()=default
Constructor.
void setDateTime(const HardwareDateTime &dateTime) const
Set the STM with the HardwareDateTime values.
Definition: StmRtc.cpp:49
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtc.cpp:55
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: StmRtc.cpp:19
The date (year, month, day) and time (hour, minute, second) fields supported by the DS3231 RTC chip.
static const int16_t kBaseYear
Base year of the DS3231 chip.
uint8_t year
[00, 99], year - 2000