char *str[] = {"hello", "nihao"};
//队列句柄
QueueHandle_t xQueue;
//任务句柄
TaskHandle_t xSenderTaskHandle, xReceiverTaskHandle;
// 发送任务
void vSenderTask( void *pvParameters )
{
vTaskDelay(2000); // 1秒的频率
int32_t itemValue = 0;
for ( ;; )
{
// 生成一个要发送到队列的值
itemValue++;
// 发送值到队列,阻塞直到有空间
if ( xQueueSend( xQueue, &itemValue, portMAX_DELAY ) != pdPASS )
{
// 发送失败(不应该发生,因为我们使用了portMAX_DELAY)
}
}
}
// 接收任务
void vReceiverTask( void *pvParameters )
{
int32_t receivedValue;
for ( ;; )
{
// 从队列接收值,阻塞直到有数据
if ( xQueueReceive( xQueue, &receivedValue, portMAX_DELAY ) == pdTRUE )
{
// 成功接收到值,这里可以处理接收到的数据
Serial.print("Received: ");
Serial.println(receivedValue);
}
}
}
void setup()
{
// 初始化串口
Serial.begin(9600);
// 创建一个队列,队列项大小为sizeof(int32_t),队列长度为5
xQueue = xQueueCreate( 5, sizeof( int32_t ) );
// 创建发送任务和接收任务
xTaskCreate(
vSenderTask, // 任务函数
"Sender", // 任务名称
configMINIMAL_STACK_SIZE, // 堆栈大小
NULL, // 任务输入参数
tskIDLE_PRIORITY + 1, // 任务优先级
&xSenderTaskHandle // 任务句柄
);
xTaskCreate(
vReceiverTask, // 任务函数
"Receiver", // 任务名称
configMINIMAL_STACK_SIZE, // 堆栈大小
NULL, // 任务输入参数
tskIDLE_PRIORITY + 2, // 任务优先级(比发送任务稍高)
&xReceiverTaskHandle // 任务句柄
);
// 启动调度器
vTaskStartScheduler();
}
void loop()
{
// 在使用FreeRTOS时,不需要使用loop()函数,因为所有的任务都在FreeRTOS的调度下运行
}