// 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 main");
dpConnect("CheckState","Controller.Set.Send:_original.._value");
}

void CheckState() 
{
int iState;
string sTask;

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

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

//Change State
if (iState==0) {
   dpSet("RST.Data:_original.._value",0);
   DebugN("if", "0");
   dpSet("Info.Data:_original.._value", "Setting LV channels...");
   LVset();
   dpSet("Info.Data:_original.._value", "Setting HV channels...");
   HVset();
   dpSet("Info.Data:_original.._value", "Finished!");
   }
else {
   dpSet("RST.Data:_original.._value",1);
   DebugN("else",  "1");
   }

}


//Function setting the LV parameters
LVset()
{
dyn_errClass err;
int  channel, i,icard,lv;
int set;


for (i=0;i<=31;i++){


channel=i+100;
dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", channel);
delay(0,50);
dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", i);
}


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

//Function setting the HV parameters
HVset()
{
dyn_errClass err;
int  i,channel,j;
int set;

dpSetWait("RST.Data:_original.._value", 1);
for (i=0;i<=31;i++){
  channel=i+100;
  delay(0,50);
  dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", channel);
}

for(i=4;i<8;i++){
  for (j=i;j<=35;j=j+4){

    delay(0,100);
    dpSetWait("Crate00.Controller.CCch_Set.Send:_original.._value", j);
 
  // DebugN(j);
  }
}
  err = getLastError();
  if (dynlen(err) > 0)
    errorDialog(err);
 
}
