#include <Arduino_FreeRTOS.h>
#include <event_groups.h>
EventGroupHandle_t xEventGroup;
const int BIT_0 = (1 << 0);
const int BIT_1 = (1 << 1);
void setup() {
Serial.begin(9600);
// Tạo một Event Group
xEventGroup = xEventGroupCreate();
if (xEventGroup != NULL) {
xTaskCreate(vTask1, "Task 1", 128, NULL, 1, NULL);
xTaskCreate(vTask2, "Task 2", 128, NULL, 1, NULL);
xTaskCreate(vTask3, "Task 3", 128, NULL, 1, NULL); // Thêm Task 3
}
}
void loop() {
// Không sử dụng loop() trong FreeRTOS
}
void vTask1(void *pvParameters) {
while (1) {
// Đặt BIT_0
xEventGroupSetBits(xEventGroup, BIT_0);
Serial.println("Task 1 set BIT_0");
vTaskDelay(1000 / portTICK_PERIOD_MS); // Chờ 1 giây
}
}
void vTask3(void *pvParameters) {
while (1) {
// Đặt BIT_1
xEventGroupSetBits(xEventGroup, BIT_1);
Serial.println("Task 3 set BIT_1");
vTaskDelay(2000 / portTICK_PERIOD_MS); // Chờ 2 giây
}
}
void vTask2(void *pvParameters) {
while (1) {
// Đợi cả BIT_0 và BIT_1 được đặt
EventBits_t uxBits = xEventGroupWaitBits(
xEventGroup,
BIT_0 | BIT_1,
pdTRUE, // Clear bits on exit
pdTRUE, // Wait for both bits
portMAX_DELAY
);
if ((uxBits & (BIT_0 | BIT_1)) == (BIT_0 | BIT_1)) {
Serial.println("Task 2 received both bits");
}
}
}