Code: Untitled.c Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "driver/gpio.h"
// Create a constant that restricts ESP32 to use only one core when creating tasks
#if CONFIGURE_FREERTOS_UNICORE
static const BaseType_t app_cpu = 0;
#else
static const BaseType_t app_cpu = 1;
#endif
static const uint8_t queue_length = 5; // set max length of queue
static QueueHandle_t queue; // handle for the queue
void print_messages(void *parameters) {
int item;
while(1) {
printf("Spaces in queue :: %d\n", uxQueueSpacesAvailable(queue));
if(xQueueReceive(queue, &item, 0) == pdTRUE) {
printf("Item received :: %d\n", item);
}
else {
printf("Item not received\n");
}
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void send_messages(void *parameters) {
static int num = 0;
while(1) {
printf("Sending num :: %d\n", num);
if(xQueueSend(queue, &num, 0) != pdTRUE) {
printf("Queue is full\n");
}
printf("Sent num\n");
num++;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void app_main(void) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
queue = xQueueCreate(queue_length, sizeof(int));
xTaskCreatePinnedToCore(
send_messages, // function handler for the task
"Send messages", // name of task
2048, // stack size for the task
NULL, // pointer to argument we'd like to pass
1, // priority
NULL, // task handler
app_cpu // which core to run
);
xTaskCreatePinnedToCore(
print_messages, // function handler for the task
"Print messages", // name of task
1024, // stack size for the task
NULL, // pointer to argument we'd like to pass
1, // priority
NULL, // task handler
app_cpu // which core to run
);
}
Code: Untitled.txt Select all
Sending num :: 0
Sent num
Spaces in queue :: 4
Item received :: 0
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5
Item not received
Spaces in queue :: 5