libvisiontransfer  10.6.0
parameter_set_batch_write_example.cpp
1 /*******************************************************************************
2  * Copyright (c) 2023 Allied Vision Technologies 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 #include <thread>
25 #include <chrono>
26 
27 using namespace visiontransfer;
28 using namespace visiontransfer::param;
29 
30 int main(int argc, const char** argv) {
31  try {
32  // Search for Nerian stereo devices
33  DeviceEnumeration deviceEnum;
34 
35  DeviceEnumeration::DeviceList devices = deviceEnum.discoverDevices();
36  if(devices.size() == 0) {
37  std::cout << "No devices discovered!" << std::endl;
38  return -1;
39  }
40 
41  // Print devices
42  std::cout << "Discovered devices:" << std::endl;
43  for(unsigned int i = 0; i< devices.size(); i++) {
44  std::cout << devices[i].toString() << std::endl;
45  }
46  std::cout << std::endl;
47 
48  // Create an image transfer object that receives data from
49  // the first detected Nerian stereo device
50  DeviceParameters parameters(devices[0]);
51 
52  // Output the current parameterization
53  bool uni = parameters.getParameter("uniqueness_check_enabled").getCurrent<bool>();
54  bool tex = parameters.getParameter("texture_filter_enabled").getCurrent<bool>();
55  std::cout << "Current values:" << std::endl;
56  std::cout << " uniqueness_check_enabled == " << (uni?"true":"false") << std::endl;
57  std::cout << " texture_filter_enabled == " << (tex?"true":"false") << std::endl;
58 
59  // Use a Transaction to guard any batch update (although for these two independent
60  // parameters it could be fine without one). Only one active transaction per thread.
61  std::cout << "Starting transaction" << std::endl;
62  {
63  auto transactionLock = parameters.transactionLock();
64  parameters.setParameter("uniqueness_check_enabled", !uni);
65  parameters.setParameter("texture_filter_enabled", !tex);
66  } // -> transaction will be automatically committed at scope exit
67  std::cout << "Transaction complete" << std::endl;
68 
69  // Output the updated parameterization
70  uni = parameters.getParameter("uniqueness_check_enabled").getCurrent<bool>();
71  tex = parameters.getParameter("texture_filter_enabled").getCurrent<bool>();
72  std::cout << "New values:" << std::endl;
73  std::cout << " uniqueness_check_enabled == " << (uni?"true":"false") << std::endl;
74  std::cout << " texture_filter_enabled == " << (tex?"true":"false") << std::endl;
75 
76  return 0;
77  } catch(const std::exception& ex) {
78  // Note: for setting parameters, there are two relevant exceptions that
79  // should be handled. ParameterException indicates an invalid UID,
80  // lack of access rights, or unacceptable value, while
81  // TransferException likely means that the connection has been lost
82  // and could not yet be reconnected automatically in the background.
83  // You might want to retry the set operation later in the latter case.
84  std::cerr << "Exception occurred: " << ex.what() << std::endl;
85  }
86 
87  return 0;
88 }
89 
DeviceList discoverDevices()
Discovers new devices and returns the list of all devices that have been found.
Allows for configuration of the parameters of a Nerian stereo device through a network connection...
Allows for the discovery of devices in the network.
Allied Vision