Skip to main content

viva_genicam/
blocking.rs

1//! GigE Vision cameras for programs without an async runtime.
2//!
3//! Discovery and opening a camera are asynchronous in the rest of this
4//! crate; here they block. A camera opened with [`connect`] carries a small
5//! runtime of its own — one worker thread that drives its socket, its
6//! control-channel keepalive and its timers — and shuts it down when it is
7//! dropped, after giving control back. Everything else is the ordinary
8//! [`Camera`]: its feature access, the standard-feature helpers and
9//! [`Camera::open_stream`] are synchronous already, and frames come from
10//! [`FrameStream::next_frame_blocking`](crate::FrameStream::next_frame_blocking).
11//! Commands wait with [`Camera::execute_and_wait_blocking`].
12//!
13//! ```rust,no_run
14//! use std::time::Duration;
15//! use viva_genicam::{blocking, StreamOptions};
16//!
17//! # fn main() -> Result<(), viva_genicam::GenicamError> {
18//! let devices = blocking::discover(Duration::from_millis(500))?;
19//! let mut camera = blocking::connect(&devices[0])?;
20//! camera.set_exposure_time(5_000.0)?;
21//! let mut frames = camera.open_stream(StreamOptions::new())?;
22//! camera.acquisition_start()?;
23//! let frame = frames.next_frame_blocking(Duration::from_secs(1))?;
24//! camera.acquisition_stop()?;
25//! # Ok(())
26//! # }
27//! ```
28//!
29//! These functions refuse to run inside an async runtime, where blocking
30//! would stall it: there, use [`gige::discover`](crate::gige::discover) and
31//! [`connect_gige`](crate::connect_gige).
32//!
33//! USB3 Vision needs nothing from here to open a camera — `connect_u3v` is
34//! synchronous — but its frame stream still runs on a Tokio runtime.
35
36use std::net::Ipv4Addr;
37use std::time::Duration;
38
39use crate::gige::{DeviceInfo, DiscoveryOptions};
40use crate::{Access, Camera, GenicamError, GigeRegisterIo, camera_runtime, connect_here};
41
42/// Refuse to block inside an async runtime, which would stall it — and which
43/// Tokio refuses with a panic.
44fn outside_a_runtime(what: &'static str) -> Result<(), GenicamError> {
45    match tokio::runtime::Handle::try_current() {
46        Ok(_) => Err(GenicamError::InsideRuntime(what)),
47        Err(_) => Ok(()),
48    }
49}
50
51/// Run `fut` to completion on a short-lived runtime.
52fn run<F: std::future::Future>(what: &'static str, fut: F) -> Result<F::Output, GenicamError> {
53    outside_a_runtime(what)?;
54    let runtime = tokio::runtime::Builder::new_current_thread()
55        .enable_all()
56        .build()
57        .map_err(crate::error::network("start a runtime"))?;
58    Ok(runtime.block_on(fut))
59}
60
61/// Find GigE Vision cameras by broadcast: [`gige::discover`](crate::gige::discover),
62/// blocking for `timeout`.
63pub fn discover(timeout: Duration) -> Result<Vec<DeviceInfo>, GenicamError> {
64    discover_with(timeout, &DiscoveryOptions::new())
65}
66
67/// Find GigE Vision cameras as `options` say:
68/// [`gige::discover_with`](crate::gige::discover_with), blocking for
69/// `timeout`.
70pub fn discover_with(
71    timeout: Duration,
72    options: &DiscoveryOptions,
73) -> Result<Vec<DeviceInfo>, GenicamError> {
74    run(
75        "discover_with",
76        crate::gige::discover_with(timeout, options),
77    )?
78    .map_err(crate::error::gige("discovery"))
79}
80
81/// Ask the camera at `ip` directly: [`gige::discover_ip`](crate::gige::discover_ip),
82/// blocking for up to `timeout`.
83pub fn discover_ip(ip: Ipv4Addr, timeout: Duration) -> Result<Option<DeviceInfo>, GenicamError> {
84    run("discover_ip", crate::gige::discover_ip(ip, timeout))?
85        .map_err(crate::error::gige("discovery"))
86}
87
88/// Open a GigE Vision camera and take control of it:
89/// [`connect_gige`](crate::connect_gige), blocking.
90pub fn connect(device: &DeviceInfo) -> Result<Camera<GigeRegisterIo>, GenicamError> {
91    connect_as(device, Access::Control).map(|(camera, _xml)| camera)
92}
93
94/// Open a GigE Vision camera with the given [`Access`], returning the
95/// GenICam XML it was built from too: [`connect_gige_as`](crate::connect_gige_as),
96/// blocking.
97pub fn connect_as(
98    device: &DeviceInfo,
99    access: Access,
100) -> Result<(Camera<GigeRegisterIo>, String), GenicamError> {
101    outside_a_runtime("connect_as")?;
102    let runtime = camera_runtime()?;
103    let (mut camera, xml) = runtime.block_on(connect_here(device, access))?;
104    camera.transport_mut().adopt_runtime(runtime);
105    Ok((camera, xml))
106}