/****************************************************************************/
/* Copyright 2004 MBARI.                                                    */
/* Monterey Bay Aquarium Research Institute Proprietary Information.        */
/* All rights reserved.                                                     */
/****************************************************************************/

#define MAX_CHARS   15
#define putEcho()   if ( echoMode ) putc('\n')
#define setEcho(X)  echoMode = (X)

int echoMode = false;

int getCommand(char* cmd)
{
    static int char_count = 0;
    static char line[MAX_CHARS] = "";
    char terminator = '\r';
    int got_line;
    int c;
    int i;

    got_line = FALSE;

    if (  kbhit() )
    {
        c = getc();

        if ( echoMode )
        {
            if (c == '\r')
            {
                putc('\r');
                putc('\n');
            }
            else
            {
                putc(c);
            }
        }

        if ( c != terminator )
        {
            if ( (c != 8) && (char_count < MAX_CHARS) )
                line[char_count++] = c;
            else if ( char_count > 0 )
                --char_count;

            cmd[0] = '\0';
        } 
        else
        {
            got_line = TRUE;
            
            if ( char_count < MAX_CHARS )
                line[char_count] = '\0';
            else
                line[MAX_CHARS - 1] = '\0';

            for (i = 0; i <= char_count; i++)
                cmd[i] = line[i];
            char_count = 0;
        }
    }

    return got_line;
}

void putErr(int err_code)
{
    if ( err_code )
    {
        printf("ERR %03d\r", err_code);
        putEcho();
    }

    printf("RDY\r");
    putEcho();

    return;
}

