/*******************************************************************************
  Main Source File

  Company:
    Microchip Technology Inc.

  File Name:
    main.c

  Summary:
    This file contains the "main" function for a project.

  Description:
    This file contains the "main" function for a project.  The
    "main" function calls the "SYS_Initialize" function to initialize the state
    machines of all modules in the system
 *******************************************************************************/

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

#include <stddef.h>                     // Defines NULL
#include <stdbool.h>                    // Defines true
#include <stdlib.h>                     // Defines EXIT_FAILURE
#include "definitions.h"                // SYS function prototypes
#include <stdio.h>
#include <string.h>

SYS_TIME_HANDLE tmrHandle;
SYS_TIME_HANDLE tmrHandle_USB_hello;
SYS_CONSOLE_HANDLE consoleHandlePLIB;
SYS_CONSOLE_HANDLE consoleHandleUSB;
char UartWriteBuf[30];
char UartReadBuf[10];

// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************
void LED2Callback(uintptr_t context)
{
    LED2_Toggle();
}

void LED3Callback(uintptr_t context)
{
    LED3_Toggle();
}

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );

    consoleHandlePLIB = SYS_CONSOLE_HandleGet( SYS_CONSOLE_INDEX_0 );
    sprintf(UartWriteBuf, "Hello world !(PLIB)\r\n");
    SYS_CONSOLE_Write(consoleHandlePLIB, UartWriteBuf, strlen(UartWriteBuf));

    SYS_TIME_DelayMS(1000, &tmrHandle);
    SYS_TIME_DelayMS(1000, &tmrHandle_USB_hello);

    SYS_TIME_CallbackRegisterMS(LED2Callback, 0, 500, SYS_TIME_PERIODIC);
    SYS_TIME_CallbackRegisterMS(LED3Callback, 0, 250, SYS_TIME_PERIODIC);

    consoleHandleUSB = SYS_CONSOLE_HandleGet( SYS_CONSOLE_INDEX_1 );

    while ( true )
    {
        if( SYS_TIME_DelayIsComplete(tmrHandle) == true )
        {
            LED1_Toggle();
            SYS_TIME_DelayMS(1000, &tmrHandle);
        }

        if( SYS_TIME_DelayIsComplete(tmrHandle_USB_hello) == true )
        {			
            sprintf(UartWriteBuf, "Hello world !(USB)\r\n");
            SYS_CONSOLE_Write(consoleHandleUSB, UartWriteBuf, strlen(UartWriteBuf));
        }

        if( SYS_CONSOLE_Read(consoleHandlePLIB, UartReadBuf, 1) > 0 )
        {
            sprintf(UartWriteBuf, "Receive : %s (PLIB)\r\n", UartReadBuf);
            SYS_CONSOLE_Write(consoleHandleUSB, UartWriteBuf, strlen(UartWriteBuf));  // Modify
        }

        if( SYS_CONSOLE_Read(consoleHandleUSB, UartReadBuf, 1) > 0 )
        {
            sprintf(UartWriteBuf, "Receive : %s (USB)\r\n", UartReadBuf);
            SYS_CONSOLE_Write(consoleHandlePLIB, UartWriteBuf, strlen(UartWriteBuf));
        }

        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
    }

    /* Execution should not come here during normal operation */

    return ( EXIT_FAILURE );
}


/*******************************************************************************
 End of File
*/

