optional RTC 3231 integration
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
#include "time_manager.h"
|
||||
#include "compressor.h"
|
||||
#include "config.h"
|
||||
#include "rtc_ds3231.h"
|
||||
#include <time.h>
|
||||
|
||||
static bool g_time_synced = false;
|
||||
static bool g_tz_set = false;
|
||||
static bool g_rtc_present = false;
|
||||
|
||||
void time_receiver_init(const char *ntp_server_1, const char *ntp_server_2) {
|
||||
const char *server1 = (ntp_server_1 && ntp_server_1[0] != '\0') ? ntp_server_1 : "pool.ntp.org";
|
||||
@@ -40,6 +42,10 @@ void time_set_utc(uint32_t epoch) {
|
||||
tv.tv_usec = 0;
|
||||
settimeofday(&tv, nullptr);
|
||||
g_time_synced = true;
|
||||
|
||||
if (g_rtc_present) {
|
||||
rtc_ds3231_set_epoch(epoch);
|
||||
}
|
||||
}
|
||||
|
||||
void time_send_timesync(uint16_t device_id_short) {
|
||||
@@ -101,3 +107,30 @@ void time_get_local_hhmm(char *out, size_t out_len) {
|
||||
localtime_r(&now, &timeinfo);
|
||||
snprintf(out, out_len, "%02d:%02d", timeinfo.tm_hour, timeinfo.tm_min);
|
||||
}
|
||||
|
||||
void time_rtc_init() {
|
||||
if (!ENABLE_DS3231) {
|
||||
g_rtc_present = false;
|
||||
return;
|
||||
}
|
||||
g_rtc_present = rtc_ds3231_init();
|
||||
}
|
||||
|
||||
bool time_try_load_from_rtc() {
|
||||
if (!g_rtc_present) {
|
||||
return false;
|
||||
}
|
||||
if (time_is_synced()) {
|
||||
return true;
|
||||
}
|
||||
uint32_t epoch = 0;
|
||||
if (!rtc_ds3231_read_epoch(epoch) || epoch == 0) {
|
||||
return false;
|
||||
}
|
||||
time_set_utc(epoch);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool time_rtc_present() {
|
||||
return g_rtc_present;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user