// 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
//                   01.08.2003 - startup of IV curve implemented- P.Ferrari
//===================================-======================================*/

main()
{ 
//DebugN("Started main");
//DebugN("========================================1");
dpConnect("RunIV","IVcurve.trigger:_original.._value");
//DebugN("========================================5");
}

void RunIV(string Text, bool Flag) 
{
int iState;
string sParameter;
int channel,start,stop,step,end,Imax,Iwarn,Twait;
string status, channelAddress;
//DebugN("========================================2");


if (Flag==1){
   DebugN("IVcurve trigger", Flag);
   
	//Get Values from DAQ
	dpGet("IVcurve.parameters:_original.._value", sParameter);
	DebugN("===================== sParameter = ", sParameter);
	
   //Split string and get IV parameters:
   //String on form: channel | start | stop | step | end | Imax | Iwarn | Twait  
   sscanf(sParameter,"%d|%d|%d|%d|%d|%d|%d|%d", channel,start,stop,step,end,Imax,Iwarn,Twait );
   DebugN("sParameters", channel,start,stop,step,end,Imax,Iwarn,Twait);

//DebugN("========================================3");
   sprintf(channelAddress, "Crate00.Channel%02d", channel);
   

	//DebugN("======================== channelAddress = ", channelAddress);


   dpSet(channelAddress+".IVCurve.HV_Start.Send:_original.._value",start);
   dpSet(channelAddress+".IVCurve.HV_Stop.Send:_original.._value",stop);
   dpSet(channelAddress+".IVCurve.HV_Step.Send:_original.._value",step);
   dpSet(channelAddress+".IVCurve.HV_End.Send:_original.._value",end);
   dpSet(channelAddress+".IVCurve.Curr_Max.Send:_original.._value",Imax);
   dpSet(channelAddress+".IVCurve.Curr_Warn.Send:_original.._value",Iwarn);
   dpSet(channelAddress+".IVCurve.TimeWait.Send:_original.._value",Twait);
   
   //set HV channel (+100) on which IV curve should run
   DebugN("Channel",channel);
   dpSet("Crate00.Controller.CCchRead.Send:_original.._value",channel+100);  
 
   // set datapoint to trigger IV Curve

   dpSet("Crate00.Controller.StartIVCurve.Send:_original.._value",1);

   dpSet("Info.Name:_original.._value", "IV scan has started!");

   //set DAQ trigger to FALSE again
   dpSet("IVcurve.response:_original.._value",0);
   dpSet("IVcurve.trigger:_original.._value",0);

   // set back the IV trigger in the ELMB to 0
    dpSet("Crate00.Controller.StartIVCurve.Send:_original.._value",0);
   //get status
     sprintf(status,"SCTmops1:Crate00.Channel%02d.IVCurve.Status.Recv:_original.._value",channel);	
      //DebugN(" IV curve dpconnect",status);
      dpConnect("EP_textFieldInCB1",status);  	
   }
//DebugN("========================================4");
}

EP_textFieldInCB1(string dp,int iNewValue)
{ 

//DebugN(" IV curve status!!", iNewValue);
if(iNewValue==255){
// the IV curve is started
dpSet("SCTmops1:Info.Name:_original.._value","IV curve is started");
//DebugN(" IV curve started!!", iNewValue);
}
if(iNewValue==251){
dpSet("SCTmops1:Info.Name:_original.._value","IV curve is stopped");
//DebugN(" IV curve stopped!!", iNewValue);
}
if((iNewValue>0)&&(iNewValue<=150)){
// the IV curve is running
dpSet("SCTmops1:Info.Name:_original.._value","IV curve is running");
//DebugN(" IV curve running!!", iNewValue);
}
if((iNewValue==0)){
// normal readout
dpSet("SCTmops1:Info.Name:_original.._value","Normal readout");
//DebugN(" Normal readout!!", iNewValue);
}

}
	
 
/* //Set Datapoints
   sprintf(dp1, "IVchannel%d.Start:_original.._value",channel);
   sprintf(dp2, "IVchannel%d.Stop:_original.._value",channel);
   sprintf(dp3, "IVchannel%d.Step:_original.._value",channel);
   sprintf(dp4, "IVchannel%d.End:_original.._value",channel);
   sprintf(dp5, "IVchannel%d.Imax:_original.._value",channel);
   sprintf(dp6, "IVchannel%d.Iwarn:_original.._value",channel);
   sprintf(dp7, "IVchannel%d.Twait:_original.._value",channel);


   dpSet(dp1,start);
   dpSet(dp2,stop);
   dpSet(dp3,step);
   dpSet(dp4,end);
   dpSet(dp5,Imax);
   dpSet(dp6,Iwarn);
   dpSet(dp7,Twait);
*/
