// DDC control 
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         DDC STATE & DATA CONTROL                         */
//-----------------------------------=--------------------------------------*/
//   AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs: Hard Reset

//
//   MODIF_HISTORY:  
//                   08.01.2004 -  H.Sandaker P.Ferrari
//                   27.02.2204 -  P.W.Phillips
//                   
//===================================-======================================*/

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


void hardReset(string Text, bool Flag) 
{
int timeout, i, all, one;
int crate,channel;
string allChannels;
string sParameter,datapoint, datapoint1, datapoint2;
dyn_string para_split;

//Get Values from DAQ
dpGet("HardReset.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", timeout);

if (Flag==1)
   {
   dpSet("Info.Name:_original.._value", "CS: Hard Reset - Pulse Sent ...");
   DebugN("CS: Hard Reset - Pulse Sent");
   
   if (allChannels == "all")
      {
       //Reset for all channels
       for (i==0; i<=47; i++) {
           sprintf(datapoint,"Crate%02d.Channel%02d.LVchCLKR.Send:_original.._value",crate, i);
           dpSet(datapoint,1);
           }
           sprintf(datapoint1,"Crate%02d.Controller.CCch_Set.Send:_original.._value",crate);           
           all = 255| (( 3 & 0x00ff )<<8); 
           dpSet(datapoint1, all); 
           
       delay(0,100);
       DebugN("goes from one loop to another - all channels");
       
       for (i==0; i<=47; i++) {
           sprintf(datapoint,"Crate%02d.Channel%02d.LVchCLKR.Send:_original.._value",crate, i);
           dpSet(datapoint,0);  
           }
           dpSet(datapoint1, all);
       delay(0,100);
      }
   else
      {
       //Reset for one channel
       //Modifed to cycle Vdd before Hard Reset in response to DORIC problems at Oxford
       
       sscanf(allChannels,"%d", channel);
       
       sprintf(datapoint1, "Crate%02d.Controller.CCch_Set.Send:_original.._value",crate);
       one = channel | (( 3 & 0x00ff )<<8);
        
       sprintf(datapoint,"Crate%02d.Channel%02d.LVch_Vdd.Send:_original.._value",crate, channel);
       dpSet(datapoint,0.0); // set Vdd = 0
       dpSet(datapoint1,one); 
       delay(0,100);
       
       // TODO - return Vdd to original value from datapoint, rather than hardcoded 4.0V
       dpSet(datapoint,4.0); // set Vdd to 4.0
       dpSet(datapoint1,one); 
       delay(0,100);       

       sprintf(datapoint,"Crate%02d.Channel%02d.LVchCLKR.Send:_original.._value",crate, channel);       
       dpSet(datapoint,1);   // set  RESET
       dpSet(datapoint1,one); 
       delay(0,100); 
              
       dpSet(datapoint,0);   // clear RESET
       dpSet(datapoint1,one); 
       delay(0,100); 
      }
   //Set response   
   dpSet("SCTmops1:HardReset.response:_original.._value", 0);
   dpSet("SCTmops1:HardReset.trigger:_original.._value", 0);  
   dpSet("Info.Name:_original.._value", "CS: Hard Reset - Pulse Ended");
   DebugN("CS: Hard Reset - Ended");
   }
}

