// #include <avr/io.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/stream_buffer.h"
#include <stdio.h>
// #include <Arduino_FreeRTOS.h>
// #include <queue.h>
#define LENGHT_OF_BUFFER 05
#define TRIGGER_LEVEL 05
#define DELAY_TO_BE_USED 500
void vInitializeStreamBuffer(void);
void vSenderTask(void *params);
void vReceiverTask(void *params);
StreamBufferHandle_t stream_buffer=NULL;
uint8_t data[LENGHT_OF_BUFFER] = {1,2,3,4,5};
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
vInitializeStreamBuffer();
xTaskCreate(vSenderTask, NULL, 25000, (void *)data,1,NULL);
xTaskCreate(vReceiverTask, NULL, 25000, NULL, 2, NULL);
vTaskStartScheduler();
}
void loop() {
// put your main code here, to run repeatedly:
delay(10); // this speeds up the simulation
}
void vInitializeStreamBuffer(void)
{
stream_buffer = xStreamBufferCreate(LENGHT_OF_BUFFER, TRIGGER_LEVEL);
if(stream_buffer != NULL)
printf("Stream buffer created successfully!\n");
else
printf("Stream buffer not created!\n");
}
void vSenderTask(void *params)
{
size_t result=0;
uint8_t *data = (uint8_t *)params;
while(1)
{
result = xStreamBufferSend(stream_buffer,(void *)data,LENGHT_OF_BUFFER, 0);//passing the address of temp into queue
vTaskDelay(pdMS_TO_TICKS(DELAY_TO_BE_USED)); // Adjust the delay as needed
}
}
void vReceiverTask(void *params)
{
uint8_t idx=0, data[LENGHT_OF_BUFFER] = {(0)};
size_t result = 0;
while(1)
{
result = xStreamBufferReceive(stream_buffer, (void *)data,LENGHT_OF_BUFFER, portMAX_DELAY);
// result = xQueueReceive(general_queue, &data, pdMS_TO_TICKS(DELAY_TO_BE_USED));
for(idx=0; idx<LENGHT_OF_BUFFER; ++idx)
printf("%d - ",data[idx]);
printf("\n");
}
}