5#include "../../Inc/Scheduler/Scheduler.h"
25#define WORK_QUEUE_SIZE 32
26#define WORKER_STACK_SIZE 1024
27#define WORKER_PRIORITY (tskIDLE_PRIORITY + 2)
35 if (xQueueReceive(
workQueue, &workItem, portMAX_DELAY) == pdTRUE) {
38 TickType_t startTime = xTaskGetTickCount();
46 TickType_t executionTime = xTaskGetTickCount() - startTime;
47 TickType_t maxTime = pdMS_TO_TICKS(1000 / workItem.
updateable->
hz) / 2;
49 if (executionTime > maxTime) {
50 printf(
"WARNING: %s took %lu ticks (max recommended: %lu)\n",
62 if (updateable != NULL &&
workQueue != NULL) {
65 .timestamp = xTaskGetTickCount()
69 BaseType_t xHigherPriorityTaskWoken = pdFALSE;
70 if (xQueueSendFromISR(
workQueue, &workItem, &xHigherPriorityTaskWoken) != pdTRUE) {
72 printf(
"ERROR: Work queue full for %s - dropping task\n", updateable->
name);
76 portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
87 printf(
"ERROR: Failed to create work queue\n");
94 printf(
"ERROR: Failed to create worker task\n");
98 printf(
"Created scheduler worker task\n");
101 for (
int i = 0; updatableArray[i] != NULL && i <
MAX_SENSORS; i++) {
104 if (updateable->
hz <= 0 || updateable->
hz >
MAX_HZ) {
105 printf(
"Warning: Skipping %s - invalid frequency %d Hz\n",
106 updateable->
name, updateable->
hz);
111 TickType_t period = pdMS_TO_TICKS(1000 / updateable->
hz);
112 if (period == 0) period = 1;
116 snprintf(timerName,
sizeof(timerName),
"T_%s", updateable->
name);
119 TimerHandle_t timer = xTimerCreate(
132 printf(
"Created timer: %s at %dHz (period: %lu ticks)\n",
133 updateable->
name, updateable->
hz, period);
135 printf(
"ERROR: Failed to create timer for %s\n", updateable->
name);
139 printf(
"Scheduler initialized with %d timers and worker task\n",
taskCount);
151 printf(
"ERROR: Failed to start timer for %s\n",
156 printf(
"Started %d/%d timers\n", started,
taskCount);
159 vTaskStartScheduler();
172 printf(
"All timers stopped\n");
181 printf(
"Suspended timer for %s\n", name);
186 printf(
"Warning: Could not find or suspend timer for %s\n", name);
194 printf(
"Resumed timer for %s\n", name);
199 printf(
"Warning: Could not find or resume timer for %s\n", name);
203 UBaseType_t queueLength = uxQueueMessagesWaiting(
workQueue);
204 UBaseType_t queueSpaces = uxQueueSpacesAvailable(
workQueue);
206 printf(
"Scheduler Statistics:\n");
207 printf(
"- Active timers: %d\n",
taskCount);
208 printf(
"- Work queue: %lu items pending, %lu spaces available\n",
209 queueLength, queueSpaces);
212 TaskStatus_t taskStatus;
214 printf(
"- Worker task stack high water mark: %u words\n",
215 taskStatus.usStackHighWaterMark);
219 printf(
"WARNING: Work queue is %lu%% full - worker may be overloaded\n",
247 printf(
"Scheduler cleaned up\n");
void SchedulerResumeUpdateable(const char *name)
static ScheduledUpdateable scheduledTasks[MAX_SENSORS]
static QueueHandle_t workQueue
void SchedulerCleanup(void)
#define WORKER_STACK_SIZE
void SchedulerInit(Scheduler *scheduler, Updateable *updatableArray[])
static TaskHandle_t workerTaskHandle
static void workerTask(void *pvParameters)
void SchedulerStop(Scheduler *scheduler)
void SchedulerRun(Scheduler *scheduler)
void SchedulerGetStats(void)
static void updateableTimerCallback(TimerHandle_t xTimer)
void SchedulerSuspendUpdateable(const char *name)
void TaskInit(Task *task, Updateable *updateable, int hz)
Initializes a task with the given sensor and update frequency.
void TaskExecute(Task *task)
Executes the task by calling the sensor's update function.
char name[MAX_NAME_LENGTH]