// Khai báo các thư viện cần thiết
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include <Arduino.h>
#define LED_SEMA_PIN 18
#define LED_INTERRUPT_SEMA_PIN 19
#define LED_QUEUE_PIN 32
#define LED_INTERRUPT_QUEUE_PIN 33
#define BUTTON_SEMA 15
#define BUTTON_SEMA_INTERRUPT 2
#define BUTTON_QUEUE 16
#define BUTTON_QUEUE_INTERRUPT 17
SemaphoreHandle_t countingSemaLed5;
QueueHandle_t ledQueue;
SemaphoreHandle_t semaInterrupt;
SemaphoreHandle_t queueInterrupt;
bool ledInterruptSemaState = LOW;
bool ledInterruptQueueState = LOW;
void IRAM_ATTR HandleSemaInterrupt() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(semaInterrupt, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void IRAM_ATTR HandleQueueInterrupt() {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xSemaphoreGiveFromISR(queueInterrupt, &xHigherPriorityTaskWoken);
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
void setup() {
pinMode(BUTTON_SEMA, INPUT_PULLUP);
pinMode(BUTTON_QUEUE, INPUT_PULLUP);
pinMode(BUTTON_SEMA_INTERRUPT, INPUT_PULLUP);
pinMode(BUTTON_QUEUE_INTERRUPT, INPUT_PULLUP);
pinMode(LED_SEMA_PIN, OUTPUT);
pinMode(LED_INTERRUPT_SEMA_PIN, OUTPUT);
pinMode(LED_QUEUE_PIN, OUTPUT);
pinMode(LED_INTERRUPT_QUEUE_PIN, OUTPUT);
digitalWrite(LED_INTERRUPT_SEMA_PIN, ledInterruptSemaState);
digitalWrite(LED_INTERRUPT_QUEUE_PIN, ledInterruptQueueState);
attachInterrupt(digitalPinToInterrupt(BUTTON_SEMA_INTERRUPT), HandleSemaInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(BUTTON_QUEUE_INTERRUPT), HandleQueueInterrupt, FALLING);
countingSemaLed5 = xSemaphoreCreateCounting(5, 0);
ledQueue = xQueueCreate(10, sizeof(int));
semaInterrupt = xSemaphoreCreateBinary();
queueInterrupt = xSemaphoreCreateBinary();
xTaskCreatePinnedToCore(ReadSemaButtonTask, "ReadSemaButtonTask", 1024, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(WriteSemaLedTask, "WriteSemaLedTask", 1024, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(HandleSemaInterruptTask, "HandleSemaInterruptTask", 1024, NULL, 1, NULL, 0);
xTaskCreatePinnedToCore(ReadQueueButtonTask, "ReadQueueButtonTask", 1024, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(HandleQueueTask, "HandleQueueTask", 1024, NULL, 1, NULL, 1);
xTaskCreatePinnedToCore(HandleQueueInterruptTask, "HandleQueueInterruptTask", 1024, NULL, 1, NULL, 1);
}
void loop() {
}
void ReadSemaButtonTask(void *pvParameters) {
while (1) {
int buttonValue = digitalRead(BUTTON_SEMA);
static int prevButtonValue = HIGH;
if (buttonValue == LOW && prevButtonValue == HIGH) {
xSemaphoreGive(countingSemaLed5);
}
prevButtonValue = buttonValue;
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void WriteSemaLedTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(countingSemaLed5, portMAX_DELAY) == pdTRUE) {
for (int i = 0; i < 3; i++) {
digitalWrite(LED_SEMA_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(LED_SEMA_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}
}
void HandleSemaInterruptTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(semaInterrupt, portMAX_DELAY) == pdTRUE) {
ledInterruptSemaState = !ledInterruptSemaState;
digitalWrite(LED_INTERRUPT_SEMA_PIN, ledInterruptSemaState);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
}
void ReadQueueButtonTask(void *pvParameters) {
while (1) {
int buttonValue = digitalRead(BUTTON_QUEUE);
static int prevButtonValue = HIGH;
if (buttonValue == LOW && prevButtonValue == HIGH) {
int msg = 1;
xQueueSend(ledQueue, &msg, portMAX_DELAY);
}
prevButtonValue = buttonValue;
vTaskDelay(pdMS_TO_TICKS(10));
}
}
void HandleQueueTask(void *pvParameters) {
int msg;
while (1) {
if (xQueueReceive(ledQueue, &msg, portMAX_DELAY) == pdTRUE) {
for (int i = 0; i < 10; i++) {
digitalWrite(LED_QUEUE_PIN, HIGH);
vTaskDelay(pdMS_TO_TICKS(500));
digitalWrite(LED_QUEUE_PIN, LOW);
vTaskDelay(pdMS_TO_TICKS(500));
}
}
}
}
void HandleQueueInterruptTask(void *pvParameters) {
while (1) {
if (xSemaphoreTake(queueInterrupt, portMAX_DELAY) == pdTRUE) {
ledInterruptQueueState = !ledInterruptQueueState;
digitalWrite(LED_INTERRUPT_QUEUE_PIN, ledInterruptQueueState);
vTaskDelay(pdMS_TO_TICKS(100));
}
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4