
 
/*  select.c
    used with tm40.c.
    Stephen D. Anderson --- May 17, 1995

    Function gets next field instrument.  Does the following:

    Checks whether any field instrument exists, searches if not.  If
    a search is done, there is an immediate return to the calling program.
    This is because the search does all of the functions that this
    function does.

    Displays list of polling addresses of field instruments that it knows
    about.  Asks user to enter the polling address of a field instrument.  
    Repeats this until it gets a number that is one of the addresses in
    the list and is not greater than 15.

    Loads the received response corresponding of the field instrument
    of the selected polling address.  Parses it to get info it needs to
    talk to this field instrument.

    Sets global variable "found" to 1 to indicate to other modules that
    field instrument exists.


    ON ENTRY:   has no arguments.

    ON EXIT:    returns nothing.

    REVISION LOG:


*/

#include "tm40.h"

void select()
{
    extern int cmd0_rec_bytes[];
    extern byte cmd0_receive[][50];
    extern byte receive[];
    extern byte status1, status2;
    extern byte polling_addr, addr[];
    extern byte found;
    extern byte status_wind;
    extern byte addrs[];

    int i1;
    byte i,j,k;
    byte *r,*s,*re;
    byte start_delim[3] = {0xff, 0xff, 6};
    byte trial_address;
    byte byte_count;
    int rec_bytes;
    byte *a;
    byte ss[5];

    /*  need field inst info?  */
    if (!found) 
    {
        found = search();
        return;
    }

    Wopen(3,18,16,62,2,WHITE|_BLUE,WHITE|_BLUE);

    /*  Determine which device to use.  */
    while (1)
    {
        Wclear();
        Wputs("\n\n   Field Instruments were found at the\n");
        Wputs("   following Polling Addresses:\n\n    ");
        for (trial_address = 0; trial_address < 16; ++trial_address)
        {
            if (addrs[trial_address] == 1) Wputf("%d, ", trial_address);
        }
        Wputs("\n");
        KwGetFmt(ss, "'\n\n   Select an Address to Use: '!R!#!R!#");
        i1 = atoi(ss);
        if ((addrs[i1] == 0) || (i1 > 15))
        {
            /*  bad address. */
            Wclear();
            Wputs("\n\n     Bad address selected.\n");
            Wputs("\n\n     Hit any key to resume.");
            KeyWait();
        }
        else break;
    }

    polling_addr = (byte)i1;

    /*  re-parse the receive info for the selected polling address.  */

    /*  first get back receive data for this address.  */
    rec_bytes = cmd0_rec_bytes[polling_addr];
    for (i=0; i<50; ++i)
    {
        receive[i] = cmd0_receive[polling_addr][i];
    }

    k = rec_bytes/256;

    /* find start delimiter.  */
    r = receive;
    for (j=1; j <= k; ++j) if ((strncmp(r++, start_delim, 3)) == 0) break;

    /* go to start delim.  */
    j += 2; ++r;

    ++j; ++r;       /* advance to address. */

    /*  advance to command.  */
    ++j; ++r;

    ++j; ++r;   /*  advance to byte count.  */

    ++j;  ++r;  /*  skip to status. */
    status1 = *r++; ++j;     /*  save it.  */
    status2 = *r++; ++j;

    /*  now pointing at 1st byte of receive data. */
    a = addr;   /*  set pointer. */

    /*  finish the message. */

    r += 3;         /*  point to numb preambles. */
    *a++ = *r++;    /*  get numb preambles. */
    r -= 3;         /*  point to MFR ID. */
    *a++ = *r++;    /*  get MFR ID (1st byte of addr). */
    *a++ = *r++;    /*  get MFR DEVICE TYPE (2nd byte). */

    r += 6;     /*  point to DEVICE ID MSB (3rd addr byte).  */
    *a++ = *r++;    /*  get 3 bytes */
    *a++ = *r++;
    *a++ = *r++;

    r -= 6;         /*  back up to software rev of dev. */
    *a++ = *r++;    /*  get software rev of device. */
    *a++ = *r++;    /*  get hardware rev of device. */
    *a = *r;        /*  get flags of device.  */

    Wclose();
    WVprtf(status_wind, 0,62, BLACK|_GREEN, "     ");
    WVprtf(status_wind, 0,64, BLACK|_GREEN, "%d", polling_addr);
    found = 1;
}

