-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathled_task.c
More file actions
61 lines (53 loc) · 2.14 KB
/
led_task.c
File metadata and controls
61 lines (53 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**************************************************************************************************
* LED Task
* ----------------
* This file is part of a FreeRTOS application that manages an LED's state and blinking frequency.
* It listens for commands from a queue to turn the LED on or off and to adjust its blinking frequency.
* It also logs actions to a logger queue.
**************************************************************************************************/
#include "led_task.h"
#include "shared_defs.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include <stdio.h>
#include <string.h>
// Internal LED state
static int ledState = 0; // 0 = off, 1 = on
static int blinkFrequency = 1000; // in milliseconds
extern SemaphoreHandle_t printMutex;
extern QueueHandle_t loggerQueue;
extern QueueHandle_t commandQueue;
void vLEDTask(void *pvParameters) {
char *logMsg;
TickType_t delayTicks;
while (1) {
delayTicks = pdMS_TO_TICKS(blinkFrequency);
if (ledState) {
xSemaphoreTake(printMutex, portMAX_DELAY);
printf("[LED] Blinking\n");
xSemaphoreGive(printMutex);
}
// Check if there's a command for LED
LEDCommand cmd;
if (xQueueReceive(ledCommandQueue, &cmd, 0)) {
if (strcmp(cmd.type, "on") == 0) {
ledState = 1;
logMsg = strdup("LED turned ON");
xQueueSend(loggerQueue, &logMsg, portMAX_DELAY);
} else if (strcmp(cmd.type, "off") == 0) {
ledState = 0;
logMsg = strdup("LED turned OFF");
xQueueSend(loggerQueue, &logMsg, portMAX_DELAY);
} else if (strcmp(cmd.type, "freq") == 0 && cmd.value > 0) {
blinkFrequency = cmd.value;
char msg[64];
snprintf(msg, sizeof(msg), "LED frequency set to %d ms", blinkFrequency);
logMsg = strdup(msg);
xQueueSend(loggerQueue, &logMsg, portMAX_DELAY);
}
}
vTaskDelay(delayTicks);
}
}