/**
   Example of a Arduino interruption and RTOS Task Notification.
   https://www.freertos.org/RTOS_Task_Notification_As_Binary_Semaphore.html
*/

// Include Arduino FreeRTOS library
#include <Arduino_FreeRTOS.h>

/**
   Declaring a global TaskHandle for the led task.
*/
TaskHandle_t taskNotificationHandler;

// set interrupt pin
#ifdef BTN_BUILTIN
uint8_t int_pin = BTN_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t int_pin = P0_8;
#else
uint8_t int_pin = 2;
#endif

void setup() {
  // Create task for FreeRTOS notification
  xTaskCreate(TaskNotification,           // Task function
              "Notification",             // Task name
              128,                        // Stack size
              NULL,
              3,                          // Priority
              &taskNotificationHandler);  // TaskHandle
}

void loop() {

}

/*
   Notification task.
*/
void TaskNotification(void *pvParameters)
{
  (void) pvParameters;

  Serial.begin(9600);

  attachInterrupt(digitalPinToInterrupt(int_pin), digitalPinInterruptHandler, RISING);

  for (;;) {

    if (ulTaskNotifyTake(pdTRUE, portMAX_DELAY)) {
      Serial.println("Notification received");
    }
  }
}


void digitalPinInterruptHandler() {
  BaseType_t xHigherPriorityTaskWoken = pdFALSE;
  vTaskNotifyGiveFromISR(taskNotificationHandler, &xHigherPriorityTaskWoken);
  portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
