-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRtc.cpp
More file actions
97 lines (72 loc) · 2.4 KB
/
Rtc.cpp
File metadata and controls
97 lines (72 loc) · 2.4 KB
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
#include "Rtc.h"
#include "config/Pins.h"
#include "Hardware.h"
#include "Logging.h"
#include <Wire.h>
using namespace utils;
namespace rtc {
#define CURRENT_LOGGER "rtc"
RTC_A_CLASS RTC_A;
void setup() {
#define CURRENT_LOGGER_FUNCTION "setup"
VERBOSE;
hardware::i2c::powerOn();
RTC_A.control(DS3231_12H, DS3231_OFF); //24 hours clock
RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_OFF); //Alarm 1 OFF
RTC_A.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
hardware::i2c::powerOff();
}
int16_t getTemperature() {
hardware::i2c::powerOn();
float temperature = RTC_A.readTempRegister();
hardware::i2c::powerOff();
return static_cast<int16_t>(temperature * 100);
}
bool isAccurate() {
hardware::i2c::powerOn();
bool accurate = RTC_A.status(DS3231_HALTED_FLAG) == DS3231_OFF;
hardware::i2c::powerOff();
return accurate;
}
timestamp_t getTime() {
tmElements_t time;
getTime(time);
return time::makeTimestamp(time);
}
void getTime(tmElements_t &time) {
#define CURRENT_LOGGER_FUNCTION "getTime"
hardware::i2c::powerOn();
RTC_A.readTime(time);
hardware::i2c::powerOff();
VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second);
}
void setTime(const tmElements_t &time) {
#define CURRENT_LOGGER_FUNCTION "setTime"
VERBOSE_FORMAT("%d/%d/%d %d:%d:%d", time.year, time.month, time.day, time.hour, time.minute, time.second);
hardware::i2c::powerOn();
RTC_A.writeTime(time);
RTC_A.control(DS3231_HALTED_FLAG, DS3231_OFF);
hardware::i2c::powerOff();
}
void setAlarm(uint16_t seconds) {
tmElements_t currentTime;
tmElements_t alarmTime;
hardware::i2c::powerOn();
getTime(currentTime);
time::breakTime(time::makeTimestamp(currentTime) + seconds, alarmTime);
setAlarm(alarmTime);
hardware::i2c::powerOff();
}
void setAlarm(const tmElements_t &time) {
#define CURRENT_LOGGER_FUNCTION "setAlarm"
hardware::i2c::powerOn();
WRITE_ALARM_1(time);
RTC_A.control(DS3231_A1_FLAG, DS3231_OFF); //reset Alarm 1 flag
RTC_A.control(DS3231_A1_INT_ENABLE, DS3231_ON); //Alarm 1 ON
RTC_A.control(DS3231_INT_ENABLE, DS3231_ON); //INTCN ON
tmElements_t alarmTime;
READ_ALARM_1(alarmTime);
NOTICE_FORMAT("Next alarm : %d:%d:%d", alarmTime.hour, alarmTime.minute, alarmTime.second);
hardware::i2c::powerOff();
}
}