// 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:  25.07.2003 - first version
//                   30.07.3003 - Preliminary - H. Sandaker
//===================================-======================================*/

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


}

void ChangeState(string Text, bool Flag) 
{
int iState;
string sParameter;

DebugN("sParameter", sParameter);

//Get Values from DAQ
dpGet("ChangeState.parameters:_original.._value", sParameter);

//Split string and get status:
//String on form: status  
sscanf(sParameter,"%d", iState);
DebugN("iState", iState);

if (Flag==1){

   //Change State
   if (iState==0) {
      DebugN("state is 0");
      dpSet("Info.Name:_original.._value", "Setting PS channels...");
      DebugN("Setting PS channels...");
      PSset2(iState);
      dpSet("Info.Name:_original.._value", "Finished!");
      dpSet("ChangeState.trigger:_original.._value",0);
   }
   if (iState==1){
      DebugN("state is 1");
      //dpSet("RST.Data:_original.._value",1);
      dpSet("Info.Name:_original.._value", "Setting PS channels...");
      DebugN("Setting PS channels...");
      PSset2(iState);
      dpSet("Info.Name:_original.._value", "Finished!");
      dpSet("ChangeState.trigger:_original.._value",0);
   }
   //Read();
  dpSetWait("ChangeState.response:_original.._value",0);
  dpSetWait("ChangeState.trigger:_original.._value", 0);
  }

}



//Function setting the HV parameters
PSset(int iState)
{
dyn_errClass err;
int  i,channel, channel2,j;
int set;
DebugN("RST ",iState);

for (i=0;i<=47;i++){
channel=i+100;
channel = channel | (( iState & 0x00ff )<<8);
  delay(0,50);
  dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", channel);
}

for(i=0;i<4;i++){
  for (j=i;j<=47;j=j+4){
    delay(0,100);
    channel2 = j | (( iState & 0x00ff )<<8);
    dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", channel2);
 
  DebugN(j);
  }
}
  err = getLastError();
  if (dynlen(err) > 0)
    errorDialog(err);
 
}

PSset2(int iState)
{
dyn_errClass err;
int  i,channel,j;
int set;

DebugN("iState ",iState);

channel = 255 | (( iState & 0x00ff )<<8);
  dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", channel);
  

}

Read() {

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

delay(10);
for (i=0; i<=47; i++){
dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value",i);
channel=i+100;
dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value", channel);

sprintf(datapoint, "Crate00.Channel%02d.LVchStat.Recv:_original.._value", i);
sprintf(datapoint2, "Crate00.Channel%02d.HVchStat.Recv:_original.._value", i);
icard=i/4;
sprintf(datapoint3, "Crate00.LV_card%02d.LVcaStat.Recv:_original.._value", icard);

dpGet(datapoint, statusLV);
dpGet(datapoint2, statusHV);
dpGet(datapoint3, statusLVCard);
DebugN("channel", i, "LV=", statusLV, "HV=", statusHV, "LV Card",statusLVCard);
  }
}