3#include "../_cexports/c_driver.h"
4#include "../_cexports/c_img.h"
6#include "../global.hpp"
8#include "../_decl/decl_device_factory.hpp"
10#include "../driver/_decl/decl_vin_device.hpp"
11#include "../driver/_decl/decl_emu_device.hpp"
12#include "../driver/_decl/decl_video_device.hpp"
13#include "../driver/_decl/decl_genicam_device.hpp"
15#include "../driver/non_streaming_device.hpp"
22 inline HandleGuard<Device> DeviceFactory::LoadAsGenTL(
const String &provider,
AcquisitionStack acquisitionStack)
24 CExports::DEVOBJ obj =
nullptr;
25 if (AllowFallback(acquisitionStack))
27 CVB_CALL_CAPI(CVDCreateOrGetDeviceObjectTyped(provider.c_str(), obj));
31 CVB_CALL_CAPI_CHECKED(CVDCreateOrGetDeviceObjectTyped(provider.c_str(), obj));
33 return HandleGuard<Device>(obj);
36 inline HandleGuard<Device> DeviceFactory::LoadAsVin(
const String &provider,
AcquisitionStack acquisitionStack)
38 CExports::IMG img =
nullptr;
39 if (AllowFallback(acquisitionStack))
41 CVB_CALL_CAPI(LoadImageFileTyped(provider.c_str(), img));
45 CVB_CALL_CAPI_CHECKED(LoadImageFileTyped(provider.c_str(), img));
47 return HandleGuard<Device>(img);
50 inline HandleGuard<Device> DeviceFactory::LoadAsNonStreaming(
const String &provider)
52 CExports::DEVOBJ obj =
nullptr;
53 CVB_CALL_CAPI_CHECKED(LoadDeviceObjectTyped(provider.c_str(), obj));
54 return HandleGuard<Device>(obj);
58 Cvb::HandleGuard<Cvb::Device> &&guard)
noexcept
61 CExports::cvbguid_t deviceGuid = {0};
62 CVB_CALL_CAPI_CHECKED(GetDriverID(guard.Handle(), deviceGuid));
64 if (deviceGuid == DeviceFactory::AviMpgGuid())
65 return Device::FromHandle<VideoDevice>(std::move(guard), provider);
66 else if (deviceGuid == DeviceFactory::EmuGuid())
67 return Device::FromHandle<EmuDevice>(std::move(guard), provider);
69 return Device::FromHandle<VinDevice>(std::move(guard), provider);
76 switch (acquisitionStack)
78 case AcquisitionStack::Vin:
79 return CreateDeviceByDriverGUID(provider, LoadAsVin(provider, acquisitionStack));
81 case AcquisitionStack::PreferVin:
83 auto vinGuard = LoadAsVin(provider, acquisitionStack);
84 if (vinGuard.Handle())
85 return CreateDeviceByDriverGUID(provider,
std::move(vinGuard));
86 return Device::FromHandle<NonStreamingDevice>(LoadAsNonStreaming(provider), provider);
89 case AcquisitionStack::GenTL:
90 return Device::FromHandle<GenICamDevice>(LoadAsGenTL(provider, acquisitionStack), provider);
92 case AcquisitionStack::PreferGenTL:
95 auto gentlGuard = LoadAsGenTL(provider, acquisitionStack);
96 if (gentlGuard.Handle())
97 return Device::FromHandle<GenICamDevice>(
std::move(gentlGuard), provider);
98 auto vinGuard = LoadAsVin(provider, acquisitionStack);
99 if (vinGuard.Handle())
100 return CreateDeviceByDriverGUID(provider,
std::move(vinGuard));
101 return Device::FromHandle<NonStreamingDevice>(LoadAsNonStreaming(provider), provider);
106 template <
class Rep,
class Period>
119 CExports::ATLIST handle =
nullptr;
120 auto discoverResult = CExports::DODiscoverTyped(accessToken.c_str(),
static_cast<CExports::cvbuint64_t
>(flags), 0,
121 handle,
static_cast<size_t>(msTimeSpan));
122 if (discoverResult < 0)
125 Driver::DiscoveryInformation::List listHandle(handle, [](
void *handle) { CVB_CALL_CAPI(
ReleaseObject(handle)); });
128 auto numEntriesResult = CExports::DOGetNumEntries(listHandle.get(), numEntries);
129 if (numEntriesResult < 0)
131 CvbException::FromCvbResult(numEntriesResult,
"failed to get number of entries for device object"));
134 discoveryInformationVector.reserve(numEntries);
136 for (
size_t i = 0; i < numEntries; ++i)
139 return discoveryInformationVector;
142 inline void DeviceFactory::ChangeBoard(
DevicePtr &device,
int board)
144 if (CExports::CanBoardSelect2(device->Handle()))
146 std::unique_lock<std::mutex>
lock(LockAnchor());
147 ChangeBoard2(device, board);
149 else if (CExports::CanBoardSelect(device->Handle()))
151 std::unique_lock<std::mutex>
lock(LockAnchor());
152 ChangeBoard1(device, board);
159 std::rethrow_exception(CvbException::FromCvbResult(
ErrorCodes::CVB_ERROR,
"no IBoardSelect or IBoardSelect2"));
163 inline void DeviceFactory::ChangeBoard1(
DevicePtr &device,
int board)
165 CExports::cvbval_t currentBoard = 0;
166 auto getBoardResult = CExports::GetBoard(device->Handle(), ¤tBoard);
167 if (getBoardResult < 0)
168 std::rethrow_exception(CvbException::FromCvbResult(getBoardResult,
"no current board"));
169 if (
static_cast<int>(currentBoard) == board)
172 auto setBoardResult = CExports::SetBoard(device->Handle(),
static_cast<CExports::cvbval_t
>(board));
173 if (setBoardResult < 0)
174 std::rethrow_exception(CvbException::FromCvbResult(setBoardResult,
"failed to set board"));
177 inline void DeviceFactory::ChangeBoard2(
DevicePtr &device,
int board)
179 CExports::cvbval_t currentBoard = 0;
180 auto getBoardResult = CExports::BS2GetBoard(device->Handle(), currentBoard);
181 if (getBoardResult < 0)
182 std::rethrow_exception(CvbException::FromCvbResult(getBoardResult,
"no current board"));
183 if (
static_cast<int>(currentBoard) == board)
185 CExports::cvbval_t numBoards = 0;
186 auto numBoardsResult = CExports::BS2GetNumBoards(device->Handle(), numBoards);
187 if (numBoardsResult < 0)
188 std::rethrow_exception(CvbException::FromCvbResult(numBoardsResult,
"no number of boards"));
189 if (board >=
static_cast<int>(numBoards))
190 throw std::out_of_range(
"board out of range");
192 CExports::IMG newImg =
nullptr;
193 auto setBoardResult = CExports::BS2SetBoard(device->Handle(), board, newImg);
194 if (setBoardResult < 0)
195 std::rethrow_exception(CvbException::FromCvbResult(setBoardResult,
"failed to set board"));
197 HandleGuard<Device> guard(newImg);
198 device = Device::FromHandle<Driver::VinDevice>(std::move(guard), device->ResourceLocator());
201 inline void DeviceFactory::ChangePort(
DevicePtr &device,
int port)
203 if (CExports::CanCameraSelect2(device->Handle()))
205 std::unique_lock<std::mutex>
lock(LockAnchor());
206 ChangePort2(device, port);
208 else if (CExports::CanCameraSelect(device->Handle()))
210 std::unique_lock<std::mutex>
lock(LockAnchor());
211 ChangePort1(device, port);
218 std::rethrow_exception(
223 inline void DeviceFactory::ChangePort1(
DevicePtr &device,
int port)
225 CExports::cvbval_t currentPort = 0;
226 auto getPortResult = CExports::GetCamPort(device->Handle(), currentPort);
227 if (getPortResult < 0)
228 std::rethrow_exception(CvbException::FromCvbResult(getPortResult,
"no current port"));
229 if (
static_cast<int>(currentPort) == port)
231 CExports::cvbval_t numPorts = 0;
232 auto numPortsResult = CExports::GetNumPorts(device->Handle(), numPorts);
233 if (numPortsResult < 0)
234 std::rethrow_exception(CvbException::FromCvbResult(numPortsResult,
"no number of ports"));
235 if (port >=
static_cast<int>(numPorts))
236 throw std::out_of_range(
"port out of range");
238 auto setPortResult = CExports::SetCamPort(device->Handle(),
static_cast<CExports::cvbval_t
>(port), 0);
239 if (setPortResult < 0)
240 std::rethrow_exception(CvbException::FromCvbResult(setPortResult,
"failed to set port"));
243 inline void DeviceFactory::ChangePort2(
DevicePtr &device,
int port)
245 CExports::cvbval_t currentPort = 0;
246 auto getPortResult = CExports::CS2GetCamPort(device->Handle(), currentPort);
247 if (getPortResult < 0)
248 std::rethrow_exception(CvbException::FromCvbResult(getPortResult,
"no current port"));
249 if (
static_cast<int>(currentPort) == port)
251 CExports::cvbval_t numPorts = 0;
252 auto numPortsResult = CExports::CS2GetNumPorts(device->Handle(), numPorts);
253 if (numPortsResult < 0)
254 std::rethrow_exception(CvbException::FromCvbResult(numPortsResult,
"no number of ports"));
255 if (port >=
static_cast<int>(numPorts))
256 throw std::out_of_range(
"port out of range");
258 CExports::IMG newImg =
nullptr;
259 auto setPortResult = CExports::CS2SetCamPort(device->Handle(),
static_cast<CExports::cvbval_t
>(port), 0, newImg);
260 if (setPortResult < 0)
261 std::rethrow_exception(CvbException::FromCvbResult(setPortResult,
"failed to set port"));
263 HandleGuard<Device> guard(newImg);
264 device = Device::FromHandle<Driver::VinDevice>(std::move(guard), device->ResourceLocator());
static std::vector< DiscoveryInformation > Discover()
Discovers available devices (not vins) with a default time span of 300ms.
Definition decl_device_factory.hpp:217
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
Opens a device with the given provider with its default board and port (if applicable).
Definition decl_device_factory.hpp:56
T duration_cast(T... args)
cvbbool_t ReleaseObject(OBJ &Object)
AcquisitionStack
Defines the acquisition stack when opening the device.
Definition driver.hpp:282
DiscoverFlags
Flags controlling the discovery process.
Definition driver.hpp:302
const int CVB_ERROR
Generic unspecified error.
Definition exception.hpp:23
Root namespace for the Image Manager interface.
Definition c_bayer_to_rgb.h:17
std::string String
String for wide characters or unicode characters.
Definition string.hpp:49
std::shared_ptr< Device > DevicePtr
Convenience shared pointer for Device.
Definition global.hpp:98
T rethrow_exception(T... args)