// DCS Testing
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         DDC STATE & DATA CONTROL                         */
//-----------------------------------=--------------------------------------*/
// AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs:  Long Term Testing 
//
//   MODIF_HISTORY:  30.10.2003 - first version H. Sandaker
//   
//===================================-======================================*/

main()
{ 
DebugN("Started ToggleState script");
dpConnect("ToggleState","ToggleState.trigger:_original.._value");
}


void ToggleState(string Text, bool Trigger) 
{
int iState;
string sParameter;
dyn_float fValues_H, fValues_L;

//Get Values from DAQ high or low
dpGet("ToggleState.parameters:_original.._value", sParameter);

if ((sParameter==0)|| (sParameter==1)) {      
// Get high and low values
   dpGet("LTT.Vcc.High:_original.._value", fValues_H[1],
         "LTT.Vdd.High:_original.._value", fValues_H[2],
         "LTT.VCSL.High:_original.._value", fValues_H[3],
         "LTT.PINV.High:_original.._value", fValues_H[4],
         "LTT.HV.High:_original.._value", fValues_H[5]);

   dpGet("LTT.Vcc.Low:_original.._value", fValues_L[1],
         "LTT.Vdd.Low:_original.._value", fValues_L[2],
         "LTT.VCSL.Low:_original.._value", fValues_L[3],
         "LTT.PINV.Low:_original.._value", fValues_L[4],
         "LTT.HV.Low:_original.._value", fValues_L[5]);
         DebugN("Got big parameters: ");
    }
      
if ((sParameter==2)|| (sParameter==3)) {      
// Get high and low values
   dpGet("LTT_small.Vcc.High:_original.._value", fValues_H[1],
         "LTT_small.Vdd.High:_original.._value", fValues_H[2],
         "LTT_small.VCSL.High:_original.._value", fValues_H[3],
         "LTT_small.PINV.High:_original.._value", fValues_H[4],
         "LTT_small.HV.High:_original.._value", fValues_H[5]);

   dpGet("LTT_small.Vcc.Low:_original.._value", fValues_L[1],
         "LTT_small.Vdd.Low:_original.._value", fValues_L[2],
         "LTT_small.VCSL.Low:_original.._value", fValues_L[3],
         "LTT_small.PINV.Low:_original.._value", fValues_L[4],
         "LTT_small.HV.Low:_original.._value", fValues_L[5]);
         DebugN("Got small parameters");
    }
//     DebugN("fValues High", fValues_H, "Vcc", fValues_H[1]);
//     DebugN("fValues Low", fValues_L);      

//String on form: status  
sscanf(sParameter,"%d", iState);

if (Trigger==1){

                            //DebugN("!!!!trigger taken");
   //Change State
   if ((iState==1) || (iState==3)) {
      dpSet("Info.Name:_original.._value", "LLT: Setting PS channels high...");
      DebugN("LLT: Setting PS channels high...", iState);
      LLTset(iState, fValues_H);
      delay(1,0);
      Sync();
      //Read();
//      dpSet("Info.Name:_original.._value", "LLT: Finished!");
 //     dpSet("ToggleState.trigger:_original.._value",0);
   }
   if ((iState==0) || (iState==2)){
      //dpSet("RST.Data:_original.._value",1);
      dpSet("Info.Name:_original.._value", "LLT: Setting PS channels low...");
      DebugN("LLT: Setting PS channels low...", iState);
      LLTset(iState, fValues_L);
      delay(1,000);
      Sync();
      //Read();
//      dpSet("Info.Name:_original.._value", "LLT: Finished!");
 //     dpSet("ToggleState.trigger:_original.._value",0);
   }
   //Read();
  dpSetWait("ToggleState.response:_original.._value",0);
  dpSetWait("ToggleState.trigger:_original.._value", 0);
  }

}

LLTset(int iState, dyn_float fValues)
{
dyn_errClass err;
int i, j, k, crate, channel, channel1, channel2, test, chHV, chLV;
string datapoint1, datapoint2, channelAdress, crateAddress, 
       channelAddress;
string systemName = getSystemName();

DebugN("Setting data... ");
dpSet("Info.Name:_original.._value", "LLT: Setting values...");

//Get crate number
dpGet("Crate.Numb:_online.._value", crate);

//Load Channel parameters 

for (i=0; i<=8; i++) {
// change for one card 
   for (j=i; j<=39; j=j+8) {
    // make address channel 0 - 7 - 15 etc 
    sprintf(channelAddress, "Crate%02d.Channel%02d", crate, j);
        // set values
        dpSet(systemName+channelAddress+".LVchVOut.Send:_original.._value", 1,
             systemName+channelAddress+".LVch_Vcc.Send:_original.._value", fValues[1],
             systemName+channelAddress+".LVch_Vdd.Send:_original.._value", fValues[2],
             systemName+channelAddress+".LVchVCSV.Send:_original.._value", fValues[3],
             systemName+channelAddress+".LVchPINV.Send:_original.._value", fValues[4],
             systemName+channelAddress+".HVchVolt.Send:_original.._value", fValues[5]);

//@     DebugN("j", j, "i", i, fValues[1]);
//DebugN("Sent value to channel:", i, fValues[1], fValues[2], fValues[3], fValues[4], fValues[5]);             
             //Debug something
//             dpGet(systemName+channelAddress+".LVch_Vcc.Send:_original.._value", test);
//             DebugN("Vcc sent value:", test);

delay(0,50);

    //setting HV
//    chHV = j + 64;
//    chHV = chHV | (( 3 & 0x00ff )<<8);
//    sprintf(datapoint1, "Crate%02d.Controller.CCch_Set.Send:_original.._value", crate);
//    dpSetWait(datapoint1, chHV);
 //   delay(0,100);
//DebugN("sending HV channel", i, chHV);
    
    // Setting LV    
//    chLV = j | (( 3 & 0x00ff )<<8);
//    dpSetWait(datapoint1, chLV);
//    delay(0,100); 
//DebugN("Sent value to channel:", j, fValues[1], fValues[2], fValues[3], fValues[4], fValues[5]);  
   }
   }
// Send values to Crates   

DebugN("Sending Values...");
dpSet("Info.Name:_original.._value", "LLT: Sending values...");

delay(0,100);

 chHV = 255 | (( 3 & 0x00ff )<<8);
 sprintf(datapoint1, "Crate%02d.Controller.CCch_Set.Send:_original.._value", crate);
 dpSetWait(datapoint1, chHV);
 
}  
  

 

// Read to check if it has been made correctly
Read() {

int i,icard, channel, statusLV, statusHV,statusLVCard, crate;
string datapoint, datapoint2,datapoint3;

delay(10);
DebugN("Reading Values..."); 
dpSet("Info.Name:_original.._value", "LLT: Reading values...");

//Get crate number
dpGet("Crate.Numb:_online.._value", crate);
 
for (i=0; i<=47; i++){
    sprintf(datapoint, "Crate%02d.Controller.CCchRead.Send:_original.._value", crate);
    dpSetWait(datapoint,i);
    channel=i+64;
    delay(0,10);
    dpSetWait(datapoint, channel);
    delay(0,10);
    }

}

Sync() {
dpSet(getSystemNAme()+"Bus00.BusSync.Send:_original.._value", 1);
}