// 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
   if (iState==0) {
      //DebugN("state is OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels OFF");
      DebugN("CS: Setting PS channels OFF");
      PSset2(iState,channel,crate);
      //dpSet("Info.Name:_original.._value", "CS: Finished!");
      dpSet("changeState.trigger:_original.._value",0);
   }
   if (iState==1){
      //DebugN("state is ON");
      //dpSet("RST.Data:_original.._value",1);
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels ON");
      DebugN("CS: Setting PS channels ON");
      PSset2(iState,channel,crate);
      //dpSet("Info.Name:_original.._value", "CS: Finished!");
      dpSet("changeState.trigger:_original.._value",0);
   }
   if (iState==2){
      //DebugN("state is STANDBY");
      //dpSet("RST.Data:_original.._value",2);
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels STANDBY");
      DebugN("CS: Setting PS channels STANDBY");
      PSset2(iState,channel,crate);
      //dpSet("Info.Name:_original.._value", "CS: Finished!");
      dpSet("changeState.trigger:_original.._value",0);
   }
   //Read();
  delay(3);
  dpSetWait("changeState.trigger:_original.._value", 0);
  dpSetWait("changeState.response:_original.._value",0);
  }

}


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



 sprintf(datapoint,"Crate%02d.Controller.CCch_Set.Send:_original.._value",crate);
// DebugN("datapoint",datapoint);
 if(channel==255){
 channel = channel | (( iState & 0x00ff )<<8);
 dpSetWait(datapoint,channel);
 }
 else if(channel<48){
 // Ramp up one channel, i.e LV and HV
 channel = channel | (( iState & 0x00ff )<<8);
 dpSetWait(datapoint,channel);
 delay(0,500);
 dpSetWait(datapoint,channel+100);
}


}


