-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeUtils.h
115 lines (98 loc) · 2.13 KB
/
TimeUtils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#pragma once
#include <RtcDS3231.h>
#include <Wire.h>
struct Time
{
uint8_t Hour;
uint8_t Min;
uint8_t Sec;
operator<(const Time &o)
{
return Hour <= o.Hour && Min <= o.Min &&
Sec <= o.Sec && Min < o.Min;
}
operator<=(const Time &o)
{
return Hour<=o.Hour && Min <= o.Min &&
Sec <= o.Sec && Min <= o.Min;
}
operator>(const Time &o)
{
return Hour >= o.Hour && Min >= o.Min &&
Sec >= o.Sec && Min > o.Min;
}
operator>=(const Time &o)
{
return Hour>=o.Hour && Min >= o.Min &&
Sec >= o.Sec && Min >= o.Min;
}
void normalize()
{
while (Sec >= 60)
{
++Min;
Sec -= 60;
while (Min >= 60)
{
Min -= 60;
++Hour;
while (Hour >= 24)
{
Hour -= 24;
}
}
}
}
Time& operator+(const Time &time)
{
Hour += time.Hour;
Min += time.Min;
Sec += time.Sec;
normalize();
}
};
struct TimeRange
{
Time begin_;
Time end_;
TimeRange(Time start, Time stop) : begin_(start),end_(stop)
{
}
bool operator<(const Time &o)
{
o < begin_;
}
bool operator>(const Time &o)
{
o >= end_;
}
};
class RTC_Interface
{
private:
RtcDS3231<TwoWire> Rtc{Wire};
public:
void init(Time &time)
{
Rtc.Begin();
Rtc.Enable32kHzPin(false);
Rtc.SetSquareWavePinClockFrequency(DS3231SquareWaveClock_1Hz);
Rtc.SetSquareWavePin(DS3231SquareWavePin_ModeClock, false);
RtcDateTime now = Rtc.GetDateTime();
time.Hour = now.Hour();
time.Min = now.Minute();
time.Sec = now.Second();
}
void update(Time &time)
{
RtcDateTime now = Rtc.GetDateTime();
time.Hour = now.Hour();
time.Min = now.Minute();
time.Sec = now.Second();
}
void set(Time &time)
{
RtcDateTime now{2020, 1, 1, time.Hour, time.Min, time.Sec};
Rtc.SetDateTime(now);
}
};