-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
162 lines (139 loc) · 5.7 KB
/
Copy pathmain.cpp
File metadata and controls
162 lines (139 loc) · 5.7 KB
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
/*
* Copyright(C) 2026, IDS Imaging Development Systems GmbH.
*
* Permission to use, copy, modify, and/or distribute this software for
* any purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE
* FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*!
* \brief This application demonstrates how to use the device manager to open a camera
*/
#include <peak/peak.hpp>
#include <cstddef>
#include <cstdlib>
#include <iostream>
namespace
{
void WaitForEnterPressed();
std::string TryReadString(peak::core::NodeMap& nodeMap, const std::string& nodeName);
} // namespace
int main()
{
std::cout << "IDS peak OpenCamera example\n";
try
{
// The library must be initialized before use.
// Each call to `Initialize` must be matched with a corresponding call to `Close`.
peak::Library::Initialize();
// Get the device manager singleton object.
auto& deviceManager = peak::DeviceManager::Instance();
// Update the device manager.
// When `Update` is called, it searches for all ProducerLibraries contained
// in the directories specified by the GENICAM_GENTL{32/64}_PATH environment variable.
// It then opens all found producers, their systems, interfaces, and lists
// all available DeviceDescriptors.
deviceManager.Update();
// Exit program if no device was found.
if (deviceManager.Devices().empty())
{
std::cout << "No device found. Exiting program.\n";
WaitForEnterPressed();
// One call to `Close` is required for each call to `Initialize`.
peak::Library::Close();
return EXIT_SUCCESS;
}
// List all available devices.
std::size_t i = 0;
std::cout << "Devices available:\n";
for (const auto& deviceDescriptor : deviceManager.Devices())
{
std::cout << i << ": " << deviceDescriptor->ModelName() << " ("
<< deviceDescriptor->ParentInterface()->DisplayName() << "; "
<< deviceDescriptor->ParentInterface()->ParentSystem()->DisplayName() << " v."
<< deviceDescriptor->ParentInterface()->ParentSystem()->Version() << ")\n";
++i;
}
// Select a device to open.
std::size_t selectedDevice = 0;
// Prompt user for device index or remove this block to always use the first device.
std::cout << "\nSelect device index to open [0-" << deviceManager.Devices().size() - 1 << "]: ";
std::cin >> selectedDevice;
if (std::cin.fail())
{
std::cout << "Invalid input! Using index 0." << std::endl;
std::cin.clear();
std::cin.ignore(1'000, '\n');
selectedDevice = 0;
}
if (selectedDevice >= deviceManager.Devices().size())
{
std::cout << "Invalid index! Using index 0." << std::endl;
selectedDevice = 0;
}
// Open the selected device with control access.
// The access types correspond to the GenTL `DEVICE_ACCESS_FLAGS`.
auto device = deviceManager.Devices().at(selectedDevice)->OpenDevice(peak::core::DeviceAccessType::Control);
// Retrieve the remote device's primary node map.
// In GenICam, a node map represents a hierarchical set of parameters (features)
// such as exposure, gain, or firmware info. This node map provides access to controls
// implemented on the device itself, typically following the GenICam SFNC, while still
// allowing for vendor-specific extensions.
auto nodeMapRemoteDevice = device->RemoteDevice()->NodeMaps().at(0);
std::cout << "Model Name: " << TryReadString(*nodeMapRemoteDevice, "DeviceModelName") << "\n";
std::cout << "User ID: " << TryReadString(*nodeMapRemoteDevice, "DeviceUserID") << "\n";
std::cout << "Sensor Name: " << TryReadString(*nodeMapRemoteDevice, "SensorName") << "\n";
try
{
// Print maximum sensor resolution (width x height).
std::cout << "Max. resolution (w x h): "
<< nodeMapRemoteDevice->FindNode<peak::core::nodes::IntegerNode>("WidthMax")->Value() << " x "
<< nodeMapRemoteDevice->FindNode<peak::core::nodes::IntegerNode>("HeightMax")->Value() << "\n";
}
catch (const std::exception&)
{
std::cout << "Max. resolution (w x h): (unknown)\n";
}
}
catch (const std::exception& e)
{
std::cout << "EXCEPTION: " << e.what() << "\n";
}
try
{
// One call to `Close` is required for each call to `Initialize`.
peak::Library::Close();
}
catch (const std::exception& e)
{
std::cout << "EXCEPTION: " << e.what() << "\n";
}
WaitForEnterPressed();
return EXIT_SUCCESS;
}
namespace
{
std::string TryReadString(peak::core::NodeMap& nodeMap, const std::string& nodeName)
{
try
{
return nodeMap.FindNode<peak::core::nodes::StringNode>(nodeName)->Value();
}
catch (const std::exception&)
{
return "(unknown)";
}
}
void WaitForEnterPressed()
{
std::cout << "\nPress Enter to exit...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cin.get();
}
} // namespace