#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
int sharedVariable = 0;
SemaphoreHandle_t xMutex;
TaskHandle_t hndlrProducer;
TaskHandle_t hndlrConsumer;
void taskProducer(void *pvParameters) {
for (;;) {
xSemaphoreTake(xMutex, portMAX_DELAY);
sharedVariable++;
xSemaphoreGive(xMutex);
Serial.print("\r\nProducer1");
vTaskDelay(pdMS_TO_TICKS(5000));
Serial.print("\r\nProducer2");
//vTaskDelay(pdMS_TO_TICKS(2000));
vTaskResume(hndlrConsumer);
vTaskSuspend(NULL);
}
}
void taskConsumer(void *pvParameters) {
for (;;) {
xSemaphoreTake(xMutex, portMAX_DELAY);
int localCopy = sharedVariable;
xSemaphoreGive(xMutex);
Serial.print("\r\nConsumer1");
Serial.printf("Shared Variable: %d\n", localCopy);
vTaskDelay(pdMS_TO_TICKS(500));
Serial.print("\r\nConsumer2");
//vTaskDelay(pdMS_TO_TICKS(1000));
delay(2000);
vTaskResume(hndlrProducer);
vTaskSuspend(NULL);
}
}
void setup() {
Serial.begin(115200);
Serial.println("Hello, ESP32-S3!");
xMutex = xSemaphoreCreateMutex();
xTaskCreate(taskProducer, "Producer", 2048, NULL, 1, &hndlrProducer);
xTaskCreate(taskConsumer, "Consumer", 2048, NULL, 1, &hndlrConsumer);
vTaskSuspend(hndlrConsumer);
String s="25101902";
Serial.println("\r\n--"+s.substring(0,2));
Serial.println(s.substring(2,4));
Serial.println(s.substring(4,6).toInt());
}
void loop() {
// put your main code here, to run repeatedly:
//delay(10); // this speeds up the simulation
}
//vTaskSuspend(TaskHandle_t handle) → Suspends a task. Pass NULL to suspend the current task.
//vTaskResume(TaskHandle_t handle) → Resumes a suspended task.
//xTaskResumeFromISR(TaskHandle_t handle) → Use this instead if resuming from an interrupt.