Queue集合_#include


上图讲述的是三个发送数据端分别向对应的队列中发送数据,sendTask1发送数据111到队列1中,sendTask2发送数据222到队列2中,sendTask3发送数据333到队列3中。

接收数据端recTask如何接收数据呢?

我们可以调用xQueueCreateSet()去创建一个队列集合,并调用xQueueAddToSet()函数将队列1 队列2 队列3添加到队列集合中。在接收数据端recTask设置为:当队列集合中的某一个队列存在数据,则会返回相应存在数据的队列的句柄给recTask,recTask会根据相应的队列句柄去相对应的队列中取数据。

#include <stdio.h>
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "freertos/queue.h"

void sendTask1(void *pvParam)
{
    QueueHandle_t QHandle;
    QHandle = (QueueHandle_t)pvParam;
    BaseType_t xStatus;
    int i = 111;
    while (1)
    {
        xStatus = xQueueSend(QHandle, &i, 0);
        if (xStatus != pdPASS)
        {
            printf("send fail\n");
        }
        else
        {
            printf("send done\n");
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void sendTask2(void *pvParam)
{
    QueueHandle_t QHandle;
    QHandle = (QueueHandle_t)pvParam;
    BaseType_t xStatus;
    int i = 222;
    while (1)
    {
        xStatus = xQueueSend(QHandle, &i, 0);
        if (xStatus != pdPASS)
        {
            printf("send fail\n");
        }
        else
        {
            printf("send done\n");
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void sendTask3(void *pvParam)
{
    QueueHandle_t QHandle;
    QHandle = (QueueHandle_t)pvParam;
    BaseType_t xStatus;
    int i = 333;
    while (1)
    {
        xStatus = xQueueSend(QHandle, &i, 0);
        if (xStatus != pdPASS)
        {
            printf("send fail\n");
        }
        else
        {
            printf("send done\n");
        }
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

void recTask(void *pvParam)
{
    QueueSetHandle_t QueueSet;
    QueueSet = (QueueSetHandle_t)pvParam;
    QueueSetMemberHandle_t QueueData;
    BaseType_t xStatus;
    int i;
    while (1)
    {
        QueueData = xQueueSelectFromSet(QueueSet,portMAX_DELAY); 
        xStatus = xQueueReceive(QueueData, &i, portMAX_DELAY);
        if (xStatus != pdPASS)
        {
            printf("rec fail\n");
        }
        else
        {
            printf("rec %d\n", i);
        }
    }
}

void app_main(void)
{
    QueueHandle_t QHandle1;
    QHandle1 = xQueueCreate(5, sizeof(int));
    QueueHandle_t QHandle2;
    QHandle2 = xQueueCreate(5, sizeof(int));
    QueueHandle_t QHandle3;
    QHandle3 = xQueueCreate(5, sizeof(int));
    QueueSetHandle_t QueueSet;
    QueueSet = xQueueCreateSet(15);
    xQueueAddToSet(QHandle1, QueueSet);
    xQueueAddToSet(QHandle2, QueueSet);
    xQueueAddToSet(QHandle3, QueueSet);

    if ((QHandle1 != NULL) && (QHandle2 != NULL) && (QHandle3 != NULL) && (QueueSet != NULL))
    {
        printf("create queue successfully\n");
        xTaskCreate(sendTask1, "sendTask1", 1024 * 5, (void *)QHandle1, 1, NULL);
        xTaskCreate(sendTask2, "sendTask2", 1024 * 5, (void *)QHandle2, 1, NULL);
        xTaskCreate(sendTask3, "sendTask3", 1024 * 5, (void *)QHandle3, 1, NULL);
        xTaskCreate(recTask, "recTask", 1024 * 5, (void *)QueueSet, 2, NULL);
    }
    else
    {
        printf("can not create queue\n");
    }
}

Queue集合_c语言_02