/*******************************************************************************
  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>

void APP_USBCDC_Printf( const char *format, ... );
int APP_USBCDC_ReadBytes( char *ReadByte, int ReadLength );
char CDCReadBytes[10];

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

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 );

    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);

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

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

        if( SYS_TIME_DelayIsComplete(tmrHandle_USB_hello) == true )
        {
            APP_USBCDC_Printf("Hello world !(USB)\r\n");
        }

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

        if( APP_USBCDC_ReadBytes(CDCReadBytes, 1) > 0 )
        {
            sprintf(UartWriteBuf, "Receive (USB): %s\r\n", CDCReadBytes);
            SYS_CONSOLE_Write(consoleHandle, 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
*/

