time_display.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 "time_conversion.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 uint8_t max(uint8_t a, uint8_t b) {
  30. return (a > b) ? a : b;
  31. }
  32. static uint16_t determine_sleep_time(struct tm *local_time) {
  33. uint8_t wakeup_time_sec;
  34. wakeup_time_sec = 60 - local_time->tm_sec;
  35. wakeup_time_sec = max(wakeup_time_sec, 1);
  36. return wakeup_time_sec * 1000;
  37. }
  38. void display_update_task(void *pvParameters) {
  39. struct tm current_time;
  40. while(1) {
  41. ds3231_read_date_time(&current_time);
  42. struct tm *local_time = convert_to_local(&current_time);
  43. update_display(local_time->tm_hour, local_time->tm_min);
  44. vTaskDelay(determine_sleep_time(local_time) / portTICK_PERIOD_MS);
  45. }
  46. vTaskDelete( NULL );
  47. }