TaskHandle_t Task0;
TaskHandle_t Task1;
TaskHandle_t flash;
QueueHandle_t queue;
#define LED0 27
#define LED1 25
#define LED2 2
size_t xx=0;
size_t xPortGetFreeHeapSize( void );
void loop0(void * parameter) {
for (;;) {
// Use Arduino implementation for a number between limits
int rndNumber = random(1, 9);
Serial.print("Loop 0 - Running on core: ");
Serial.println(xPortGetCoreID());
// Add to the queue - wait forever until space is available
Serial.println("Mgr 0 - Adding " + String(rndNumber) + " to queue");
xQueueSend(queue, &rndNumber, portMAX_DELAY);
// Artificial wait here
digitalWrite(LED0, HIGH);
delay(200);
digitalWrite(LED0, LOW);
delay(500);
}
}
void loop1(void * parameter) {
for (;;) {
// Get the number of flashes required
int flashTotal;
xQueueReceive(queue, &flashTotal, portMAX_DELAY);
Serial.print("Loop 1 - Running on core: ");
Serial.println(xPortGetCoreID());
Serial.println("Worker - reading " + String(flashTotal));
// Flash that number
for (int cnt = 0; cnt < flashTotal; cnt++) {
digitalWrite(LED1, HIGH);
delay(150);
digitalWrite(LED1, LOW);
delay(150);
}
}
}
void blink(void * parameter) {
for (;;) {
xx=xPortGetFreeHeapSize( );
Serial.println(xx);
digitalWrite(LED_BUILTIN, HIGH);
delay(500);
digitalWrite(LED_BUILTIN, LOW);
delay(500);
}
}
void setup()
{
Serial.begin(115200);
delay(1000);
Serial.println("Setup started.");
Serial.print("###");
int cpuSpeed = getCpuFrequencyMhz();
Serial.println(cpuSpeed);
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED2, HIGH);
// Create the queue with 5 slots of 2 bytes
queue = xQueueCreate(5, sizeof(int));
xTaskCreatePinnedToCore(
loop0, /* Function to implement the task */
"Task0", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&Task0, /* Task handle. */
1); /* Core where the task should run */
xTaskCreatePinnedToCore(
loop1, /* Function to implement the task */
"Task1", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&Task1, /* Task handle. */
0); /* Core where the task should run */
xTaskCreatePinnedToCore(
blink, /* Function to implement the task */
"flash", /* Name of the task */
1000, /* Stack size in words */
NULL, /* Task input parameter */
0, /* Priority of the task */
&flash, /* Task handle. */
0); /* Core where the task should run */
Serial.println("Setup completed.");
}
void loop()
{
Serial.println("Setup completed.");
vTaskDelete (NULL);
}