ifm3d
frame_grabber.h
1 /*
2  * Copyright 2022-present ifm electronic, gmbh
3 
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef IFM3D_FG_FRAMEGRABBER_H
8 #define IFM3D_FG_FRAMEGRABBER_H
9 #pragma once
10 
11 #include <cstdint>
12 #include <future>
13 #include <ifm3d/device/device.h>
14 #include <ifm3d/device/err.h>
15 #include <ifm3d/device/pcic_command.h>
16 #include <ifm3d/fg/buffer.h>
17 #include <ifm3d/fg/frame.h>
18 #include <ifm3d/fg/module_frame_grabber.h>
19 #include <ifm3d/fg/organizer.h>
20 #include <memory>
21 #include <optional>
22 #include <variant>
23 #include <vector>
24 
25 namespace ifm3d
26 {
31  class IFM3D_EXPORT FrameGrabber
32  {
33  public:
34  using Ptr = std::shared_ptr<FrameGrabber>;
35  using NewFrameCallback = std::function<void(Frame::Ptr)>;
36  using AsyncErrorCallback = std::function<void(int, const std::string&)>;
37  using AsyncNotificationCallback =
38  std::function<void(const std::string&, const std::string&)>;
39  using ErrorCallback = std::function<void(const ifm3d::Error&)>;
40  using BufferList =
41  std::vector<std::variant<std::uint64_t, int, ifm3d::buffer_id>>;
42  using PCICCommandResponse =
43  std::variant<std::monostate, std::string, std::vector<std::uint8_t>>;
44 
51  FrameGrabber(ifm3d::Device::Ptr cam,
52  std::optional<std::uint16_t> pcic_port = std::nullopt);
53 
58  virtual ~FrameGrabber();
59 
60  // disable copy/move semantics
61  FrameGrabber(FrameGrabber&&) = delete;
62  FrameGrabber& operator=(FrameGrabber&&) = delete;
63  FrameGrabber(FrameGrabber&) = delete;
64  FrameGrabber& operator=(const FrameGrabber&) = delete;
65 
81  std::shared_future<void> SWTrigger();
82 
87  void OnNewFrame(NewFrameCallback callback = nullptr);
88 
110  std::shared_future<void> Start(
111  const BufferList& buffers,
112  const std::optional<json>& schema = std::nullopt);
113 
119  std::shared_future<void> Stop();
120 
124  bool IsRunning();
125 
129  std::shared_future<Frame::Ptr> WaitForFrame();
130 
137  void SetOrganizer(std::unique_ptr<Organizer> organizer);
138 
145  void OnAsyncError(AsyncErrorCallback callback = nullptr);
146 
152  void OnAsyncNotification(AsyncNotificationCallback callback = nullptr);
153 
158  void OnError(ErrorCallback callback = nullptr);
159 
166  void SetMasking(bool mask);
167 
171  bool IsMasking();
172 
185  std::shared_future<PCICCommandResponse> SendCommand(
186  const PCICCommand& command);
187 
188  private:
189  class Impl;
190  std::unique_ptr<Impl> _impl;
191 
192  }; // end: class FrameGrabber
193 
194 } // end: namespace ifm3d
195 
196 #endif // IFM3D_FG_FRAMEGRABBER_H
Exception wrapper for library and system errors encountered by the library.
Definition: err.h:115
Implements a TCP FrameGrabber connected to the device passed to its ctor.
Definition: frame_grabber.h:32
std::shared_future< void > SWTrigger()
Triggers the device for image acquisition.
bool IsMasking()
return masking flag
std::shared_future< void > Start(const BufferList &buffers, const std::optional< json > &schema=std::nullopt)
Starts the worker thread for streaming in pixel data from the device.
bool IsRunning()
Returns true if the worker thread is currently running.
void OnAsyncError(AsyncErrorCallback callback=nullptr)
This function will enable the async error messages on device.
std::shared_future< void > Stop()
Stops the worker thread for streaming in pixel data from the device.
FrameGrabber(ifm3d::Device::Ptr cam, std::optional< std::uint16_t > pcic_port=std::nullopt)
Stores a reference to the passed in Device shared pointer.
std::shared_future< Frame::Ptr > WaitForFrame()
Returns a future that will resolve when a new frame is available.
void OnAsyncNotification(AsyncNotificationCallback callback=nullptr)
This function will enable the async notifications on device.
virtual ~FrameGrabber()
Cleans up resources held by the framegrabbing thread object and blocks until the operating system thr...
void OnNewFrame(NewFrameCallback callback=nullptr)
The callback will be executed whenever a new frame is available.
void OnError(ErrorCallback callback=nullptr)
The callback will be executed whenever an error condition occur while grabbing the data from device.
void SetMasking(bool mask)
enable/disable masking on supported buffer
void SetOrganizer(std::unique_ptr< Organizer > organizer)
This allows to override the Organizer which is used for extracting the data from the raw PCIC stream.
std::shared_future< PCICCommandResponse > SendCommand(const PCICCommand &command)
Sends a command to the frame grabber.
Definition: pcic_command.h:11