/*Biblioteca do Arduino*/
#include <Arduino.h>
/*Bibliotecas FreeRTOS */
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
/*mapeamento de pinos*/
#define BUTTON1_PIN 13 //1o botao
#define BUTTON2_PIN 12 //2o botao
// Criação do mutex
SemaphoreHandle_t mutex;
/* Variáveis para armazenamento do handle das tasks*/
TaskHandle_t taks1Handle = NULL;
TaskHandle_t taks2Handle = NULL;
/*protítipos das Tasks*/
void vTask1(void *pvParameters);
void vTask2(void *pvParameters);
/*função setup*/
void setup() {
pinMode(BUTTON1_PIN, INPUT_PULLUP);
pinMode(BUTTON2_PIN, INPUT_PULLUP);
// Criação do mutex
mutex = xSemaphoreCreateMutex();
if (mutex == NULL) {
Serial.println("Falha ao criar o mutex");
while (1); // Travar o sistema em caso de erro
}
Serial.begin(9600); //configura comunicação serial com baudrate de 9600
Serial.println("Inicializada a serial....");
/*criação das tasks*/
xTaskCreate(vTask1,"TASK1",configMINIMAL_STACK_SIZE+1024,NULL,1,&taks1Handle);
xTaskCreate(vTask2,"TASK2",configMINIMAL_STACK_SIZE+1024,NULL,2,&taks2Handle);
}
/*função loop*/
void loop() {
//vazio
}
/*
vTask1
*/
void vTask1(void *pvParameters){
while (1) {
if (digitalRead(BUTTON1_PIN) == LOW) { // Botão pressionado
if (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) {
Serial.println("Botão 1 pressionado");
vTaskDelay(pdMS_TO_TICKS(100));
xSemaphoreGive(mutex); //libera o mutex
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}
/*
vTask2
*/
void vTask2(void *pvParameters){
while (1) {
if (digitalRead(BUTTON2_PIN) == LOW) { // Botão pressionado
if (xSemaphoreTake(mutex, portMAX_DELAY) == pdTRUE) {
Serial.println("Botão 2 pressionado");
vTaskDelay(pdMS_TO_TICKS(100));
xSemaphoreGive(mutex); //libera o mutex
}
}
vTaskDelay(pdMS_TO_TICKS(100));
}
}