libvisiontransfer  10.0.0
parameter_enumeration_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2022 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include <visiontransfer/deviceenumeration.h>
16 #include <visiontransfer/imagetransfer.h>
17 #include <visiontransfer/imageset.h>
18 #include <visiontransfer/deviceparameters.h>
19 #include <iostream>
20 #include <exception>
21 #include <iomanip>
22 #include <stdio.h>
23 #include <stdlib.h>
24 
25 using namespace visiontransfer;
26 using namespace visiontransfer::param;
27 
28 int main(int argc, const char** argv) {
29  try {
30  // Search for Nerian stereo devices
31  DeviceEnumeration deviceEnum;
32 
33  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
34  if(devices.size() == 0) {
35  std::cout << "No devices discovered!" << std::endl;
36  return -1;
37  }
38 
39  // Print devices
40  std::cout << "Discovered devices:" << std::endl;
41  for(unsigned int i = 0; i< devices.size(); i++) {
42  std::cout << devices[i].toString() << std::endl;
43  }
44  std::cout << std::endl;
45 
46  // Create an image transfer object that receives data from
47  // the first detected Nerian stereo device
48  DeviceParameters parameters(devices[0]);
49 
50  // Output the current parameterization
51 
52  const int colW = 40;
53  const int valueW = 8;
54  std::cout << std::boolalpha << std::left;
55  std::cout << "Server-side Parameter Enumeration" << std::endl;
56  std::cout << "=================================" << std::endl << std::endl;
57  ParameterSet allParams = parameters.getParameterSet();
58  std::cout << "All " << allParams.size() << " parameters reported by server:" << std::endl;
59  for (ParameterSet::iterator it = allParams.begin(); it != allParams.end(); ++it) {
60  Parameter& param = it->second;
61  switch (param.getType()) {
62  case ParameterValue::TYPE_INT: {
63  std::cout << std::setw(colW) << (param.getUid()+" (int)") << " = " << std::setw(valueW) << param.getCurrent<int>();
64  if (param.hasRange()) {
65  std::cout << " range " << param.getMin<int>() << "-" << param.getMax<int>();
66  }
67  if (param.getIncrement<int>() != 1) {
68  std::cout << " increment " << param.getIncrement<int>();
69  }
70  std::cout << std::endl;
71  break;
72  }
73  case ParameterValue::TYPE_BOOL: {
74  std::cout << std::setw(colW) << (param.getUid() + " (bool)") << " = " << (param.getCurrent<bool>()?"true":"false") << std::endl;
75  break;
76  }
77  case ParameterValue::TYPE_DOUBLE: {
78  std::cout << std::setw(colW) << (param.getUid()+" (double)") << " = " << std::setw(valueW) << param.getCurrent<double>();
79  if (param.hasRange()) {
80  std::cout << " range " << param.getMin<double>() << "-" << param.getMax<double>();
81  }
82  if (param.hasIncrement()) {
83  std::cout << " increment " << param.getIncrement<double>();
84  }
85  std::cout << std::endl;
86  break;
87  }
88  case ParameterValue::TYPE_STRING: {
89  std::cout << std::setw(colW) << (param.getUid() + " (string)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
90  break;
91  }
92  case ParameterValue::TYPE_SAFESTRING: {
93  std::cout << std::setw(colW) << (param.getUid() + " (safestring)") << " = \"" << param.getCurrent<std::string>() << "\"" << std::endl;
94  break;
95  }
96  case ParameterValue::TYPE_TENSOR: {
97  std::cout << std::setw(colW) << (param.getUid() + " (tensor) - shape: ");
98  for (unsigned int i=0; i<param.getTensorDimension(); ++i) {
99  if (i) std::cout << "x";
100  std::cout << param.getTensorShape()[i];
101  }
102  std::cout << std::endl;
103 
104  std::vector<double> data = param.getTensorData();
105  std::cout << std::setw(colW) << (" ");
106  std::cout << " - data: ";
107  int perline = (param.getTensorDimension()==2) ? param.getTensorShape()[1] : param.getTensorNumElements();
108  for (unsigned int i=0; i<param.getTensorNumElements(); ++i) {
109  std::cout << data[i] << " ";
110  if (((i+1)%perline)==0) {
111  std::cout << std::endl << std::setw(colW) << (" ") << " ";
112  }
113  }
114  std::cout << std::endl;
115  break;
116  }
117  case ParameterValue::TYPE_COMMAND: {
118  std::cout << std::setw(colW) << (param.getUid() + " (-> command trigger)") << std::endl;
119  break;
120  default:
121  break;
122  }
123  }
124  /*
125  // Show description texts, where defined
126  auto descr = param.getDescription();
127  if (descr != "") {
128  std::cout << "\tDescription: " << descr << std::endl;
129  }
130  */
131  }
132  std::cout << std::endl;
133 
134  // Setting an enumerated parameter
135  if (argc > 1) {
136  std::string argname(argv[1]);
137  if (argc > 2) {
138  std::string val(argv[2]);
139  std::cout << "Sending request to set " << argname << " to " << val << std::endl;
140  parameters.setParameter(argname, val);
141  } else {
142  std::cout << "Requesting single parameter " << argname << std::endl;
143  std::cout << "-> cast as a string: " << parameters.getParameter(argname).getCurrent<std::string>() << std::endl;
144  }
145  } else {
146  std::cout << "You can launch this with a parameter name to get (and a value to set it)" << std::endl;
147  std::cout << " e.g. " << argv[0] << " operation_mode [2]" << std::endl;
148  }
149 
150  return 0;
151  } catch(const std::exception& ex) {
152  std::cerr << "Exception occurred: " << ex.what() << std::endl;
153  }
154 
155  return 0;
156 }
157 
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
unsigned int getTensorDimension() const
Definition: parameter.h:122
std::string getUid() const
Definition: parameter.h:72
Allows for configuration of the parameters of a Nerian stereo device through a network connection...
std::vector< unsigned int > getTensorShape() const
Definition: parameter.h:125
ParameterValue::ParameterType getType() const
Definition: parameter.h:84
Allows for the discovery of devices in the network.
unsigned int getTensorNumElements() const
Definition: parameter.h:128
std::vector< double > getTensorData() const
Definition: parameter.cpp:238
Nerian Vision Technologies