/*******************************************************************************
  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>
#include <stdarg.h>

uint32_t i = 0;
uint8_t USARTRTxBuffer[100];
uint8_t USART5_ReceiveData[1];
volatile uint8_t USART5_IsReceived = 0;
volatile uint8_t USART5_IsTransmitted = 1;

#define SYS_CONSOLE_PRINT_BUFFER_SIZE	200
static char consolePrintBuffer[SYS_CONSOLE_PRINT_BUFFER_SIZE];
volatile uint8_t TC3_HasExpired = 0;
volatile uint8_t TC4_HasExpired = 0;

// TODO 13.01
// uint16_t ADC_Result;




void myprintf(const char *format, ...)
{
    size_t len = 0;
    va_list args = {0};
    va_start(args, format);
    len = vsnprintf(consolePrintBuffer, SYS_CONSOLE_PRINT_BUFFER_SIZE, format, args);
    va_end(args);

    if ((len > 0) && (len < SYS_CONSOLE_PRINT_BUFFER_SIZE))
    {
        consolePrintBuffer[len] = '\0';
        SERCOM5_USART_Write(consolePrintBuffer, len);
        while (SERCOM5_USART_WriteIsBusy());
    }
}

void USART5_Transmit( uintptr_t context )
{
    USART5_IsTransmitted = 1;
} 

void USART5_Receive( uintptr_t context )
{
    USART5_IsReceived = 1;
}

void TC3_TimerExpired(TC_TIMER_STATUS status, uintptr_t context) 
{
    if( status & TC_INTFLAG_OVF_Msk)
    {
        TC3_HasExpired = 1;
    }
}

void TC4_TimerExpired(TC_TIMER_STATUS status, uintptr_t context) 
{
    if( status & TC_INTFLAG_OVF_Msk)
    {
        TC4_HasExpired = 1;
    }
}

// TODO 13.02













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

int main ( void )
{
    /* Initialize all modules */
    SYS_Initialize ( NULL );
    
    TC3_TimerCallbackRegister( TC3_TimerExpired, (uintptr_t )NULL );
    TC3_TimerStart();
    TC4_TimerCallbackRegister( TC4_TimerExpired, (uintptr_t )NULL );
    TC4_TimerStart();
    
    SERCOM5_USART_ReadCallbackRegister( USART5_Receive, (uintptr_t )NULL );
    SERCOM5_USART_WriteCallbackRegister( USART5_Transmit, (uintptr_t )NULL );
    SERCOM5_USART_Read( USART5_ReceiveData, 1 );
    
    // TODO 13.03


    
    myprintf("\033[2J");
    myprintf("\033[?25l");

    while ( true )
    {
        /* Maintain state machines of all polled MPLAB Harmony modules. */
        SYS_Tasks ( );
        
        if (TC3_HasExpired)
        {
            TC3_HasExpired = 0;
            LED1_Toggle();
            myprintf( "\033[1;1HHello World!" );
        }
        
        if (TC4_HasExpired)
        {
            TC4_HasExpired = 0;
            LED2_Toggle();
            // TODO 13.04

            // while( !ADC_ConversionStatusGet() );
            // ADC_Result = ADC_ConversionResultGet();
            // myprintf("\033[2;1HVR : %4d", ADC_Result );
        }
        
        if( USART5_IsReceived )
        {
            USART5_IsReceived = 0;
            myprintf("\033[6;1HReceived Data : %1c", USART5_ReceiveData[0] );
            SERCOM5_USART_Read( USART5_ReceiveData, 1 );
        }
        
        // TODO 13.05






        
    }

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

    return ( EXIT_FAILURE );
}


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

