/*******************************************************************************
  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_CONSOLE_HANDLE consoleHandlePLIB0;
SYS_CONSOLE_HANDLE consoleHandlePLIB1;
char UartWriteBuf[30];
char UartReadBuf[30];

void LED2Callback(uintptr_t context)
{
    LED2_Toggle();
}

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

// *****************************************************************************
// *****************************************************************************
// Section: Main Entry Point
// *****************************************************************************
// *****************************************************************************

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

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

    consoleHandlePLIB1 = SYS_CONSOLE_HandleGet( SYS_CONSOLE_INDEX_1 );
    sprintf(UartWriteBuf, "Hello world !(PLIB1)\r\n");
    SYS_CONSOLE_Write(consoleHandlePLIB1, UartWriteBuf, strlen(UartWriteBuf));

    SYS_TIME_DelayMS(1000, &tmrHandle);

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

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

        if( SYS_CONSOLE_Read(consoleHandlePLIB0, UartReadBuf, 1) > 0 )
        {
            sprintf(UartWriteBuf, "Receive : %s (PLIB0)\r\n", UartReadBuf);
            SYS_CONSOLE_Write(consoleHandlePLIB1, UartWriteBuf, strlen(UartWriteBuf));
        }
		
        if( SYS_CONSOLE_Read(consoleHandlePLIB1, UartReadBuf, 1) > 0 )
        {
            sprintf(UartWriteBuf, "Receive : %s (PLIB1)\r\n", UartReadBuf);
            SYS_CONSOLE_Write(consoleHandlePLIB0, 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
*/

