29 #if defined(__arm__) || defined(__aarch64__) 34 #include "interface/vcos/vcos.h" 37 #include "interface/mmal/mmal.h" 38 #include "interface/mmal/mmal_logging.h" 39 #include "interface/mmal/util/mmal_default_components.h" 40 #include "interface/mmal/util/mmal_util.h" 41 #include "interface/mmal/util/mmal_util_params.h" 42 #include "interface/vmcs_host/vc_vchi_gencmd.h" 45 typedef struct xref_t {
52 static XREF_T exposure_map[] = { {
"off", MMAL_PARAM_EXPOSUREMODE_OFF },
53 {
"auto", MMAL_PARAM_EXPOSUREMODE_AUTO },
54 {
"night", MMAL_PARAM_EXPOSUREMODE_NIGHT },
55 {
"nightpreview", MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW },
56 {
"backlight", MMAL_PARAM_EXPOSUREMODE_BACKLIGHT },
57 {
"spotlight", MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT },
58 {
"sports", MMAL_PARAM_EXPOSUREMODE_SPORTS },
59 {
"snow", MMAL_PARAM_EXPOSUREMODE_SNOW },
60 {
"beach", MMAL_PARAM_EXPOSUREMODE_BEACH },
61 {
"verylong", MMAL_PARAM_EXPOSUREMODE_VERYLONG },
62 {
"fixedfps", MMAL_PARAM_EXPOSUREMODE_FIXEDFPS },
63 {
"antishake", MMAL_PARAM_EXPOSUREMODE_ANTISHAKE },
64 {
"fireworks", MMAL_PARAM_EXPOSUREMODE_FIREWORKS } };
66 static const int exposure_map_size =
sizeof(exposure_map) /
sizeof(exposure_map[0]);
70 static XREF_T awb_map[] = { {
"off", MMAL_PARAM_AWBMODE_OFF },
71 {
"auto", MMAL_PARAM_AWBMODE_AUTO },
72 {
"sun", MMAL_PARAM_AWBMODE_SUNLIGHT },
73 {
"cloud", MMAL_PARAM_AWBMODE_CLOUDY },
74 {
"shade", MMAL_PARAM_AWBMODE_SHADE },
75 {
"tungsten", MMAL_PARAM_AWBMODE_TUNGSTEN },
76 {
"fluorescent", MMAL_PARAM_AWBMODE_FLUORESCENT },
77 {
"incandescent", MMAL_PARAM_AWBMODE_INCANDESCENT },
78 {
"flash", MMAL_PARAM_AWBMODE_FLASH },
79 {
"horizon", MMAL_PARAM_AWBMODE_HORIZON } };
81 static const int awb_map_size =
sizeof(awb_map) /
sizeof(awb_map[0]);
85 static XREF_T imagefx_map[] = { {
"none", MMAL_PARAM_IMAGEFX_NONE },
86 {
"negative", MMAL_PARAM_IMAGEFX_NEGATIVE },
87 {
"solarise", MMAL_PARAM_IMAGEFX_SOLARIZE },
88 {
"sketch", MMAL_PARAM_IMAGEFX_SKETCH },
89 {
"denoise", MMAL_PARAM_IMAGEFX_DENOISE },
90 {
"emboss", MMAL_PARAM_IMAGEFX_EMBOSS },
91 {
"oilpaint", MMAL_PARAM_IMAGEFX_OILPAINT },
92 {
"hatch", MMAL_PARAM_IMAGEFX_HATCH },
93 {
"gpen", MMAL_PARAM_IMAGEFX_GPEN },
94 {
"pastel", MMAL_PARAM_IMAGEFX_PASTEL },
95 {
"watercolour", MMAL_PARAM_IMAGEFX_WATERCOLOUR },
96 {
"film", MMAL_PARAM_IMAGEFX_FILM },
97 {
"blur", MMAL_PARAM_IMAGEFX_BLUR },
98 {
"saturation", MMAL_PARAM_IMAGEFX_SATURATION },
99 {
"colourswap", MMAL_PARAM_IMAGEFX_COLOURSWAP },
100 {
"washedout", MMAL_PARAM_IMAGEFX_WASHEDOUT },
101 {
"posterise", MMAL_PARAM_IMAGEFX_POSTERISE },
102 {
"colourpoint", MMAL_PARAM_IMAGEFX_COLOURPOINT },
103 {
"colourbalance", MMAL_PARAM_IMAGEFX_COLOURBALANCE },
104 {
"cartoon", MMAL_PARAM_IMAGEFX_CARTOON } };
106 static const int imagefx_map_size =
sizeof(imagefx_map) /
sizeof(imagefx_map[0]);
108 static XREF_T metering_mode_map[] = { {
"average", MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE },
109 {
"spot", MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT },
110 {
"backlit", MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT },
111 {
"matrix", MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX } };
113 static const int metering_mode_map_size =
sizeof(metering_mode_map) /
sizeof(metering_mode_map[0]);
115 #define CommandSharpness 0 116 #define CommandContrast 1 117 #define CommandBrightness 2 118 #define CommandSaturation 3 120 #define CommandVideoStab 5 121 #define CommandEVComp 6 122 #define CommandExposure 7 124 #define CommandImageFX 9 125 #define CommandColourFX 10 126 #define CommandMeterMode 11 127 #define CommandRotation 12 128 #define CommandHFlip 13 129 #define CommandVFlip 14 130 #define CommandROI 15 132 #define parameter_reset -99999 141 static int update_cycle_parameter(
int* option,
int min,
int max,
int increment) {
146 if (*option == parameter_reset)
147 *option = min - increment;
149 *option += increment;
152 *option = parameter_reset;
171 static int parameter = 0;
172 static int parameter_option = parameter_reset;
176 if (parameter == 0) {
178 if (update_cycle_parameter(¶meter_option, -100, 100, 10))
184 }
else if (parameter == 1) {
186 if (update_cycle_parameter(¶meter_option, -100, 100, 10))
192 }
else if (parameter == 2) {
194 if (update_cycle_parameter(¶meter_option, 0, 100, 10))
200 }
else if (parameter == 3) {
202 if (update_cycle_parameter(¶meter_option, -100, 100, 10))
208 }
else if (parameter == 4) {
210 if (update_cycle_parameter(¶meter_option, -10, 10, 4))
216 }
else if (parameter == 5) {
218 if (update_cycle_parameter(¶meter_option, 0, exposure_map_size, 1))
224 }
else if (parameter == 6) {
226 if (update_cycle_parameter(¶meter_option, 0, awb_map_size, 1))
233 if (parameter == 7) {
235 if (update_cycle_parameter(¶meter_option, 0, imagefx_map_size, 1))
242 if (parameter == 8) {
244 switch (parameter_option) {
245 case parameter_reset:
246 parameter_option = 1;
251 parameter_option = 2;
256 parameter_option = parameter_reset;
265 if (parameter == 9) {
266 switch (parameter_option) {
267 case parameter_reset:
269 parameter_option = 1;
274 parameter_option = 2;
279 parameter_option = 3;
285 parameter_option = 4;
290 parameter_option = 5;
295 parameter_option = 6;
300 parameter_option = parameter_reset;
307 if (parameter == 10) {
322 static int map_xref(
const char* str,
const XREF_T* map,
int num_refs) {
325 for (i = 0; i < num_refs; i++) {
326 if (!strcasecmp(str, map[i].mode)) {
327 return map[i].mmal_mode;
340 static const char* unmap_xref(
const int en, XREF_T* map,
int num_refs) {
343 for (i = 0; i < num_refs; i++) {
344 if (en == map[i].mmal_mode) {
358 int i = map_xref(str, exposure_map, exposure_map_size);
361 return (MMAL_PARAM_EXPOSUREMODE_T)i;
363 vcos_log_error(
"Unknown exposure mode: %s", str);
364 return MMAL_PARAM_EXPOSUREMODE_AUTO;
374 int i = map_xref(str, awb_map, awb_map_size);
377 return (MMAL_PARAM_AWBMODE_T)i;
379 vcos_log_error(
"Unknown awb mode: %s", str);
380 return MMAL_PARAM_AWBMODE_AUTO;
389 MMAL_PARAM_IMAGEFX_T imagefx_mode_from_string(
const char* str) {
390 int i = map_xref(str, imagefx_map, imagefx_map_size);
393 return (MMAL_PARAM_IMAGEFX_T)i;
395 vcos_log_error(
"Unknown image fx: %s", str);
396 return MMAL_PARAM_IMAGEFX_NONE;
405 MMAL_PARAM_EXPOSUREMETERINGMODE_T metering_mode_from_string(
const char* str) {
406 int i = map_xref(str, metering_mode_map, metering_mode_map_size);
409 return (MMAL_PARAM_EXPOSUREMETERINGMODE_T)i;
411 vcos_log_error(
"Unknown metering mode: %s", str);
412 return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE;
422 const char* exp_mode = unmap_xref(params.
exposureMode, exposure_map, exposure_map_size);
423 const char* awb_mode = unmap_xref(params.
awbMode, awb_map, awb_map_size);
424 const char* image_effect = unmap_xref(params.
imageEffect, imagefx_map, imagefx_map_size);
425 const char* metering_mode = unmap_xref(params.
exposureMeterMode, metering_mode_map, metering_mode_map_size);
428 fprintf(stderr,
"Saturation %d, ISO %d, Video Stabilisation %s, Exposure " 431 fprintf(stderr,
"Exposure Mode '%s', AWB Mode '%s', Image Effect '%s'\n", exp_mode, awb_mode, image_effect);
432 fprintf(stderr,
"Metering Mode '%s', Colour Effect Enabled %s with U = %d, V = %d\n", metering_mode,
434 fprintf(stderr,
"Rotation %d, hflip %s, vflip %s\n", params.
rotation, params.
hflip ?
"Yes" :
"No",
435 params.
vflip ?
"Yes" :
"No");
436 fprintf(stderr,
"ROI x %lf, y %f, w %f h %f\n", params.
roi.
x, params.
roi.
y, params.
roi.
w, params.
roi.
h);
446 int mmal_status_to_int(MMAL_STATUS_T status) {
447 if (status == MMAL_SUCCESS)
452 vcos_log_error(
"Out of memory");
455 vcos_log_error(
"Out of resources (other than memory)");
458 vcos_log_error(
"Argument is invalid");
461 vcos_log_error(
"Function not implemented");
464 vcos_log_error(
"No such file or directory");
467 vcos_log_error(
"No such device or address");
470 vcos_log_error(
"I/O error");
473 vcos_log_error(
"Illegal seek");
476 vcos_log_error(
"Data is corrupt \attention FIXME: not POSIX");
479 vcos_log_error(
"Component is not ready \attention FIXME: not POSIX");
482 vcos_log_error(
"Component is not configured \attention FIXME: not POSIX");
485 vcos_log_error(
"Port is already connected ");
488 vcos_log_error(
"Port is disconnected");
491 vcos_log_error(
"Resource temporarily unavailable. Try again later");
494 vcos_log_error(
"Bad address");
497 vcos_log_error(
"Unknown status error");
519 params.
awbMode = MMAL_PARAM_AWBMODE_AUTO;
571 if (saturation >= -100 && saturation <= 100) {
572 MMAL_RATIONAL_T value = { saturation, 100 };
573 ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera.control, MMAL_PARAMETER_SATURATION, value));
575 vcos_log_error(
"Invalid saturation value");
590 if (sharpness >= -100 && sharpness <= 100) {
591 MMAL_RATIONAL_T value = { sharpness, 100 };
592 ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera.control, MMAL_PARAMETER_SHARPNESS, value));
594 vcos_log_error(
"Invalid sharpness value");
610 if (contrast >= -100 && contrast <= 100) {
611 MMAL_RATIONAL_T value = { contrast, 100 };
612 ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera.control, MMAL_PARAMETER_CONTRAST, value));
614 vcos_log_error(
"Invalid contrast value");
630 if (brightness >= 0 && brightness <= 100) {
631 MMAL_RATIONAL_T value = { brightness, 100 };
632 ret = mmal_status_to_int(mmal_port_parameter_set_rational(camera.control, MMAL_PARAMETER_BRIGHTNESS, value));
634 vcos_log_error(
"Invalid brightness value");
648 return mmal_status_to_int(mmal_port_parameter_set_uint32(camera.control, MMAL_PARAMETER_ISO, ISO));
662 MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = { { MMAL_PARAMETER_EXP_METERING_MODE,
sizeof(meter_mode) },
665 return mmal_status_to_int(mmal_port_parameter_set(camera.control, &meter_mode.hdr));
675 return mmal_status_to_int(
676 mmal_port_parameter_set_boolean(camera.control, MMAL_PARAMETER_VIDEO_STABILISATION, vstabilisation));
686 return mmal_status_to_int(mmal_port_parameter_set_int32(camera.control, MMAL_PARAMETER_EXPOSURE_COMP, exp_comp));
710 MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = { { MMAL_PARAMETER_EXPOSURE_MODE,
sizeof(exp_mode) }, mode };
712 return mmal_status_to_int(mmal_port_parameter_set(camera.control, &exp_mode.hdr));
732 MMAL_PARAMETER_AWBMODE_T
param = { { MMAL_PARAMETER_AWB_MODE,
sizeof(param) }, awb_mode };
734 return mmal_status_to_int(mmal_port_parameter_set(camera.control, ¶m.hdr));
767 MMAL_PARAMETER_IMAGEFX_T imgFX = { { MMAL_PARAMETER_IMAGE_EFFECT,
sizeof(imgFX) }, imageFX };
769 return mmal_status_to_int(mmal_port_parameter_set(camera.control, &imgFX.hdr));
786 MMAL_PARAMETER_COLOURFX_T colfx = { { MMAL_PARAMETER_COLOUR_EFFECT,
sizeof(colfx) }, 0, 0, 0 };
789 colfx.u = colourFX->
u;
790 colfx.v = colourFX->
v;
792 return mmal_status_to_int(mmal_port_parameter_set(camera.control, &colfx.hdr));
804 int my_rotation = ((rotation % 360) / 90) * 90;
806 ret = mmal_port_parameter_set_int32(camera.output[0], MMAL_PARAMETER_ROTATION, my_rotation);
807 mmal_port_parameter_set_int32(camera.output[1], MMAL_PARAMETER_ROTATION, my_rotation);
808 mmal_port_parameter_set_int32(camera.output[2], MMAL_PARAMETER_ROTATION, my_rotation);
822 MMAL_PARAMETER_MIRROR_T mirror = { { MMAL_PARAMETER_MIRROR,
sizeof(MMAL_PARAMETER_MIRROR_T) },
823 MMAL_PARAM_MIRROR_NONE };
826 mirror.value = MMAL_PARAM_MIRROR_BOTH;
828 mirror.value = MMAL_PARAM_MIRROR_HORIZONTAL;
830 mirror.value = MMAL_PARAM_MIRROR_VERTICAL;
832 mmal_port_parameter_set(camera.output[0], &mirror.hdr);
833 mmal_port_parameter_set(camera.output[1], &mirror.hdr);
834 return mmal_port_parameter_set(camera.output[2], &mirror.hdr);
845 MMAL_PARAMETER_INPUT_CROP_T crop = { { MMAL_PARAMETER_INPUT_CROP,
sizeof(MMAL_PARAMETER_INPUT_CROP_T) } };
847 crop.rect.x = (65536 * rect.
x);
848 crop.rect.y = (65536 * rect.
y);
849 crop.rect.width = (65536 * rect.
w);
850 crop.rect.height = (65536 * rect.
h);
852 return mmal_port_parameter_set(camera.control, &crop.hdr);
862 return mmal_status_to_int(
863 mmal_port_parameter_set_uint32(camera.control, MMAL_PARAMETER_SHUTTER_SPEED, shutter_speed));
905 int supported = 1, detected = 1;
908 vcos_log_error(
"Camera is not enabled in this build. Try running \"sudo " 909 "raspi-config\" and ensure that \"camera\" has been enabled\n");
910 else if (gpu_mem < min_gpu_mem)
911 vcos_log_error(
"Only %dM of gpu_mem is configured. Try running \"sudo " 912 "raspi-config\" and ensure that \"memory_split\" has a value of %d " 914 gpu_mem, min_gpu_mem);
916 vcos_log_error(
"Camera is not detected. Please check carefully the camera module " 917 "is installed correctly\n");
919 vcos_log_error(
"Failed to run camera app. Please check for firmware updates\n");
922 #endif // __arm__ || __aarch64__
MMAL_PARAM_AWBMODE_T awbMode
MMAL_PARAM_COLOURFX_T colourEffects
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
int raspicamcontrol_set_sharpness(MMAL_COMPONENT_T &camera, int sharpness)
int exposureCompensation
0 or 1 (false or true)
int raspicamcontrol_set_exposure_compensation(MMAL_COMPONENT_T &camera, int exp_comp)
int raspicamcontrol_set_shutter_speed(MMAL_COMPONENT_T &camera, int shutter_speed)
PARAM_FLOAT_RECT_T roi
0 or 1
MMAL_PARAM_IMAGEFX_T imageEffect
int raspicamcontrol_set_brightness(MMAL_COMPONENT_T &camera, int brightness)
MMAL_PARAM_EXPOSUREMETERINGMODE_T exposureMeterMode
int raspicamcontrol_set_video_stabilisation(MMAL_COMPONENT_T &camera, int vstabilisation)
void raspicamcontrol_dump_parameters(const RASPICAM_CAMERA_PARAMETERS ¶ms)
void raspicamcontrol_check_configuration(int min_gpu_mem)
MMAL_PARAM_EXPOSUREMODE_T exposure_mode_from_string(const char *str)
int raspicamcontrol_set_exposure_mode(MMAL_COMPONENT_T &camera, MMAL_PARAM_EXPOSUREMODE_T mode)
void raspicamcontrol_set_defaults(RASPICAM_CAMERA_PARAMETERS ¶ms)
int raspicamcontrol_set_ROI(MMAL_COMPONENT_T &camera, PARAM_FLOAT_RECT_T rect)
int raspicamcontrol_set_flips(MMAL_COMPONENT_T &camera, int hflip, int vflip)
int raspicamcontrol_set_awb_mode(MMAL_COMPONENT_T &camera, MMAL_PARAM_AWBMODE_T awb_mode)
MMAL_PARAM_EXPOSUREMODE_T exposureMode
-10 to +10 ?
int raspicamcontrol_set_ISO(MMAL_COMPONENT_T &camera, int ISO)
struct contain camera settings
int videoStabilisation
TODO : what range?
MMAL_PARAM_AWBMODE_T awb_mode_from_string(const char *str)
int raspicamcontrol_set_all_parameters(MMAL_COMPONENT_T &camera, const RASPICAM_CAMERA_PARAMETERS ¶ms)
int raspicamcontrol_set_rotation(MMAL_COMPONENT_T &camera, int rotation)
int raspicamcontrol_set_imageFX(MMAL_COMPONENT_T &camera, MMAL_PARAM_IMAGEFX_T imageFX)
int u
Turn colourFX on or off.
int raspicamcontrol_set_metering_mode(MMAL_COMPONENT_T &camera, MMAL_PARAM_EXPOSUREMETERINGMODE_T mode)
int raspicamcontrol_cycle_test(MMAL_COMPONENT_T &camera)
int raspicamcontrol_set_colourFX(MMAL_COMPONENT_T &camera, const MMAL_PARAM_COLOURFX_T *colourFX)
int raspicamcontrol_set_contrast(MMAL_COMPONENT_T &camera, int contrast)
int brightness
-100 to 100
int raspicamcontrol_set_saturation(MMAL_COMPONENT_T &camera, int saturation)