// 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.2003 - Preliminary - H. Sandaker
//                   26.11.2003 - midofications for Finite State Machine P.Ferrari
//===================================-======================================*/

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


}

void changeState(string Text, bool Flag) 
{
int iState,crate,channel,timeout;
string allChannels;
string sParameter;
dyn_string para_split;



//Get Values from DAQ
dpGet("changeState.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|Channel|State
 
sscanf(para_split[1],"%d", crate);
sscanf(para_split[2],"%s", allChannels);
sscanf(para_split[3],"%d", iState);
sscanf(para_split[4],"%d", timeout);

if(allChannels == "all")
  {
   //IV curve for all channels
   channel = 255;
   }
   else
   {
   sscanf(allChannels,"%d", channel);
   }

 //DebugN("iState", iState,"crate",crate,"channel",channel);

if (Flag==1){

   //Change State

      //DebugN("state is OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels to new state...");
      DebugN("CS: Setting PS channels to new state...");
      PSset2(iState,channel,crate);
      //dpSet("Info.Name:_original.._value", "CS: Finished!");
   //Read();
  delay(3);
  dpSetWait("changeState.trigger:_original.._value", 0);
  }
}


PSset2(int iState, int channel, int crate)
{
dyn_errClass err;
int  i,j,ichan;
int set;
string datapoint,datapoint2;
int ack,OK;


 sprintf(datapoint,"Crate%02d.Controller.CCch_Set.Send:_original.._value",crate);
 sprintf(datapoint2,"Crate%02d.Controller.CCcf_Ack.Recv:_online.._value",crate);
// DebugN("datapoint",datapoint);
 if(channel==255){
 if(iState==1)
   {
   OK=0;
   channel = 194 | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     //DebugN("on hv ",i,ack,channel);
     }
   i=0;
   if( ack==channel) OK=1; 
   //DebugN("on hv",OK,ack); 
   // send back confirmation to DAQ
   if(OK==1) dpSetWait("changeState.response:_original.._value",0);
   }
 if(iState==2)
   {
   OK=0;
   // ramp to STB1
   channel = 128 | (( iState & 0x00ff )<<8); 
   dpSetWait(datapoint,channel); 
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel) OK=1;	
   channel = 192 | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if(ack==channel&&OK==1) OK=2;
   // send back confirmation to DAQ
   if(OK==2)
   dpSetWait("changeState.response:_original.._value",0);
   // wait for DAQ CONFIGURING MODULES ( not yet implemented)
   //
   // ramp the LV's to ON 
   channel = 130 | (( 1 & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   // check that Ewa has done the transition
     dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   if(ack==channel&&OK==2) OK=3;
   i=0;
   // send back confirmation to DAQ
   if(OK==3)
   dpSetWait("changeState.response:_original.._value",0);
   }
   if(iState==0)
   {
   channel = 255 | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel) OK=1; 
   // send back confirmation to DAQ
   if(OK==1) dpSetWait("changeState.response:_original.._value",0);
   }
 if(iState==4)
   {
   iState=2;
   channel = 193 | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<20))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel) OK=1; 
      // send back confirmation to DAQ
   if(OK==1) dpSetWait("changeState.response:_original.._value",0);
   }


 }
 else if(channel<48){
 // Ramp up one channel, i.e LV and HV
 if(iState==1)
   {
   OK=0;
   channel = channel | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel) OK=1; 
   DebugN("ack0",ack,OK,channel);
   dpSetWait(datapoint,channel+64);   
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);
   while ((ack!=channel+64)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel+64&&OK==1) OK=2; 
   // send back confirmation to DAQ
   DebugN("ack1",ack,OK,channel+64);
   if(OK==2) dpSetWait("changeState.response:_original.._value",0);
   }


if(iState==2)
   {
   OK=0;
   // ramp to STB1
   ichan=channel;
   channel = channel | (( iState & 0x00ff )<<8); 
   dpSetWait(datapoint,channel); 
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel) OK=1;	
   DebugN("acklv0",ack,OK,channel);
   dpSetWait(datapoint,channel+64);
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);

   while ((ack!=channel+64)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if(ack==channel+64&&OK==1) OK=2;
   DebugN("ackhv",ack,OK,channel+64);
   // send back confirmation to DAQ
   if(OK==2)
   dpSetWait("changeState.response:_original.._value",0);
   // wait for DAQ CONFIGURING MODULES ( not yet implemented)
   //
   // ramp the LV's to ON 
   channel = ichan | (( 1 & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if(ack==channel&&OK==2) OK=3;
   DebugN("acklv1",ack,OK,channel);
   // send back confirmation to DAQ
   if(OK==3)
   dpSetWait("changeState.response:_original.._value",0);
   }

   if(iState==0)
   {
   channel = channel | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     DebugN("ack",ack);
     } 
   i=0;
   DebugN("acklv",ack,OK,channel);
   if( ack==channel) OK=1; 
   dpSetWait(datapoint,channel+64);   
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);
   while ((ack!=channel+64)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     } 
   i=0;
   if( ack==channel+64&&OK==1) OK=2; 
   // send back confirmation to DAQ
   DebugN("ackhv",ack,OK,channel+64);
   if(OK==2) dpSetWait("changeState.response:_original.._value",0);
   }

 if(iState==4)
   {
   iState=2;
   channel = channel | (( iState & 0x00ff )<<8);
   dpSetWait(datapoint,channel);
   dpGet(datapoint2,ack);
   while ((ack!=channel)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     DebugN("ack",ack);
     } 
   i=0;
   if( ack==channel) OK=1; 
   DebugN("acklv",ack,OK);
   dpSetWait(datapoint,channel+64);
   // check that Ewa has done the transition
   dpGet(datapoint2,ack);
   while ((ack!=channel+64)&&(i<10))
     {
     delay(1,0);
     i=i+1;
     dpGet(datapoint2,ack);
     }
   i=0;
   if( ack==channel+64&&OK==1) OK=2; 
   DebugN("ackhv",ack,OK,channel+64);
   // send back confirmation to DAQ
   if(OK==2) dpSetWait("changeState.response:_original.._value",0);
   }
  }
}

Read() 
{
int i,icard, channel, statusLV, statusHV,statusLVCard;
string datapoint, datapoint2,datapoint3;
for (i=0; i<=47; i++){
dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value",i);
channel=i+64;
delay(0,10);
dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value", channel);
delay(0,10);
  }
}
