libvisiontransfer  8.3.0
parameter_enumeration_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2020 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 
27 int main(int argc, const char** argv) {
28  try {
29  // Search for Nerian stereo devices
30  DeviceEnumeration deviceEnum;
31 
32  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
33  if(devices.size() == 0) {
34  std::cout << "No devices discovered!" << std::endl;
35  return -1;
36  }
37 
38  // Print devices
39  std::cout << "Discovered devices:" << std::endl;
40  for(unsigned int i = 0; i< devices.size(); i++) {
41  std::cout << devices[i].toString() << std::endl;
42  }
43  std::cout << std::endl;
44 
45  // Create an image transfer object that receives data from
46  // the first detected Nerian stereo device
47  DeviceParameters parameters(devices[0]);
48 
49  // Output the current parameterization
50 
51  const int colW = 40;
52  const int valueW = 8;
53  std::cout << std::boolalpha << std::left;
54  std::cout << "Server-side Parameter Enumeration" << std::endl;
55  std::cout << "=================================" << std::endl << std::endl;
56  std::map<std::string, ParameterInfo> all_params = parameters.getAllParameters();
57  std::cout << "All " << all_params.size() << " parameters reported by server:" << std::endl;
58  for (std::map<std::string, ParameterInfo>::iterator it = all_params.begin(); it != all_params.end(); ++it) {
59  ParameterInfo& param = it->second;
60  ParameterInfo::ParameterType t = param.getType();
61  switch (t) {
62  case ParameterInfo::TYPE_INT: {
63  std::cout << std::setw(colW) << (param.getName()+" (int)") << " = " << std::setw(valueW) << param.getValue<int>();
64  if (param.getMin<int>() != param.getMax<int>()) {
65  std::cout << " range " << param.getMin<int>() << "-" << param.getMax<int>();
66  if (param.getInc<int>() != 1) {
67  std::cout << ", increment " << param.getInc<int>();
68  }
69  }
70  std::cout << std::endl;
71  break;
72  }
73  case ParameterInfo::TYPE_BOOL: {
74  std::cout << std::setw(colW) << (param.getName() + " (bool)") << " = " << (param.getValue<bool>()?"true":"false") << std::endl;
75  break;
76  }
77  case ParameterInfo::TYPE_DOUBLE: {
78  std::cout << std::setw(colW) << (param.getName()+" (double)") << " = " << std::setw(valueW) << param.getValue<double>();
79  if (param.getMin<double>() != param.getMax<double>()) {
80  std::cout << " range " << param.getMin<double>() << "-" << param.getMax<double>();
81  }
82  std::cout << std::endl;
83  break;
84  }
85  }
86  }
87  std::cout << std::endl;
88 
89  // Setting an enumerated parameter
90  if (argc > 1) {
91  std::string argname(argv[1]);
92  if (argc > 2) {
93  double val = atof(argv[2]);
94  std::cout << "Sending request to set " << argname << " to " << val << std::endl;
95  parameters.setNamedParameter(argname, val);
96  } else {
97  std::cout << "Requesting single parameter " << argname << std::endl;
98  std::cout << "-> cast as a double: " << parameters.getNamedParameter<double>(argname) << std::endl;
99  }
100  } else {
101  std::cout << "You can launch this with a parameter name to get (and a value to set it)" << std::endl;
102  std::cout << " e.g. " << argv[0] << " operation_mode [2]" << std::endl;
103  }
104 
105  return 0;
106  } catch(const std::exception& ex) {
107  std::cerr << "Exception occurred: " << ex.what() << std::endl;
108  }
109 
110  return 0;
111 }
112 
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
T getMin() const
Returns the minimum parameter value, cast to the desired type (int, double or bool) ...
std::string getName() const
Returns the string representation of the parameter name.
T getInc() const
Returns the increment of the parameter (i.e. increment for raising / lowering the value)...
Allows for configuration of the parameters of a Nerian stereo device through a network connection...
ParameterType getType() const
Returns the type of the parameter.
T getValue() const
Returns the current parameter value, cast to the desired type (int, double or bool) ...
T getMax() const
Returns the maximum parameter value, cast to the desired type (int, double or bool) ...
Allows for the discovery of devices in the network.
Nerian Vision Technologies