/*******************************************************************************
  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 CommandHello(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    SYS_CMD_MESSAGE("Hello world !\r\n");
}

void CommandLED(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    if(argc == 3)
    {
        int Led, OnOff = 0;
		
        if(!strcmp( argv[2], "on" ))
            OnOff = 1;
		
        Led = atoi(argv[1]);		
        switch(Led)
        {
            case 1: (OnOff ? LED1_Set() : LED1_Clear()); break;
            case 2: (OnOff ? LED2_Set() : LED2_Clear()); break;
            case 3: (OnOff ? LED3_Set() : LED3_Clear()); break;
        }
    }
}

void CommandErrLv(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    if(argc == 2)
    {
        int ErrorLevel = atoi(argv[1]);					
        if(ErrorLevel>=0 && ErrorLevel<=4 )
        {
            SYS_DEBUG_ErrorLevelSet(ErrorLevel);
            SYS_CMD_PRINT("Change Error Level to %d\r\n", SYS_DEBUG_ErrorLevelGet());
        }
    }
    SYS_DEBUG_MESSAGE(SYS_ERROR_FATAL,  "Error Level 0 - Fatal\r\n");
    SYS_DEBUG_MESSAGE(SYS_ERROR_ERROR,  "Error Level 1 - Error\r\n");
    SYS_DEBUG_MESSAGE(SYS_ERROR_WARNING,"Error Level 2 - Warning\r\n");
    SYS_DEBUG_MESSAGE(SYS_ERROR_INFO,   "Error Level 3 - Info\r\n");
    SYS_DEBUG_MESSAGE(SYS_ERROR_DEBUG,  "Error Level 4 - Debug\r\n");	
}

static const SYS_CMD_DESCRIPTOR MyCmdTbl[]=
{
    { "hello", CommandHello, ": Say Hello" },
    { "led",   CommandLED,	 ": led # on[off]" },
    { "errlv", CommandErrLv, ": errlv #" },
};

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

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

    SYS_CMD_ADDGRP(MyCmdTbl, 3, "MYCMD", ": My Commands");   // Modify

    while ( true )
    {
        /* 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
*/

