IoT25S IoT Klocka 0.1
Grupparbete för IoT25S på Jensen YH
Loading...
Searching...
No Matches
alarm_service.c File Reference
#include "alarm_service.h"
#include <stdbool.h>
#include <time.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "esp_check.h"
#include "esp_log.h"
#include "sdkconfig.h"
#include "app_state.h"
#include "time_service.h"
+ Include dependency graph for alarm_service.c:

Go to the source code of this file.

Macros

#define CONFIG_ALARM_ACTIVE_SECONDS   2
 
#define CONFIG_ALARM_TASK_PERIOD_MS   500
 
#define ALARM_ACTIVE_SECONDS   CONFIG_ALARM_ACTIVE_SECONDS
 
#define ALARM_TASK_PERIOD_MS   CONFIG_ALARM_TASK_PERIOD_MS
 
#define BUZZER_GPIO_PIN   1
 

Functions

static void alarm_service_task (void *arg)
 
esp_err_t alarm_service_start (void)
 Start the alarm service task.
 

Variables

static const char * TAG = "alarm_service"
 

Macro Definition Documentation

◆ ALARM_ACTIVE_SECONDS

#define ALARM_ACTIVE_SECONDS   CONFIG_ALARM_ACTIVE_SECONDS

Definition at line 26 of file alarm_service.c.

◆ ALARM_TASK_PERIOD_MS

#define ALARM_TASK_PERIOD_MS   CONFIG_ALARM_TASK_PERIOD_MS

Definition at line 27 of file alarm_service.c.

◆ BUZZER_GPIO_PIN

#define BUZZER_GPIO_PIN   1

Definition at line 28 of file alarm_service.c.

◆ CONFIG_ALARM_ACTIVE_SECONDS

#define CONFIG_ALARM_ACTIVE_SECONDS   2

Definition at line 19 of file alarm_service.c.

◆ CONFIG_ALARM_TASK_PERIOD_MS

#define CONFIG_ALARM_TASK_PERIOD_MS   500

Definition at line 23 of file alarm_service.c.

Function Documentation

◆ alarm_service_start()

esp_err_t alarm_service_start ( void  )

Start the alarm service task.

Returns
ESP_OK on success, otherwise an ESP_ERR_* code.

Definition at line 88 of file alarm_service.c.

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}
static const char * TAG
static void alarm_service_task(void *arg)
#define BUZZER_GPIO_PIN
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ alarm_service_task()

static void alarm_service_task ( void *  arg)
static

Definition at line 32 of file alarm_service.c.

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}
#define ALARM_TASK_PERIOD_MS
#define ALARM_ACTIVE_SECONDS
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
#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.
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ TAG

const char* TAG = "alarm_service"
static

Definition at line 30 of file alarm_service.c.