From 65e696a3f36a8f93a81fba619e65f63b24ad971f Mon Sep 17 00:00:00 2001 From: Richard Franks Date: Wed, 19 May 2021 14:29:19 +0100 Subject: [PATCH] Config file options (#12) Added config parameters for some camera settings --- etc/raspindi.conf.default | 10 ++ src/main.cpp | 340 ++++++++++++++++++++++++++++++-------- 2 files changed, 285 insertions(+), 65 deletions(-) diff --git a/etc/raspindi.conf.default b/etc/raspindi.conf.default index c83b0d4..25136cc 100644 --- a/etc/raspindi.conf.default +++ b/etc/raspindi.conf.default @@ -3,3 +3,13 @@ camera_number = "-1"; height="720"; width="1280"; fps="25"; + +# awb: "auto"; // Options: auto, sunlight, cloudy, shade, tungsten, fluorescent, incadescent, flash, horizon, max, off +# saturation: 0; // Value in range 0 - 100 +# sharpness: 0; // Value in range 0 - 100 +# contrast: 0; // Value in range 0 - 100 +# brightness: 50; // Value in range 0 - 100 +# exposuremode: "auto"; // Options: auto, night, nightpreview, backlight, spotlight, sports, snow, beach, verylong, fixedfps, antishake, fireworks, max, off +# meteringmode: "average"; // Options: average, spot, backlit, matrix, max +# rotation: 0; // Options: 0, 90, 180, 270 +# mirror: "none"; // Options: none, horizontal, vertical, both \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index fc31fb2..ba11c0b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -78,6 +78,239 @@ int loadConfig() return 0; } +MMAL_PARAM_AWBMODE_T getAwbMode() +{ + try + { + std::string value = cfg.lookup("awb"); + if (value == "sunlight") + { + return MMAL_PARAM_AWBMODE_SUNLIGHT; + } + if (value == "cloudy") + { + return MMAL_PARAM_AWBMODE_CLOUDY; + } + if (value == "shade") + { + return MMAL_PARAM_AWBMODE_SHADE; + } + if (value == "tungsten") + { + return MMAL_PARAM_AWBMODE_TUNGSTEN; + } + if (value == "fluorescent") + { + return MMAL_PARAM_AWBMODE_FLUORESCENT; + } + if (value == "incandescent") + { + return MMAL_PARAM_AWBMODE_INCANDESCENT; + } + if (value == "flash") + { + return MMAL_PARAM_AWBMODE_FLASH; + } + if (value == "horizon") + { + return MMAL_PARAM_AWBMODE_HORIZON; + } + if (value == "max") + { + return MMAL_PARAM_AWBMODE_MAX; + } + if (value == "off") + { + return MMAL_PARAM_AWBMODE_OFF; + } + return MMAL_PARAM_AWBMODE_AUTO; + } catch (libconfig::SettingNotFoundException) + { + return MMAL_PARAM_AWBMODE_AUTO; + } +} +MMAL_PARAM_EXPOSUREMODE_T getExposureMode() +{ + // Options: auto, night, nightpreview, backlight, spotlight, sports, snow, beach, verylong, fixedfps, antishake, fireworks, max, off + try + { + std::string value = cfg.lookup("exposuremode"); + if (value == "night") + { + return MMAL_PARAM_EXPOSUREMODE_NIGHT; + } + if (value == "nightpreview") + { + return MMAL_PARAM_EXPOSUREMODE_NIGHTPREVIEW; + } + if (value == "backlight") + { + return MMAL_PARAM_EXPOSUREMODE_BACKLIGHT; + } + if (value == "spotlight") + { + return MMAL_PARAM_EXPOSUREMODE_SPOTLIGHT; + } + if (value == "sports") + { + return MMAL_PARAM_EXPOSUREMODE_SPORTS; + } + if (value == "snow") + { + return MMAL_PARAM_EXPOSUREMODE_SNOW; + } + if (value == "beach") + { + return MMAL_PARAM_EXPOSUREMODE_BEACH; + } + if (value == "verylong") + { + return MMAL_PARAM_EXPOSUREMODE_VERYLONG; + } + if (value == "fixedfps") + { + return MMAL_PARAM_EXPOSUREMODE_FIXEDFPS; + } + if (value == "antishake") + { + return MMAL_PARAM_EXPOSUREMODE_ANTISHAKE; + } + if (value == "fireworks") + { + return MMAL_PARAM_EXPOSUREMODE_FIREWORKS; + } + if (value == "max") + { + return MMAL_PARAM_EXPOSUREMODE_MAX; + } + if (value == "off") + { + return MMAL_PARAM_EXPOSUREMODE_OFF; + } + return MMAL_PARAM_EXPOSUREMODE_AUTO; + } catch (libconfig::SettingNotFoundException) + { + return MMAL_PARAM_EXPOSUREMODE_AUTO; + } +} +MMAL_PARAM_EXPOSUREMETERINGMODE_T getMeteringMode() +{ + // Options: average, spot, backlit, matrix, max + try + { + std::string value = cfg.lookup("meteringmode"); + if (value == "spot") + { + return MMAL_PARAM_EXPOSUREMETERINGMODE_SPOT; + } + if (value == "backlit") + { + return MMAL_PARAM_EXPOSUREMETERINGMODE_BACKLIT; + } + if (value == "matrix") + { + return MMAL_PARAM_EXPOSUREMETERINGMODE_MATRIX; + } + if (value == "max") + { + return MMAL_PARAM_EXPOSUREMETERINGMODE_MAX; + } + return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE; + } catch (libconfig::SettingNotFoundException) + { + return MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE; + } +} +MMAL_PARAM_MIRROR_T getMirror() +{ + // Options: none, horizontal, vertical, both + try + { + std::string value = cfg.lookup("mirror"); + if (value == "horizontal") + { + return MMAL_PARAM_MIRROR_HORIZONTAL; + } + if (value == "vertical") + { + return MMAL_PARAM_MIRROR_VERTICAL; + } + if (value == "both") + { + return MMAL_PARAM_MIRROR_BOTH; + } + return MMAL_PARAM_MIRROR_NONE; + } catch (libconfig::SettingNotFoundException) + { + return MMAL_PARAM_MIRROR_NONE; + } +} +int _getIntVal(std::string parameter, int defaultValue) +{ + try + { + int value = cfg.lookup(parameter); + if (value > 100) + { + std::cerr << "Invalid value for " << parameter << ": " << value << std::endl; + return 100; + } + if (value < 0) + { + std::cerr << "Invalid value for " << parameter << ": " << value << std::endl; + return 0; + } + return value; + } catch (libconfig::SettingNotFoundException) + { + return defaultValue; + } +} +int getSaturation() +{ + // Values between 0 - 100; default 0 + return _getIntVal("saturation", 0); +} +int getSharpness() +{ + // Values between 0 - 100; default 0 + return _getIntVal("sharpness", 0); +} +int getContrast() +{ + // Values between 0 - 100; default 0 + return _getIntVal("contrast", 0); +} +int getBrightness() +{ + // Values between 0 - 100; default 0 + return _getIntVal("brightness", 50); +} +int getRotation() +{ + // Options: 0, 90, 180, 270 + try + { + int value = cfg.lookup("rotation"); + switch(value) + { + case 90: + return 90; + case 180: + return 180; + case 270: + return 270; + case 0: + default: + return 0; + } + } catch (libconfig::SettingNotFoundException) + { + return 0; + } +} + + int main(int argc, char* argv[]) { for (int i=0; i < argc; i++) { @@ -204,75 +437,52 @@ int main(int argc, char* argv[]) exit(1); } - // if ( mmal_port_parameter_set_rational ( camera->control, MMAL_PARAMETER_SATURATION, ( MMAL_RATIONAL_T ) { - // 0, 100 - // } ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set saturation parameter.\n"; - // if ( mmal_port_parameter_set_rational ( camera->control, MMAL_PARAMETER_SHARPNESS, ( MMAL_RATIONAL_T ) { - // 0, 100 - // } ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set sharpness parameter.\n"; - - // if ( mmal_port_parameter_set_rational ( camera->control, MMAL_PARAMETER_CONTRAST, ( MMAL_RATIONAL_T ) { - // 0, 100 - // } ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set contrast parameter.\n"; - - // mmal_port_parameter_set_rational ( camera->control, MMAL_PARAMETER_BRIGHTNESS, ( MMAL_RATIONAL_T ) { - // 50, 100 - // } ); - - // if ( mmal_port_parameter_set_uint32 ( camera->control, MMAL_PARAMETER_ISO, 400 ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set ISO parameter.\n"; - - // if ( mmal_port_parameter_set_uint32 ( camera->control, MMAL_PARAMETER_SHUTTER_SPEED, 0 ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set shutter parameter.\n"; - - - // MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = {{MMAL_PARAMETER_EXPOSURE_MODE,sizeof ( exp_mode ) }, MMAL_PARAM_EXPOSUREMODE_AUTO }; - // if ( mmal_port_parameter_set ( camera->control, &exp_mode.hdr ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set exposure parameter.\n"; - - // if ( mmal_port_parameter_set_int32 ( camera->control, MMAL_PARAMETER_EXPOSURE_COMP , 0 ) !=MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set Exposure Compensation parameter.\n"; - - - // MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = {{MMAL_PARAMETER_EXP_METERING_MODE, sizeof ( meter_mode ) }, MMAL_PARAM_EXPOSUREMETERINGMODE_AVERAGE }; - // if ( mmal_port_parameter_set ( camera->control, &meter_mode.hdr ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set metering parameter.\n"; - - // MMAL_PARAMETER_IMAGEFX_T imgFX = {{MMAL_PARAMETER_IMAGE_EFFECT,sizeof ( imgFX ) }, MMAL_PARAM_IMAGEFX_NONE }; - // if ( mmal_port_parameter_set ( camera->control, &imgFX.hdr ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set image effect parameter.\n"; - - - // mmal_port_parameter_set_int32 ( camera->output[0], MMAL_PARAMETER_ROTATION, 0 ); - // mmal_port_parameter_set_int32 ( camera->output[1], MMAL_PARAMETER_ROTATION, 0 ); - // mmal_port_parameter_set_int32 ( camera->output[2], MMAL_PARAMETER_ROTATION, 0 ); - - - // MMAL_PARAMETER_MIRROR_T mirror = {{MMAL_PARAMETER_MIRROR, sizeof ( MMAL_PARAMETER_MIRROR_T ) }, MMAL_PARAM_MIRROR_NONE}; - // if ( mmal_port_parameter_set ( camera->output[0], &mirror.hdr ) != MMAL_SUCCESS || - // mmal_port_parameter_set ( camera->output[1], &mirror.hdr ) != MMAL_SUCCESS || - // mmal_port_parameter_set ( camera->output[2], &mirror.hdr ) ) - // std::cout << __func__ << ": Failed to set horizontal/vertical flip parameter.\n"; - + MMAL_PARAMETER_AWBMODE_T awbParam = {{MMAL_PARAMETER_AWB_MODE,sizeof(MMAL_PARAMETER_AWBMODE_T)}, getAwbMode()}; + if(mmal_port_parameter_set(camera->control, &awbParam.hdr) != MMAL_SUCCESS) + { + std::cout << "Failed to set awb parameter." << std::endl; + } + if(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_SATURATION, (MMAL_RATIONAL_T){getSaturation(), 100}) != MMAL_SUCCESS) + { + std::cout << "Failed to set saturation parameter." << std::endl; + } + if(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_SHARPNESS, (MMAL_RATIONAL_T) {getSharpness(), 100}) != MMAL_SUCCESS) + { + std::cout << "Failed to set sharpness parameter." << std::endl; + } + if(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_CONTRAST, (MMAL_RATIONAL_T) {getContrast(), 100}) != MMAL_SUCCESS) + { + std::cout << "Failed to set contrast parameter." << std::endl; + } + if(mmal_port_parameter_set_rational(camera->control, MMAL_PARAMETER_BRIGHTNESS, (MMAL_RATIONAL_T) {getBrightness(), 100}) != MMAL_SUCCESS) + { + std::cout << "Failed to set brightness parameter." << std::endl; + } + MMAL_PARAMETER_EXPOSUREMODE_T exp_mode = {{MMAL_PARAMETER_EXPOSURE_MODE, sizeof(MMAL_PARAMETER_EXPOSUREMODE_T)}, getExposureMode()}; + if(mmal_port_parameter_set(camera->control, &exp_mode.hdr) != MMAL_SUCCESS) + { + std::cout << "Failed to set exposure parameter." << std::endl; + } - // if ( mmal_port_parameter_set_boolean ( camera->control, MMAL_PARAMETER_VIDEO_STABILISATION, false ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set video stabilization parameter.\n"; + MMAL_PARAMETER_EXPOSUREMETERINGMODE_T meter_mode = {{MMAL_PARAMETER_EXP_METERING_MODE, sizeof(MMAL_PARAMETER_EXPOSUREMETERINGMODE_T)}, getMeteringMode()}; + if(mmal_port_parameter_set(camera->control, &meter_mode.hdr) != MMAL_SUCCESS) + { + std::cout << "Failed to set metering parameter." << std::endl; + } + int rotation = getRotation(); + mmal_port_parameter_set_int32(camera->output[0], MMAL_PARAMETER_ROTATION, rotation); + mmal_port_parameter_set_int32(camera->output[1], MMAL_PARAMETER_ROTATION, rotation); + mmal_port_parameter_set_int32(camera->output[2], MMAL_PARAMETER_ROTATION, rotation); - // MMAL_PARAMETER_AWBMODE_T awbParam = {{MMAL_PARAMETER_AWB_MODE,sizeof ( awbParam ) }, MMAL_PARAM_AWBMODE_AUTO }; - // if ( mmal_port_parameter_set ( camera->control, &awbParam.hdr ) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set AWB parameter.\n"; + MMAL_PARAMETER_MIRROR_T mirror = {{MMAL_PARAMETER_MIRROR, sizeof(MMAL_PARAMETER_MIRROR_T)}, getMirror()}; - // MMAL_PARAMETER_AWB_GAINS_T param = {{MMAL_PARAMETER_CUSTOM_AWB_GAINS,sizeof(param)}, {0,0}, {0,0}}; - // param.r_gain.num = (unsigned int)(65536); - // param.b_gain.num = (unsigned int)(65536); - // param.r_gain.den = param.b_gain.den = 65536; - // if ( mmal_port_parameter_set(camera->control, ¶m.hdr) != MMAL_SUCCESS ) - // std::cout << __func__ << ": Failed to set AWBG gains parameter.\n"; - + if ( mmal_port_parameter_set(camera->output[0], &mirror.hdr) != MMAL_SUCCESS || + mmal_port_parameter_set(camera->output[1], &mirror.hdr) != MMAL_SUCCESS || + mmal_port_parameter_set(camera->output[2], &mirror.hdr)) + { + std::cout << "Failed to set flip parameter." << std::endl; + } // Start Capture if (mmal_port_parameter_set_boolean(video_port, MMAL_PARAMETER_CAPTURE, 1) != MMAL_SUCCESS)