#include <stdio.h>
#include <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#define LED_PIN GPIO_NUM_2
typedef struct {
int id;
int start_hour;
int start_minute;
int end_hour;
int end_minute;
int day_of_week;
bool turn_on;
} schedule_t;
// Mock function to turn the LED on or off
void turn_led(bool on) {
if (on) {
printf("LED turned ON\n");
} else {
printf("LED turned OFF\n");
}
}
// Task to execute a schedule
void schedule_task(void *param) {
schedule_t *schedule = (schedule_t *) param;
while (1) {
// Get the current time
time_t now;
time(&now);
struct tm timeinfo;
localtime_r(&now, &timeinfo);
// Check if the schedule should execute today
if ((schedule->day_of_week == timeinfo.tm_wday) &&
(schedule->start_hour < schedule->end_hour ||
(schedule->start_hour == schedule->end_hour &&
schedule->start_minute <= schedule->end_minute)) &&
(schedule->start_hour <= timeinfo.tm_hour &&
(schedule->start_hour != timeinfo.tm_hour ||
schedule->start_minute <= timeinfo.tm_min)) &&
(schedule->end_hour >= timeinfo.tm_hour &&
(schedule->end_hour != timeinfo.tm_hour ||
schedule->end_minute >= timeinfo.tm_min))) {
// Execute the schedule
turn_led(schedule->turn_on);
}
// Wait for 1 minute before checking again
vTaskDelay(pdMS_TO_TICKS(60000));
}
}
void app_main(void) {
// Create some sample schedules
schedule_t schedule1 = {
.id = 1,
.start_hour = 8,
.start_minute = 0,
.end_hour = 17,
.end_minute = 0,
.day_of_week = 1,
.turn_on = true
};
schedule_t schedule2 = {
.id = 2,
.start_hour = 9,
.start_minute = 0,
.end_hour = 16,
.end_minute = 0,
.day_of_week = 2,
.turn_on = false
};
// Create tasks for each schedule
xTaskCreate(schedule_task, "Schedule 1", 2048, &schedule1, 5, NULL);
xTaskCreate(schedule_task, "Schedule 2", 2048, &schedule2, 5, NULL);
}