/*******************************************************************************
  MPLAB Harmony Application Source File
  
  Company:
    Microchip Technology Inc.
  
  File Name:
    app.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It 
    implements the logic of the application's state machine and it may call 
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc.  All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
 *******************************************************************************/
// DOM-IGNORE-END


// *****************************************************************************
// *****************************************************************************
// Section: Included Files 
// *****************************************************************************
// *****************************************************************************

#include "app.h"

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_Initialize function.
    
    Application strings and buffers are be defined outside this structure.
*/

APP_DATA appData;

char rxString[100];
char idCommand[100];
int rxCommand;
int newPWM;
char stringPWM[3];

const char *commands[NBR_COMMANDS] = {"enable", "disable", "forward", "backward",
"stop", "pwm","reset","help"};

const char helpMessage[] = "\r\nenable \t\tset EN to 1"
"\r\ndisable \tset EN to 0"
"\r\nforward \tmove in forward direction"
"\r\nbackward \tmove in backward direction"
"\r\nstop \t\tstop the motor"
"\r\npwm \t\tset new pulse width value"
"\r\nreset \t\tsoftware reset the MCU\r\n";

static const PORTS_CHANNEL mdriver_port_channel_map[] =
{
    PORT_CHANNEL_K,
    PORT_CHANNEL_J,
    PORT_CHANNEL_J
};

static const PORTS_BIT_POS mdriver_port_bit_pos_map[] =
{
    PORTS_BIT_POS_3,
    PORTS_BIT_POS_5,
    PORTS_BIT_POS_7
};


// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO:  Add any necessary callback functions.
*/
/**
 * @fn void adcConfig(void)
 * @brief identify the console command to an integer
 * 0 -- enable
 * 1 -- disable
 * 2 -- forward 
 * 3 -- backward
 * 4 -- stop
 * 5 -- pwm
 * 6 -- reset
 * 7 -- help
 * 8 -- invalide command
 * 9 -- enter key
 */
int identifyCommand(char inputStr[100]) {
    int i;
    for(i = 0; i < NBR_COMMANDS; i++){
        if(strcmp(inputStr,commands[i]) == 0){
            return i;
        }
    }
    if(strcmp(inputStr,"\0") > 0){
        return 8;
    }
    else{
        return 9;
    }
}

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************


/* TODO:  Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_Initialize ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;
    
    
    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
    uartPutS("\r\n**************************\r\n");
    uartPutS("***PWM Test Application***");
    uartPutS("\r\n**************************\r\n");
    
}


/******************************************************************************
  Function:
    void APP_Tasks ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Tasks ( void )
{

    /* Check the application's current state. */
    switch ( appData.state )
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            DRV_TMR0_Start();
            DRV_OC0_Start();
            PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_EN],
                    mdriver_port_bit_pos_map[MDRIVER_PIN_EN], 0);
            PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INA],
                    mdriver_port_bit_pos_map[MDRIVER_PIN_INA], 0);
            PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INB],
                    mdriver_port_bit_pos_map[MDRIVER_PIN_INB], 0);
            appData.state = APP_STATE_SERVICE_TASKS;
            
            break;
        }

        case APP_STATE_SERVICE_TASKS:
        {
           uartPutS("\n\rPWMTestRoutine: # ");
        uartGetS(rxString);
        rxCommand = identifyCommand(rxString);
//        sprintf(idCommand,"\r\nIdentified command: %d\r\n", rxCommand);
//        uartPutS(idCommand);
        switch ( rxCommand )
        {
            case 0: //enable
            {
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_EN],
            mdriver_port_bit_pos_map[MDRIVER_PIN_EN], 1);
                break;
            }
            case 1: //disable
            {
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_EN],
            mdriver_port_bit_pos_map[MDRIVER_PIN_EN], 0);
                break;
            }
            case 2: //forward
            {
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INA],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INA], 1);
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INB],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INB], 0);
                break;
            }
            case 3: //backward
            {
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INA],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INA], 0);
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INB],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INB], 1);
                break;
            }
            case 4: //stop
            {
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INA],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INA], 0);
                PLIB_PORTS_PinWrite(PORTS_ID_0,mdriver_port_channel_map[MDRIVER_PIN_INB],
            mdriver_port_bit_pos_map[MDRIVER_PIN_INB], 0);
                break;
            }
            case 5: //pwm
            {
                uartPutS("Enter new value (0-100): ");
                uartGetS(stringPWM);
                newPWM = atoi(stringPWM);
                if ((0 <= newPWM) && (180>= newPWM))
                {
                    PLIB_OC_PulseWidth16BitSet(OC_ID_1, (400 + (uint32_t) (400 / 180) *newPWM));
                    uartPutS("New PWM value set!\r\n");
                    break;
                }
                else
                {
                    uartPutS("Invalid value!\r\n");
                    break;
                }
            }
            case 6: //reset
            {
                SYS_RESET_SoftwareReset();
                break;
            case 7: //help
            {
                uartPutS(helpMessage);
                break;
            }
            case 8: //invalid command
            {
                uartPutS("Invalid command!\r\n");
                break;
            }
            case 9: //enter key
            {
                break;
            }

        }
        }
            break;
        }

        /* TODO: implement your application state machine.*/
        

        /* The default state should never be executed. */
        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}

 

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