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 <chrono>
12 #include <cstdint>
13 #include <future>
14 #include <ifm3d/device/device.h>
15 #include <ifm3d/device/err.h>
16 #include <ifm3d/device/pcic_command.h>
17 #include <ifm3d/fg/buffer.h>
18 #include <ifm3d/fg/frame.h>
19 #include <ifm3d/fg/module_frame_grabber.h>
20 #include <ifm3d/fg/organizer.h>
21 #include <memory>
22 #include <optional>
23 #include <variant>
24 #include <vector>
25 
26 namespace ifm3d
27 {
32  class IFM3D_EXPORT FrameGrabber
33  {
34  public:
35  using Ptr = std::shared_ptr<FrameGrabber>;
36  using NewFrameCallback = std::function<void(Frame::Ptr)>;
37  using AsyncErrorCallback = std::function<void(int, const std::string&)>;
38  using AsyncNotificationCallback =
39  std::function<void(const std::string&, const std::string&)>;
40  using ErrorCallback = std::function<void(const ifm3d::Error&)>;
41  using BufferList =
42  std::vector<std::variant<std::uint64_t, int, ifm3d::buffer_id>>;
43  using PCICCommandResponse =
44  std::variant<std::monostate, std::string, std::vector<std::uint8_t>>;
45 
52  FrameGrabber(ifm3d::Device::Ptr cam,
53  std::optional<std::uint16_t> pcic_port = std::nullopt);
54 
59  virtual ~FrameGrabber();
60 
61  // disable copy/move semantics
62  FrameGrabber(FrameGrabber&&) = delete;
63  FrameGrabber& operator=(FrameGrabber&&) = delete;
64  FrameGrabber(FrameGrabber&) = delete;
65  FrameGrabber& operator=(const FrameGrabber&) = delete;
66 
82  std::shared_future<void> SWTrigger();
83 
88  void OnNewFrame(NewFrameCallback callback = nullptr);
89 
111  std::shared_future<void> Start(
112  const BufferList& buffers,
113  const std::optional<json>& schema = std::nullopt);
114 
120  std::shared_future<void> Stop();
121 
125  bool IsRunning();
126 
141  void SetHeartbeatInterval(std::chrono::milliseconds interval);
142 
146  std::shared_future<Frame::Ptr> WaitForFrame();
147 
154  void SetOrganizer(std::unique_ptr<Organizer> organizer);
155 
162  void OnAsyncError(AsyncErrorCallback callback = nullptr);
163 
169  void OnAsyncNotification(AsyncNotificationCallback callback = nullptr);
170 
175  void OnError(ErrorCallback callback = nullptr);
176 
183  void SetMasking(bool mask);
184 
188  bool IsMasking();
189 
202  std::shared_future<PCICCommandResponse> SendCommand(
203  const PCICCommand& command);
204 
205  private:
206  class Impl;
207  std::unique_ptr<Impl> _impl;
208 
209  }; // end: class FrameGrabber
210 
211 } // end: namespace ifm3d
212 
213 #endif // IFM3D_FG_FRAMEGRABBER_H
Exception wrapper for library and system errors encountered by the library.
Definition: err.h:126
Implements a TCP FrameGrabber connected to the device passed to its ctor.
Definition: frame_grabber.h:33
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.
void SetHeartbeatInterval(std::chrono::milliseconds interval)
Configures the PCIC heartbeat interval.
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