/***************************************************************************/
/*  Description:                                                           */
/*                                                                         */
/*   This program is a simple command interface via the serial channel     */
/*                                                                         */
/*        (C) Copyright 2003 Philips Semiconductors B.V.                   */
/*                                                                         */
/***************************************************************************/
#include "uart.h"

#define CMD_LENGTH      32

static BYTE cha;
static BYTE position = 0;
static BYTE commandLine[CMD_LENGTH];


static BYTE upper(BYTE c)                  // convert char to upper case
{
    if ((c >= 'a') && (c <= 'z'))
        return(c - 0x20);
    else
        return(c);
}

void SetBufIndex(BYTE index)
{
    position = index;
}

BYTE GetBufIndex()                         // read index
{
    return position;
}

BYTE GetBufAsc(BYTE index)                 // return character at index
{
    return (commandLine[index]);
}

BYTE GetCommand(const char* cmdTabptr, BYTE cmdTabLgth)
{
  BYTE i,c1,c2;

    c1 = commandLine[0];                   // get first character
    c2 = commandLine[1];                   // get second character
    for (i = 0; i < cmdTabLgth; i++)       // table is x commands long
    {
        if ((c1 == cmdTabptr[i*2]) && (c2 == cmdTabptr[i*2+1]))
            break;
    }
    cha = commandLine[2];
    position = 3;
    return i;
}

LONG GetArgument(void)
{
  LONG	save;
  BYTE	t;

    while (cha == ' ' || cha == ',')       // skip spaces from commandline buffer
        cha = commandLine[position++];

    save = 0;
    if (cha == '#')                        // decimal number ?
    {
        cha = commandLine[position++];
        while ((cha >= '0') && (cha <= '9'))
        {
            save = (save * 10) + (cha - '0');
            cha = commandLine[position++];
        }
    }
    else                                   // no then hexadecimal
    {
        while (1)
        {
            for (t=0; t<16; t++)
            {
                if (ascii[t] == cha)
                {
                    save = (save<<4) + t;
                    break;
                }
            }
            if (t < 16)
                cha = commandLine[position++];
            else
                break;
        }
    }
    return save;
}

BYTE ReadCommandLine(void)
{
  BYTE	c;

    c = ua_inchar();                       // look if there is a character available
    if (c == 0x08 && position != 0)        // BACKSPACE
    {
        ua_outchar('\b');
        ua_outchar(' ');
        ua_outchar('\b');
        position --;
    }
    else if (c == '\r')                    // RETURN
    {
        commandLine[position]   = '\r';
        commandLine[position+1] = '\n';
        position = 0;
        ua_outchar('\r');
        ua_outchar('\n');
        return 1;
    }
    else if (position == CMD_LENGTH-2)
    {
        ua_outchar('\a');
    }
    else if (c > 31 && c < 128)
    {
        commandLine[position] = upper(c);
        position ++;
        ua_outchar(c);                     // echo character
    }
    return 0;
}
