// #include <avr/io.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include <stdio.h>
// #include <Arduino_FreeRTOS.h>
// #include <queue.h>
#define LENGHT_OF_QUEUE 05
#define DELAY_TO_BE_USED 500
#define SIZE_OF_STRING 10
QueueHandle_t general_queue;
typedef enum{SENDER1,SENDER2}SENDER_ID;
typedef struct {
SENDER_ID sendId;
uint8_t data;
}QueueData;
void vInitializeQueue(void);
void vSenderTask(void *params);
void vReceiverTask(void *params);
void vStringCopy(uint8_t *src, uint8_t *dest);
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
vInitializeQueue();
xTaskCreate(vSenderTask, NULL, 50000, (void *)"Hi !",2,NULL);
xTaskCreate(vSenderTask, NULL, 50000, (void *)"Hello !",2,NULL);
xTaskCreate(vReceiverTask, NULL, 50000, NULL, 1, NULL);
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void vInitializeQueue(void)
{
general_queue = xQueueCreate(LENGHT_OF_QUEUE, sizeof(QueueData));
if(general_queue != NULL)
printf("Queue is created successfully!\n");
else
printf("Queue is not created!\n");
}
void vSenderTask(void *params)
{
uint8_t *temp = (uint8_t *)pvPortMalloc(SIZE_OF_STRING);
BaseType_t result=0;
vStringCopy((uint8_t *)params,temp);
while(1)
{
result = xQueueSend(general_queue, &temp, 0);//passing the address of temp into queue
if(result != pdPASS)
printf("Unable to send data over queue!\n");
// Introduce a delay or yield here
vTaskDelay(pdMS_TO_TICKS(50)); // Adjust the delay as needed
// taskYIELD();
}
}
void vReceiverTask(void *params)
{
uint8_t *data;
BaseType_t result;
while(1)
{
result = xQueueReceive(general_queue, &data, pdMS_TO_TICKS(DELAY_TO_BE_USED));
if(result == pdPASS)
{
printf("Received -> %s\n",data);
}
}
}
void vStringCopy(uint8_t *src, uint8_t *dest)
{
while((*dest++ = *src++) != '\0');
}