From 4fefe53feba4127592cbcb372d2d321afe27d166 Mon Sep 17 00:00:00 2001
From: Gwenaelle Abeille <gwenaelle.abeille@synchrotron-soleil.fr>
Date: Mon, 25 Jun 2007 10:19:32 +0000
Subject: [PATCH] save data in files+bug fix on config cpy

---
 src/Doxyfile     |   4 +-
 src/RetrigAO.cpp | 286 +++++++++++++++++++++++++++++++++++++----------
 src/RetrigAO.h   |  12 +-
 3 files changed, 241 insertions(+), 61 deletions(-)

diff --git a/src/Doxyfile b/src/Doxyfile
index d6e04ce..3c1785b 100644
--- a/src/Doxyfile
+++ b/src/Doxyfile
@@ -30,7 +30,7 @@ PROJECT_NUMBER         =
 # If a relative path is entered, it will be relative to the location 
 # where doxygen was started. If left blank the current directory will be used.
 
-OUTPUT_DIRECTORY       =	D:\devices\devices_adlink\RetrigAO\src/doc_html
+OUTPUT_DIRECTORY       =	D:\RetrigAO\src/doc_html
 
 # The OUTPUT_LANGUAGE tag is used to specify the language in which all 
 # documentation generated by doxygen is written. Doxygen will use this 
@@ -304,7 +304,7 @@ WARN_LOGFILE           =
 # directories like "/usr/src/myproject". Separate the files or directories 
 # with spaces.
 
-INPUT                  =	D:\devices\devices_adlink\RetrigAO\src
+INPUT                  =	D:\RetrigAO\src
 
 # If the value of the INPUT tag contains directories, you can use the 
 # FILE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp 
diff --git a/src/RetrigAO.cpp b/src/RetrigAO.cpp
index d63e876..7df3801 100644
--- a/src/RetrigAO.cpp
+++ b/src/RetrigAO.cpp
@@ -1,4 +1,4 @@
-static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/InputOutput/ADLINK/RetrigAO/src/RetrigAO.cpp,v 1.9 2007-05-10 12:20:21 abeilleg Exp $";
+static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/InputOutput/ADLINK/RetrigAO/src/RetrigAO.cpp,v 1.10 2007-06-25 10:19:05 abeilleg Exp $";
 //+=============================================================================
 //
 // file :	  RetrigAO.cpp
@@ -13,9 +13,12 @@ static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/InputOutput/AD
 //
 // $Author: abeilleg $
 //
-// $Revision: 1.9 $
+// $Revision: 1.10 $
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.9  2007/05/10 12:20:21  abeilleg
+// do stop only if running
+//
 // Revision 1.8  2006/11/24 10:04:56  abeilleg
 // correction on bug zero forcing.
 //
@@ -71,6 +74,8 @@ static const char *RcsId = "$Header: /users/chaize/newsvn/cvsroot/InputOutput/AD
 #include <TangoExceptionsHelper.h>
 #include <asl/AOData.h>
 #include <math.h>
+#include <xstring.h>
+
 namespace RetrigAO
 {
 
@@ -272,8 +277,19 @@ void RetrigAO::init_device()
     if(aORefSource == adl::external_reference)
       ac.volt_ref = aORefA;
     _config.add_active_channel(ac);
-    
-    if(channel0Waveform.empty())
+   
+	for (int i = 0; i < bufferDepth; i++)
+      ch0[i] = 0;
+  
+    this->load_waveform(ch0,"Channel0Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch0[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(0, data);
+
+	/*
+	if(channel0Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 0, using a default one"<<endl;
       _config.set_channel_periodic_data(0, default_waveformA);
@@ -294,7 +310,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(0, data);
       //cpy data in the attribute of the device
       ::memcpy(ch0, data.base(), data.size());
-    }
+    }*/
     
   }
   //------active/config. channel 1---------
@@ -305,7 +321,16 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefA;
     _config.add_active_channel(ac);
 
-    if(channel1Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch1[i] = 0;
+
+	this->load_waveform(ch1,"Channel1Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch1[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(1, data);
+    /*if(channel1Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 1, using a default one"<<endl;
       _config.set_channel_periodic_data(1, default_waveformA);
@@ -326,7 +351,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(1, data);
       //cpy data in the attribute of the device
       ::memcpy(ch1, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 2---------
   if(channel2Enable)
@@ -336,7 +361,17 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefA;
     _config.add_active_channel(ac);
     
-    if(channel2Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch2[i] = 0;
+
+	this->load_waveform(ch2,"Channel2Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch2[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(2, data);
+
+   /* if(channel2Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 2, using a default one"<<endl;
       _config.set_channel_periodic_data(2, default_waveformA);
@@ -357,7 +392,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(2, data);
       //cpy data in the attribute of the device
       ::memcpy(ch2, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 3---------
   if(channel3Enable)
@@ -367,7 +402,16 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefA;
     _config.add_active_channel(ac);
     
-    if(channel3Waveform.empty())
+   	for (int i = 0; i < bufferDepth; i++)
+      ch3[i] = 0;
+	this->load_waveform(ch3,"Channel3Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch3[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(3, data);
+
+	/*if(channel3Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 3, using a default one"<<endl;
       _config.set_channel_periodic_data(3, default_waveformA);
@@ -388,7 +432,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(3, data);
       //cpy data in the attribute of the device
       ::memcpy(ch3, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 4---------
   if(channel4Enable)
@@ -398,7 +442,17 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefB;
     _config.add_active_channel(ac);
     
-    if(channel4Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch4[i] = 0;
+
+	this->load_waveform(ch4,"Channel4Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch4[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(4, data);
+
+    /*if(channel4Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 4, using a default one"<<endl;
       _config.set_channel_periodic_data(4, default_waveformB);
@@ -419,7 +473,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(4, data);
       //cpy data in the attribute of the device
       ::memcpy(ch4, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 5---------
   if(channel5Enable)
@@ -429,7 +483,17 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefB;
     _config.add_active_channel(ac);
     
-    if(channel5Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch5[i] = 0;
+
+	this->load_waveform(ch5,"Channel5Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch5[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(5, data);
+
+   /* if(channel5Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 5, using a default one"<<endl;
       _config.set_channel_periodic_data(5, default_waveformB);
@@ -450,7 +514,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(5, data);
       //cpy data in the attribute of the device
       ::memcpy(ch5, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 6---------
   if(channel6Enable)
@@ -460,7 +524,16 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefB;
     _config.add_active_channel(ac);
 
-    if(channel6Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch6[i] = 0;
+
+    this->load_waveform(ch6,"Channel6Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch6[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(6, data);
+	/*if(channel6Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 6, using a default one"<<endl;
       _config.set_channel_periodic_data(6, default_waveformB);
@@ -481,7 +554,7 @@ void RetrigAO::init_device()
       _config.set_channel_periodic_data(6, data);
       //cpy data in the attribute of the device
       ::memcpy(ch6, data.base(), data.size());
-    }
+    }*/
   }
   //------active/config. channel 7---------
   if(channel7Enable)
@@ -491,7 +564,17 @@ void RetrigAO::init_device()
       ac.volt_ref = aORefB;
     _config.add_active_channel(ac);
 
-    if(channel7Waveform.empty())
+	for (int i = 0; i < bufferDepth; i++)
+      ch7[i] = 0;
+
+	this->load_waveform(ch7,"Channel7Waveform");
+	asl::AOScaledData data(bufferDepth);
+    for(i=0; i<bufferDepth; i++)
+	data[i] = ch7[i]; 
+    // put the waveform in the configuration
+    _config.set_channel_periodic_data(7, data);
+
+   /* if(channel7Waveform.empty())
     {
       WARN_STREAM<<"No waveform is saved in database for channel 7, using a default one"<<endl;
       _config.set_channel_periodic_data(7, default_waveformB);
@@ -511,7 +594,7 @@ void RetrigAO::init_device()
       // put the waveform in the configuration
       _config.set_channel_periodic_data(7, data);
       //cpy data in the attribute of the device
-    }
+    }*/
   }
   //--------------------------------trigger config---------------------------------
   _config.enable_retrigger();
@@ -617,6 +700,8 @@ void RetrigAO::get_device_property()
   bufferDepth = 4096;
   double default_delay = 0.001638375; //secs
   delay = 65535; // counts
+
+  filePath = "\\\\DeviceServers\\configFiles\\RetrigAO\\";
   
   //	Read device properties from database.(Automatic code generation)
   //-------------------------------------------------------------
@@ -650,39 +735,11 @@ void RetrigAO::get_device_property()
 	data.push_back(Tango::DbDatum("Channel5Waveform"));
 	data.push_back(Tango::DbDatum("Channel6Waveform"));
 	data.push_back(Tango::DbDatum("Channel7Waveform"));
+	data.push_back(Tango::DbDatum("FilePath"));
 
 	//	Call database and extract values
 	//--------------------------------------------
 	get_db_device()->get_property(data);
-	if (data[0].is_empty()==false)	data[0]  >>  boardNum;
-	if (data[1].is_empty()==false)	data[1]  >>  frequency;
-	if (data[2].is_empty()==false)	data[2]  >>  triggerMode;
-	if (data[3].is_empty()==false)	data[3]  >>  triggerSource;
-	if (data[4].is_empty()==false)	data[4]  >>  aTRIGSelection;
-	if (data[5].is_empty()==false)	data[5]  >>  aTRIGLevel;
-	if (data[6].is_empty()==false)	data[6]  >>  dTRIGPolarity;
-	if (data[7].is_empty()==false)	data[7]  >>  aORefA;
-	if (data[8].is_empty()==false)	data[8]  >>  aORefB;
-	if (data[9].is_empty()==false)	data[9]  >>  polarity;
-	if (data[10].is_empty()==false)	data[10]  >>  aORefSource;
-	if (data[11].is_empty()==false)	data[11]  >>  channel0Enable;
-	if (data[12].is_empty()==false)	data[12]  >>  channel1Enable;
-	if (data[13].is_empty()==false)	data[13]  >>  channel2Enable;
-	if (data[14].is_empty()==false)	data[14]  >>  channel3Enable;
-	if (data[15].is_empty()==false)	data[15]  >>  channel4Enable;
-	if (data[16].is_empty()==false)	data[16]  >>  channel5Enable;
-	if (data[17].is_empty()==false)	data[17]  >>  channel6Enable;
-	if (data[18].is_empty()==false)	data[18]  >>  channel7Enable;
-	if (data[19].is_empty()==false)	data[19]  >>  bufferDepth;
-	if (data[20].is_empty()==false)	data[20]  >>  delay;
-	if (data[21].is_empty()==false)	data[21]  >>  channel0Waveform;
-	if (data[22].is_empty()==false)	data[22]  >>  channel1Waveform;
-	if (data[23].is_empty()==false)	data[23]  >>  channel2Waveform;
-	if (data[24].is_empty()==false)	data[24]  >>  channel3Waveform;
-	if (data[25].is_empty()==false)	data[25]  >>  channel4Waveform;
-	if (data[26].is_empty()==false)	data[26]  >>  channel5Waveform;
-	if (data[27].is_empty()==false)	data[27]  >>  channel6Waveform;
-	if (data[28].is_empty()==false)	data[28]  >>  channel7Waveform;
 
 
   //	End of Automatic code generation
@@ -1046,15 +1103,20 @@ void RetrigAO::get_device_property()
     WARN_STREAM<<"Delay property is not set in database, loading default value in database:"
       <<default_delay<<endl;
   }
+  //-------------------------------------------------------------
+	if (data[21].is_empty()==false)
+  {
+    data[21]  >>  filePath;
+  }
   //-----------------------attribute persistancy------------------
-  if (data[21].is_empty()==false)	data[21]  >>  channel0Waveform;
+ /* if (data[21].is_empty()==false)	data[21]  >>  channel0Waveform;
 	if (data[22].is_empty()==false) data[22]  >>  channel1Waveform;
 	if (data[23].is_empty()==false) data[23]  >>  channel2Waveform;
 	if (data[24].is_empty()==false) data[24]  >>  channel3Waveform;
 	if (data[25].is_empty()==false) data[25]  >>  channel4Waveform;
 	if (data[26].is_empty()==false) data[26]  >>  channel5Waveform;
 	if (data[27].is_empty()==false) data[27]  >>  channel6Waveform;
-	if (data[28].is_empty()==false) data[28]  >>  channel7Waveform;
+	if (data[28].is_empty()==false) data[28]  >>  channel7Waveform;*/
   
   //update database for not initiliazed properties
   get_db_device()->put_property(data_put);
@@ -1297,7 +1359,7 @@ void RetrigAO::stop_with_zero_forcing()
   DEBUG_STREAM << "RetrigAO::stop(): entering... !" << endl;
   
   // save the configuration
-  this->config = ao->configuration();
+  //this->config = ao->configuration();
 
   _ASL_TRY_ACTION
     (
@@ -1414,7 +1476,38 @@ void RetrigAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin)
     );
 
   // get the configuration
-  asl::ContinuousAOConfig _config = ao->configuration();
+ //asl::ContinuousAOConfig _config = ao->configuration();
+
+ //--------------------------------trigger config---------------------------------
+ /* _config.enable_retrigger();
+
+  _config.set_trigger_source((adl::AIOTriggerSource)triggerSource);
+  _config.set_trigger_mode((adl::AOTriggerMode)triggerMode);
+  _config.set_nb_waveforms(1);
+
+  if(triggerMode == adl::ao_delay)
+  {
+    //cout<<"delay: "<<delay<<endl;
+    _config.set_delay_counter(delay);
+  }
+  if(triggerSource == adl::external_digital)
+  {
+    _config.set_trigger_polarity((adl::AOTriggerPolarity)dTRIGPolarity);
+  }
+  else if(triggerSource == adl::external_analog)
+  {
+    _config.set_analog_trigger_source(adl::analog_trigger_ext);
+    _config.set_analog_trigger_condition((adl::AnalogTriggerCondition)aTRIGSelection);
+    unsigned short level;
+    if(aTRIGLevel>=0)
+      level = (aTRIGLevel / 0.078125) + 127;
+    else // v < 0
+      level = (-aTRIGLevel / 0.078125);
+    _config.set_analog_low_level_condition(level);
+    _config.set_analog_high_level_condition(level);
+  }
+  
+  _config.set_output_rate(frequency);*/
 
   // copy the input data in a buffer of type asl::AOScaledData
   asl::AOScaledData data(bufferDepth);
@@ -1422,9 +1515,9 @@ void RetrigAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin)
   
   //NB: if a chan hasn't been enable with properties, 'ao' will do nothing
  // cout << "RetrigAO::set_aoscaled_data - set periodic channel : " <<chan - '0'<< endl;
-  _config.set_channel_periodic_data(chan - '0', data);
+  this->config.set_channel_periodic_data(chan - '0', data);
 
-  // copy the new data in the attributes of the device.
+	  // copy the new data in the attributes of the device.
   switch(chan)
   {
   case '0':
@@ -1476,7 +1569,7 @@ void RetrigAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin)
     break;
   }
     
-   this->config = _config;
+  // this->config = _config;
   /*  DEBUG_STREAM<<"RetrigAO::init_device : init acq"<<std::endl;
   _ASL_TRY_ACTION
     (
@@ -1542,7 +1635,7 @@ void  RetrigAO::set_internal_state(void)
 void RetrigAO::save_waveform(double* waveform, string wfm_channel)
 {
   // cpy the waveform in a double vector 
-  vector<double> vec;
+ /* vector<double> vec;
   for(int i=0; i<bufferDepth; i++)
     vec.push_back(waveform[i]);
 
@@ -1560,7 +1653,86 @@ void RetrigAO::save_waveform(double* waveform, string wfm_channel)
     get_db_device()->put_property(db_data),
     "put_property",
     "ContinuousAO::save_waveform",
-    );
+    );*/
+	// save temporaly in file because of slow tangodb
+  string device_name = this->get_name();
+  long idx = 0;
+  while( (idx =device_name.find("/", idx)) != string::npos)
+  {
+    device_name.replace( idx,1, "#");
+    cout << "device name: " << device_name <<endl;
+  }
+  cout << "filePath: " << filePath <<endl;
+  string file_name = filePath + device_name +"_"+wfm_channel+ ".dat";
+  INFO_STREAM << "Saving "<<wfm_channel<<" in file "<<file_name<< endl;
+  try
+  {
+    //create file if not existant
+    {ofstream file(file_name.c_str(), ios::in);}
+    
+    ofstream file(file_name.c_str());
+    if(!file)
+    {
+      ERROR_STREAM<<"Cannot open file, check if path if correctly set! (save_waveform)"<<endl;
+      Tango::Except::throw_exception (
+        (const char *)"TANGO_NON_SUPPORTED_FEATURE_ERROR",
+        (const char *)"Cannot open file, check if path if correctly set!",
+        (const char *)"RetrigAO::save_waveform");
+    }
+    
+     for(int i=0; i<this->bufferDepth; i++)
+      file<<waveform[i] << endl;
+    
+    file.close();
+    
+  }
+  catch(...)
+  {
+    ERROR_STREAM<<"Cannot write data to file (save_waveform)"<<endl;
+    Tango::Except::throw_exception (
+      (const char *)"TANGO_NON_SUPPORTED_FEATURE_ERROR",
+      (const char *)"Cannot write data to file",
+      (const char *)"RetrigAO::save_waveform");
+  }
+  INFO_STREAM <<wfm_channel<<"saved in file "<<file_name<< endl;
+}
+
+//+------------------------------------------------------------------
+/**
+*	method: RetrigAO::save_waveform
+* load a waveform from tango database
+*/
+//+------------------------------------------------------------------
+void RetrigAO::load_waveform(double* waveform, string wfm_channel)
+{
+  // saved temporaly in file because of slow tangodb 
+  string device_name = this->get_name();
+  long idx = 0;
+  while( (idx =device_name.find("/", idx)) != string::npos)
+  {
+    device_name.replace( idx,1, "#");
+    cout << "device name: " << device_name <<endl;
+  }
+  string file_name = filePath + device_name +"_"+wfm_channel+ ".dat";
+  INFO_STREAM << "Getting data "<<wfm_channel<<" in file "<<file_name<< endl;
+
+  ifstream file (file_name.c_str());
+  string fileline;  
+  if(! file.fail() )   // file has been found
+  {
+    for(int i=0; i<this->bufferDepth; i++)
+    {
+      getline(file, fileline);
+      waveform[i] = XString<double>::convertFromString(fileline);
+    }
+    file.close();
+  
+  }
+  else
+  {
+    INFO_STREAM<<"RetrigAO::load_waveform - cannot retrieve table file, default tables loaded" <<std::endl;
+  }
+  INFO_STREAM << "Data retrieved"<<wfm_channel<<" in file "<<file_name<< endl;
 }
 
 }	//	namespace
diff --git a/src/RetrigAO.h b/src/RetrigAO.h
index 4989ee4..5f0103c 100644
--- a/src/RetrigAO.h
+++ b/src/RetrigAO.h
@@ -8,9 +8,12 @@
 //
 // $Author: abeilleg $
 //
-// $Revision: 1.5 $
+// $Revision: 1.6 $
 //
 // $Log: not supported by cvs2svn $
+// Revision 1.5  2006/09/18 10:32:52  abeilleg
+// added internal software support.
+//
 // Revision 1.4  2005/09/26 15:20:14  abeilleg
 // zero forcing after stopping generation.
 //
@@ -47,7 +50,7 @@
 
 /**
  * @author	$Author: abeilleg $
- * @version	$Revision: 1.5 $ $
+ * @version	$Revision: 1.6 $ $
  */
 
  //	Add your own constants definitions here.
@@ -229,6 +232,10 @@ public :
  *	The waveform for channel 7 (attribute persistency).
  */
 	vector<double>	channel7Waveform;
+/**
+ *	The path to waveforms persistancy files
+ */
+	string	filePath;
 //@}
 
 /**@name Constructors
@@ -341,6 +348,7 @@ protected :
    double err_ctr;
    short use_fifo;
    void save_waveform(double* waveform, string wfm_channel);
+   void load_waveform(double* waveform, string wfm_channel);
    asl::ContinuousAOConfig config;
    asl::ContinuousAOConfig stop_config;
 };
-- 
GitLab