/*
https://github.com/RalphBacon/ESP32-Variable-Passing-Using-Queues-Global-Vars/blob/master/ESP32_SIMPLE_QUEUE.ino
队列Queue:任务之间传参
1)建立队列变量
QueueHandle_t queue=xQueueCreate(5, sizeof(int));
2) 建立任务,不须传参
xTaskCreate(loop0,"task0",1000,NULL,0,NULL);
xTaskCreate(loop1,"task1",1000,NULL,0,NULL);
3)task0Handle将数据地址传入队列
xQueueSend(queue, &data, portMAX_DELAY);
4)task1Handle将数据从队列取走
xQueueReceive(queue, &flashTotal, portMAX_DELAY);
*/
// Create the queue with 5 slots of 2 bytes
QueueHandle_t queue = xQueueCreate(8, sizeof(int));
#define manager 32
#define worker 2
#define SWITCH 33
void loop0(void * parameter) {
for (;;) {
// Add a random number to the queue
// auto rndNumber = esp_random();
// Use Arduino implementation for a number between limits
int rndNumber = random(1, 9);
// Add to the queue - wait forever until space is available
Serial.println("Mgr - Adding " + String(rndNumber) + " to queue");
xQueueSend(queue, &rndNumber, portMAX_DELAY);
// Artificial wait here
digitalWrite(manager, HIGH);
delay(200);
digitalWrite(manager, LOW);
delay(500);
// Slow motion delay here
//delay(5000);
// Force a tea-break here if switch is pressed
bool TeaBreak = false;
while (digitalRead(SWITCH) == LOW) {
delay(100);
if (!TeaBreak) {
Serial.println("tMgr - Tea Break");
TeaBreak = true;
}
}
}
}
void loop1(void * parameter) {
for (;;) {
// Get the number of flashes required
int flashTotal;
xQueueReceive(queue, &flashTotal, portMAX_DELAY);
Serial.println("\t\t\t\t\tWorker - reading " + String(flashTotal));
// Flash that number
for (int cnt = 0; cnt < flashTotal; cnt++) {
digitalWrite(worker, HIGH);
delay(150);
digitalWrite(worker, LOW);
delay(150);
}
// Slow motion delay here
//delay(1000);
}
}
void setup()
{
Serial.begin(115200);
Serial.println("Setup started.");
pinMode(manager, OUTPUT);
pinMode(worker, OUTPUT);
pinMode(SWITCH, INPUT_PULLUP);
xTaskCreate(
loop0, /* Function to implement the task */
"task0Handle", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
NULL);
xTaskCreate(
loop1, /* Function to implement the task */
"task1Handle", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
NULL);
Serial.println("Setup completed.");
}
void loop()
{
vTaskDelete (NULL);
}