Эх сурвалжийг харах

time_sync: Add code to synchronize time

Helmut Pozimski 1 жил өмнө
parent
commit
acc157b945

+ 1 - 1
main/CMakeLists.txt

@@ -1,2 +1,2 @@
-idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c"
+idf_component_register(SRCS "esp32_alarm_clock_main.c" "wifi.c" "storage.c" "api.c" "ds3231.c" "bcd.c" "tm1637.c" "time_sync.c" "time_display.c"
                     INCLUDE_DIRS "")

+ 34 - 0
main/time_sync.c

@@ -0,0 +1,34 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+#include <apps/esp_sntp.h>
+#include <esp_log.h>
+
+#include "ds3231.h"
+
+#define TAG "sntp"
+ 
+static void time_sync_callback(struct timeval *tv) {
+    struct tm date_time;
+    localtime_r(&tv->tv_sec, &date_time);
+    ds3231_write_date_time(date_time);
+}
+
+void sntp_start(uint32_t sync_interval) {
+	sntp_setoperatingmode(SNTP_OPMODE_POLL);
+   sntp_setservername(0, "pool.ntp.org");
+   sntp_set_time_sync_notification_cb(time_sync_callback);
+   sntp_init();
+   sntp_set_sync_interval(sync_interval);
+}
+
+void await_sntp_sync() {
+    while (sntp_get_sync_status() == SNTP_SYNC_STATUS_RESET) {
+        ESP_LOGI(TAG, "Waiting for system time to be set...)");
+        vTaskDelay(2000 / portTICK_PERIOD_MS);
+    }
+
+}

+ 8 - 0
main/time_sync.h

@@ -0,0 +1,8 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+ void sntp_start(uint32_t sync_interval);
+ void await_sntp_sync(void);