/*******************************************************************************
  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");	
}

SYS_FS_HANDLE fHandle = SYS_FS_HANDLE_INVALID;
SYS_FS_RESULT fResult;
char fMedia[20] = "/dev/mmcblka1";
char fDrive[20] = "/mnt/myDrive1";
void CommandMount(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    fResult = SYS_FS_Mount(fMedia, fDrive, FAT, 0, NULL);
    if(fResult == SYS_FS_RES_SUCCESS)
    {
        fResult = SYS_FS_CurrentDriveSet(fDrive);
        if(fResult == SYS_FS_RES_SUCCESS)
        {
            SYS_CMD_PRINT("Mount %s success!\r\n", fMedia);
            SYS_CMD_PRINT("Current Drive is %s\r\n", fDrive);
            return;
        }
    }
    SYS_CMD_PRINT("Mount %s failure!\r\n", fMedia);
}

void CommandOpen(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    fHandle = SYS_FS_FileOpen(argv[1], SYS_FS_FILE_OPEN_APPEND_PLUS);
    if(fHandle != SYS_FS_HANDLE_INVALID)
        SYS_CMD_PRINT("File [%s] opened!\r\n", argv[1] );
    else
        SYS_CMD_MESSAGE("Open failure!\r\n");
}

void CommandWrite(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    int fWriteSize = 0;

    fWriteSize = SYS_FS_FileWrite(fHandle, argv[1], strlen(argv[1]));
    if(fWriteSize != -1)
        SYS_CMD_PRINT("Write [%s](%d) to File!\r\n", argv[1], fWriteSize );	
    else
        SYS_CMD_MESSAGE("Write failure!\r\n");
}

void CommandRead(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    int fReadSize = 0;
    char fReadBuf[50] = "";
	
    SYS_FS_FileSeek(fHandle, 0, SYS_FS_SEEK_SET);
    fReadSize = SYS_FS_FileRead(fHandle, (void *)fReadBuf, atoi(argv[1]));
    if(fReadSize != -1)
        SYS_CMD_PRINT("Read [%s](%d) from File!\r\n", fReadBuf, fReadSize );	
    else
        SYS_CMD_MESSAGE("Read failure!\r\n");
}

void CommandClose(SYS_CMD_DEVICE_NODE* pCmdIO, int argc, char** argv)
{
    SYS_FS_FileClose(fHandle);
    SYS_CMD_MESSAGE("File Closed!\r\n");
}

static const SYS_CMD_DESCRIPTOR MyCmdTbl[]=
{
    { "hello", CommandHello, ": Say Hello" },
    { "led",   CommandLED,	 ": led # on[off]" },
    { "errlv", CommandErrLv, ": errlv #" },
    { "mount", CommandMount, ": Mount Media" },
    { "open",  CommandOpen,  ": open $" },
    { "write", CommandWrite, ": write $" },
    { "read",  CommandRead,  ": read #" },
    { "close", CommandClose, ": Close file" },
};

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

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

    SYS_CMD_ADDGRP(MyCmdTbl, sizeof(MyCmdTbl)/sizeof(SYS_CMD_DESCRIPTOR), "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
*/

