forked from espressif/arduino-esp32
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSemaphore.ino
56 lines (48 loc) · 1.79 KB
/
Semaphore.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/* Basic Multi Threading Arduino Example
This example code is in the Public Domain (or CC0 licensed, at your option.)
Unless required by applicable law or agreed to in writing, this
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied.
*/
// Please read file README.md in the folder containing this example.
#include <Arduino.h>
SemaphoreHandle_t package_delivered_semaphore;
void delivery_truck_task(void *pvParameters) {
int truck_number = (int) pvParameters;
while(1) {
// Wait for a package to be delivered
// ...
// Notify the warehouse that a package has been delivered
xSemaphoreGive(package_delivered_semaphore);
Serial.printf("Package delivered by truck: %d\n", truck_number);
//wait for some time
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
void warehouse_worker_task(void *pvParameters) {
int worker_number = (int) pvParameters;
while(1) {
// Wait for a package to be delivered
xSemaphoreTake(package_delivered_semaphore, portMAX_DELAY);
Serial.printf("Package received by worker: %d\n", worker_number);
// Receive the package
// ...
}
}
void setup() {
Serial.begin(115200);
while(!Serial){ delay(100); }
// Create the semaphore
package_delivered_semaphore = xSemaphoreCreateCounting(10, 0);
// Create multiple delivery truck tasks
for (int i = 0; i < 5; i++) {
xTaskCreate(delivery_truck_task, "Delivery Truck", 2048, (void *)i, tskIDLE_PRIORITY, NULL);
}
// Create multiple warehouse worker tasks
for (int i = 0; i < 3; i++) {
xTaskCreate(warehouse_worker_task, "Warehouse Worker", 2048, (void *)i, tskIDLE_PRIORITY, NULL);
}
}
void loop() {
// Empty loop
}