AceTimeClock  1.3.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 
41 class StmRtcClock: public Clock {
42  public:
44  explicit StmRtcClock() = default;
45 
47  void setup() {}
48 
49  acetime_t getNow() const override {
50  hw::HardwareDateTime hardwareDateTime;
51  mStmRtc.readDateTime(&hardwareDateTime);
52  return toDateTime(hardwareDateTime).toEpochSeconds();
53  }
54 
55  void setNow(acetime_t epochSeconds) override {
56  if (epochSeconds == kInvalidSeconds) return;
57 
58  LocalDateTime now = LocalDateTime::forEpochSeconds(epochSeconds);
59  mStmRtc.setDateTime(toHardwareDateTime(now));
60  }
61 
63  bool isTimeSet() const {
64  return mStmRtc.isTimeSet();
65  }
66 
67  private:
72  static LocalDateTime toDateTime(const hw::HardwareDateTime& dt) {
73  return LocalDateTime::forComponents(
75  dt.hour, dt.minute, dt.second);
76  }
77 
83  static hw::HardwareDateTime toHardwareDateTime(const LocalDateTime& dt) {
84  return hw::HardwareDateTime{
85  (uint8_t) (dt.year() - hw::HardwareDateTime::kBaseYear),
86  dt.month(),
87  dt.day(),
88  dt.hour(),
89  dt.minute(),
90  dt.second(),
91  dt.dayOfWeek()};
92  }
93 
94  hw::StmRtc mStmRtc;
95 };
96 
97 } // hw
98 } // ace_time
99 
100 #endif // #if defined(ARDUINO_ARCH_STM32) || defined(EPOXY_DUINO)
101 #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:41
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtcClock.h:63
void setNow(acetime_t epochSeconds) override
Set the time to the indicated seconds.
Definition: StmRtcClock.h:55
acetime_t getNow() const override
Return the number of seconds since the AceTime epoch (2000-01-01T00:00:00Z).
Definition: StmRtcClock.h:49
void setup()
Setup does nothing.
Definition: StmRtcClock.h:47
StmRtcClock()=default
Constructor.
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
void setDateTime(const HardwareDateTime &dateTime) const
Set the STM with the HardwareDateTime values.
Definition: StmRtc.cpp:59
bool isTimeSet() const
Return true if the RTC is available and the time is set.
Definition: StmRtc.cpp:69
void readDateTime(HardwareDateTime *dateTime) const
Read the time into the HardwareDateTime object.
Definition: StmRtc.cpp:18