alarm_task.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * SPDX-FileCopyrightText: 2023 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 <esp_log.h>
  11. #include "time_conversion.h"
  12. #include "storage.h"
  13. #include "alarm.h"
  14. #include "ds3231.h"
  15. #include "alarm_task.h"
  16. #define SECONDS_PER_DAY 86400
  17. #define TAG "alarm-task"
  18. static esp_err_t determine_wakeup_minute_for_day(struct tm* local_time, int16_t* result) {
  19. esp_err_t ret = read_wakeup_time_tm(local_time, result);
  20. if (ret == ESP_OK) {
  21. return ESP_OK;
  22. }
  23. ret |= read_wakeup_time_str(WEEK_DAYS[local_time->tm_wday], result);
  24. return ret;
  25. }
  26. static uint16_t calculate_current_minute(struct tm* local_time) {
  27. return local_time->tm_hour * 60 + local_time->tm_min;
  28. }
  29. static uint32_t calculate_current_second(struct tm* local_time) {
  30. return local_time->tm_hour * 60 * 60 + local_time->tm_min * 60 + local_time->tm_sec;
  31. }
  32. static uint32_t determine_next_wakeup_time(struct tm* current_time) {
  33. struct tm *local_time = convert_to_local(current_time);
  34. uint32_t current_second = calculate_current_second(local_time);
  35. int16_t wakeup_minute_today;
  36. esp_err_t ret = determine_wakeup_minute_for_day(local_time, &wakeup_minute_today);
  37. if (ret == ESP_OK) {
  38. if (wakeup_minute_today >= (current_second / 60)) {
  39. int32_t wakeup_seconds = wakeup_minute_today * 60 - current_second;
  40. if (wakeup_seconds > 0) {
  41. return wakeup_seconds;
  42. }
  43. }
  44. }
  45. current_time->tm_mday++;
  46. struct tm *local_time_tomorrow = convert_to_local(current_time);
  47. int16_t wakeup_minute_tomorrow;
  48. ret = determine_wakeup_minute_for_day(local_time_tomorrow, &wakeup_minute_tomorrow);
  49. if (ret == ESP_OK) {
  50. ESP_LOGI(TAG, "Wakeup minute for tomorrow: %d", wakeup_minute_tomorrow);
  51. if (wakeup_minute_tomorrow != -1) {
  52. return (SECONDS_PER_DAY - current_second) + wakeup_minute_tomorrow * 60;
  53. }
  54. }
  55. return SECONDS_PER_DAY;
  56. }
  57. static void assert_alarm(uint8_t* button_pressed_flag) {
  58. *button_pressed_flag = 0;
  59. for (uint8_t i = 0; i < 60; i++) {
  60. beep(100);
  61. vTaskDelay(100 / portTICK_PERIOD_MS);
  62. beep(100);
  63. vTaskDelay(2000 / portTICK_PERIOD_MS);
  64. if (*button_pressed_flag) {
  65. break;
  66. }
  67. }
  68. }
  69. void alarm_task(void *pvParameters) {
  70. ESP_LOGI(TAG, "Starting alarm task");
  71. alarm_parameters* parameters = (alarm_parameters*) pvParameters;
  72. *parameters->task_handle = xTaskGetCurrentTaskHandle();
  73. struct tm current_time;
  74. alarm_init(200);
  75. uint8_t* button_pressed_flag = parameters->button_pressed_flag;
  76. while (1) {
  77. ds3231_read_date_time(&current_time);
  78. struct tm *local_time = convert_to_local(&current_time);
  79. uint16_t current_minute = calculate_current_minute(local_time);
  80. int16_t wakeup_minute_today;
  81. esp_err_t ret = determine_wakeup_minute_for_day(local_time, &wakeup_minute_today);
  82. ESP_LOGI(TAG, "Current minute: %d", current_minute);
  83. ESP_LOGI(TAG, "Wakeup minute for today: %d", wakeup_minute_today);
  84. if (ret == ESP_OK && current_minute == wakeup_minute_today) {
  85. assert_alarm(button_pressed_flag);
  86. }
  87. ds3231_read_date_time(&current_time);
  88. uint32_t seconds_to_wait = determine_next_wakeup_time(&current_time);
  89. ESP_LOGI(TAG, "Sleeping for %ld seconds", seconds_to_wait);
  90. xTaskNotifyWait(0, 0, NULL, seconds_to_wait * 1000 / portTICK_PERIOD_MS);
  91. }
  92. vTaskDelete( NULL );
  93. }