Janis Streib
19.05.23 b21ff09151ca7e87348f1ea38944decc1f46f5db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <iostream>
#include "Quaternion.h"
#include "struct.h"
#include "G4Trk.h"
#include "G4Hub.h"
#include <string.h>
#include <unistd.h>
#include <memory>
#include "clipp.h"
#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;
 
#define OUTPUT_BUFFER_SIZE 512
 
int RunCollect=0;
std::unique_ptr<UdpTransmitSocket> transmitSocket;
bool use_text = false;
int pollPeriod;
 
 
void* Collect(REND_STRUCT* prs){
    int iter = 0;
    CG4Trk* pTrk = prs->pTrk;
    while (RunCollect){
        // periodically check for new hubs coming on line
        if(iter % 1000 == 0) {
            int numHubs;
            if (prs->tot_hubs_on_system!=prs->numHub){
                prs->numHub=numHubs=prs->tot_hubs_on_system=pTrk->UpdateHubs();
                int colorInd=0;
                if (prs->hubList) {
                    delete[] prs->hubList;
                }
                prs->hubList=new CG4Hub[numHubs];
                int* hubIds=new int[numHubs];
                pTrk->GetHubList(hubIds,numHubs);
                for (int i=0;i<numHubs;i++){
                    prs->hubList[i].SetId(hubIds[i]);
                }
                delete[] hubIds;
            }
 
        }
        iter++;
        prs->hubreadmap=0;
        prs->tot_hubs_on_system=prs->pTrk->GetHubsPno(prs);
        char buffer[OUTPUT_BUFFER_SIZE];
 
        for (int i=0;i<prs->numHub;i++){
            osc::OutboundPacketStream p( buffer, OUTPUT_BUFFER_SIZE );
            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;
                }
                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[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[j][0] << dat[j][1] << dat[j][2] << dat[j][3] << dat[j][4] << dat[j][5];
                }
                p << osc::EndMessage;
            }
            if(!use_text) {
                p << osc::EndBundle;
            }
            transmitSocket->Send( p.Data(), p.Size() );
        }
        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[]){
 
  CG4Trk trk;
  REND_STRUCT rs;
  string cfgFile;
  string oscSinkAddr;
  int oscSinkPort;
 
  auto cli = (
    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("polling period in ms (imprecise). You realistically won't need less than 8 ms.", pollPeriod)
  );
 
  if(!parse(argc, argv, cli) || pollPeriod <= 0) {
      cout << make_man_page(cli, argv[0]);
    return 1;
  }
  transmitSocket = std::make_unique<UdpTransmitSocket>( IpEndpointName( oscSinkAddr.c_str(), oscSinkPort ) );
  memset(rs.viewTrans,0,sizeof(float)*3);
  rs.srcScale=rs.senScale=0.5;
  rs.counter=0;
 
  int resp=trk.Connect(const_cast<char*>(cfgFile.c_str())); // whaaaaa
  if (!resp){
    printf("Error Connecting to G4\n");
    return -1;
  }
 
  usleep(500000);
  rs.numSrc=trk.GetNumSrc();
  rs.srcList=new float[rs.numSrc][6];
  trk.GetSrcLoc(rs.srcList);
 
  rs.numHub=trk.UpdateHubs();
  rs.hubList=new CG4Hub[rs.numHub];
  int* hubIds=new int[rs.numHub];
  trk.GetHubList(hubIds,rs.numHub);
  for (int i=0;i<rs.numHub;i++){
    rs.hubList[i].SetId(hubIds[i]);
  }
  delete[] hubIds;
 
  rs.pTrk=&trk;
 
  RunCollect=1;
 
  // register signal handler
  struct sigaction sigIntHandler;
 
  sigIntHandler.sa_handler = sighandler;
  sigemptyset(&sigIntHandler.sa_mask);
  sigIntHandler.sa_flags = 0;
 
  sigaction(SIGINT, &sigIntHandler, NULL);
 
  Collect(&rs);
 
  return 0;
}