From b21ff09151ca7e87348f1ea38944decc1f46f5db Mon Sep 17 00:00:00 2001
From: Janis Streib <me@janis-streib.de>
Date: Fr., 19 Mai 2023 18:12:03 +0200
Subject: [PATCH] FIXUP

---
 src/G4Export.cpp |   53 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/src/G4Export.cpp b/src/G4Export.cpp
index ba61301..18adf0b 100644
--- a/src/G4Export.cpp
+++ b/src/G4Export.cpp
@@ -11,6 +11,9 @@
 #include <ip/UdpSocket.h>
 #include <osc/OscOutboundPacketStream.h>
 #include <sstream>
+#include <signal.h>
+#include <chrono>
+#include <thread>
 
 using namespace clipp; using std::cout; using std::string;
 
@@ -19,14 +22,15 @@
 int RunCollect=0;
 std::unique_ptr<UdpTransmitSocket> transmitSocket;
 bool use_text = false;
+int pollPeriod;
 
 
 void* Collect(REND_STRUCT* prs){
-	int i = 0;
+	int iter = 0;
 	CG4Trk* pTrk = prs->pTrk;
 	while (RunCollect){
 		// periodically check for new hubs coming on line
-		if(i % 1000 == 0) {
+		if(iter % 1000 == 0) {
 			int numHubs;
 			if (prs->tot_hubs_on_system!=prs->numHub){
 				prs->numHub=numHubs=prs->tot_hubs_on_system=pTrk->UpdateHubs();
@@ -44,7 +48,7 @@
 			}
 
 		}
-		i++;
+		iter++;
 		prs->hubreadmap=0;
 		prs->tot_hubs_on_system=prs->pTrk->GetHubsPno(prs);
 		char buffer[OUTPUT_BUFFER_SIZE];
@@ -54,20 +58,29 @@
 			if(!use_text) {
 				p << osc::BeginBundleImmediate;
 			}
+			p << osc::BeginMessage( (string("/g4/hub/") + std::to_string(prs->hubList[i].GetId()) + string("/digio")).c_str());
+			std::ostringstream ss;
+			if(use_text) {
+				ss << prs->hubList[i].GetDigIOData();
+				auto res = ss.str();
+				p << res.c_str();
+			} else {
+				p << osc::int64(prs->hubList[i].GetDigIOData());
+			}
+			p << osc::EndMessage;
+			auto dat = prs->hubList[i].GetPnoData();
 			for(int j=0;j<G4_SENSORS_PER_HUB;j++) {
 				if(!prs->hubList[i].IsSenActive(j)){
 					continue;
 				}
-				auto dat = prs->hubList[i].GetPnoData()[j];
-				//printf("sns: %i: [%f;%f;%f],[%f,%f,%f]\n",i, dat[0],dat[1],dat[2], dat[3],dat[4],dat[5]);
 				p << osc::BeginMessage( (string("/g4/hub/") + std::to_string(prs->hubList[i].GetId()) + string("/") + std::to_string(j)).c_str());
 				std::ostringstream ss;
 				if(use_text) {
-					ss << "[" << dat[0] << "," << dat[1] << "," << dat[2] << "," << dat[3] << "," << dat[4] << "," << dat[5] << "]";
+					ss << "[" << dat[j][0] << "," << dat[j][1] << "," << dat[j][2] << "," << dat[j][3] << "," << dat[j][4] << "," << dat[j][5] << "]";
 					auto res = ss.str();
 					p << res.c_str();
 				} else {
-					p << dat[0] << dat[1] << dat[2] << dat[3] << dat[4] << dat[5];
+					p << dat[j][0] << dat[j][1] << dat[j][2] << dat[j][3] << dat[j][4] << dat[j][5];
 				}
 				p << osc::EndMessage;
 			}
@@ -76,11 +89,16 @@
 			}
 			transmitSocket->Send( p.Data(), p.Size() );
 		}
-		usleep(15000);
-		// usleep(8000);
+		std::this_thread::sleep_for(std::chrono::milliseconds(pollPeriod));
 	}
 
+	g4_close_tracker();
 	return NULL;
+}
+
+void sighandler(int s){
+	printf("Caught signal %d\n",s);
+	RunCollect=0;
 }
 
 int main(int argc,char* argv[]){
@@ -95,10 +113,11 @@
 	option("-t", "--text").set(use_text).doc("send strings in osc messages instead of a float bundle"),
         value("c4g configuration file", cfgFile),
         value("osc sink address", oscSinkAddr),
-        integer("osc sink port", oscSinkPort)
+        integer("osc sink port", oscSinkPort),
+        integer("polling period in ms (imprecise). You realistically won't need less than 8 ms.", pollPeriod)
   );
 
-  if(!parse(argc, argv, cli)) {
+  if(!parse(argc, argv, cli) || pollPeriod <= 0) {
   	cout << make_man_page(cli, argv[0]);
 	return 1;
   }
@@ -131,10 +150,16 @@
  
   RunCollect=1;
 
-  Collect(&rs);
+  // register signal handler
+  struct sigaction sigIntHandler;
 
-  delete[] rs.srcList;
-  delete[] rs.hubList;
+  sigIntHandler.sa_handler = sighandler;
+  sigemptyset(&sigIntHandler.sa_mask);
+  sigIntHandler.sa_flags = 0;
+
+  sigaction(SIGINT, &sigIntHandler, NULL);
+
+  Collect(&rs);
 
   return 0;
 }

--
Gitblit v1.9.1