bool V4Linux2Camera::getCameraSettingAuto(int mode) {

    if (!hasCameraSetting(mode)) return false;
    //v4l2_ext_control v4l2_ctrl[2];
    //v4l2_control v4l2_ctrl;
    //memset(&v4l2_ctrl, 0, sizeof (v4l2_ctrl));

    switch (mode) {
        case GAIN: return getCameraSetting(AUTO_GAIN);
        case EXPOSURE:
            v4l2_auto_ctrl[0].id = V4L2_CID_EXPOSURE_AUTO;
            v4l2_auto_ctrl[1].id = V4L2_CID_EXPOSURE_AUTO_PRIORITY;
           break;
        case WHITE: return getCameraSetting(AUTO_WHITE);
        case COLOR_HUE: return getCameraSetting(AUTO_HUE);
        default: return false;
    }

    if ((xioctl(dev_handle, VIDIOC_G_EXT_CTRLS, &v4l2_auto_ctrl)) < 0) {
        printf("Unable to get AUTO mode: %s\n" , strerror(errno));
        return false;
    }

    if (mode==EXPOSURE) {
        //printf("get auto mode: %d %d\n",v4l2_auto_ctrl[0].value,v4l2_auto_ctrl[1].value);
        return (v4l2_auto_ctrl[1].value<1);
    } else return false;
}
	void CameraEngine::drawGUI(SDL_Surface *display) {
		if(!settingsDialog) return;
		unsigned char* disp = (unsigned char*)(display->pixels);
		
		int settingValue = getCameraSetting(currentCameraSetting);
		int maxValue = getMaxCameraSetting(currentCameraSetting);
		int minValue = getMinCameraSetting(currentCameraSetting);
		int valueRange = maxValue - minValue;

		const char *settingText = NULL;
		switch (currentCameraSetting) {
			case BRIGHTNESS:	settingText = "Brightness"; break;
			case GAIN:		settingText = "Gain"; break;
			case SHUTTER:		settingText = "Shutter"; break;
			case EXPOSURE:		settingText = "Exposure"; break;
			case SHARPNESS:		settingText = "Sharpness"; break;
			case FOCUS:		settingText = "Focus"; break;
			case GAMMA:		settingText = "Gamma"; break;
		}
		
		int x_offset=width/2-128;
		int y_offset=height-100;

		char displayText[256];
		sprintf(displayText,"%s %d",settingText,settingValue);
		FontTool::drawText(x_offset+128-(FontTool::getTextWidth(displayText)/2),y_offset-FontTool::getFontHeight(),displayText,display);
		
		// draw the border
		for (int i=x_offset;i<(x_offset+256);i++) {
			int pixel=(width*y_offset+i)*3+1;
			disp[pixel]=255;
			pixel=(width*(y_offset+25)+i)*3+1;
			disp[pixel]=255;
		}
		for (int i=y_offset;i<(y_offset+25);i++) {
			int pixel=(width*i+x_offset)*3+1;
			disp[pixel]=255;
			pixel=(width*i+x_offset+256)*3+1;
			disp[pixel]=255;
		}

		// draw the bar
		int xpos = (int)(254*(((float)settingValue-(float)minValue)/(float)valueRange));
		for (int i=x_offset+2;i<=(x_offset+xpos);i++) {
			for (int j=y_offset+2;j<=(y_offset+23);j++) {
				int pixel=(width*j+i)*3+1;
				disp[pixel]=255;
			}
		}


	}
void CameraEngine::control(int key) {
    if(!settingsDialog) return;
    int value,min,max,step = 0;
    switch(key) {
    case SDLK_LEFT:
        value = getCameraSetting(currentCameraSetting);
        min = getMinCameraSetting(currentCameraSetting);
        max = getMaxCameraSetting(currentCameraSetting);
        step = getCameraSettingStep(currentCameraSetting);
        if (step==1) step = (int)((float)max/256.0f);
        if (step<1) step=1;
        value -= step;
        if (value<min) value=min;
        setCameraSetting(currentCameraSetting,value);
        break;
    case SDLK_RIGHT:
        value = getCameraSetting(currentCameraSetting);
        min = getMinCameraSetting(currentCameraSetting);
        max = getMaxCameraSetting(currentCameraSetting);
        step = getCameraSettingStep(currentCameraSetting);
        if (step==1) step = (int)((float)max/256.0f);
        if (step<1) step=1;
        value += step;
        if (value>max) value=max;
        setCameraSetting(currentCameraSetting,value);
        break;
    case SDLK_UP:
        currentCameraSetting--;
        if(currentCameraSetting<0) currentCameraSetting=GAMMA;
        break;
    case SDLK_DOWN:
        currentCameraSetting++;
        if(currentCameraSetting>GAMMA) currentCameraSetting=0;
        break;
    }

}
void CameraEngine::updateSettings()  {

    int brightness = getCameraSetting(BRIGHTNESS);
    if (brightness==getMinCameraSetting(BRIGHTNESS)) config.brightness=SETTING_MIN;
    if (brightness==getMaxCameraSetting(BRIGHTNESS)) config.brightness=SETTING_MAX;
    if (brightness==getDefaultCameraSetting(BRIGHTNESS)) config.brightness=SETTING_DEFAULT;
    //printf("brightness %d\n",brightness);

    int contrast = getCameraSetting(CONTRAST);
    if (contrast==getMinCameraSetting(CONTRAST)) config.contrast=SETTING_MIN;
    if (contrast==getMaxCameraSetting(CONTRAST)) config.contrast=SETTING_MAX;
    if (contrast==getDefaultCameraSetting(CONTRAST)) config.contrast=SETTING_DEFAULT;
    //printf("contrast %d\n",contrast);

    int gain = getCameraSetting(GAIN);
    if (gain==getMinCameraSetting(GAIN)) config.gain=SETTING_MIN;
    if (gain==getMaxCameraSetting(GAIN)) config.gain=SETTING_MAX;
    if (gain==getDefaultCameraSetting(GAIN)) config.gain=SETTING_DEFAULT;
    //printf("gain %d\n",gain);

    int exposure = getCameraSetting(EXPOSURE);
    if (exposure==getMinCameraSetting(EXPOSURE)) config.exposure=SETTING_MIN;
    if (exposure==getMaxCameraSetting(EXPOSURE)) config.exposure=SETTING_MAX;
    if (getCameraSettingAuto(EXPOSURE)==true) config.exposure=SETTING_AUTO;
    if (exposure==getDefaultCameraSetting(EXPOSURE)) config.exposure=SETTING_DEFAULT;
    //printf("exposure %d\n",exposure);

    int sharpness = getCameraSetting(SHARPNESS);
    if (sharpness==getMinCameraSetting(SHARPNESS)) config.sharpness=SETTING_MIN;
    if (sharpness==getMaxCameraSetting(SHARPNESS)) config.sharpness=SETTING_MAX;
    if (sharpness==getDefaultCameraSetting(SHARPNESS)) config.sharpness=SETTING_DEFAULT;
    //printf("sharpness %d\n",sharpness);

    int focus = getCameraSetting(FOCUS);
    if (focus==getMinCameraSetting(FOCUS)) config.focus=SETTING_MIN;
    if (focus==getMaxCameraSetting(FOCUS)) config.focus=SETTING_MAX;
    if (focus==getDefaultCameraSetting(FOCUS)) config.focus=SETTING_DEFAULT;
    //printf("focus %d\n",focus);

    int gamma = getCameraSetting(GAMMA);
    if (gamma==getMinCameraSetting(GAMMA)) config.gamma=SETTING_MIN;
    if (gamma==getMaxCameraSetting(GAMMA)) config.gamma=SETTING_MAX;
    if (gamma==getDefaultCameraSetting(GAMMA)) config.gamma=SETTING_DEFAULT;
    if (getCameraSettingAuto(GAMMA)==true) config.gamma=SETTING_AUTO;
    //printf("gamma %d\n",gamma);

}
bool videoInputCamera::setCameraSetting(int mode, int setting) {

	int current_setting = getCameraSetting(mode);
	if (setting==current_setting) return true;

	long flags = VideoProcAmp_Flags_Manual;

	switch (mode) {
	case BRIGHTNESS: setVideoSettingValue(VideoProcAmp_Brightness, setting, flags); break;
	case CONTRAST: setVideoSettingValue(VideoProcAmp_Contrast, setting, flags); break;
	case GAIN: setVideoSettingValue(VideoProcAmp_Gain, setting, flags); break;
	case EXPOSURE: setVideoControlValue(CameraControl_Exposure, setting, flags); break;
	case SHARPNESS: setVideoSettingValue(VideoProcAmp_Sharpness, setting, flags); break;
	case FOCUS: setVideoControlValue(CameraControl_Focus, setting, flags); break;
	case GAMMA: setVideoSettingValue(VideoProcAmp_Gamma, setting, flags); break;
	case WHITE: setVideoSettingValue(VideoProcAmp_WhiteBalance, setting, flags); break;
	case BACKLIGHT: setVideoSettingValue(VideoProcAmp_BacklightCompensation, setting, flags); break;
	case SATURATION: setVideoSettingValue(VideoProcAmp_Saturation, setting, flags); break;
	case COLOR_HUE: setVideoSettingValue(VideoProcAmp_Hue, setting, flags); break;
	default: return false;
	}

	return true;
}
void cameraSettingsWidget::createConnections()                    //!< Connect all of the child widgets.
{
    // Setup Shift Gain signals
    connect(shiftGainSlider,SIGNAL(valueChanged(int)),shiftGainSpinBox,SLOT(setValue(int)));
    connect(shiftGainSpinBox,SIGNAL(valueChanged(int)),shiftGainSlider,SLOT(setValue(int)));
    connect(shiftGainSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift Exposure signals
    connect(shiftExposureSlider,SIGNAL(valueChanged(int)),shiftExposureSpinBox,SLOT(setValue(int)));
    connect(shiftExposureSpinBox,SIGNAL(valueChanged(int)),shiftExposureSlider,SLOT(setValue(int)));
    connect(shiftExposureSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift BlueChroma signals
    connect(shiftBlueChromaSlider,SIGNAL(valueChanged(int)),shiftBlueChromaSpinBox,SLOT(setValue(int)));
    connect(shiftBlueChromaSpinBox,SIGNAL(valueChanged(int)),shiftBlueChromaSlider,SLOT(setValue(int)));
    connect(shiftBlueChromaSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift RedChroma signals
    connect(shiftRedChromaSlider,SIGNAL(valueChanged(int)),shiftRedChromaSpinBox,SLOT(setValue(int)));
    connect(shiftRedChromaSpinBox,SIGNAL(valueChanged(int)),shiftRedChromaSlider,SLOT(setValue(int)));
    connect(shiftRedChromaSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift Brightness signals
    connect(shiftBrightnessSlider,SIGNAL(valueChanged(int)),shiftBrightnessSpinBox,SLOT(setValue(int)));
    connect(shiftBrightnessSpinBox,SIGNAL(valueChanged(int)),shiftBrightnessSlider,SLOT(setValue(int)));
    connect(shiftBrightnessSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift Saturation signals
    connect(shiftSaturationSlider,SIGNAL(valueChanged(int)),shiftSaturationSpinBox,SLOT(setValue(int)));
    connect(shiftSaturationSpinBox,SIGNAL(valueChanged(int)),shiftSaturationSlider,SLOT(setValue(int)));
    connect(shiftSaturationSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift Contrast signals.
    connect(shiftContrastSlider,SIGNAL(valueChanged(int)),shiftContrastSpinBox,SLOT(setValue(int)));
    connect(shiftContrastSpinBox,SIGNAL(valueChanged(int)),shiftContrastSlider,SLOT(setValue(int)));
    connect(shiftContrastSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Shift Hue signals
    connect(shiftHueSlider,SIGNAL(valueChanged(int)),shiftHueSpinBox,SLOT(setValue(int)));
    connect(shiftHueSpinBox,SIGNAL(valueChanged(int)),shiftHueSlider,SLOT(setValue(int)));
    connect(shiftHueSlider,SIGNAL(valueChanged(int)),this,SLOT(cameraSettingsChanged()));

    // Setup Auto:
    //QCheckBox* TopCameraSelected;
    //QCheckBox* BottomCameraSelected;
    connect(AutoGainSelected,SIGNAL(clicked()),this,SLOT(cameraSettingsChanged()));
    connect(AutoWhiteBalanceSelected,SIGNAL(clicked()),this,SLOT(cameraSettingsChanged()));
    connect(AutoExposureSelected,SIGNAL(clicked()),this,SLOT(cameraSettingsChanged()));

    connect(TopCameraSelected,SIGNAL(clicked()),BottomCameraSelected,SLOT(toggle()));
    connect(BottomCameraSelected,SIGNAL(clicked()),TopCameraSelected,SLOT(toggle()));
    connect(TopCameraSelected,SIGNAL(clicked()),this,SLOT(cameraSettingsChanged()));
    connect(BottomCameraSelected,SIGNAL(clicked()),this,SLOT(cameraSettingsChanged()));


    connect(nameLineEdit, SIGNAL(textChanged(QString)),this,SLOT(updateRobotName(QString)));
    connect(getCameraSettingsButton,SIGNAL(pressed()),this,SLOT(getCameraSetting()));
    connect(streamCameraSettingsButton,SIGNAL(pressed()),this,SLOT(streamCameraSetting()));
    connect(stopStreamCameraSettingsButton,SIGNAL(pressed()),this,SLOT(stopStreamCameraSetting()));

    connect(&readPacketTimer,SIGNAL(timeout()),this,SLOT(readPendingData()));
    connect(&timer,SIGNAL(timeout()),this,SLOT(sendSettingsToRobot()));
    connect(StartSavingImagesButton, SIGNAL(pressed()),this,SLOT(sendStartSavingImagesJob()));
    connect(StopSavingImagesButton, SIGNAL(pressed()),this,SLOT(sendStopSavingImagesJob()));
}