/****************************************************************************/
/* Copyright 2010 MBARI.                                                    */
/* MBARI Proprietary Information. All rights reserved.                      */
/****************************************************************************/
#include <p24fxxxx.h>
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "sys_defs.h"
#include "actions.h"
#include "errors.h"
#include "parser.h"
#include "serial.h"
#include "spi.h"

static char msg_buff[64];
int actDebug1 = 0;

/****************************************************************************/
/*                               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 actSpiCmd(char* s)
{
    unsigned int reply;
    unsigned int bits = (unsigned int)atoi(s);
    
    spiCS(1);
    reply = spiReadWrite(bits);
    spiCS(0);


    sprintf(msg_buff, "SPI = %u\r\n", reply);
    serPutString(SER_CONSOLE, msg_buff);

    return ERR_SUCCESS;
}

int actResetCmd(char* s)
{
    asm("reset");
    return ERR_SUCCESS;
}

int actHelloKittyCmd(char* s)
{
    serPutString(SER_CONSOLE, "MEOW\r\n");
    return ERR_SUCCESS;
}

/****************************************************************************/
/*                               Get commands                               */
/****************************************************************************/
int actGetSpiMemCmd(char* s)
{
    unsigned int add = (unsigned int)atoi(s);
    unsigned int dat;
    
    if ( prsNameMatch(s, "all") )
    {
        for (add = 0; add < 16; ++add)
        {
            dat = spiSlaveRead(add, READ_CMD);
            sprintf(msg_buff, "SPI MEM[%02u] = %u\r\n", add, dat);
            serPutString(SER_CONSOLE, msg_buff);
        }

    }
    else
    {
        dat = spiSlaveRead(add, READ_CMD);
        sprintf(msg_buff, "SPI MEM[%u] = %u\r\n", add, dat);
        serPutString(SER_CONSOLE, msg_buff);
    }

    return ERR_SUCCESS;
}

int actGetSpiInfoCmd(char* s)
{
    unsigned int add = (unsigned int)atoi(s);
    unsigned int dat = spiSlaveRead(add, INFO_CMD);
    
    sprintf(msg_buff, "SPI INFO[%u] = %u\r\n", add, dat);
    serPutString(SER_CONSOLE, msg_buff);

    return ERR_SUCCESS;
}

/****************************************************************************/
/*                               Set commands                               */
/****************************************************************************/
int actSetSpiMemCmd(char* s)
{
    unsigned int add;
    unsigned int dat;
    char *token;
    char *separator = " ,\t";
   
    /* get address */
    token = strtok(s, separator);
    add = (unsigned int)atol(token);

    /* get data */
    token = strtok(NULL, separator);
    dat = (unsigned int)atol(token);
    
    spiSlaveWrite(add, dat);

    sprintf(msg_buff, "SPI MEM[%u] = %u\r\n", add, dat);
    serPutString(SER_CONSOLE, msg_buff);

    return ERR_SUCCESS;
}

int actSetDebug1Cmd(char* s)
{
    actDebug1 = atoi(s);

    sprintf(msg_buff, "actDebug1 = %i\r\n", actDebug1);
    serPutString(SER_CONSOLE, msg_buff);

    return ERR_SUCCESS;
}


/****************************************************************************/
/*                              Clear commands                              */
/****************************************************************************/
int actClrStubCmd(char* s)
{
    int num = atoi(s);
    
    sprintf(msg_buff, "actClrStubCmd() = %d\r\n", num);
    serPutString(SER_CONSOLE, msg_buff);

    return ERR_SUCCESS;
}


