// Configure a Software Interrupt and its handler such that it transmits an
// integer to a Task1 over a queue.
// Task1 responsibility would be to take that integer and pass a string over
// queue corresponding to that to Task2 who will print the string received.
#include "Arduino.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/queue.h"
#define QUEUE_LENGTH 10
QueueHandle_t intQueue;
QueueHandle_t stringQueue;
#define INTERRUPT_PIN 2
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void isr() {
// Read sensor or any other event that triggers the interrupt
int data = 123; // Replace with your logic to get integer data
portENTER_CRITICAL_ISR(&mux);
xQueueSendFromISR(intQueue, &data, NULL);
portEXIT_CRITICAL_ISR(&mux);
}
void setupInterrupt() {
pinMode(INTERRUPT_PIN, INPUT_PULLUP);
attachInterrupt(INTERRUPT_PIN, isr, FALLING);
}
void Task1(void *pvParameters) {
int data;
char buffer[20]; // Adjust the size according to your needs
while (1) {
xQueueReceive(intQueue, &data, portMAX_DELAY);
sprintf(buffer, "%d", data);
xQueueSend(stringQueue, buffer, portMAX_DELAY);
}
}
void Task2(void *pvParameters) {
char receivedString[20]; // Adjust the size according to your needs
while (1) {
xQueueReceive(stringQueue, receivedString, portMAX_DELAY);
Serial.println(receivedString); // Print the received string
}
}
void setup() {
Serial.begin(115200);
intQueue = xQueueCreate(QUEUE_LENGTH, sizeof(int));
stringQueue = xQueueCreate(QUEUE_LENGTH, sizeof(char) * 20); // Adjust the size according to your needs
setupInterrupt();
xTaskCreate(Task1, "Task1", 5000, NULL, 1, NULL);
xTaskCreate(Task2, "Task2", 5000, NULL, 1, NULL);
}
void loop() {
// Empty loop as all tasks are managed by FreeRTOS
vTaskDelay(1000); // Optional delay to not overload the CPU
}