Quellcode durchsuchen

api/alarm_task/main: Implement notification of alarm task when the wakeup time is updated

Helmut Pozimski vor 1 Jahr
Ursprung
Commit
d1c293257a
4 geänderte Dateien mit 43 neuen und 17 gelöschten Zeilen
  1. 9 3
      main/alarm_task.c
  2. 5 0
      main/alarm_task.h
  3. 21 11
      main/api.c
  4. 8 3
      main/esp32_alarm_clock_main.c

+ 9 - 3
main/alarm_task.c

@@ -16,6 +16,7 @@
 #include "storage.h"
 #include "alarm.h"
 #include "ds3231.h"
+#include "alarm_task.h"
 
 #define SECONDS_PER_DAY 86400
 #define TAG "alarm-task"
@@ -46,7 +47,10 @@ static uint32_t determine_next_wakeup_time(struct tm* current_time) {
 	
 	if (ret == ESP_OK) {
 		if (wakeup_minute_today >= (current_second / 60)) {
-			return wakeup_minute_today * 60 - current_second;
+			int32_t wakeup_seconds = wakeup_minute_today * 60 - current_second;
+			if (wakeup_seconds > 0) {
+				return wakeup_seconds;
+			}
 		}
 	}
 	
@@ -80,9 +84,11 @@ static void assert_alarm(uint8_t* button_pressed_flag) {
  
 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 = (uint8_t*) pvParameters;
+	uint8_t* button_pressed_flag = parameters->button_pressed_flag;
 	
 	while (1) {
 		ds3231_read_date_time(&current_time);
@@ -98,7 +104,7 @@ void alarm_task(void *pvParameters) {
 		ds3231_read_date_time(&current_time);
 		uint32_t seconds_to_wait = determine_next_wakeup_time(&current_time);
 		ESP_LOGI(TAG, "Sleeping for %ld seconds", seconds_to_wait);
-		vTaskDelay(seconds_to_wait * 1000 / portTICK_PERIOD_MS);
+		xTaskNotifyWait(0, 0, NULL, seconds_to_wait * 1000 / portTICK_PERIOD_MS);
 	}	
 	vTaskDelete( NULL ); 
 }

+ 5 - 0
main/alarm_task.h

@@ -4,4 +4,9 @@
  * SPDX-License-Identifier: GPL-2.0-only
  */
  
+typedef struct {
+	uint8_t* button_pressed_flag;
+	TaskHandle_t* task_handle;
+} alarm_parameters;
+ 
 void alarm_task(void *pvParameters);

+ 21 - 11
main/api.c

@@ -17,7 +17,7 @@
 #define TAG "api"
 #define BUF_LEN 1000
 
-
+static TaskHandle_t* alarm_task_handle = NULL;
 
 static char* extract_wakeup(const char* uri) {
 	return strrchr(uri, '/') +1;
@@ -77,6 +77,12 @@ static uint8_t validate_time(int hour, int minute) {
 	return (hour >= 0 && hour < 24) && (minute >= 0 && minute < 60);
 }
 
+static void notify_alarm_task() {
+	if (alarm_task_handle != NULL) {
+		xTaskNotify(*alarm_task_handle, 0, eNoAction);	
+	}
+}
+
 static esp_err_t wakeup_put_handler(httpd_req_t *req) {
 	char* wakeup_str = extract_wakeup(req->uri);
 	char buf[BUF_LEN];
@@ -126,6 +132,7 @@ static esp_err_t wakeup_put_handler(httpd_req_t *req) {
 	esp_err_t ret = write_wakeup_time_str(wakeup_str, minute_of_day);
 	if (ret == ESP_OK) {
 		httpd_resp_sendstr(req, "");
+		notify_alarm_task();
 	} else {
 		httpd_resp_send_err(req, HTTPD_500_INTERNAL_SERVER_ERROR, "Error while writing data");		
 	}
@@ -165,19 +172,22 @@ static const httpd_uri_t wakeup_delete = {
 };
 
 
-httpd_handle_t start_webserver() {
-    httpd_handle_t server;
-    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
-    config.lru_purge_enable = true;
-    config.uri_match_fn = httpd_uri_match_wildcard;
+httpd_handle_t start_webserver(TaskHandle_t* task_handle) {
+   httpd_handle_t server;
+   httpd_config_t config = HTTPD_DEFAULT_CONFIG();
+   config.lru_purge_enable = true;
+   config.uri_match_fn = httpd_uri_match_wildcard;
+   if (task_handle != NULL) {
+    	alarm_task_handle = task_handle;
+ 	}
     
     ESP_LOGI(TAG, "Starting server on port: '%d'", config.server_port);
     if (httpd_start(&server, &config) == ESP_OK) {
-        ESP_LOGI(TAG, "Registering URI handlers");
-        httpd_register_uri_handler(server, &wakeup_get);
-        httpd_register_uri_handler(server, &wakeup_put);
+      ESP_LOGI(TAG, "Registering URI handlers");
+      httpd_register_uri_handler(server, &wakeup_get);
+     	httpd_register_uri_handler(server, &wakeup_put);
 		httpd_register_uri_handler(server, &wakeup_delete);
-        return server;
+      return server;
     }
 
     ESP_LOGI(TAG, "Error starting server!");
@@ -206,6 +216,6 @@ void connect_handler(void* arg, esp_event_base_t event_base,
     httpd_handle_t* server = (httpd_handle_t*) arg;
     if (*server == NULL) {
         ESP_LOGI(TAG, "Starting webserver");
-        *server = start_webserver();
+        *server = start_webserver(NULL);
     }
 }

+ 8 - 3
main/esp32_alarm_clock_main.c

@@ -53,13 +53,18 @@ static void init_peripherals(uint8_t* button_pressed_flag) {
 void app_main(void) {
 	static httpd_handle_t server;
 	static uint8_t button_pressed_flag = 0;
+	static TaskHandle_t alarm_task_handle;
+	static alarm_parameters alarm_task_parameters = {
+		.button_pressed_flag = &button_pressed_flag,
+		.task_handle = &alarm_task_handle
+	};
 	struct tm current_time;
 
-	init_peripherals();
+	init_peripherals(&button_pressed_flag);
 
    ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &connect_handler, &server));
    ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &disconnect_handler, &server));
-   server = start_webserver();
+   server = start_webserver(&alarm_task_handle);
 
 	ds3231_read_date_time(&current_time);
 	sntp_start(21600);
@@ -68,5 +73,5 @@ void app_main(void) {
 	}
 	
 	xTaskCreate(display_update_task, "display_update_task", 2048, NULL, 7, NULL);
-	xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &button_pressed_flag, 7, NULL);
+	xTaskCreate(alarm_task, "alarm_task", 2048, (void*) &alarm_task_parameters, 7, NULL);
 }