From 2b15fb719adfefe766ae551781553ed4431421bb Mon Sep 17 00:00:00 2001
From: Alessio Igor Bogani <alessio.bogani@elettra.eu>
Date: Mon, 16 May 2022 23:21:08 +0200
Subject: [PATCH] Split windows into three groups (normal, medium, important)
 to poll them with different rates

---
 TODO                            |  2 +
 src/Agilent4uhv.cpp             | 88 +++++++++++++++++++++++----------
 src/Agilent4uhv.xmi             |  2 +-
 src/Agilent4uhvDynAttrUtils.cpp |  2 +-
 4 files changed, 66 insertions(+), 28 deletions(-)
 create mode 100644 TODO

diff --git a/TODO b/TODO
new file mode 100644
index 0000000..3051d5e
--- /dev/null
+++ b/TODO
@@ -0,0 +1,2 @@
+Rimuovere attributi FixedX
+Leggere pressione, corrente e tensione di un canale solo quanto questo e' ON
diff --git a/src/Agilent4uhv.cpp b/src/Agilent4uhv.cpp
index 2b7ae00..4d08653 100644
--- a/src/Agilent4uhv.cpp
+++ b/src/Agilent4uhv.cpp
@@ -289,38 +289,55 @@ void Agilent4uhv::init_device()
 		strncpy(attr_SerialNumber_read[0], serial.c_str(), MAX_DEVSTRING_LENGTH);
 
 		/* Read all the device data */
-		static const int nw[] = { 8, /*, 108, 205, 320, 319, 323, 503, 504, 600 */
-			601, 602, 603, 800, 801, 802, 803, 804, 808, 809, 810, 811};
-		vector<int> normal_windows(nw, nw + sizeof(nw) / sizeof(nw[0]));
+		static const int lpw[] = { 8, /*, 108, 205, 320, 319, 323, 503, 504, 600 */
+			601, 602, 603, 800, 801, 802, 803, 804, 808, 809 };
+		vector<int> lowprio_windows(lpw, lpw + sizeof(lpw) / sizeof(lpw[0]));
 		for (size_t i=0; i<channels.size(); ++i) {
 			int chan;
 			convert(channels[i], chan);
-			normal_windows.push_back(10 + chan);
-			normal_windows.push_back(2060 + chan);
-			normal_windows.push_back(600 + chan * 10);
-			normal_windows.push_back(602 + chan * 10);
-			normal_windows.push_back(603 + chan * 10);
-			normal_windows.push_back(604 + chan * 10);
-			normal_windows.push_back(605 + chan * 10);
-			normal_windows.push_back(800 + chan * 10);
-			normal_windows.push_back(801 + chan * 10);
+			lowprio_windows.push_back(10 + chan);
+			lowprio_windows.push_back(2060 + chan);
+			lowprio_windows.push_back(600 + chan * 10);
+			lowprio_windows.push_back(602 + chan * 10);
+			lowprio_windows.push_back(603 + chan * 10);
+			lowprio_windows.push_back(604 + chan * 10);
+			lowprio_windows.push_back(605 + chan * 10);
 		}
-		for (size_t i=0; i<normal_windows.size(); ++i) {
-			read_window_into_cache(normal_windows[i]);
+		for (size_t i=0; i<lowprio_windows.size(); ++i) {
+			read_window_into_cache(lowprio_windows[i]);
 		}
-		static const int iw[] = { 812, 822, 832, 842 };
-		vector<int> important_windows(iw, iw + sizeof(iw) / sizeof(iw[0]));
-		for (size_t i=0; i<important_windows.size(); ++i) {
-			read_window_into_cache(important_windows[i]);
+
+		vector<int> medprio_windows;
+		for (size_t i=0; i<channels.size(); ++i) {
+			int chan;
+			convert(channels[i], chan);
+			medprio_windows.push_back(800 + chan * 10);
+			medprio_windows.push_back(801 + chan * 10);
+		}
+		for (size_t i=0; i<medprio_windows.size(); ++i) {
+			read_window_into_cache(medprio_windows[i]);
+		}
+
+		static const int hpw[] = { 812, 822, 832, 842 };
+		vector<int> highprio_windows(hpw, hpw + sizeof(hpw) / sizeof(hpw[0]));
+		for (size_t i=0; i<highprio_windows.size(); ++i) {
+			read_window_into_cache(highprio_windows[i]);
 		}
 		read_window_into_cache(205);
 
 		// Contruct polled windows vector
-		for (size_t i=0; i<normal_windows.size(); ++i) {
-			polled_windows.push_back(normal_windows[i]);
+		unsigned int medidx = 0;
+		for (size_t i=0; i<lowprio_windows.size(); ++i) {
+			if (i % 5) {
+				if (medidx >= medprio_windows.size())
+					medidx = 0;
+				polled_windows.push_back(medprio_windows[medidx++]);
+			} else {
+				polled_windows.push_back(lowprio_windows[i]);
+			}
 			polled_windows.insert(polled_windows.end(),
-					important_windows.begin(),
-					important_windows.end());
+					highprio_windows.begin(),
+					highprio_windows.end());
 		}
 		polled_windows.push_back(205 /* it MUST be latest */);
 
@@ -731,12 +748,31 @@ void Agilent4uhv::write_VTargetX(Tango::WAttribute &attr)
 	/*----- PROTECTED REGION ID(Agilent4uhv::write_VTargetX) ENABLED START -----*/
 	check_init();
 
+	int value = w_val;
+	if (value > 0 && value < 3000)
+		Tango::Except::throw_exception( "",
+				"Out of range [3000-7000]",
+				"Agilent4uhv::write_VTargetX()");
+
+	string channel_str = attr.get_name().substr(attr.get_name().size()-1);
+	int channel;
+	convert(channel_str, channel);
+	
+ 	string fs;
+ 	read_window_from_cache(603, fs);
+	fs[fs.size()-channel] = '0';
+	write_window(603, fs);
+
 	int win;
-	convert("6" + attr.get_name().substr(attr.get_name().size()-1) + "3", win);
+	convert("6" + channel_str + "3", win);
+	if (value != 0) {
+		write_window(win, value);
+		read_window_into_cache(win);
 
-	int value = w_val;
-	write_window(win, value);
-	read_window_into_cache(win);
+		fs[fs.size()-channel] = '1';
+		write_window(603, fs);
+	}
+	read_window_into_cache(603);
 
 	/*----- PROTECTED REGION END -----*/	//	Agilent4uhv::write_VTargetX
 }
diff --git a/src/Agilent4uhv.xmi b/src/Agilent4uhv.xmi
index 92bade3..c845810 100644
--- a/src/Agilent4uhv.xmi
+++ b/src/Agilent4uhv.xmi
@@ -125,7 +125,7 @@
       <archiveEvent fire="false" libCheckCriteria="false"/>
       <dataReadyEvent fire="false" libCheckCriteria="true"/>
       <status abstract="false" inherited="false" concrete="true" concreteHere="true"/>
-      <properties description="" label="" unit="V" standardUnit="" displayUnit="" format="" maxValue="7000" minValue="3000" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
+      <properties description="" label="" unit="V" standardUnit="" displayUnit="" format="" maxValue="7000" minValue="0" maxAlarm="" minAlarm="" maxWarning="" minWarning="" deltaTime="" deltaValue=""/>
       <readExcludedStates>UNKNOWN</readExcludedStates>
       <readExcludedStates>INIT</readExcludedStates>
       <writeExcludedStates>UNKNOWN</writeExcludedStates>
diff --git a/src/Agilent4uhvDynAttrUtils.cpp b/src/Agilent4uhvDynAttrUtils.cpp
index 7d87300..f11c350 100644
--- a/src/Agilent4uhvDynAttrUtils.cpp
+++ b/src/Agilent4uhvDynAttrUtils.cpp
@@ -271,7 +271,7 @@ void Agilent4uhv::add_VTargetX_dynamic_attribute(string attname)
 	//	display_unit	not set for VTargetX
 	//	format	not set for VTargetX
 	vtargetx_prop.set_max_value("7000");
-	vtargetx_prop.set_min_value("3000");
+	vtargetx_prop.set_min_value("0");
 	//	max_alarm	not set for VTargetX
 	//	min_alarm	not set for VTargetX
 	//	max_warning	not set for VTargetX
-- 
GitLab