#include <Arduino_FreeRTOS.h>

//define task handles
TaskHandle_t TaskBlink_Handler;
TaskHandle_t TaskSerial_Handler;

// define two tasks for Blink & Serial
void TaskBlink( void *pvParameters );
void TaskSerial(void* pvParameters);

// set led pin
#ifdef LED_BUILTIN
uint8_t blink_pin = LED_BUILTIN;
#elif defined(ARDUINO_ELSOMIK)
uint8_t blink_pin = P0_0;
#else
uint8_t blink_pin = 2;
#endif

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
  
  // Now set up two tasks to run independently.
   xTaskCreate(
    TaskBlink,
    "Blink",              // A name just for humans
    128,                  // This stack size can be checked & adjusted by reading the Stack Highwater
    NULL,                 // Parameters passed to the task function
    2,                    // Priority, with 3 (configMAX_PRIORITIES - 1) being the highest, and 0 being the lowest.
    &TaskBlink_Handler);  // Task handle

   xTaskCreate(
    TaskSerial,
    "Serial",
    128,                  // Stack size
    NULL,                 // Parameters passed to the task function
    1,                    // Priority
    &TaskSerial_Handler); // Task handle
}
    

void loop()
{
  // Empty. Things are done in Tasks.
}

/*--------------------------------------------------*/
/*---------------------- Tasks ---------------------*/
/*--------------------------------------------------*/

void TaskSerial(void* pvParameters){
/*
 Serial
 Send "s" or "r" through the serial port to control the suspend and resume of the LED light task.
 This example code is in the public domain.
*/
  (void) pvParameters;
   for (;;) // A Task shall never return or exit.
   {
    while(Serial.available()>0){
      switch(Serial.read()){
        case 's':
          vTaskSuspend(TaskBlink_Handler); 
          Serial.println("Suspend!");
          break;
        case 'r':
          vTaskResume(TaskBlink_Handler);
          Serial.println("Resume!");
          break;
      }
      vTaskDelay(1);
    }
   }
}

void TaskBlink(void *pvParameters)  // This is a task.
{
  (void) pvParameters;
  pinMode(blink_pin, OUTPUT);
  for (;;) // A Task shall never return or exit.
  {
    digitalWrite(blink_pin, HIGH);          // turn the pin on (HIGH is the voltage level)
    vTaskDelay(1000 / portTICK_PERIOD_MS);  // wait for one second
    digitalWrite(blink_pin, LOW);           // turn the pin off by making the voltage LOW
    vTaskDelay(1000 / portTICK_PERIOD_MS);  // wait for one second
  }
}
