//#include <Arduino.h>
typedef struct {
int deviceID;
int value1;
int value2;
} SENSOR;
//创建队列
QueueHandle_t xQueue = xQueueCreate(8, sizeof(SENSOR));
void sensorA(void *pvParam) {
SENSOR sensorA = {1, 0, 0}; // 初始化结构体
while (1) {
sensorA.value1 = (sensorA.value1 + 1) % 256; // 使用模运算防止溢出
sensorA.value2 = (sensorA.value2 + 1) % 256; // 同上
TickType_t timeOut = 2000;
if (xQueueSend(xQueue, &sensorA, timeOut) != pdPASS) {
Serial.println("SensorA: Queue is full.");
}
vTaskDelay(1000); // 将延时放入循环内
}
}
void Display(void *pvParam) {
SENSOR data;
TickType_t timeOut = 2000;
while (1) { // 添加循环,确保任务持续运行
if (xQueueReceive(xQueue, &data, timeOut) == pdPASS) {
Serial.print("DeviceID:" );
Serial.println(data.deviceID);
Serial.print("VALUE1:");
Serial.println(data.value1);
Serial.print("VALUE2:");
Serial.println(data.value2);
} else {
Serial.println("Display: Message Queue is Empty");
}
vTaskDelay(1000); // 每秒尝试接收一次数据
}
}
void setup() {
Serial.begin(9600);
xTaskCreatePinnedToCore(sensorA, "SensorA", 1024, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(Display, "Display", 1024, NULL, 1, NULL, 1);
}
void loop() {}
// #include <Arduino.h>
// QueueHandle_t xQueue = xQueueCreate(8, sizeof(int));
// void sensorA(void *pvParam) {
// int counter = 0; // 初始化counter
// while (1) {
// counter++; // 每次循环增加counter的值
// TickType_t timeOut = 2000;
// if (xQueueSend(xQueue, &counter, timeOut) != pdPASS) {
// Serial.println("SensorA: Queue is full.");
// }
// vTaskDelay(1000); // 将延时放入循环内,以便每秒发送一次数据
// }
// }
// void Display(void *pvParam) {
// int data;
// TickType_t timeOut = 2000;
// while (1) { // 添加循环,确保任务持续运行
// if (xQueueReceive(xQueue, &data, timeOut) == pdPASS) {
// Serial.println(data);
// } else {
// Serial.println("Display: Message Queue is Empty");
// }
// vTaskDelay(1000); // 每秒尝试接收一次数据
// }
// }
// void setup() {
// Serial.begin(9600);
// xTaskCreatePinnedToCore(sensorA, "SensorA", 1024, NULL, 1, NULL, 1);
// xTaskCreatePinnedToCore(Display, "Display", 1024, NULL, 1, NULL, 1);
// }
// void loop() {}