#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "UART.h"
#include "extern.h"
#include "ts_actions.h"

#include "../CommonCode/i2c.h"
#include "../CommonCode/parser.h"
#include "../CommonCode/cmd_types.h"

static char msg_buff[128];

/****************************************************************************/
/*                               Root commands                              */
/****************************************************************************/
int actGetCmd(char* s)
{
    return prsParse(s, cmdsGet, ERR_NO_SUB_COMMAND);
}

int actSetCmd(char* s)
{
    return prsParse(s, cmdsSet, ERR_NO_SUB_COMMAND);
}

int actClrCmd(char* s)
{
    return prsParse(s, cmdsClr, ERR_NO_SUB_COMMAND);
}

int actLedOutputCmd(char* s)
{
    long led_state;

    /* parse LED request */
    led_state = strtol(s, NULL, 16);

    /* check LED request */
    if ( (led_state < 0) || (led_state > 255) )
        return ERR_FAILURE;

    /* update LED state var */
    npLedState = (unsigned char)led_state;

    return ERR_SUCCESS;
}

int actHelpCmd(char* s)
{
    u2PutString("NP ON - enable all navproc output\r\n");
    u2PutString("NP OFF - disable all navproc output\r\n");

    u2PutString("CAM ON - enable camera output\r\n");
    u2PutString("CAM OFF - disable camera output\r\n");

    u2PutString("ENV ON - enable environmental output\r\n");
    u2PutString("ENV OFF - disable environmental output\r\n");

    u2PutString("PTB ON - enable pan, tilt and button output\r\n");
    u2PutString("PTB OFF - disable pan, tilt and button output\r\n");

    u2PutString("ECHO ON - enable CLI echo\r\n");
    u2PutString("ECHO OFF - disable CLI echo\r\n");

    u2PutString("TS_DEBUG ON - enable topside debug output\r\n");
    u2PutString("TS_DEBUG OFF - disable topside debug output\r\n");

    u2PutString("LED_OUT - set lapbox LED states\r\n");

    u2PutString("HELP - display this menu\r\n");
    u2PutString("RESET - reset the controller\r\n");
    u2PutString("\r\n");
    u2PutString("GET HUM - get labbox humidity temperature\r\n");
    u2PutString("\r\n");
    u2PutString("SET STUB - stub for future set cmds\r\n");
    u2PutString("\r\n");
    u2PutString("CLR STUB - stub for future clear cmds\r\n");
    u2PutString("\r\n");

    return ERR_SUCCESS;
}

int actResetCmd(char* s)
{
    asm("reset");
    return ERR_SUCCESS;
}

int actNpOnCmd(char* s)
{
    /* turn on all the topics */
    camOutput = 1;
    envOutput = 1;
    ptbOutput = 1;

    return ERR_SUCCESS;
}

int actNpOffCmd(char* s)
{
    /* turn off all the topics */
    camOutput = 0;
    envOutput = 0;
    ptbOutput = 0;

    return ERR_SUCCESS;
}

int actCamOnCmd(char* s)
{
    camOutput = 1;
    return ERR_SUCCESS;
}

int actCamOffCmd(char* s)
{
    camOutput = 0;
    return ERR_SUCCESS;
}

int actEnvOnCmd(char* s)
{
    envOutput = 1;
    return ERR_SUCCESS;
}

int actEnvOffCmd(char* s)
{
    envOutput = 0;
    return ERR_SUCCESS;
}

int actPtbOnCmd(char* s)
{
    ptbOutput = 1;
    return ERR_SUCCESS;
}

int actPtbOffCmd(char* s)
{
    ptbOutput = 0;
    return ERR_SUCCESS;
}

int actEchoOnCmd(char* s)
{
    echoMode = 1;
    return ERR_SUCCESS;
}

int actEchoOffCmd(char* s)
{
    echoMode = 0;
    return ERR_SUCCESS;
}

int actTsDebugOnCmd(char* s)
{
    tsDebugOutput = 1;
    return ERR_SUCCESS;
}

int actTsDebugOffCmd(char* s)
{
    tsDebugOutput = 0;
    return ERR_SUCCESS;
}

int actHelloKittyCmd(char* s)
{
    u2PutString("MEOW\r\n");
    return ERR_SUCCESS;
}

/****************************************************************************/
/*                               Get commands                               */
/****************************************************************************/

int actGetHumCmd(char* s)
{
    float rh, temp;

    i2c_getHumTemp(&rh, &temp);

    sprintf(msg_buff, "HUM    : %.2f %%\r\n", (double)rh);
    u2PutString(msg_buff);
    sprintf(msg_buff, "TEMP   : %.2f C\r\n", (double)temp);
    u2PutString(msg_buff);

    return ERR_SUCCESS;
}

/****************************************************************************/
/*                               Set commands                               */
/****************************************************************************/

int actSetStubCmd(char* s)
{
    sprintf(msg_buff, "actSetStubCmd(%s)\r\n", s);
    u2PutString(msg_buff);

    return ERR_SUCCESS;
}

/****************************************************************************/
/*                              Clear commands                              */
/****************************************************************************/

int actClrStubCmd(char* s)
{
    sprintf(msg_buff, "actClrStubCmd(%s)\r\n", s);
    u2PutString(msg_buff);

    return ERR_SUCCESS;
}
