// DDC control 
//123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456*/
//===================================-======================================*/
//                         DDC STATE & DATA CONTROL                         */
//-----------------------------------=--------------------------------------*/
// AUTHORs:        P.Ferrari & Heidi Sandaker
//
//   DESIGN ISSUEs:  Status word for each channel
//
//   MODIF_HISTORY:  26.03.2004 - first version H. Sandaker
//
//===================================-======================================*/

main()
{ 
DebugN("Started status script");
dpConnect("statusLoop","SetStatus.Stat:_original.._value");

}

void statusLoop(string Text, bool Flag) 
{

int syncinterval;
string statuslvDP, statushvDP, statusDP;
int statuslv, statushv, status, crate, channel;

// Get the syncinterval
  dpGet(getSystemName()+"Bus00.BusSyncInt.Send:_original.._value", syncinterval);
  // Since syncinterval not set at start up:
  syncinterval= 3;

//make addresses
  dpGet("Crate.Numb:_original.._value", crate);
  // for one crate
  crate=0;  

  for (channel=0; channel<=47; channel++) {
  
//make addresses  
      sprintf(statuslvDP, "Crate%02d.Channel%02d.LVchStat.Recv:_original.._value", crate, channel);  
      sprintf(statushvDP, "Crate%02d.Channel%02d.HVchStat.Recv:_original.._value", crate, channel);
      sprintf(statusDP, "Crate%02d.Channel%02d.Stat:_original.._value", crate, channel);  

//get values  
      dpGet(statuslvDP, statuslv);  
      dpGet(statushvDP, statushv);
//get old status
      dpGet(statusDP, status);
  
//set status word
      if ((statuslv & 0x20) || (statushv & 0xC0)){ 
         //fail - trip error 
         status = status | 0x003;
         dpSetWait(statusDP, status);
      } 
      else if ((statuslv & 0x40) && (statushv & 0x08)) {
         //mask off - disabled
         status = status - 0x030;
         }
      else {
         //mask off - enabled / no errors
         status = status | 0x030;
      }
      
  }
  
  // wait one read cycle
  delay(syncinterval);
}

