program AutoTest

/* Assign all PV links and get them connected */

// Flag that tells the tester to start
uint8_t start;
assign start to "{P}{S}test_start";
monitor start;

// General information I may want for some reason
uint8_t boardID;
assign boardID to "{P}{S}boardID";
monitor boardID;

uint8_t nbr_BO;
assign nbr_BO to "{P}{S}nbr_bo";
monitor nbr_BO;

uint8_t nbr_BI;
assign nbr_BI to "{P}{S}nbr_bi";
monitor nbr_BI;

// Global control and status for the card
uint8_t AlarmLtch;
assign AlarmLtch to "{P}{S}ltch_alrm_status";
monitor AlarmLtch;

uint8_t AlarmReset;
assign AlarmReset to "{P}{S}rst_ltch";


// These are all the MASK commands to the card
uint8_t masks[9];
assign masks[8] to "{P}{S}mask_8";
assign masks[7] to "{P}{S}mask_7";
assign masks[6] to "{P}{S}mask_6";
assign masks[5] to "{P}{S}mask_5";
assign masks[4] to "{P}{S}mask_4";
assign masks[3] to "{P}{S}mask_3";
assign masks[2] to "{P}{S}mask_2";
assign masks[1] to "{P}{S}mask_1";

// Here are all the Channel Inputs to the card
uint8_t lupinInputs[9];
assign lupinInputs[8] to "{P}{S}in_8";
assign lupinInputs[7] to "{P}{S}in_7";
assign lupinInputs[6] to "{P}{S}in_6";
assign lupinInputs[5] to "{P}{S}in_5";
assign lupinInputs[4] to "{P}{S}in_4";
assign lupinInputs[3] to "{P}{S}in_3";
assign lupinInputs[2] to "{P}{S}in_2";
assign lupinInputs[1] to "{P}{S}in_1";

// Capture all the input channels now
uint8_t lupinOKs[9];
assign lupinOKs[8] to "{P}{S}ok_8";
assign lupinOKs[7] to "{P}{S}ok_7";
assign lupinOKs[6] to "{P}{S}ok_6";
assign lupinOKs[5] to "{P}{S}ok_5";
assign lupinOKs[4] to "{P}{S}ok_4";
assign lupinOKs[3] to "{P}{S}ok_3";
assign lupinOKs[2] to "{P}{S}ok_2";
assign lupinOKs[1] to "{P}{S}ok_1";


// Reset the Alarm Latch
void resetAlarmLatch()
{
    pvPut(AlarmReset,0);
    pvPut(AlarmReset,1);
}

// Starting on Test #1.  First, just turn all the channels OFF and then ON
ss Test_One
{
    uint8_t i;

    // First, go through and turn OFF all the masks and Lupin inputs
    state init
    {
        when ()
        {
            for(i = 8; i > 0; i--)
            {
                masks[i] = 0;
                pvPut(masks[i],SYNC);
            }

            for(i = 8; i > 0; i--)
            {
                lupinInputs[i] = 0;
                pvPut(lupinInputs[i],SYNC);
            }
        } state masks_on
    }

// Start of Test 1

// First, turn ON all the mask inputs
    state masks_on
    {
        when ()
        {
            for(i = 8; i > 0; i--)
            {
                masks[i] = 1;
                pvPut(masks[i],SYNC);
            }
        } state reset_latch
    }
    state reset_latch
    {
        when ()
        {
            resetAlarmLatch();
        } state inputs_off
    }

    // Now, I'm going to turn OFF all the inputs
    state inputs_off
    {
        when()
        {
            for(i = 8; i > 0; i--)
            {
                lupinInputs[i] = 0;
                pvPut(lupinInputs[i],SYNC);
            }
        } state inputs_on
    }

    // In here, check the latch to make sure not in error

    // Now, I'm going to turn ON all the inputs
    state inputs_on
    {
        when()
        {
            for(i = 8; i > 0; i--)
            {
                lupinInputs[i] = 1;
                pvPut(lupinInputs[i],SYNC);
            }
        } state masks_off
    }

    // Need to add a "check latch" here

    // Turn OFF all the MASKs now
    state masks_off
    {
        when ()
        {
            for(i = 8; i > 0; i--)
            {
                masks[i] = 0;
                pvPut(masks[i],SYNC);
            }
        } state masks_on
    }


    // Another check latch should follow here
}

