Pārlūkot izejas kodu

time_display: Add code for a task to update the displayed time

Helmut Pozimski 1 gadu atpakaļ
vecāks
revīzija
19b7ee1c63
2 mainītis faili ar 70 papildinājumiem un 0 dzēšanām
  1. 64 0
      main/time_display.c
  2. 6 0
      main/time_display.h

+ 64 - 0
main/time_display.c

@@ -0,0 +1,64 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+ 
+#include <stdint.h>
+
+#include <esp_err.h>
+#include <freertos/FreeRTOS.h>
+#include <freertos/event_groups.h>
+
+#include "tm1637.h"
+#include "ds3231.h"
+#include "configuration.h"
+
+static void update_display(uint8_t hour, uint8_t minute) {
+    if (hour < 10) {
+        tm1637_set_segment(10, 0, 1);
+        tm1637_set_segment(hour, 1, 1);
+    } else {
+        tm1637_set_segment(hour / 10, 0, 1);
+        tm1637_set_segment(hour % 10, 1, 1);
+    }
+
+    if (minute < 10) {
+        tm1637_set_segment(0, 2, 1);
+        tm1637_set_segment(minute, 3, 1);
+    } else {
+        tm1637_set_segment(minute / 10, 2, 1);
+        tm1637_set_segment(minute % 10, 3, 1);
+    }
+}
+
+static struct tm *convert_to_local(struct tm *timeinfo) {
+    setenv("TZ", "GMT+0GMT+0", 1);
+    tzset();
+    time_t epoch_time = mktime(timeinfo);
+    setenv("TZ", LOCAL_TIMEZONE, 1);
+    tzset();
+    return localtime(&epoch_time);
+}
+
+static uint8_t max(uint8_t a, uint8_t b) {
+    return (a > b) ? a : b;
+}
+
+static uint8_t determine_sleep_time(struct tm *local_time) {
+    uint8_t wakeup_time_sec;
+    wakeup_time_sec = 60 - local_time->tm_sec;
+    wakeup_time_sec = max(wakeup_time_sec, 1);
+    return wakeup_time_sec;
+}
+
+void display_update_task(void *pvParameters) {
+	struct tm current_time;
+	while(1) {
+		ds3231_read_date_time(&current_time);
+		struct tm *local_time = convert_to_local(&current_time);
+		update_display(local_time->tm_hour, local_time->tm_min);
+		vTaskDelay(determine_sleep_time(local_time) / portTICK_PERIOD_MS);
+	}
+	vTaskDelete( NULL );
+}

+ 6 - 0
main/time_display.h

@@ -0,0 +1,6 @@
+/*
+ * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
+ *
+ * SPDX-License-Identifier: GPL-2.0-only
+ */
+void display_update_task(void *pvParameters);