alarm_task.c 3.3 KB

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