From 725d1a5c8ca94725525ccb1b4b6a9dacad3ff950 Mon Sep 17 00:00:00 2001
From: System User <boissy@flyscan-el6-1.ica.synchrotron-soleil.fr>
Date: Tue, 2 Aug 2022 13:35:35 +0200
Subject: [PATCH] Cleaning the code to to communalize lines

---
 src/ContinuousAO.cpp | 626 ++++++++++++-------------------------------
 src/ContinuousAO.h   |  27 +-
 2 files changed, 191 insertions(+), 462 deletions(-)

diff --git a/src/ContinuousAO.cpp b/src/ContinuousAO.cpp
index 2bda9f1..fad7822 100755
--- a/src/ContinuousAO.cpp
+++ b/src/ContinuousAO.cpp
@@ -75,18 +75,20 @@ namespace ContinuousAO
 //	- s : Device name 
 //
 //-----------------------------------------------------------------------------
-ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,string &s):Tango::Device_2Impl(cl,s.c_str())
+ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,string &s) : Tango::Device_2Impl(cl,s.c_str()),
+	m_default_waveform(2, asl::AOScaledData(0))
 {
 	init_device();
 }
 
-ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,const char *s):Tango::Device_2Impl(cl,s)
+ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,const char *s) : Tango::Device_2Impl(cl,s),
+	m_default_waveform(2, asl::AOScaledData(0))
 {
 	init_device();
 }
 
-ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,const char *s,const char *d)
-:Tango::Device_2Impl(cl,s,d)
+ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,const char *s,const char *d) : Tango::Device_2Impl(cl,s,d),
+	m_default_waveform(2, asl::AOScaledData(0))
 {
 	init_device();
 }
@@ -99,52 +101,78 @@ ContinuousAO::ContinuousAO(Tango::DeviceClass *cl,const char *s,const char *d)
 //-----------------------------------------------------------------------------
 void ContinuousAO::delete_device()
 {
-	//	Delete device's allocated object
-  if(ao)
-  {
-    delete ao;
-    ao = 0;
-  }
-  if(ch0)
-  {
-    delete ch0;
-    ch0 = 0;
-  }
-  if(ch1)
-  {
-    delete ch1;
-    ch1 = 0;
-  }
-  if(ch2)
-  {
-    delete ch2;
-    ch2 = 0;
-  }
-  if(ch3)
-  {
-    delete ch3;
-    ch3 = 0;
-  }
-  if(ch4)
-  {
-    delete ch4;
-    ch4 = 0;
-  }
-  if(ch5)
-  {
-    delete ch5;
-    ch5 = 0;
-  }
-  if(ch6)
-  {
-    delete ch6;
-    ch6 = 0;
-  }
-  if(ch7)
-  {
-    delete ch7;
-    ch7 = 0;
-  }
+	if(m_ao)
+	{
+		delete m_ao;
+		m_ao = 0;
+	}
+
+	for (int i = 0; i < 8; i++)
+	{
+		if (m_channel[i] != nullptr)
+		{
+			delete m_channel[i];
+			m_channel[i] = nullptr;
+		}
+	}
+}
+
+
+asl::AOScaledData ContinuousAO::assign_default_waveform(Tango::DevDouble p_ref, std::function <double(int, int)> p_lambda, int p_first_channel, int p_last_channel)
+{
+	// default waveform for group defined
+	asl::AOScaledData result = asl::AOScaledData(bufferDepth);
+	
+	double max_val;
+	
+	if(aORefSource == adl::external_reference)
+		max_val = p_ref;
+	else
+		max_val = 10.0;
+	
+	// fill buffer with a sin signal 
+	for (unsigned long i = 0; i < bufferDepth; i++)
+	{
+		(result)[i] = p_lambda(i, max_val);
+	}
+	
+	//Assignation of waveform for group
+	for (int i = p_first_channel; i <= p_last_channel; i++)
+	{
+		ACE_OS::memcpy(m_channel[i], result.base(), result.size());
+	}
+	
+	return (result);
+}
+
+
+void ContinuousAO::activate_channel(int p_channel_index, vector<double>& p_channel_wave_form, asl::AOScaledData& p_default_waveform)
+{
+	m_ac.id = p_channel_index;
+	if(aORefSource == adl::external_reference)
+		m_ac.volt_ref = aORefA;
+	m_config.add_active_channel(m_ac);
+	
+	if(p_channel_wave_form.empty())
+	{
+	  WARN_STREAM<<"No waveform is saved in database for channel " << p_channel_index << ", using a default one"<<endl;
+	  m_config.set_channel_periodic_data(p_channel_index, p_default_waveform);
+	}
+	else if(p_channel_wave_form.size() != bufferDepth)
+	{
+	  WARN_STREAM<<"The waveform saved in database for channel " << p_channel_index << " has a different size from property BufferDepth, using default waveform"<<endl;
+	  m_config.set_channel_periodic_data(p_channel_index, p_default_waveform);
+	}
+	else
+	{
+	  asl::AOScaledData data(bufferDepth);
+	  for(int i=0; i<bufferDepth; i++)
+		data[i] = p_channel_wave_form[i]; 
+	  // put the waveform in the configuration
+	  m_config.set_channel_periodic_data(p_channel_index, data);
+	  //cpy data in the attribute of the device
+	  ::memcpy(m_channel[p_channel_index], data.base(), data.size());
+	}
 }
 
 //+----------------------------------------------------------------------------
@@ -158,15 +186,8 @@ void ContinuousAO::init_device()
 {
   INFO_STREAM << "ContinuousAO::ContinuousAO() create device " << device_name << endl;
   
-  ao = 0;
-  ch0 = 0;
-  ch1 = 0;
-  ch2 = 0;
-  ch3 = 0;
-  ch4 = 0;
-  ch5 = 0;
-  ch6 = 0;
-  ch7 = 0;
+  m_ao = nullptr;
+  
   m_error_message = "An error has occured";
   
   // Initialise variables to default values
@@ -174,71 +195,39 @@ void ContinuousAO::init_device()
   get_device_property();
   
   //- create a continuous analog output daq
-  ao = new ADLinkContinuousAO(this);
+  m_ao = new ADLinkContinuousAO(this);
   
   //- check memory allocation
-  if (ao == 0)
+  if (m_ao == nullptr)
   {
 	raise_error("ContinuousAO::init_device\nout of memory error while initializing ADLinkContinuousAO");
 	return;
   }
   
   asl::ContinuousAOConfig config;
-  ch0 = new double[bufferDepth];
-  ch1 = new double[bufferDepth];
-  ch2 = new double[bufferDepth];
-  ch3 = new double[bufferDepth];
-  ch4 = new double[bufferDepth];
-  ch5 = new double[bufferDepth];
-  ch6 = new double[bufferDepth];
-  ch7 = new double[bufferDepth];
-  if(ch0 == 0 || ch1 == 0 || ch2 == 0 || ch3 == 0 || ch4 == 0 || ch5 == 0 ||ch6 == 0 || ch7 == 0)
-  {
-	raise_error("ContinuousAO::init_device\nout of memory error while initializing channel's datas");
-	return;
-  }
-  
-  
-  // default waveform for group A (channel 0 to 3)
-  asl::AOScaledData default_waveformA(bufferDepth);
-  double max_valA;
-  if(aORefSource == adl::external_reference)
-    max_valA = aORefA ;
-  else
-    max_valA = 10.0;
   
-  // fill buffer with a sin signal 
-  for (unsigned long i = 0; i < bufferDepth; i++)
+  for (int i = 0; i < 8; i++)
   {
-    (default_waveformA)[i] = ::sin((double)i * 2. * 3.14159 / (double) bufferDepth) * max_valA;
+    m_channel[i] = new double[bufferDepth];
+	if(m_channel[i] == nullptr)
+    {
+	  raise_error("ContinuousAO::init_device\nout of memory error while initializing channel's datas");
+	  return;
+    }
   }
   
-  ACE_OS::memcpy(ch0, default_waveformA.base(), default_waveformA.size());
-  ACE_OS::memcpy(ch1, default_waveformA.base(), default_waveformA.size());
-  ACE_OS::memcpy(ch2, default_waveformA.base(), default_waveformA.size());
-  ACE_OS::memcpy(ch3, default_waveformA.base(), default_waveformA.size());
-  
-  // default waveform for group A (channel 4 to 7)
-  asl::AOScaledData default_waveformB(bufferDepth);
-  double max_valB;
-  if(aORefSource == adl::external_reference)
-    max_valB = aORefB;
-  else
-    max_valB = 10.0;
-  
-  // fill buffer with a square signal 
-  for (i = 0; i < bufferDepth/2; i++)
-  {
-    default_waveformB[i] = max_valB - 0.01;
-  }
-  for (i = bufferDepth/2; i < bufferDepth; i++)
-  {
-    default_waveformB[i] = -max_valB + 0.01;
-  }
-  ACE_OS::memcpy(ch4, default_waveformB.base(), default_waveformB.size());
-  ACE_OS::memcpy(ch5, default_waveformB.base(), default_waveformB.size());
-  ACE_OS::memcpy(ch6, default_waveformB.base(), default_waveformB.size());
-  ACE_OS::memcpy(ch7, default_waveformB.base(), default_waveformB.size());
+  // default waveform for group A (channel 0 to 3)
+  m_default_waveform[0] = assign_default_waveform(aORefA, [&](int p_value, int p_max_value){
+		return (::sin((double)p_value * 2. * 3.14159 / (double) bufferDepth) * p_max_value);
+	}, 0, 3);
+	
+  // default waveform for group A (channel 0 to 3)
+  m_default_waveform[1] = assign_default_waveform(aORefB, [&](int p_value, int p_max_value){
+		if (p_value < bufferDepth/2)
+			return (p_max_value - 0.01);
+		else
+			return (-p_max_value + 0.01);
+	}, 4, 7);
   
   //--------------------------channels config------------------------------------ 
   asl::ActiveAOChannel ac;
@@ -248,255 +237,38 @@ void ContinuousAO::init_device()
   ac.volt_ref_src = (adl::VoltageReferenceSource)aORefSource;
   ac.volt_ref = 10.0;
   
-  //- active/config. channel 0
-  if(channel0Enable)
-  {
-    ac.id = 0;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefA;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel0Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 0 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(0, default_waveformA);
-    }
-    else //put waveform of the Tango database in ch0
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel0Waveform[i]; 
-      // put the waveform in the configuration
-      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
- if(channel1Enable)
-  {
-    ac.id = 1;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefA;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel1Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 1 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(1, default_waveformA);
-    }
-    else //put waveform of the Tango database in ch1
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel1Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 2;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefA;
-    config.add_active_channel(ac);
-    config.set_channel_periodic_data(2, default_waveformA);
-    
-    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);
-    }
-    else if(channel2Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 2 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(2, default_waveformA);
-    }
-    else //put waveform of the Tango database in ch2
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel2Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 3;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefA;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel3Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 3 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(3, default_waveformA);
-    }
-    else //put waveform of the Tango database in ch3
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel3Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 4;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefB;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel4Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 4 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(4, default_waveformB);
-    }
-    else //put waveform of the Tango database in ch4
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel4Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 5;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefB;
-    config.add_active_channel(ac);
-  
-    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);
-    }
-    else if(channel5Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 5 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(5, default_waveformB);
-    }
-    else //put waveform of the Tango database in ch5
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel5Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 6;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefB;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel6Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 6 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(6, default_waveformB);
-    }
-    else //put waveform of the Tango database in ch6
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel6Waveform[i]; 
-      // put the waveform in the configuration
-      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)
-  {
-    ac.id = 7;
-    if(aORefSource == adl::external_reference)
-      ac.volt_ref = aORefB;
-    config.add_active_channel(ac);
-    
-    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);
-    }
-    else if(channel7Waveform.size() != bufferDepth)
-    {
-      WARN_STREAM<<"The waveform saved in database for channel 7 has a different size from property BufferDepth, "<<
-        "using default waveform"<<endl;
-      config.set_channel_periodic_data(7, default_waveformB);
-    }
-    else //put waveform of the Tango database in ch7
-    {
-      // copy the input data in a buffer of type asl::AOScaledData
-      asl::AOScaledData data(bufferDepth);
-      for(int i=0; i<bufferDepth; i++)
-        data[i] = channel7Waveform[i]; 
-      // put the waveform in the configuration
-      config.set_channel_periodic_data(7, data);
-      //cpy data in the attribute of the device
-      ::memcpy(ch7, data.base(), data.size());
-    }
-  }
+	//------active/m_config. channel 0---------
+	if(channel0Enable)
+		activate_channel(0, channel0Waveform, m_default_waveform[0]);
+	
+	//------active/m_config. channel 1---------
+	if(channel1Enable)
+		activate_channel(1, channel1Waveform, m_default_waveform[0]);
+	
+	//------active/m_config. channel 2---------
+	if(channel2Enable)
+		activate_channel(2, channel2Waveform, m_default_waveform[0]);
+	
+	//------active/m_config. channel 3---------
+	if(channel3Enable)
+		activate_channel(3, channel3Waveform, m_default_waveform[0]);
+	
+	//------active/m_config. channel 4---------
+	if(channel4Enable)
+		activate_channel(4, channel4Waveform, m_default_waveform[1]);
+	
+	//------active/m_config. channel 5---------
+	if(channel5Enable)
+		activate_channel(5, channel5Waveform, m_default_waveform[1]);
+	
+	//------active/m_config. channel 6---------
+	if(channel6Enable)
+		activate_channel(6, channel6Waveform, m_default_waveform[1]);
+	
+	//------active/m_config. channel 7---------
+	if(channel7Enable)
+		activate_channel(7, channel7Waveform, m_default_waveform[1]);
+	
   //--------------------------------start trigger config---------------------------------
   if(startTrigger)
   {
@@ -532,7 +304,7 @@ void ContinuousAO::init_device()
   DEBUG_STREAM<<"ContinuousAO::init_device : init acq"<<std::endl;
   try{
 	_ASL_TRY(
-		ao->init(adl::DAQ2502, boardNum),
+		m_ao->init(adl::DAQ2502, boardNum),
 		"init", 
 		"ContinuousAO::init_device"
 	);
@@ -540,7 +312,7 @@ void ContinuousAO::init_device()
 	DEBUG_STREAM<<"ContinuousAO::init_device : config acq"<<std::endl;
 	try{
 		_ASL_TRY(
-			ao->configure(config),
+			m_ao->configure(config),
 			"configure", 
 			"ContinuousAO::init_device"
 		);	 
@@ -557,7 +329,7 @@ void ContinuousAO::init_device()
 	return;
   }
 
-  //ao->configure(config);	
+  //m_ao->configure(config);	
 }
 
 
@@ -601,14 +373,14 @@ void ContinuousAO::get_device_property()
   string default_aoref = "INTERN";
   aORefSource = adl::internal_reference;
 
-  channel0Enable = 1;
-  channel1Enable = 0;
-  channel2Enable = 0;
-  channel3Enable = 0;
-  channel4Enable = 0;
-  channel5Enable = 0;
-  channel6Enable = 0;
-  channel7Enable = 0;
+  channel0Enable = true;
+  channel1Enable = false;
+  channel2Enable = false;
+  channel3Enable = false;
+  channel4Enable = false;
+  channel5Enable = false;
+  channel6Enable = false;
+  channel7Enable = false;
 
   bufferDepth = 4096;
   
@@ -1073,11 +845,11 @@ void ContinuousAO::read_attr_hardware(vector<long> &attr_list)
 	
 	//	Add your own code here
 	//---------------------------------
-  ao->lock_data();
+  m_ao->lock_data();
 
-  err_ctr = ao->err_ctr;
+  m_err_ctr = m_ao->err_ctr;
   
-  ao->unlock_data();
+  m_ao->unlock_data();
  
 }
 
@@ -1100,68 +872,68 @@ void ContinuousAO::read_attr(Tango::Attribute &attr)
   if (attr_name == "channel0")
   {
     if(channel0Enable)
-      attr.set_value(ch0, bufferDepth);
+      attr.set_value(m_channel[0], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel1")
   {
     if(channel1Enable)
-      attr.set_value(ch1, bufferDepth);
+      attr.set_value(m_channel[1], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel2")
   {
     if(channel2Enable)
-      attr.set_value(ch2, bufferDepth);
+      attr.set_value(m_channel[2], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel3")
   {
     if(channel3Enable)
-      attr.set_value(ch3, bufferDepth);
+      attr.set_value(m_channel[3], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel4")
   {
     if(channel4Enable)
-      attr.set_value(ch4, bufferDepth);
+      attr.set_value(m_channel[4], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel5")
   {
     if(channel5Enable)
-      attr.set_value(ch5, bufferDepth);
+      attr.set_value(m_channel[5], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel6")
   {
     if(channel6Enable)
-      attr.set_value(ch6, bufferDepth);
+      attr.set_value(m_channel[6], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "channel7")
   { 
     if(channel7Enable)
-      attr.set_value(ch7, bufferDepth);
+      attr.set_value(m_channel[7], bufferDepth);
     else 
       attr.set_quality(Tango::ATTR_INVALID);
   }
   else if (attr_name == "errorCounter")
   {
-    attr.set_value(&err_ctr);
+    attr.set_value(&m_err_ctr);
   }
 	else if (attr_name == "useBoardFifo")
 	{
     // the information about data loading on fifo in available only after Start command.
     if(get_state() == Tango::RUNNING)
-      attr.set_value(&use_fifo);
+      attr.set_value(&m_use_fifo);
     else
       attr.set_quality(Tango::ATTR_INVALID);
 	}
@@ -1221,15 +993,15 @@ void ContinuousAO::start()
 {
   DEBUG_STREAM << "ContinuousAO::start(): entering... !" << endl;
   
-  ao->lock_data();
+  m_ao->lock_data();
 
-  ao->err_ctr = 0;
+  m_ao->err_ctr = 0;
   
-  ao->unlock_data();
+  m_ao->unlock_data();
 
   _ASL_TRY_ACTION
     (
-    ao->start(),
+    m_ao->start(),
     "start", 
     "ContinuousAO::start",
     set_internal_state()
@@ -1237,7 +1009,7 @@ void ContinuousAO::start()
   
   // get if the output data have been loaded in the onboard FIFO.
   // depends on the size of the buffer.
-  use_fifo = ao->use_board_fifo();
+  m_use_fifo = m_ao->use_board_fifo();
   
 }
 
@@ -1257,7 +1029,7 @@ void ContinuousAO::stop()
 
 	_ASL_TRY_ACTION
     (
-    ao->stop(),
+    m_ao->stop(),
     "stop", 
     "ContinuousAO::stop",
     set_internal_state()
@@ -1290,8 +1062,8 @@ void ContinuousAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin
   }
   Tango::DevVarStringArray chan_dvsa = (*argin).svalue;
   string chan_s = chan_dvsa[0];
-  char chan = chan_s[0];
-  if(chan < '0' || chan > '7')
+  int channel_index = chan_s[0] - '0';
+  if(channel_index < 0 || channel_index > 7)
   {
     Tango::Except::throw_exception(
       (const char*)("OPERATION_NOT_ALLOWED"),
@@ -1311,78 +1083,32 @@ void ContinuousAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin
   // stop the current generation
   _ASL_TRY_ACTION
     (
-    ao->stop(),
+    m_ao->stop(),
     "stop", 
     "ContinuousAO::set_aoscaled_data",
     set_internal_state()
     );
   
   // get the configuration
-  asl::ContinuousAOConfig config = ao->configuration();
+  asl::ContinuousAOConfig config = m_ao->configuration();
   
   // copy the input data in a buffer of type asl::AOScaledData
   asl::AOScaledData data(bufferDepth);
   ::memcpy(data.base(), (*argin).dvalue.get_buffer(), ((*argin).dvalue.length())*sizeof(double));
   
-  //NB: if a chan hasn't been enable with properties, 'ao' will do nothing
-  config.set_channel_periodic_data(chan - '0', data);
+  
+  //NB: if a chan hasn't been enable with properties, 'm_ao' will do nothing
+  config.set_channel_periodic_data(channel_index, data);
   
   // copy the new data in the attributes of the device.
-  switch(chan)
-  {
-  case '0':
-    ::memcpy(ch0, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch0, "Channel0Waveform");
-    break;
-  case '1':
-    ::memcpy(ch1, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch1, "Channel1Waveform");
-    break;
-  case '2':
-    ::memcpy(ch2, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch2, "Channel2Waveform");
-    break;
-  case '3':
-    ::memcpy(ch3, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch3, "Channel3Waveform");
-    break;
-  case '4':
-    ::memcpy(ch4, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch4, "Channel4Waveform");
-    break;
-  case '5':
-    ::memcpy(ch5, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch5, "Channel5Waveform");
-    break;
-  case '6':
-    ::memcpy(ch6, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch6, "Channel6Waveform");
-    break;
-  case '7':
-    ::memcpy(ch7, data.base(), data.size());
-    //save the waveform in the database
-    save_waveform(ch7, "Channel7Waveform");
-    break;
-  default:
-    Tango::Except::throw_exception(
-      (const char*)("OPERATION_NOT_ALLOWED"),
-      (const char*)("The string argument must be between 0 and 7"),
-      (const char*)("ContinuousAO::set_aoscaled_data")
-      );
-    break;
-  }
+  ::memcpy(m_channel[channel_index], data.base(), data.size());
+  //save the waveform in the database
+  save_waveform(m_channel[channel_index], "Channel" + std::to_string(channel_index) + "Waveform");
   
   // give the configuration with a new channel
   _ASL_TRY_ACTION
     (
-    ao->configure(config),
+    m_ao->configure(config),
     "configure", 
     "ContinuousAO::set_aoscaled_data",
     set_internal_state()
@@ -1396,14 +1122,14 @@ void ContinuousAO::set_aoscaled_data(const Tango::DevVarDoubleStringArray *argin
 //+------------------------------------------------------------------
 void  ContinuousAO::set_internal_state(void)
 {
-  if(ao == 0)
+  if(m_ao == 0)
   {
     set_state(Tango::UNKNOWN);
     set_status("The acquisition was not initialized properly");
   }
   else
   {
-    switch(ao->state())
+    switch(m_ao->state())
     {
     case asl::ContinuousAO::STANDBY:
       set_state(Tango::STANDBY);
diff --git a/src/ContinuousAO.h b/src/ContinuousAO.h
index 5829b23..4b015d8 100755
--- a/src/ContinuousAO.h
+++ b/src/ContinuousAO.h
@@ -323,23 +323,26 @@ public :
 
 	//	Here is the end of the automatic code generation part
 	//------------------------------------------------------------- 
-   ADLinkContinuousAO* ao;
+   ADLinkContinuousAO* m_ao;
+   std::vector<asl::AOScaledData> m_default_waveform;
+   double* m_channel[8];
+   double m_err_ctr;
+   short m_use_fifo;
+   std::string m_error_message;
+   
+   asl::ActiveAOChannel m_ac;
+   
+   asl::ContinuousAOConfig m_config;
+   asl::ContinuousAOConfig m_stop_config;
+   
+   
   // asl::ContinuousAOConfig* config;
    void set_internal_state(void);
    void raise_error(std::string p_error_message);
    // save a channel waveform in the database 
    void ContinuousAO::save_waveform(double* waveform, string wfm_channel);
-   double* ch0;
-   double* ch1;
-   double* ch2;
-   double* ch3;
-   double* ch4;
-   double* ch5;
-   double* ch6;
-   double* ch7;
-   double err_ctr;
-   short use_fifo;
-   std::string m_error_message;
+   asl::AOScaledData assign_default_waveform(Tango::DevDouble p_ref, std::function <double(int, int)> p_lambda, int p_first_channel, int p_last_channel);
+   void activate_channel(int p_channel_index, vector<double>& p_channel_wave_form, asl::AOScaledData& p_default_waveform);
 
 protected :	
 	//	Add your own data members here
-- 
GitLab