Repository navigation
Expand file tree
/
Copy pathcameracontrol.cpp
More file actions
605 lines (529 loc) · 20.8 KB
/
Copy pathcameracontrol.cpp
File metadata and controls
605 lines (529 loc) · 20.8 KB
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
/*
Chris Cummings
This is a stripped down version of the original RaspiCamControl module from the
raspberry pi userland-master branch, 24/10/2013. It contains the control functions
but none of the command line stuff, and is tweaked to work as a cpp file. Original
copyright info below
*/
/*
Copyright (c) 2013, Broadcom Europe Ltd
Copyright (c) 2013, James Hughes
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdio.h>
#include <memory.h>
extern "C"
{
#include "interface/vcos/vcos.h"
#include "interface/vmcs_host/vc_vchi_gencmd.h"
#include "interface/mmal/mmal.h"
#include "interface/mmal/mmal_logging.h"
#include "interface/mmal/util/mmal_util.h"
#include "interface/mmal/util/mmal_util_params.h"
#include "interface/mmal/util/mmal_default_components.h"
};
#include "cameracontrol.h"
/**
* Convert a MMAL status return value to a simple boolean of success
* ALso displays a fault if code is not success
*
* @param status The error code to convert
* @return 0 if status is sucess, 1 otherwise
*/
int mmal_status_to_int(MMAL_STATUS_T status)
{
if (status == MMAL_SUCCESS)
return 0;
else
{
switch (status)
{
case MMAL_ENOMEM : vcos_log_error("Out of memory"); break;
case MMAL_ENOSPC : vcos_log_error("Out of resources (other than memory)"); break;
case MMAL_EINVAL: vcos_log_error("Argument is invalid"); break;
case MMAL_ENOSYS : vcos_log_error("Function not implemented"); break;
case MMAL_ENOENT : vcos_log_error("No such file or directory"); break;
case MMAL_ENXIO : vcos_log_error("No such device or address"); break;
case MMAL_EIO : vcos_log_error("I/O error"); break;
case MMAL_ESPIPE : vcos_log_error("Illegal seek"); break;
case MMAL_ECORRUPT : vcos_log_error("Data is corrupt \attention FIXME: not POSIX"); break;
case MMAL_ENOTREADY :vcos_log_error("Component is not ready \attention FIXME: not POSIX"); break;
case MMAL_ECONFIG : vcos_log_error("Component is not configured \attention FIXME: not POSIX"); break;
case MMAL_EISCONN : vcos_log_error("Port is already connected "); break;
case MMAL_ENOTCONN : vcos_log_error("Port is disconnected"); break;
case MMAL_EAGAIN : vcos_log_error("Resource temporarily unavailable. Try again later"); break;
case MMAL_EFAULT : vcos_log_error("Bad address"); break;
default : vcos_log_error("Unknown status error"); break;
}
return 1;
}
}
/**
* Give the supplied parameter block a set of default values
* @params Pointer to parameter block
*/
void raspicamcontrol_set_defaults(RASPICAM_CAMERA_PARAMETERS *params)
{
vcos_assert(params);
params->sharpness = 0;
params->contrast = 0;
params->brightness = 50;
params->saturation = 0;
params->ISO = 0; // 0 = auto
params->videoStabilisation = 0;
params->exposureCompensation = 0;
params->exposureMode = MMAL_PARAM_EXPOSUREMODE_AUTO;
params->exposureMeterMode = MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
params->awbMode = MMAL_PARAM_AWBMODE_AUTO;
params->imageEffect = MMAL_PARAM_IMAGEFX_NONE;
params->colourEffects.enable = 0;
params->colourEffects.u = 128;
params->colourEffects.v = 128;
params->rotation = 0;
params->hflip = params->vflip = 0;
params->roi.x = params->roi.y = 0.0;
params->roi.w = params->roi.h = 1.0;
params->shutter_speed = 0; // 0 = auto
}
/**
* Get all the current camera parameters from specified camera component
* @param camera Pointer to camera component
* @param params Pointer to parameter block to accept settings
* @return 0 if successful, non-zero if unsuccessful
*/
int raspicamcontrol_get_all_parameters(MMAL_COMPONENT_T *camera, RASPICAM_CAMERA_PARAMETERS *params)
{
vcos_assert(camera);
vcos_assert(params);
if (!camera || !params)
return 1;
/* TODO : Write these get functions
params->sharpness = raspicamcontrol_get_sharpness(camera);
params->contrast = raspicamcontrol_get_contrast(camera);
params->brightness = raspicamcontrol_get_brightness(camera);
params->saturation = raspicamcontrol_get_saturation(camera);
params->ISO = raspicamcontrol_get_ISO(camera);
params->videoStabilisation = raspicamcontrol_get_video_stabilisation(camera);
params->exposureCompensation = raspicamcontrol_get_exposure_compensation(camera);
params->exposureMode = raspicamcontrol_get_exposure_mode(camera);
params->awbMode = raspicamcontrol_get_awb_mode(camera);
params->imageEffect = raspicamcontrol_get_image_effect(camera);
params->colourEffects = raspicamcontrol_get_colour_effect(camera);
params->thumbnailConfig = raspicamcontrol_get_thumbnail_config(camera);
*/
return 0;
}
/**
* Set the specified camera to all the specified settings
* @param camera Pointer to camera component
* @param params Pointer to parameter block containing parameters
* @return 0 if successful, none-zero if unsuccessful.
*/
int raspicamcontrol_set_all_parameters(MMAL_COMPONENT_T *camera, const RASPICAM_CAMERA_PARAMETERS *params)
{
int result;
result = raspicamcontrol_set_saturation(camera, params->saturation);
result += raspicamcontrol_set_sharpness(camera, params->sharpness);
result += raspicamcontrol_set_contrast(camera, params->contrast);
result += raspicamcontrol_set_brightness(camera, params->brightness);
result += raspicamcontrol_set_ISO(camera, params->ISO);
result += raspicamcontrol_set_video_stabilisation(camera, params->videoStabilisation);
result += raspicamcontrol_set_exposure_compensation(camera, params->exposureCompensation);
result += raspicamcontrol_set_exposure_mode(camera, params->exposureMode);
result += raspicamcontrol_set_metering_mode(camera, params->exposureMeterMode);
result += raspicamcontrol_set_awb_mode(camera, params->awbMode);
result += raspicamcontrol_set_imageFX(camera, params->imageEffect);
result += raspicamcontrol_set_colourFX(camera, ¶ms->colourEffects);
//result += raspicamcontrol_set_thumbnail_parameters(camera, ¶ms->thumbnailConfig); TODO Not working for some reason
result += raspicamcontrol_set_rotation(camera, params->rotation);
result += raspicamcontrol_set_flips(camera, params->hflip, params->vflip);
result += raspicamcontrol_set_ROI(camera, params->roi);
result += raspicamcontrol_set_shutter_speed(camera, params->shutter_speed);
return result;
}
/**
* Adjust the saturation level for images
* @param camera Pointer to camera component
* @param saturation Value to adjust, -100 to 100
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_saturation(MMAL_COMPONENT_T *camera, int saturation)
{
int ret = 0;
if (!camera)
return 1;
if (saturation >= -100 && saturation <= 100)
{
MMAL_RATIONAL_T value = {saturation, 100};
ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_SATURATION, value));
}
else
{
vcos_log_error("Invalid saturation value");
ret = 1;
}
return ret;
}
/**
* Set the sharpness of the image
* @param camera Pointer to camera component
* @param sharpness Sharpness adjustment -100 to 100
*/
int raspicamcontrol_set_sharpness(MMAL_COMPONENT_T *camera, int sharpness)
{
int ret = 0;
if (!camera)
return 1;
if (sharpness >= -100 && sharpness <= 100)
{
MMAL_RATIONAL_T value = {sharpness, 100};
ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_SHARPNESS, value));
}
else
{
vcos_log_error("Invalid sharpness value");
ret = 1;
}
return ret;
}
/**
* Set the contrast adjustment for the image
* @param camera Pointer to camera component
* @param contrast Contrast adjustment -100 to 100
* @return
*/
int raspicamcontrol_set_contrast(MMAL_COMPONENT_T *camera, int contrast)
{
int ret = 0;
if (!camera)
return 1;
if (contrast >= -100 && contrast <= 100)
{
MMAL_RATIONAL_T value = {contrast, 100};
ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_CONTRAST, value));
}
else
{
vcos_log_error("Invalid contrast value");
ret = 1;
}
return ret;
}
/**
* Adjust the brightness level for images
* @param camera Pointer to camera component
* @param brightness Value to adjust, 0 to 100
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_brightness(MMAL_COMPONENT_T *camera, int brightness)
{
int ret = 0;
if (!camera)
return 1;
if (brightness >= 0 && brightness <= 100)
{
MMAL_RATIONAL_T value = {brightness, 100};
ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_BRIGHTNESS, value));
}
else
{
vcos_log_error("Invalid brightness value");
ret = 1;
}
return ret;
}
/**
* Adjust the ISO used for images
* @param camera Pointer to camera component
* @param ISO Value to set TODO :
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_ISO(MMAL_COMPONENT_T *camera, int ISO)
{
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set_uint32(camera->control, MMAL_PARAMETER_ISO, ISO));
}
/**
* Adjust the metering mode for images
* @param camera Pointer to camera component
* @param saturation Value from following
* - MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE,
* - MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT,
* - MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT,
* - MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_metering_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_EXPOSUREMETERINGMODE_T m_mode )
{
MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = {{MMAL_PARAMETER_EXP_METERING_MODE,sizeof(meter_mode)},
m_mode};
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set(camera->control, &meter_mode.hdr));
}
/**
* Set the video stabilisation flag. Only used in video mode
* @param camera Pointer to camera component
* @param saturation Flag 0 off 1 on
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_video_stabilisation(MMAL_COMPONENT_T *camera, int vstabilisation)
{
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set_boolean(camera->control, MMAL_PARAMETER_VIDEO_STABILISATION, vstabilisation));
}
/**
* Adjust the exposure compensation for images (EV)
* @param camera Pointer to camera component
* @param exp_comp Value to adjust, -10 to +10
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_exposure_compensation(MMAL_COMPONENT_T *camera, int exp_comp)
{
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set_int32(camera->control, MMAL_PARAMETER_EXPOSURE_COMP , exp_comp));
}
/**
* Set exposure mode for images
* @param camera Pointer to camera component
* @param mode Exposure mode to set from
* - MMAL_PARAM_EXPOSUREMODE_OFF,
* - MMAL_PARAM_EXPOSUREMODE_AUTO,
* - MMAL_PARAM_EXPOSUREMODE_NIGHT,
* - MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW,
* - MMAL_PARAM_EXPOSUREMODE_BACKLIGHT,
* - MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT,
* - MMAL_PARAM_EXPOSUREMODE_SPORTS,
* - MMAL_PARAM_EXPOSUREMODE_SNOW,
* - MMAL_PARAM_EXPOSUREMODE_BEACH,
* - MMAL_PARAM_EXPOSUREMODE_VERYLONG,
* - MMAL_PARAM_EXPOSUREMODE_FIXEDFPS,
* - MMAL_PARAM_EXPOSUREMODE_ANTISHAKE,
* - MMAL_PARAM_EXPOSUREMODE_FIREWORKS,
*
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_exposure_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_EXPOSUREMODE_T mode)
{
MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = {{MMAL_PARAMETER_EXPOSURE_MODE,sizeof(exp_mode)}, mode};
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set(camera->control, &exp_mode.hdr));
}
/**
* Set the aWB (auto white balance) mode for images
* @param camera Pointer to camera component
* @param awb_mode Value to set from
* - MMAL_PARAM_AWBMODE_OFF,
* - MMAL_PARAM_AWBMODE_AUTO,
* - MMAL_PARAM_AWBMODE_SUNLIGHT,
* - MMAL_PARAM_AWBMODE_CLOUDY,
* - MMAL_PARAM_AWBMODE_SHADE,
* - MMAL_PARAM_AWBMODE_TUNGSTEN,
* - MMAL_PARAM_AWBMODE_FLUORESCENT,
* - MMAL_PARAM_AWBMODE_INCANDESCENT,
* - MMAL_PARAM_AWBMODE_FLASH,
* - MMAL_PARAM_AWBMODE_HORIZON,
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_awb_mode(MMAL_COMPONENT_T *camera, MMAL_PARAM_AWBMODE_T awb_mode)
{
MMAL_PARAMETER_AWBMODE_T param = {{MMAL_PARAMETER_AWB_MODE,sizeof(param)}, awb_mode};
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set(camera->control, ¶m.hdr));
}
/**
* Set the image effect for the images
* @param camera Pointer to camera component
* @param imageFX Value from
* - MMAL_PARAM_IMAGEFX_NONE,
* - MMAL_PARAM_IMAGEFX_NEGATIVE,
* - MMAL_PARAM_IMAGEFX_SOLARIZE,
* - MMAL_PARAM_IMAGEFX_POSTERIZE,
* - MMAL_PARAM_IMAGEFX_WHITEBOARD,
* - MMAL_PARAM_IMAGEFX_BLACKBOARD,
* - MMAL_PARAM_IMAGEFX_SKETCH,
* - MMAL_PARAM_IMAGEFX_DENOISE,
* - MMAL_PARAM_IMAGEFX_EMBOSS,
* - MMAL_PARAM_IMAGEFX_OILPAINT,
* - MMAL_PARAM_IMAGEFX_HATCH,
* - MMAL_PARAM_IMAGEFX_GPEN,
* - MMAL_PARAM_IMAGEFX_PASTEL,
* - MMAL_PARAM_IMAGEFX_WATERCOLOUR,
* - MMAL_PARAM_IMAGEFX_FILM,
* - MMAL_PARAM_IMAGEFX_BLUR,
* - MMAL_PARAM_IMAGEFX_SATURATION,
* - MMAL_PARAM_IMAGEFX_COLOURSWAP,
* - MMAL_PARAM_IMAGEFX_WASHEDOUT,
* - MMAL_PARAM_IMAGEFX_POSTERISE,
* - MMAL_PARAM_IMAGEFX_COLOURPOINT,
* - MMAL_PARAM_IMAGEFX_COLOURBALANCE,
* - MMAL_PARAM_IMAGEFX_CARTOON,
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_imageFX(MMAL_COMPONENT_T *camera, MMAL_PARAM_IMAGEFX_T imageFX)
{
MMAL_PARAMETER_IMAGEFX_T imgFX = {{MMAL_PARAMETER_IMAGE_EFFECT,sizeof(imgFX)}, imageFX};
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set(camera->control, &imgFX.hdr));
}
/* TODO :what to do with the image effects parameters?
MMAL_PARAMETER_IMAGEFX_PARAMETERS_T imfx_param = {{MMAL_PARAMETER_IMAGE_EFFECT_PARAMETERS,sizeof(imfx_param)},
imageFX, 0, {0}};
mmal_port_parameter_set(camera->control, &imfx_param.hdr);
*/
/**
* Set the colour effect for images (Set UV component)
* @param camera Pointer to camera component
* @param colourFX Contains enable state and U and V numbers to set (e.g. 128,128 = Black and white)
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_colourFX(MMAL_COMPONENT_T *camera, const MMAL_PARAM_COLOURFX_T *colourFX)
{
MMAL_PARAMETER_COLOURFX_T colfx = {{MMAL_PARAMETER_COLOUR_EFFECT,sizeof(colfx)}, 0, 0, 0};
if (!camera)
return 1;
colfx.enable = colourFX->enable;
colfx.u = colourFX->u;
colfx.v = colourFX->v;
return mmal_status_to_int(mmal_port_parameter_set(camera->control, &colfx.hdr));
}
/**
* Set the rotation of the image
* @param camera Pointer to camera component
* @param rotation Degree of rotation (any number, but will be converted to 0,90,180 or 270 only)
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_rotation(MMAL_COMPONENT_T *camera, int rotation)
{
int ret;
int my_rotation = ((rotation % 360 ) / 90) * 90;
ret = mmal_port_parameter_set_int32(camera->output[0], MMAL_PARAMETER_ROTATION, my_rotation);
mmal_port_parameter_set_int32(camera->output[1], MMAL_PARAMETER_ROTATION, my_rotation);
mmal_port_parameter_set_int32(camera->output[2], MMAL_PARAMETER_ROTATION, my_rotation);
return ret;
}
/**
* Set the flips state of the image
* @param camera Pointer to camera component
* @param hflip If true, horizontally flip the image
* @param vflip If true, vertically flip the image
*
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_flips(MMAL_COMPONENT_T *camera, int hflip, int vflip)
{
MMAL_PARAMETER_MIRROR_T mirror = {{MMAL_PARAMETER_MIRROR, sizeof(MMAL_PARAMETER_MIRROR_T)}, MMAL_PARAM_MIRROR_NONE};
if (hflip && vflip)
mirror.value = MMAL_PARAM_MIRROR_BOTH;
else
if (hflip)
mirror.value = MMAL_PARAM_MIRROR_HORIZONTAL;
else
if (vflip)
mirror.value = MMAL_PARAM_MIRROR_VERTICAL;
mmal_port_parameter_set(camera->output[0], &mirror.hdr);
mmal_port_parameter_set(camera->output[1], &mirror.hdr);
return mmal_port_parameter_set(camera->output[2], &mirror.hdr);
}
/**
* Set the ROI of the sensor to use for captures/preview
* @param camera Pointer to camera component
* @param rect Normalised coordinates of ROI rectangle
*
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_ROI(MMAL_COMPONENT_T *camera, PARAM_FLOAT_RECT_T rect)
{
MMAL_PARAMETER_INPUT_CROP_T crop = {{MMAL_PARAMETER_INPUT_CROP, sizeof(MMAL_PARAMETER_INPUT_CROP_T)}};
crop.rect.x = (65536 * rect.x);
crop.rect.y = (65536 * rect.y);
crop.rect.width = (65536 * rect.w);
crop.rect.height = (65536 * rect.h);
return mmal_port_parameter_set(camera->control, &crop.hdr);
}
/**
* Adjust the exposure time used for images
* @param camera Pointer to camera component
* @param shutter speed in microseconds
* @return 0 if successful, non-zero if any parameters out of range
*/
int raspicamcontrol_set_shutter_speed(MMAL_COMPONENT_T *camera, int speed)
{
if (!camera)
return 1;
return mmal_status_to_int(mmal_port_parameter_set_uint32(camera->control, MMAL_PARAMETER_SHUTTER_SPEED, speed));
}
/**
* Asked GPU how much memory it has allocated
*
* @return amount of memory in MB
*/
static int raspicamcontrol_get_mem_gpu(void)
{
char response[80] = "";
int gpu_mem = 0;
if (vc_gencmd(response, sizeof response, "get_mem gpu") == 0)
vc_gencmd_number_property(response, "gpu", &gpu_mem);
return gpu_mem;
}
/**
* Ask GPU about its camera abilities
* @param supported None-zero if software supports the camera
* @param detected None-zero if a camera has been detected
*/
static void raspicamcontrol_get_camera(int *supported, int *detected)
{
char response[80] = "";
if (vc_gencmd(response, sizeof response, "get_camera") == 0)
{
if (supported)
vc_gencmd_number_property(response, "supported", supported);
if (detected)
vc_gencmd_number_property(response, "detected", detected);
}
}
/**
* Check to see if camera is supported, and we have allocated enough meooryAsk GPU about its camera abilities
* @param supported None-zero if software supports the camera
* @param detected None-zero if a camera has been detected
*/
void raspicamcontrol_check_configuration(int min_gpu_mem)
{
int gpu_mem = raspicamcontrol_get_mem_gpu();
int supported = 0, detected = 0;
raspicamcontrol_get_camera(&supported, &detected);
if (!supported)
vcos_log_error("Camera is not enabled in this build. Try running \"sudo raspi-config\" and ensure that \"camera\" has been enabled\n");
else if (gpu_mem < min_gpu_mem)
vcos_log_error("Only %dM of gpu_mem is configured. Try running \"sudo raspi-config\" and ensure that \"memory_split\" has a value of %d or greater\n", gpu_mem, min_gpu_mem);
else if (!detected)
vcos_log_error("Camera is not detected. Please check carefully the camera module is installed correctly\n");
else
vcos_log_error("Failed to run camera app. Please check for firmware updates\n");
}