 
/*  selpv.c
    for use by tm40.c
    Stephen D. Anderson --- October 29, 1994

    In multi-variable field instrument, tells field instrum which variable 
    to use as the primary variable by rotating the variables.  Does the
    following:

    Checks whether field instrum exists, searches if not.

    Puts up a window explaining what will happen if user proceeds.  That
    is, the variables will be rotated.  Give user the option of quitting
    if this is not what he/she intended.

    If user goes ahead, HART command 50 is sent to get current variable
    assignments.  If there are errors, these are reported and there is
    a return to calling program.  Variable "status1" is saved as 
    "old_status1".  If no error, the variables are rotated.

    The rotated variables are written back to the field instrument.  If
    errors, they are reported and there is a return to calling program.
    Both "status1" and "old_status1" are checked to see whether error.

    Returns to calling program.


    ON ENTRY:

    ON EXIT:

    REVISION LOG:


*/

#include "tm40.h"

void selpv()
{
    extern byte found;
    extern byte search(void);
    extern int hartcmd(byte *, byte, byte *, byte, byte *, byte);
    extern void err(byte);
    extern void err1(byte);
    extern byte addr[];
    extern byte status1,status2;

    int i,k;
    byte s[20];
    byte old_status1;

    /*  need field instr info? */
    if (!found) found = search();
    if (!found) return;

    /*  proceed with function. */
    Wopen(2,15,15,65,2,WHITE|_BLUE,WHITE|_BLUE);
    Wputs("\n\n   This function will try to rotate the");
    Wputs("\n    process variable assignments within");
    Wputs("\n    the field instrument so that:");
    Wputs("\n\n       2nd becomes Primary");
    Wputs("\n       3rd becomes 2nd");
    Wputs("\n       4th becomes 3rd");
    Wputs("\n       Primary becomes 4th\n\n");
    KwGetFmt(s, "'     Is this acceptable (y or n)? '!UR!<yn>");

    /*  quit?  */
    if (TcxlErr == W_ESCPRESS)
    {
        Wclose(); return;
    }

    if (s[0] == 'N')
    {
        Wclear();
        WputCen("\n\n\nNo Changes Made in Variable Assignments");
        WputCen("\n\n\nHit any key to Resume");
        KeyWait(); Wclose(); return;
    }

    /*  first read the variable assignments.  */
    Wclear();
    WputCen("\n\nGetting Field Instrum");
    WputCen("\nVariable Assignments...");
    i = hartcmd(s, 0, s, 4, addr, 50);

    if ((i & 1) != 0)
    {
        /*  command not executed.  */
        delay(1000); err1(50); Wclose(); return;
    }

    /*  get the number of variables.  */
    k = i/256;

    /*  check on number.  */
    if ((k < 1) || (k > 4))
    {
        /*  quit if incorrect number of bytes returned. */
        err(50);
        Wclose(); return;
    }

    /*  report any errors. */
    if (status1 != 0) err(50);
    old_status1 = status1;

    /*  perform the rotation.  */
    s[k] = s[0];
    for (i=0; i<k; ++i) s[i] = s[i+1];

    /*  now write the assignment back to the field instrum.  */
    Wclear();
    WputCen("\n\nWriting New Field Instrum");
    WputCen("\nVariable Assignments...");
    i = hartcmd(s, k, s, 4, addr, 51);

    if ((i & 1) != 0)
    {
        /*  command not executed.  */
        delay(1000); err1(51); Wclose(); return;
    }

    k = i/256;

    /*  check number bytes returned.  */
    if ((k < 1) || (k > 4))
    {
        err(51);
        Wclose(); return;
    }

    /*  report success or error.  */
    /*  check status1.  */
    if (status1 != 0) err(51);
    Wclear();
    if ( (status1 != 0) || (old_status1 != 0) )
    {
        /*  error.  */
        WputCen("\n\nVariables may not have");
        WputCen("been rotated.");
    }
    else
    {
        WputCen("\n\nVariables Successfully");
        WputCen("\nRotated.");
    }
    WputCen("\n\nHit any key to resume.");
    KeyWait(); Wclose();
}

