1 | #include <Arduino.h> |
---|
2 | xQueueHandle queue_handle = NULL; |
---|
3 | |
---|
4 | void tx(void *p){ |
---|
5 | int putvalue = 0; |
---|
6 | while (true) |
---|
7 | { |
---|
8 | //Serial.print("tx() send to queue: "); |
---|
9 | // 1 experiment we not wait to insert Queue and pls comment out rx() left only while loop do nothing |
---|
10 | //if (xQueueSend(queue_handle, &putvalue, 0) != NULL) |
---|
11 | // 2 experiment we wait a bit longer, result will show the FAIL msg will slowly update |
---|
12 | // notice that the OK 2 msg insert same speed as in 1st experiment since the queue is not full |
---|
13 | |
---|
14 | |
---|
15 | //if (xQueueSend(queue_handle, &putvalue, 3000) != NULL) |
---|
16 | //{ |
---|
17 | // Serial.println("OK"); |
---|
18 | //}else{ |
---|
19 | // Serial.println("FAIL!!"); |
---|
20 | //} |
---|
21 | |
---|
22 | vTaskDelay(200 / portTICK_PERIOD_MS); |
---|
23 | |
---|
24 | } |
---|
25 | } |
---|
26 | |
---|
27 | void rx(void *p){ |
---|
28 | int getvalue = 0; |
---|
29 | while(true){ |
---|
30 | // 1 experiment comment every thing in side this while loop to make tx() fill the queue and see adjust timeout on |
---|
31 | // tx() queue to wait longer or shorter and observe the delay of FAIL msg print out. |
---|
32 | // 3 experiment, if we retrive from queue as same TIMEOUT as put in queue |
---|
33 | // We should not see fail. Moreover, it will do task in this rx queue |
---|
34 | // before return to tx queue. it will print rx 1, rx 2 before print "OK" in tx queue |
---|
35 | if (xQueueReceive(queue_handle, &getvalue, 3000)){ |
---|
36 | Serial.println("rx() got item from queue"); |
---|
37 | Serial.println("\t\t\t\t rx 1"); |
---|
38 | Serial.println("\t\t\t\t rx 2"); |
---|
39 | // 4 if rx cannot get any item from queue and wait for 3000 sec |
---|
40 | // To test, just comment out everything left only vTaskDelay in while loop of sending queue |
---|
41 | // |
---|
42 | }else{ |
---|
43 | Serial.println("rx() failed to get items from queue within 3000 tick"); |
---|
44 | } |
---|
45 | |
---|
46 | } |
---|
47 | |
---|
48 | } |
---|
49 | |
---|
50 | void setup() |
---|
51 | { |
---|
52 | Serial.begin(115200); |
---|
53 | vTaskDelay(5000 / portTICK_PERIOD_MS); |
---|
54 | Serial.println("\r\n -------- FreeRTOS ESP32_Queue_TxRx ----------"); |
---|
55 | queue_handle = xQueueCreate(2, sizeof(int)); |
---|
56 | xTaskCreate(tx, "txQueueTask", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
---|
57 | xTaskCreate(rx, "rxQueueTask", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
---|
58 | } |
---|
59 | |
---|
60 | void loop() {} |
---|