IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
alarm_service.c
Go to the documentation of this file.
1#include "alarm_service.h"
2
3#include <stdbool.h>
4#include <time.h>
5
6#include "freertos/FreeRTOS.h"
7#include "freertos/task.h"
8
9#include "driver/gpio.h"
10#include "esp_check.h"
11#include "esp_log.h"
12
13#include "sdkconfig.h"
14
15#include "app_state.h"
16#include "time_service.h"
17
18#ifndef CONFIG_ALARM_ACTIVE_SECONDS
19#define CONFIG_ALARM_ACTIVE_SECONDS 2 //30 in prod
20#endif
21
22#ifndef CONFIG_ALARM_TASK_PERIOD_MS
23#define CONFIG_ALARM_TASK_PERIOD_MS 500
24#endif
25
26#define ALARM_ACTIVE_SECONDS CONFIG_ALARM_ACTIVE_SECONDS
27#define ALARM_TASK_PERIOD_MS CONFIG_ALARM_TASK_PERIOD_MS
28#define BUZZER_GPIO_PIN 1
29
30static const char *TAG = "alarm_service";
31
32static void alarm_service_task(void *arg)
33{
34 int64_t alarm_until_epoch = 0;
35 int64_t last_trigger_minute[APP_ALARM_MAX_COUNT];
36 uint8_t i;
37
38 (void)arg;
39
40 for (i = 0; i < APP_ALARM_MAX_COUNT; i++) {
41 last_trigger_minute[i] = -1;
42 }
43
44 while (true) {
45 app_state_snapshot_t snapshot;
46 struct tm local_time;
47 int64_t now_epoch = 0;
48 bool active = false;
49 bool time_valid = time_service_get_local_time(&local_time, &now_epoch);
50
51 app_state_get_snapshot(&snapshot);
52
53 if (time_valid) {
54 int64_t current_minute = now_epoch / 60;
55
56 for (i = 0; i < snapshot.config.alarm_count && i < APP_ALARM_MAX_COUNT; i++) {
57 if (!snapshot.config.alarms[i].enabled) {
58 continue;
59 }
60
61 bool due = (local_time.tm_hour == snapshot.config.alarms[i].hour) &&
62 (local_time.tm_min == snapshot.config.alarms[i].minute);
63
64 if (due && current_minute != last_trigger_minute[i]) {
65 last_trigger_minute[i] = current_minute;
66 alarm_until_epoch = now_epoch + ALARM_ACTIVE_SECONDS;
67 ESP_LOGI(TAG, "Alarm %u triggered at %02d:%02d",
68 (unsigned int)i, local_time.tm_hour, local_time.tm_min);
69 }
70 }
71 }
72
73 if (alarm_until_epoch > 0 && time_valid && now_epoch < alarm_until_epoch) {
74 active = true;
75 }
76
77 if (!time_valid) {
78 active = false;
79 alarm_until_epoch = 0;
80 }
81
83 gpio_set_level((gpio_num_t)BUZZER_GPIO_PIN, active ? 1 : 0);
84 vTaskDelay(pdMS_TO_TICKS(ALARM_TASK_PERIOD_MS));
85 }
86}
87
88esp_err_t alarm_service_start(void)
89{
90 gpio_config_t gpio_cfg = {
91 .pin_bit_mask = (1ULL << BUZZER_GPIO_PIN),
92 .mode = GPIO_MODE_OUTPUT,
93 .pull_up_en = GPIO_PULLUP_DISABLE,
94 .pull_down_en = GPIO_PULLDOWN_DISABLE,
95 .intr_type = GPIO_INTR_DISABLE,
96 };
97
98 ESP_RETURN_ON_ERROR(gpio_config(&gpio_cfg), TAG, "GPIO config failed");
99 gpio_set_level((gpio_num_t)BUZZER_GPIO_PIN, 0);
100
101 BaseType_t task_ok = xTaskCreate(alarm_service_task, "alarm_task", 4096, NULL, 5, NULL);
102
103 return (task_ok == pdPASS) ? ESP_OK : ESP_FAIL;
104}
#define ALARM_TASK_PERIOD_MS
#define ALARM_ACTIVE_SECONDS
static const char * TAG
static void alarm_service_task(void *arg)
esp_err_t alarm_service_start(void)
Start the alarm service task.
#define BUZZER_GPIO_PIN
Alarm scheduling service API.
void app_state_set_alarm_active(bool active)
Update runtime alarm active flag.
Definition app_state.c:275
void app_state_get_snapshot(app_state_snapshot_t *snapshot)
Get a thread-safe snapshot of the full state.
Definition app_state.c:207
Thread-safe application state and persisted configuration APIs.
#define APP_ALARM_MAX_COUNT
Maximum number of configurable alarms.
Definition app_state.h:26
bool enabled
Definition app_state.h:34
uint8_t hour
Definition app_state.h:32
uint8_t minute
Definition app_state.h:33
alarm_entry_t alarms[APP_ALARM_MAX_COUNT]
Definition app_state.h:46
uint8_t alarm_count
Definition app_state.h:47
Snapshot of full app state.
Definition app_state.h:69
app_config_t config
Definition app_state.h:70
bool time_service_get_local_time(struct tm *out_tm, int64_t *out_epoch)
Get local time adjusted by the configured timezone offset.
Time service APIs for local time access and synchronization triggers.