Debian Codegraph @

src/ArvGeneric.cpp

raw
1 /*
2 GigE interface wrapper on araviss
3 Copyright (C) 2016 Hendrik Beijeman ([email protected])
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 #include "ArvGeneric.h"
20
21 using namespace arv;
22
23 const char *ArvGeneric::_str_val(const char *s)
24 {
25 return (s ? s : "None");
26 }
27 const char *ArvGeneric::vendor_name()
28 {
29 return this->_str_val(this->cam.vendor_name);
30 }
31 const char *ArvGeneric::model_name()
32 {
33 return this->_str_val(this->cam.model_name);
34 }
35 const char *ArvGeneric::device_id()
36 {
37 return this->_str_val(this->cam.device_id);
38 }
39 min_max_property<int> ArvGeneric::get_bin_x()
40 {
41 return min_max_property<int>(this->cam.bin_x);
42 }
43 min_max_property<int> ArvGeneric::get_bin_y()
44 {
45 return min_max_property<int>(this->cam.bin_y);
46 }
47 min_max_property<int> ArvGeneric::get_x_offset()
48 {
49 return min_max_property<int>(this->cam.x_offset);
50 }
51 min_max_property<int> ArvGeneric::get_y_offset()
52 {
53 return min_max_property<int>(this->cam.y_offset);
54 }
55 min_max_property<int> ArvGeneric::get_width()
56 {
57 return min_max_property<int>(this->cam.width);
58 }
59 min_max_property<int> ArvGeneric::get_height()
60 {
61 return min_max_property<int>(this->cam.height);
62 }
63 min_max_property<int> ArvGeneric::get_bpp()
64 {
65 return min_max_property<int>(16, 16, 16);
66 }
67 min_max_property<double> ArvGeneric::get_pixel_pitch()
68 {
69 return min_max_property<double>(this->cam.pixel_pitch);
70 }
71 min_max_property<double> ArvGeneric::get_exposure()
72 {
73 return min_max_property<double>(this->cam.exposure);
74 }
75 min_max_property<double> ArvGeneric::get_gain()
76 {
77 return min_max_property<double>(this->cam.gain);
78 }
79 min_max_property<double> ArvGeneric::get_frame_rate()
80 {
81 return min_max_property<double>(this->cam.frame_rate);
82 }
83
84 template <typename T>
85 bool ArvGeneric::_get_bounds(void (*fn_arv_bounds)(::ArvCamera *, T *min, T *max, GError**), min_max_property<T> *prop)
86 {
87 T min, max;
88 fn_arv_bounds(this->camera, &min, &max, &(this->error));
89 prop->update(min, max);
90 }
91
92 bool ArvGeneric::is_exposing()
93 {
94 return this->stream_active;
95 }
96 bool ArvGeneric::is_connected()
97 {
98 return (this->camera ? true : false);
99 }
100 bool ArvGeneric::_stream_active()
101 {
102 return this->stream_active;
103 }
104
105 ArvGeneric::ArvGeneric(void *camera_device) : ArvCamera(camera_device)
106 {
107 this->_init();
108 this->camera = (::ArvCamera *)camera_device;
109 this->dev = arv_camera_get_device(this->camera);
110
111 this->cam.model_name = arv_camera_get_model_name(this->camera, &(this->error));
112 this->cam.vendor_name = arv_camera_get_vendor_name(this->camera, &(this->error));
113 this->cam.device_id = arv_camera_get_device_id(this->camera, &(this->error));
114 }
115
116 ArvGeneric::~ArvGeneric()
117 {
118 if (this->is_connected())
119 {
120 this->disconnect();
121 }
122 this->_init();
123 }
124
125 bool ArvGeneric::connect()
126 {
127 /* (Re-)connect by means of the device-id */
128 if (!this->camera)
129 {
130 this->camera = ::arv_camera_new(this->cam.device_id, &(this->error));
131 if (!this->camera)
132 return false;
133
134 this->dev = arv_camera_get_device(this->camera);
135 this->cam.model_name = arv_camera_get_model_name(this->camera, &(this->error));
136 this->cam.vendor_name = arv_camera_get_vendor_name(this->camera, &(this->error));
137 this->cam.device_id = arv_camera_get_device_id(this->camera, &(this->error));
138 }
139 return true;
140 }
141
142 bool ArvGeneric::_configure(void)
143 {
144 this->_set_initial_config();
145 return this->_get_initial_config();
146 }
147
148 void ArvGeneric::_init()
149 {
150 this->camera = nullptr;
151 this->buffer = nullptr;
152 this->stream = nullptr;
153 this->stream_active = false;
154
155 /* Don't clear device_id, its needed to re-attach with connect() */
156 }
157
158 bool ArvGeneric::disconnect()
159 {
160 if (this->is_connected())
161 {
162 this->_test_exposure_and_abort();
163 g_clear_object(&this->camera);
164 }
165 this->_init();
166 }
167
168 bool ArvGeneric::_set_initial_config()
169 {
170 /* Configure "manual" mode
171 * (1) disable auto exposure
172 * (2) disable auto framerate (to enable maximum possible exposure time)
173 * (3) set binning to 1x1
174 * (4) set software trigger */
175 arv_camera_set_binning(camera, 1, 1, &error);
176 arv_camera_set_gain_auto(camera, ARV_AUTO_OFF, &error);
177 arv_camera_set_exposure_time_auto(camera, ARV_AUTO_OFF, &error);
178 arv_camera_set_trigger(camera, "Software", &error);
179 return true;
180 }
181
182 bool ArvGeneric::_get_initial_config()
183 {
184 this->_get_bounds<gint>(arv_camera_get_x_binning_bounds, &this->cam.bin_x);
185 this->_get_bounds<gint>(arv_camera_get_y_binning_bounds, &this->cam.bin_y);
186 this->_get_bounds<gint>(arv_camera_get_x_offset_bounds, &this->cam.x_offset);
187 this->_get_bounds<gint>(arv_camera_get_y_offset_bounds, &this->cam.y_offset);
188 this->_get_bounds<gint>(arv_camera_get_width_bounds, &this->cam.width);
189 this->_get_bounds<gint>(arv_camera_get_height_bounds, &this->cam.height);
190 this->_get_bounds<double>(arv_camera_get_frame_rate_bounds, &this->cam.frame_rate);
191 this->_get_bounds<double>(arv_camera_get_exposure_time_bounds, &this->cam.exposure);
192 this->_get_bounds<double>(arv_camera_get_gain_bounds, &this->cam.gain);
193
194 this->cam.vendor_name = arv_camera_get_vendor_name(camera, &error);
195 this->cam.model_name = arv_camera_get_model_name(camera, &error);
196 this->cam.device_id = arv_camera_get_device_id(camera, &error);
197
198 /* No GVCP call for this..., specialize if necessary */
199 this->cam.pixel_pitch.set_single(1.0);
200
201 return true;
202 }
203
204 int ArvGeneric::get_frame_byte_size()
205 {
206 return arv_camera_get_payload(this->camera, &(this->error));
207 }
208
209 void ArvGeneric::set_geometry(int const x, int const y, int const w, int const h)
210 {
211 this->cam.x_offset.set(x);
212 this->cam.y_offset.set(y);
213 this->cam.width.set(w);
214 this->cam.height.set(h);
215
216 arv_camera_set_region(this->camera, this->cam.x_offset.val(), this->cam.y_offset.val(), this->cam.width.val(),
217 this->cam.height.val(), &(this->error));
218 }
219
220 void ArvGeneric::update_geometry(void)
221 {
222 gint x, y, w, h, binx, biny;
223
224 arv_camera_get_region(this->camera, &x, &y, &w, &h, &(this->error));
225 arv_camera_get_binning(this->camera, &binx, &biny, &(this->error));
226
227 this->cam.x_offset.set(x);
228 this->cam.y_offset.set(y);
229 this->cam.width.set(w);
230 this->cam.height.set(h);
231 this->cam.bin_x.set(binx);
232 this->cam.bin_y.set(biny);
233 }
234
235 void ArvGeneric::set_bin(int const bin_x, int const bin_y)
236 {
237 this->cam.bin_x.set(bin_x);
238 this->cam.bin_y.set(bin_y);
239
240 arv_camera_set_binning(this->camera, this->cam.bin_x.val(), this->cam.bin_y.val(), &(this->error));
241 }
242
243 void ArvGeneric::_test_exposure_and_abort(void)
244 {
245 if (this->_stream_active())
246 this->exposure_abort();
247 }
248
249 template <typename T>
250 void ArvGeneric::_set_cam_exposure_property(void (*arv_set)(::ArvCamera *, T, GError**), min_max_property<T> *prop,
251 T const new_val)
252 {
253 this->_test_exposure_and_abort();
254 prop->set(new_val);
255 arv_set(this->camera, prop->val(), &(this->error));
256 }
257
258 void ArvGeneric::set_gain(double const val)
259 {
260 this->_set_cam_exposure_property(arv_camera_set_gain, &this->cam.gain, val);
261 }
262 void ArvGeneric::set_exposure_time(double const val)
263 {
264 this->_set_cam_exposure_property(arv_camera_set_exposure_time, &this->cam.exposure, val);
265 }
266
267 ::ArvBuffer *ArvGeneric::_buffer_create(void)
268 {
269 ::ArvBuffer *buffer;
270
271 /* Ensure no buffers in stream */
272 while (1)
273 {
274 buffer = arv_stream_try_pop_buffer(this->stream);
275 if (buffer)
276 g_clear_object(&buffer);
277 else
278 break;
279 }
280
281 gint const payload = arv_camera_get_payload(this->camera, &(this->error));
282 buffer = arv_buffer_new(payload, nullptr);
283 arv_stream_push_buffer(this->stream, buffer);
284 return buffer;
285 }
286
287 ::ArvStream *ArvGeneric::_stream_create(void)
288 {
289 ::ArvStream *stream = arv_camera_create_stream(this->camera, nullptr, nullptr, &(this->error));
290 return stream;
291 }
292
293 void ArvGeneric::_stream_start()
294 {
295 this->stream_active = true;
296
297 /* Start the acquisition stream */
298 arv_camera_set_acquisition_mode(this->camera, ARV_ACQUISITION_MODE_SINGLE_FRAME, &(this->error));
299 arv_camera_start_acquisition(this->camera, &(this->error));
300 }
301
302 void ArvGeneric::_stream_stop()
303 {
304 /* stop the acquisition stream */
305 arv_camera_stop_acquisition(this->camera, &(this->error));
306 g_object_unref(this->stream);
307
308 this->stream_active = false;
309 }
310
311 void ArvGeneric::_trigger_exposure()
312 {
313 /* Trigger for an exposure */
314 arv_camera_software_trigger(this->camera, &(this->error));
315 }
316
317 void ArvGeneric::exposure_start(void)
318 {
319 this->_test_exposure_and_abort();
320 this->stream = this->_stream_create();
321 this->buffer = this->_buffer_create();
322
323 this->_stream_start();
324 this->_trigger_exposure();
325 }
326
327 void ArvGeneric::exposure_abort(void)
328 {
329 if (this->_stream_active())
330 {
331 arv_camera_abort_acquisition(this->camera, &(this->error));
332 this->_stream_stop();
333 }
334 }
335
336 void ArvGeneric::_get_image(void (*fn_image_callback)(void *const, uint8_t const *const, size_t), void *const usr_ptr)
337 {
338 ArvBuffer *const popped_buf = arv_stream_timeout_pop_buffer(this->stream, 100000);
339 if ((popped_buf != nullptr) && (popped_buf == this->buffer) &&
340 arv_buffer_get_status(this->buffer) == ARV_BUFFER_STATUS_SUCCESS)
341 {
342 if (fn_image_callback != nullptr)
343 {
344 size_t size;
345 uint8_t const *const data = (uint8_t const *const)arv_buffer_get_data(this->buffer, &size);
346 fn_image_callback(usr_ptr, data, size);
347 }
348 }
349 else
350 {
351 //TODO: failure...
352 }
353 }
354
355 ARV_EXPOSURE_STATUS ArvGeneric::exposure_poll(void (*fn_image_callback)(void *const, uint8_t const *const, size_t),
356 void *const usr_ptr)
357 {
358 if (!this->_stream_active())
359 return ARV_EXPOSURE_UNKNOWN;
360
361 ::ArvBufferStatus const status = arv_buffer_get_status(this->buffer);
362 switch (status)
363 {
364 case ARV_BUFFER_STATUS_CLEARED:
365 return ARV_EXPOSURE_BUSY;
366 case ARV_BUFFER_STATUS_FILLING:
367 return ARV_EXPOSURE_FILLING;
368 case ARV_BUFFER_STATUS_UNKNOWN:
369 return ARV_EXPOSURE_UNKNOWN;
370 case ARV_BUFFER_STATUS_SUCCESS:
371 this->_get_image(fn_image_callback, usr_ptr);
372 this->_stream_stop();
373 return ARV_EXPOSURE_FINISHED;
374 case ARV_BUFFER_STATUS_TIMEOUT:
375 case ARV_BUFFER_STATUS_MISSING_PACKETS:
376 case ARV_BUFFER_STATUS_WRONG_PACKET_ID:
377 case ARV_BUFFER_STATUS_SIZE_MISMATCH:
378 case ARV_BUFFER_STATUS_ABORTED:
379 this->_stream_stop();
380 return ARV_EXPOSURE_FAILED;
381 default:
382 return ARV_EXPOSURE_UNKNOWN;
383 }
384 }