// 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;


//Get Values from DAQ
dpGet("ChangeState.parameters:_original.._value", sParameter);
//DebugN("sParameter", 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 OFF");
      dpSet("Info.Name:_original.._value", "CS: Setting PS channels OFF");
      DebugN("CS: Setting PS channels OFF");
      PSset2(iState);
      //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);
      //dpSet("Info.Name:_original.._value", "CS: Finished!");
      dpSet("ChangeState.:_original.._value",0);
   }
   //Read();
  dpSetWait("ChangeState.response:_original.._value",0);
  delay(3);
  dpSetWait("ChangeState.trigger:_original.._value", 0);
  }

}


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

//DebugN("iState ",iState);

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

}

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+100;
delay(0,10);
dpSetWait("Crate00.Controller.CCchRead.Send:_original.._value", channel);
delay(0,10);
  }
}
