#![cfg_attr(feature = "backtrace", feature(error_generic_member_access))]
#[cfg(feature = "backtrace")]
use std::backtrace::Backtrace;
use basic_frame::DynamicFrame;
pub use ci2_types::{AcquisitionMode, AutoMode, TriggerMode, TriggerSelector};
use machine_vision_formats as formats;
pub type Result<M> = std::result::Result<M, Error>;
#[derive(thiserror::Error, Debug)]
pub enum Error {
#[error("SingleFrameError({0})")]
SingleFrameError(String),
#[error("Timeout")]
Timeout,
#[error("CI2Error({msg})")]
CI2Error {
msg: String,
#[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("feature not present")]
FeatureNotPresent(#[cfg(feature = "backtrace")] Backtrace),
#[error("BackendError({0})")]
BackendError(
#[from]
#[cfg_attr(feature = "backtrace", backtrace)]
anyhow::Error,
),
#[error("io error: {source}")]
IoError {
#[from]
source: std::io::Error,
#[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("utf8 error: {source}")]
Utf8Error {
#[from]
source: std::str::Utf8Error,
#[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
#[error("try from int error: {source}")]
TryFromIntError {
#[from]
source: std::num::TryFromIntError,
#[cfg(feature = "backtrace")]
backtrace: Backtrace,
},
}
fn _test_error_is_send() {
fn implements<T: Send>() {}
implements::<Error>();
}
impl<'a> From<&'a str> for Error {
fn from(orig: &'a str) -> Error {
Error::CI2Error {
msg: orig.to_string(),
#[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
}
}
}
impl From<String> for Error {
fn from(msg: String) -> Error {
Error::CI2Error {
msg,
#[cfg(feature = "backtrace")]
backtrace: Backtrace::capture(),
}
}
}
pub trait CameraModule: Send {
type CameraType: Camera;
type Guard;
fn name(&self) -> &str;
fn camera_infos(&self) -> Result<Vec<Box<dyn CameraInfo>>>;
fn camera(&mut self, name: &str) -> Result<Self::CameraType>;
fn settings_file_extension(&self) -> &str;
fn frame_info_extractor(&self) -> &'static dyn ExtractFrameInfo;
}
pub struct FrameInfo {
pub device_timestamp: Option<std::num::NonZeroU64>,
pub frame_id: Option<std::num::NonZeroU64>,
pub host_framenumber: usize,
pub host_timestamp: chrono::DateTime<chrono::Utc>,
}
pub trait ExtractFrameInfo: Sync + Send {
fn extract_frame_info(&self, _frame: &DynamicFrame) -> FrameInfo;
}
pub trait CameraInfo {
fn name(&self) -> &str;
fn serial(&self) -> &str;
fn model(&self) -> &str;
fn vendor(&self) -> &str;
}
pub trait Camera: CameraInfo + Send {
fn command_execute(&self, name: &str, verify: bool) -> Result<()>;
fn feature_bool(&self, name: &str) -> Result<bool>;
fn feature_bool_set(&self, name: &str, value: bool) -> Result<()>;
fn feature_enum(&self, name: &str) -> Result<String>;
fn feature_enum_set(&self, name: &str, value: &str) -> Result<()>;
fn feature_float(&self, name: &str) -> Result<f64>;
fn feature_float_set(&self, name: &str, value: f64) -> Result<()>;
fn feature_int(&self, name: &str) -> Result<i64>;
fn feature_int_set(&self, name: &str, value: i64) -> Result<()>;
fn node_map_load(&self, settings: &str) -> Result<()>;
fn node_map_save(&self) -> Result<String>;
fn width(&self) -> Result<u32>;
fn height(&self) -> Result<u32>;
fn pixel_format(&self) -> Result<formats::PixFmt>;
fn possible_pixel_formats(&self) -> Result<Vec<formats::PixFmt>>;
fn set_pixel_format(&mut self, pixel_format: formats::PixFmt) -> Result<()>;
fn exposure_time(&self) -> Result<f64>;
fn exposure_time_range(&self) -> Result<(f64, f64)>;
fn set_exposure_time(&mut self, _: f64) -> Result<()>;
fn exposure_auto(&self) -> Result<AutoMode>;
fn set_exposure_auto(&mut self, _: AutoMode) -> Result<()>;
fn gain(&self) -> Result<f64>;
fn gain_range(&self) -> Result<(f64, f64)>;
fn set_gain(&mut self, _: f64) -> Result<()>;
fn gain_auto(&self) -> Result<AutoMode>;
fn set_gain_auto(&mut self, _: AutoMode) -> Result<()>;
fn trigger_mode(&self) -> Result<TriggerMode>;
fn set_trigger_mode(&mut self, _: TriggerMode) -> Result<()>;
fn acquisition_frame_rate_enable(&self) -> Result<bool>;
fn set_acquisition_frame_rate_enable(&mut self, value: bool) -> Result<()>;
fn acquisition_frame_rate(&self) -> Result<f64>;
fn acquisition_frame_rate_range(&self) -> Result<(f64, f64)>;
fn set_acquisition_frame_rate(&mut self, value: f64) -> Result<()>;
fn trigger_selector(&self) -> Result<TriggerSelector>;
fn set_trigger_selector(&mut self, _: TriggerSelector) -> Result<()>;
fn acquisition_mode(&self) -> Result<AcquisitionMode>;
fn set_acquisition_mode(&mut self, _: AcquisitionMode) -> Result<()>;
fn start_default_external_triggering(&mut self) -> Result<()> {
self.set_trigger_selector(TriggerSelector::FrameStart)?;
self.set_trigger_mode(TriggerMode::On)
}
fn set_software_frame_rate_limit(&mut self, fps_limit: f64) -> Result<()> {
self.set_acquisition_frame_rate_enable(true)?;
self.set_acquisition_frame_rate(fps_limit)
}
fn acquisition_start(&mut self) -> Result<()>;
fn acquisition_stop(&mut self) -> Result<()>;
fn next_frame(&mut self) -> Result<DynamicFrame>;
}