// DDC control 
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         DDC STATE & DATA CONTROL                         */
//-----------------------------------=--------------------------------------*/
//   AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs:  Run state transitions
//
//   MODIF_HISTORY:  17.11.2003 - first version - H. Sandaker
//                   27.11.2003 - modify it according to changes in runstate 
//					    DDC commands P.Ferrari
//                   07.01.2004 - Adapt to change of DDC command
//                   
//===================================-======================================*/

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


void loadConfiguration(string Text, bool Flag) 
{
int iRST,skip,timeout;
int crate,channel;
string allChannels;
string sParameter;
dyn_string para_split;

//Get Values from DAQ
dpGet("loadConfiguration.parameters:_original.._value", sParameter);
para_split=strsplit(sParameter,"|");
//DebugN("sParameter", sParameter);

//Split string and get status. The format of the parameters send by DDC is Crate|State|timeout
 
sscanf(para_split[1],"%d", crate);
// Load OFF parameters OFF=> iRST=0, ON=> iRST=1
sscanf(para_split[2],"%d", iRST);
sscanf(para_split[3],"%d", timeout);
//DebugN("NEW",crate,iRST);
if (Flag==1)
   {
   dpSet("Info.Name:_original.._value", "CS: Loading new configuration to CC");
   DebugN("CS: Loading new configuration to CC");
        
   LoadConfig(iRST); // Load configuration into ELMB
        // change the values in the PVSS datapoints to be sure that we are reading back the 
        // configuration in the ELMB
   // change(iRST);       
   // TestConfig(iRST); // Test configuration in ELMB
   dpSet("loadConfiguration.trigger:_original.._value",0);
   skip=1;
   if(skip!=1) 
     {
     dpSetWait("loadConfiguration.response:_original.._value",0);
     dpSetWait("loadConfiguration.trigger:_original.._value", 0);
     }
   }
}


LoadConfig(int iRST)
{
dyn_errClass err;
int  k;
int iStoreEnd;

//Tell crate controller what datapoints to store 0 = off:

dpSetWait("Crate00.Controller.CCcf_RST.Send:_original.._value", iRST);
   dpSet("Info.Name:_original.._value", "CS: Setting CC in ", iRST, "mode");
   DebugN("CS: Setting CC in", iRST," mode");

//Trigger loading of OFF parameters (0) from file to PVSS 
// SDO's are sendt

Test(iRST);

//When finished tell crate controller to save these values as off 
//parameters:
  
dpSetWait("Crate00.Controller.CCcf_Set.Send:_original.._value", 1702257011);
   dpSet("Info.Name:_original.._value", "CS: Saving parameters to CC memory");
   DebugN("CS: Saving parameters to CC memory");  

// Read the save to check if storing values has finished
delay(120,0);
dpGet("Crate00.Controller.CCcf_RST.Recv:_original.._value", iStoreEnd); 
DebugN("RST",iStoreEnd);
if (iStoreEnd == 255){
    dpSet("Info.Name:_original.._value", "CS: Finished saving parameters to CC memory");
    DebugN("CS: Finished saving parameters to CC memory");   
    }
else {
    dpSet("Info.Name:_original.._value", "CS: Stopped - Timeout");
    DebugN("RST",iStoreEnd);     
    } 
}

Test(int iRST) {

// Function to simulate the Loading of values from file to PVSS

int icard, channel, statusLV, statusHV,statusLVCard;
string datapoint1, datapoint2,datapoint3; 
dyn_errClass err;
int i, j, l;
dyn_string valsetLV, valsetHV; 
dyn_float initValueLV, initValueHV;

if (iRST == 0) { 

   
   }
if (iRST == 1) { 
   initValueLV = makeDynFloat(46.5,15,3.7,4,4,6,50,50,50,60,0,0,1);
   initValueHV = makeDynFloat(250,1100,3);
   }
if (iRST == 2) { 
   initValueLV = makeDynFloat(46.5,15,2.5,4,4,6,50,50,50,60,0,0,1);
   initValueHV = makeDynFloat(20,1100,3);
   }

valsetLV = makeDynString("LVcaTrip","LVcaMask","LVch_Vcc","LVch_Vdd",
                         "LVchVCSV","LVchPINV","LVchDTrp","LVchCTrp",
                         "LVchTTrp","LVchTLim","LVchCLKS","LVchCLKR",
                         "LVchVOut");
valsetHV = makeDynString("HVchVolt","HVchITrp","HVchRamp");


// Loading off values for the parameters: 
 for (l=0; l<=47; l++){
     for (i=1; i<=3; i++) {
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetHV[i] );
         dpSetWait(datapoint1, initValueHV[i]);     
         DebugN(" HV channel", l, valsetHV[i], " = ",initValueHV[i]);
         }
         
     for (j=3;j<=13;j++){
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetLV[j] );
         dpSetWait(datapoint1, initValueLV[j]);
         //DebugN(" LV channel", l, valsetLV[j], " = ",initValueLV[j]);
         }

     }
 
  err = getLastError();
  if (dynlen(err) > 0)
    errorDialog(err);       
 
}

change(int iRST) {

// Function to change the values in PVSS datapoints to make sure we are reloading
//  the values from ELMB

int icard, channel, statusLV, statusHV,statusLVCard;
string datapoint1, datapoint2,datapoint3; 
dyn_errClass err;
int i, j, l;
dyn_string valsetLV, valsetHV; 
dyn_float initValueLV, initValueHV;

if (iRST == 0) { 
   initValueLV = makeDynFloat(0,0,0,0,0,0,0,0,0,0,0,0,0);
   initValueHV = makeDynFloat(0,0,0);
   }
if (iRST == 1) { 
   initValueLV = makeDynFloat(0,0,0,0,0,0,0,0,0,0,0,0,0);
   initValueHV = makeDynFloat(0,0,0);
   }
if (iRST == 2) { 
   initValueLV = makeDynFloat(0,0,0,0,0,0,0,0,0,0,0,0,0);
   initValueHV = makeDynFloat(0,0,0);
   }

valsetLV = makeDynString("LVcaTrip","LVcaMask","LVch_Vcc","LVch_Vdd",
                         "LVchVCSV","LVchPINV","LVchDTrp","LVchCTrp",
                         "LVchTTrp","LVchTLim","LVchCLKS","LVchCLKR",
                         "LVchVOut");
valsetHV = makeDynString("HVchVolt","HVchITrp","HVchRamp");


// Loading off values for the parameters: 
 for (l=0; l<=47; l++){
DebugN("Changing PVSS datapoints values for cross check");
     for (i=1; i<=3; i++) {
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetHV[i] );
         dpSetWait(datapoint1, initValueHV[i]);     
         }
         
     for (j=3;j<=13;j++){
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetLV[j] );
         dpSetWait(datapoint1, initValueLV[j]);
         }

     }
 
  err = getLastError();
  if (dynlen(err) > 0)
    errorDialog(err);       
 
}


TestConfig(int iRST) {

dyn_errClass err;
int  k;
int iStoreEnd;

//Tell crate controller what datapoints to read 0 = off:

dpSetWait("Crate00.Controller.CCcf_RST.Send:_original.._value", iRST);
   dpSet("Info.Name:_original.._value", "CS: chosing to read parameters from CC");
   DebugN("CS: chosing to read parameters from CC");

//Tell crate controller to send off parameters back to PVSS:
  
dpSetWait("Crate00.Controller.CCcf_Set.Send:_original.._value", 1684107116);
   dpSet("Info.Name:_original.._value", "CS: Reading parameters from CC memory...");
   DebugN("CS: Reading parameters from CC memory...");  

delay(10);
//Trigger reading of OFF parameters (0) from file to PVSS 
// SDO's are sendt

Read();
   dpSet("Info.Name:_original.._value", "CS: Reading parameters from CC memory");
   DebugN("CS: Reading parameters from CC memory");  
   
// check if readed values has finished
delay(10,0);

     dpGet("Crate00.Controller.CCcf_RST.Recv:_original.._value", iStoreEnd); 
     if (iStoreEnd = 255){
        dpSet("Info.Name:_original.._value", "CS: Finished reading parameters to CC memory");
        DebugN("CS: Finished reading parameters to CC memory");   
        }
     else {
        dpSet("Info.Name:_original.._value", "CS: Stopped - Timeout");
        DebugN("CS: Stopped - Timeout");     
     } 
// check if values are ok:
   Check(iRST);

}

Read() {

int i,icard, channel;
delay(0,10);

DebugN("CS: Reading Values"); 
dpSet("Info.Name:_original.._value", "CS: Reading values...");
 
for (i=0; i<=47; i++){
    dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value",i);
    channel=i+100;
    delay(0,10);
    dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value", channel);
    delay(0,10);
    }
}


Check (int iRST) {
string datapoint1; 
dyn_errClass err;
int i, j, l;
float abs,x;
dyn_string valsetLV, valsetHV; 
dyn_float initValueLV, initValueHV, readValueLV, readValueHV;
int HV, LV;

valsetLV = makeDynString("LVcaTrip","LVcaMask","LVch_Vcc","LVch_Vdd",
                         "LVchVCSV","LVchPINV","LVchDTrp","LVchCTrp",
                         "LVchTTrp","LVchTLim","LVchCLKS","LVchCLKR",
                         "LVchVOut");
valsetHV = makeDynString("HVchVolt","HVchITrp","HVchRamp");

     if (iRST == 0) { 
        initValueLV = makeDynFloat(46,15,0,0,0,0,50,50,50,60,0,0,0);
        initValueHV = makeDynFloat(0,1100,3);
        }
     if (iRST == 1) { 
        initValueLV = makeDynFloat(46.5,15,3.5,4,4,6,50,50,50,60,0,0,1);
        initValueHV = makeDynFloat(300,1100,3);
        }
     if (iRST == 2) { 
        initValueLV = makeDynFloat(46.5,15,2.0,4,4,6,50,50,50,60,0,0,1);
        initValueHV = makeDynFloat(20,1100,3);
        }


// Reading values for the parameters: 
 for (l=0; l<=47; l++){
     HV=0;
     LV=0;
     for (i=1; i<=3; i++) {
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetHV[i] );
         dpGet(datapoint1, readValueHV[i]);
         DebugN(" read value Hv channel= ",l," is ",readValueHV[i], "set value is =", initValueHV[i]);
         x=readValueHV[i] - initValueHV[i];
	   abs=(x<0) ? -x : x; // abs= abs(x)
	   //DebugN( "absolute value", abs);
         if (abs <0.01) {
            HV = HV +1; 
            }     
         }        
     for (j=3;j<=13;j++){
         sprintf(datapoint1, "Crate00.Channel%02d.%s.Send:_original.._value",l,valsetLV[j] );
         dpGet(datapoint1, readValueLV[j]);
         DebugN(" read value LV channel= ",l," is ",readValueLV[j], "set value is =", initValueLV[j]);
         x=readValueLV[j] - initValueLV[j];
	   abs=(x<0) ? -x : x; // abs= abs(x)
         if (abs <0.01) {
            LV = LV +1; 
            } 
         }
     if (HV == 3 && LV == 11) {     
        DebugN("channel ok:", l);
        }
     else {
        DebugN("channel bad:", l);
        }
     }
        
}
