/* Include Files */
#include "KTag.h"

TaskHandle_t Sample_Task_Handle;

// LED Functionality
static void LED_CoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );
#define LED_COROUTINE_PRIORITY 0
static const TickType_t Delay_50ms = 50 / portTICK_PERIOD_MS;
static const TickType_t Delay_100ms = 100 / portTICK_PERIOD_MS;
static const TickType_t Delay_600ms = 600 / portTICK_PERIOD_MS;

// Serial Debug Functionality
static void Serial_Debug_CoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex );
#define SERIAL_DEBUG_COROUTINE_PRIORITY 0
static const TickType_t Delay_1s = 1000 / portTICK_PERIOD_MS;

void Sample_Task_Init(void)
{
}

//! Sample task: blinks the LED and sends text out on the UART.
/*!
 *  \param  pvParameters  (not used)
 *  \return               None (infinite loop)
 */
void Sample_Task(void * pvParameters)
{
    xCoRoutineCreate(LED_CoRoutine, LED_COROUTINE_PRIORITY, 0 );
    xCoRoutineCreate(Serial_Debug_CoRoutine, SERIAL_DEBUG_COROUTINE_PRIORITY, 0 );
   
    while (true)
	{       
        vCoRoutineSchedule();
        
        // Delay a bit here so as to not starve the idle task.
        vTaskDelay(10 / portTICK_PERIOD_MS);
	}
}

//! Blinks the LED.
static void LED_CoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
{
    crSTART( xHandle );
    
    static bool is_startup = false;

    while (true)
    {
        if (is_startup == false)
        {
            Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 1);
            crDELAY(xHandle, Delay_100ms);
            
            Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 0);
            crDELAY(xHandle, Delay_50ms);
            
            Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 1); 
            crDELAY(xHandle, Delay_100ms);
            
            Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 0);
            crDELAY(xHandle, Delay_50ms);
            
        	Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 1);
            crDELAY(xHandle, Delay_100ms);
            
            Cy_GPIO_Write(Red_LED_PORT, Red_LED_NUM, 0);        
            
            is_startup = true;
        }
        crDELAY(xHandle, Delay_600ms);
    }

    crEND();
}

static void Serial_Debug_CoRoutine( CoRoutineHandle_t xHandle, UBaseType_t uxIndex )
{
    crSTART( xHandle );
    
    //static uint32_t i = 0;
    
    while (true)
    {
        //Debug_printf("%lu\n", i++);
        //vSerialPutString(" * ", 50);        
        crDELAY(xHandle, Delay_1s);
    }

    crEND();
}

/* [] END OF FILE */
