1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#![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;

// TODO add binning support

// ---------------------------
// errors

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() {
    // Compile-time test to ensure Error implements Send trait.
    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(),
        }
    }
}

// ---------------------------
// CameraModule

/// A module for opening cameras (e.g. pylon).
pub trait CameraModule: Send {
    type CameraType: Camera;
    type Guard;

    // TODO: have full_name and friendly_name?
    fn name(&self) -> &str;
    fn camera_infos(&self) -> Result<Vec<Box<dyn CameraInfo>>>;
    fn camera(&mut self, name: &str) -> Result<Self::CameraType>;

    /// The file extension for node map settings.
    ///
    /// The strings used in [Camera::node_map_load] and [Camera::node_map_save]
    /// would typically be stored in files with this extension.
    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;
}

// ---------------------------
// CameraInfo

pub trait CameraInfo {
    fn name(&self) -> &str;
    fn serial(&self) -> &str;
    fn model(&self) -> &str;
    fn vendor(&self) -> &str;
}

// ---------------------------
// Camera

pub trait Camera: CameraInfo + Send {
    // ----- start: weakly typed but easier to implement API -----

    // fn feature_access_query(&self, name: &str) -> Result<AccessQueryResult>;
    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<()>;

    // ----- end: weakly typed but easier to implement API -----

    /// Load camera settings from an implementation-dependent settings string.
    ///
    /// This would typically be read from a file with extension given by
    /// [CameraModule::settings_file_extension].
    fn node_map_load(&self, settings: &str) -> Result<()>;
    /// Read camera settings to an implementation-dependent settings string.
    ///
    /// This would typically be saved to a file with extension given by
    /// [CameraModule::settings_file_extension].
    fn node_map_save(&self) -> Result<String>;

    /// Return the sensor width in pixels
    fn width(&self) -> Result<u32>;
    /// Return the sensor height in pixels
    fn height(&self) -> Result<u32>;

    // TODO: add this
    // fn stride(&self) -> Result<u32>;

    // Settings: PixFmt ----------------------------
    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<()>;

    // Settings: Exposure Time ----------------------------
    /// value given in microseconds
    fn exposure_time(&self) -> Result<f64>;
    /// value given in microseconds
    fn exposure_time_range(&self) -> Result<(f64, f64)>;
    /// value given in microseconds
    fn set_exposure_time(&mut self, _: f64) -> Result<()>;

    // Settings: Exposure Time Auto Mode ----------------------------
    fn exposure_auto(&self) -> Result<AutoMode>;
    fn set_exposure_auto(&mut self, _: AutoMode) -> Result<()>;

    // Settings: Gain ----------------------------
    /// value given in dB
    fn gain(&self) -> Result<f64>;
    /// value given in dB
    fn gain_range(&self) -> Result<(f64, f64)>;
    /// value given in dB
    fn set_gain(&mut self, _: f64) -> Result<()>;

    // Settings: Gain Auto Mode ----------------------------
    fn gain_auto(&self) -> Result<AutoMode>;
    fn set_gain_auto(&mut self, _: AutoMode) -> Result<()>;

    // Settings: TriggerMode ----------------------------
    fn trigger_mode(&self) -> Result<TriggerMode>;
    fn set_trigger_mode(&mut self, _: TriggerMode) -> Result<()>;

    // Settings: AcquisitionFrameRateEnable ----------------------------
    fn acquisition_frame_rate_enable(&self) -> Result<bool>;
    fn set_acquisition_frame_rate_enable(&mut self, value: bool) -> Result<()>;

    // Settings: AcquisitionFrameRate ----------------------------
    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<()>;

    // Settings: TriggerSelector ----------------------------
    fn trigger_selector(&self) -> Result<TriggerSelector>;
    fn set_trigger_selector(&mut self, _: TriggerSelector) -> Result<()>;

    // Settings: AcquisitionMode ----------------------------
    fn acquisition_mode(&self) -> Result<AcquisitionMode>;
    fn set_acquisition_mode(&mut self, _: AcquisitionMode) -> Result<()>;

    // Set external triggering ------------------------------
    /// Set the camera to use external triggering using default parameters.
    ///
    /// The default parameters may vary by camera backend will ideally use
    /// a hardware trigger to trigger the start of each frame.
    fn start_default_external_triggering(&mut self) -> Result<()> {
        // This is the generic default implementation which may be overriden by
        // implementors.

        // The trigger selector must be set before the trigger mode.
        self.set_trigger_selector(TriggerSelector::FrameStart)?;
        self.set_trigger_mode(TriggerMode::On)
    }

    fn set_software_frame_rate_limit(&mut self, fps_limit: f64) -> Result<()> {
        // This is the generic default implementation which may be overriden by
        // implementors.
        self.set_acquisition_frame_rate_enable(true)?;
        self.set_acquisition_frame_rate(fps_limit)
    }

    // Acquisition ----------------------------
    fn acquisition_start(&mut self) -> Result<()>;
    fn acquisition_stop(&mut self) -> Result<()>;

    /// synchronous (blocking) frame acquisition
    // TODO: enable the ability to enqueue memory locations for new frame data.
    // This way pre-allocated can be stored to by the library and copies of the
    // data do not have to be made.
    // TODO: specify timeout
    fn next_frame(&mut self) -> Result<DynamicFrame>;
}

// #[derive(Debug, Clone, PartialEq)]
// pub struct AccessQueryResult {
//     pub is_readable: bool,
//     pub is_writeable: bool,
// }