#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define LED_PIN 2
#define NOTIFY_BIT 1
TaskHandle_t led10TaskHandle = NULL;
TaskHandle_t led01TaskHandle = NULL;
void setup()
{
Serial.begin(115200);
Serial.println("ready");
//onboard led..
pinMode(LED_PIN, OUTPUT);
xTaskCreatePinnedToCore(
led10Task,
"LED10 Task",
1000,
NULL,
0,
&led10TaskHandle, 0
);
xTaskCreatePinnedToCore(
led01Task,
"LED01 Task",
1000,
NULL,
1,
&led01TaskHandle, 1
);
Serial.println("Setup completed.");
//trigger task led01 to start..
xTaskNotify( led01TaskHandle,
NOTIFY_BIT,
eSetBits);
}
void loop()
{
delay(1);
}
void led10Task(void * parameter)
{
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 3000 );
BaseType_t xResult;
uint32_t ulNotifiedValue;
unsigned long lastBlink;
int intervalBlink = 500;
byte blinkTimes = 0;
while (1)
{
// Wait to be notified..
xResult = xTaskNotifyWait( pdFALSE, /* Don't clear bits on entry. */
ULONG_MAX, /* Clear all bits on exit. */
&ulNotifiedValue, /* Stores the notified value. */
xMaxBlockTime );
if ( xResult == pdPASS )
{
if ( ( ulNotifiedValue & NOTIFY_BIT ) != 0 )
{
Serial.println("Task10 triggered..");
lastBlink = millis();
//do our work..
intervalBlink = 1000;
blinkTimes = 0;
while (blinkTimes < 7) {
if (millis() - lastBlink >= intervalBlink) {
lastBlink = millis();
blinkTimes++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}
//notify led01
xTaskNotify( led01TaskHandle,
NOTIFY_BIT,
eSetBits);
}//only if it's our bit..
}//only if we dont timeout
vTaskDelay(1);//breath
}
}
void led01Task(void * parameter)
{
const TickType_t xMaxBlockTime = pdMS_TO_TICKS( 6000 );
BaseType_t xResult;
uint32_t ulNotifiedValue;
unsigned long lastBlink;
int intervalBlink = 500;
byte blinkTimes = 0;
while (1)
{
// Wait to be notified..
xResult = xTaskNotifyWait( pdFALSE, /* Don't clear bits on entry. */
ULONG_MAX, /* Clear all bits on exit. */
&ulNotifiedValue, /* Stores the notified value. */
xMaxBlockTime );
if ( xResult == pdPASS )
{
if ( ( ulNotifiedValue & NOTIFY_BIT ) != 0 )
{
//do our work..
Serial.println("Task01 Triggered..");
lastBlink = millis();
intervalBlink = 500;
blinkTimes = 0;
while (blinkTimes < 7) {
if (millis() - lastBlink >= intervalBlink) {
lastBlink = millis();
blinkTimes++;
digitalWrite(LED_PIN, !digitalRead(LED_PIN));
}
}
//notify led10
xTaskNotify( led10TaskHandle,
NOTIFY_BIT,
eSetBits);
} //only if it's or bit..
}
vTaskDelay(1);
}
}