| 1 | #include <Arduino.h> |
|---|
| 2 | |
|---|
| 3 | SemaphoreHandle_t one_Hz_sem; |
|---|
| 4 | QueueHandle_t data_q_handle; |
|---|
| 5 | QueueSetHandle_t data_or_sem_qs_handle; |
|---|
| 6 | |
|---|
| 7 | void producer_1Hz_sem(void *p) |
|---|
| 8 | { |
|---|
| 9 | while (true) |
|---|
| 10 | { |
|---|
| 11 | vTaskDelay(3000 / portTICK_PERIOD_MS); |
|---|
| 12 | xSemaphoreGive(one_Hz_sem); |
|---|
| 13 | } |
|---|
| 14 | } |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | void producer_data(void *p) |
|---|
| 18 | { |
|---|
| 19 | int x = 0; |
|---|
| 20 | while (true) |
|---|
| 21 | { |
|---|
| 22 | // use with processor_1 |
|---|
| 23 | x = x+1; |
|---|
| 24 | // use with processer_2 |
|---|
| 25 | //x = random(0,10); |
|---|
| 26 | xQueueSend(data_q_handle, &x, 0); |
|---|
| 27 | vTaskDelay(((rand() % 1000) + 100) / portTICK_PERIOD_MS); |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | void processor_1(void *p) |
|---|
| 32 | { |
|---|
| 33 | int count = 0; |
|---|
| 34 | while (true) |
|---|
| 35 | { |
|---|
| 36 | // DO NOT Do This |
|---|
| 37 | // xSemaphoreTake(one_Hz_sem); |
|---|
| 38 | QueueSetMemberHandle_t who_unblocked = xQueueSelectFromSet(data_or_sem_qs_handle, 2000); |
|---|
| 39 | if (who_unblocked == one_Hz_sem) |
|---|
| 40 | { |
|---|
| 41 | if (xSemaphoreTake(one_Hz_sem, 0)) |
|---|
| 42 | { |
|---|
| 43 | Serial.println("One Hz Timeout"); |
|---|
| 44 | } |
|---|
| 45 | else |
|---|
| 46 | { |
|---|
| 47 | Serial1.println("Error, semaphore should not happen "); |
|---|
| 48 | } |
|---|
| 49 | } |
|---|
| 50 | else if (who_unblocked == data_q_handle) |
|---|
| 51 | { |
|---|
| 52 | int y = 0; |
|---|
| 53 | if(xQueueReceive(data_q_handle, &y, 0)){ |
|---|
| 54 | Serial.print("Retrive "); |
|---|
| 55 | Serial.println( y ); |
|---|
| 56 | |
|---|
| 57 | }else{ |
|---|
| 58 | Serial.println("Error, queueRx should not happen "); |
|---|
| 59 | } |
|---|
| 60 | } |
|---|
| 61 | else |
|---|
| 62 | { |
|---|
| 63 | Serial.println("Invalid case should not print this line"); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | } |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | |
|---|
| 70 | |
|---|
| 71 | |
|---|
| 72 | |
|---|
| 73 | |
|---|
| 74 | void processor_2(void *p) |
|---|
| 75 | { |
|---|
| 76 | int sample[10]; |
|---|
| 77 | int count = 0; |
|---|
| 78 | while (true) |
|---|
| 79 | { |
|---|
| 80 | // DO NOT Do This |
|---|
| 81 | // xSemaphoreTake(one_Hz_sem); |
|---|
| 82 | |
|---|
| 83 | QueueSetMemberHandle_t who_unblocked = xQueueSelectFromSet(data_or_sem_qs_handle, 2000); |
|---|
| 84 | if (who_unblocked == one_Hz_sem) |
|---|
| 85 | { |
|---|
| 86 | if (xSemaphoreTake(one_Hz_sem, 0)) |
|---|
| 87 | { |
|---|
| 88 | Serial.println("One Hz Timeout"); |
|---|
| 89 | float avg = 0; |
|---|
| 90 | for (int i = 0; i< count; i++){ |
|---|
| 91 | avg += sample[i]; |
|---|
| 92 | } |
|---|
| 93 | avg = avg/count; |
|---|
| 94 | count = 0; |
|---|
| 95 | Serial.print("avg = "); |
|---|
| 96 | Serial.println(avg); |
|---|
| 97 | } |
|---|
| 98 | else |
|---|
| 99 | { |
|---|
| 100 | Serial1.println("Error, semaphore should not happen "); |
|---|
| 101 | } |
|---|
| 102 | } |
|---|
| 103 | else if (who_unblocked == data_q_handle) |
|---|
| 104 | { |
|---|
| 105 | int y = 0; |
|---|
| 106 | if(xQueueReceive(data_q_handle, &y, 0)){ |
|---|
| 107 | Serial.print("Retrive "); |
|---|
| 108 | Serial.println( y ); |
|---|
| 109 | sample[count++] = y; |
|---|
| 110 | |
|---|
| 111 | }else{ |
|---|
| 112 | Serial.println("Error, queueRx should not happen "); |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | else |
|---|
| 116 | { |
|---|
| 117 | Serial.println("Invalid case should not print this line"); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | |
|---|
| 124 | |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | void setup() |
|---|
| 128 | { |
|---|
| 129 | Serial.begin(115200); |
|---|
| 130 | vTaskDelay(5000 / portTICK_PERIOD_MS); |
|---|
| 131 | Serial.println("\r\n -------- FreeRTOS ESP32_Queue_SET_semp_que ----------"); |
|---|
| 132 | |
|---|
| 133 | one_Hz_sem = xSemaphoreCreateBinary(); |
|---|
| 134 | data_q_handle = xQueueCreate(10, sizeof(int)); |
|---|
| 135 | // xQueueSetHandle ( no. of data_q(10) + no of Binary semaphore(1) ) |
|---|
| 136 | data_or_sem_qs_handle = xQueueCreateSet(10+1); |
|---|
| 137 | |
|---|
| 138 | // add one_Hz_sem to queueSet |
|---|
| 139 | xQueueAddToSet(one_Hz_sem, data_or_sem_qs_handle); |
|---|
| 140 | // add data_q_handle to queueSet |
|---|
| 141 | xQueueAddToSet(data_q_handle, data_or_sem_qs_handle); |
|---|
| 142 | |
|---|
| 143 | xTaskCreate(producer_1Hz_sem, "producer_1Hz_sem", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
|---|
| 144 | xTaskCreate(producer_data, "producer_data", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
|---|
| 145 | xTaskCreate(processor_1, "processor_1", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
|---|
| 146 | //xTaskCreate(processor_2, "processor_2", 1024, NULL, tskIDLE_PRIORITY - 1, NULL); |
|---|
| 147 | |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | void loop() |
|---|
| 151 | { |
|---|
| 152 | // put your main code here, to run repeatedly: |
|---|
| 153 | } |
|---|