#include <Arduino.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
QueueHandle_t xQueue;
// inisialisasi struct values
struct sValues{
int id;
int value;
};
void Task1(void *pvParameters) {
sValues *p;
int id_count = 0;
while (1) {
// Dynamically allocate memory for struct values
p = (sValues *)pvPortMalloc(sizeof(sValues));
p->id = id_count++; // Increment id
p->value = random(0, 100); // Generate a random number between 0 and 100
// Attempt to send the pointer to the queue, wait indefinitely if needed
if (xQueueSend(xQueue, &p, portMAX_DELAY) != pdPASS) {
Serial.println("Failed to post to queue");
vPortFree(p); // Free memory if message could not be sent to the queue
}
vTaskDelay(1000 / portTICK_PERIOD_MS); // Delay for 1 second
}
}
void Task2(void *pvParameters) {
sValues *p;
while (1) {
// Wait to receive a pointer from the queue, wait indefinitely if needed
if (xQueueReceive(xQueue, &p, portMAX_DELAY)) {
// Print the received value
Serial.print("Received ID: ");
Serial.print(p->id);
Serial.print(" | Value: ");
Serial.println(p->value);
vPortFree(p); // Free memory after processing
}
}
}
void setup() {
Serial.begin(115200);
// Create a queue capable of holding 10 pointers to struct values
xQueue = xQueueCreate(10, sizeof(sValues *));
if (xQueue == NULL) {
Serial.println("Error creating the queue");
}
// Create two tasks pinned to core 1
xTaskCreatePinnedToCore(Task1, "Task1", 10000, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(Task2, "Task2", 10000, NULL, 1, NULL, 1);
vTaskDelete(NULL); // Delete the setup task to free memory
}
void loop() {
// Empty loop since tasks are handled in FreeRTOS tasks
}