time_display.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * SPDX-FileCopyrightText: 2022 Helmut Pozimski <helmut@pozimski.eu>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0-only
  5. */
  6. #include <stdint.h>
  7. #include <esp_err.h>
  8. #include <freertos/FreeRTOS.h>
  9. #include <freertos/event_groups.h>
  10. #include "tm1637.h"
  11. #include "ds3231.h"
  12. #include "configuration.h"
  13. static void update_display(uint8_t hour, uint8_t minute) {
  14. if (hour < 10) {
  15. tm1637_set_segment(10, 0, 1);
  16. tm1637_set_segment(hour, 1, 1);
  17. } else {
  18. tm1637_set_segment(hour / 10, 0, 1);
  19. tm1637_set_segment(hour % 10, 1, 1);
  20. }
  21. if (minute < 10) {
  22. tm1637_set_segment(0, 2, 1);
  23. tm1637_set_segment(minute, 3, 1);
  24. } else {
  25. tm1637_set_segment(minute / 10, 2, 1);
  26. tm1637_set_segment(minute % 10, 3, 1);
  27. }
  28. }
  29. static struct tm *convert_to_local(struct tm *timeinfo) {
  30. setenv("TZ", "GMT+0GMT+0", 1);
  31. tzset();
  32. time_t epoch_time = mktime(timeinfo);
  33. setenv("TZ", LOCAL_TIMEZONE, 1);
  34. tzset();
  35. return localtime(&epoch_time);
  36. }
  37. static uint8_t max(uint8_t a, uint8_t b) {
  38. return (a > b) ? a : b;
  39. }
  40. static uint16_t determine_sleep_time(struct tm *local_time) {
  41. uint8_t wakeup_time_sec;
  42. wakeup_time_sec = 60 - local_time->tm_sec;
  43. wakeup_time_sec = max(wakeup_time_sec, 1);
  44. return wakeup_time_sec * 1000;
  45. }
  46. void display_update_task(void *pvParameters) {
  47. struct tm current_time;
  48. while(1) {
  49. ds3231_read_date_time(&current_time);
  50. struct tm *local_time = convert_to_local(&current_time);
  51. setenv("TZ", "GMT+0GMT+0", 1);
  52. tzset();
  53. update_display(local_time->tm_hour, local_time->tm_min);
  54. vTaskDelay(determine_sleep_time(local_time) / portTICK_PERIOD_MS);
  55. }
  56. vTaskDelete( NULL );
  57. }