123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110 |
- /*
- * SPDX-FileCopyrightText: 2023 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 <esp_log.h>
- #include "time_conversion.h"
- #include "storage.h"
- #include "alarm.h"
- #include "ds3231.h"
- #include "alarm_task.h"
- #define SECONDS_PER_DAY 86400
- #define TAG "alarm-task"
- static esp_err_t determine_wakeup_minute_for_day(struct tm* local_time, int16_t* result) {
- esp_err_t ret = read_wakeup_time_tm(local_time, result);
- if (ret == ESP_OK) {
- return ESP_OK;
- }
- ret |= read_wakeup_time_str(WEEK_DAYS[local_time->tm_wday], result);
- return ret;
- }
- static uint16_t calculate_current_minute(struct tm* local_time) {
- return local_time->tm_hour * 60 + local_time->tm_min;
- }
- static uint32_t calculate_current_second(struct tm* local_time) {
- return local_time->tm_hour * 60 * 60 + local_time->tm_min * 60 + local_time->tm_sec;
- }
- static uint32_t determine_next_wakeup_time(struct tm* current_time) {
- struct tm *local_time = convert_to_local(current_time);
- uint32_t current_second = calculate_current_second(local_time);
- int16_t wakeup_minute_today;
-
- esp_err_t ret = determine_wakeup_minute_for_day(local_time, &wakeup_minute_today);
-
- if (ret == ESP_OK) {
- if (wakeup_minute_today >= (current_second / 60)) {
- int32_t wakeup_seconds = wakeup_minute_today * 60 - current_second;
- if (wakeup_seconds > 0) {
- return wakeup_seconds;
- }
- }
- }
-
- current_time->tm_mday++;
- struct tm *local_time_tomorrow = convert_to_local(current_time);
- int16_t wakeup_minute_tomorrow;
- ret = determine_wakeup_minute_for_day(local_time_tomorrow, &wakeup_minute_tomorrow);
-
- if (ret == ESP_OK) {
- ESP_LOGI(TAG, "Wakeup minute for tomorrow: %d", wakeup_minute_tomorrow);
- if (wakeup_minute_tomorrow != -1) {
- return (SECONDS_PER_DAY - current_second) + wakeup_minute_tomorrow * 60;
- }
- }
- return SECONDS_PER_DAY;
-
- }
- static void assert_alarm(uint8_t* button_pressed_flag) {
- *button_pressed_flag = 0;
- for (uint8_t i = 0; i < 60; i++) {
- beep(100);
- vTaskDelay(100 / portTICK_PERIOD_MS);
- beep(100);
- vTaskDelay(2000 / portTICK_PERIOD_MS);
- if (*button_pressed_flag) {
- break;
- }
- }
- }
-
- void alarm_task(void *pvParameters) {
- ESP_LOGI(TAG, "Starting alarm task");
- alarm_parameters* parameters = (alarm_parameters*) pvParameters;
- *parameters->task_handle = xTaskGetCurrentTaskHandle();
- struct tm current_time;
- alarm_init(200);
- uint8_t* button_pressed_flag = parameters->button_pressed_flag;
-
- while (1) {
- ds3231_read_date_time(¤t_time);
- struct tm *local_time = convert_to_local(¤t_time);
- uint16_t current_minute = calculate_current_minute(local_time);
- int16_t wakeup_minute_today;
- esp_err_t ret = determine_wakeup_minute_for_day(local_time, &wakeup_minute_today);
- ESP_LOGI(TAG, "Current minute: %d", current_minute);
- ESP_LOGI(TAG, "Wakeup minute for today: %d", wakeup_minute_today);
- if (ret == ESP_OK && current_minute == wakeup_minute_today) {
- assert_alarm(button_pressed_flag);
- }
- ds3231_read_date_time(¤t_time);
- uint32_t seconds_to_wait = determine_next_wakeup_time(¤t_time);
- ESP_LOGI(TAG, "Sleeping for %ld seconds", seconds_to_wait);
- xTaskNotifyWait(0, 0, NULL, seconds_to_wait * 1000 / portTICK_PERIOD_MS);
- }
- vTaskDelete( NULL );
- }
|