k4a_result_t UVCCameraReader::GetCameraControl()

in src/color/uvc_camerareader.cpp [708:894]


k4a_result_t UVCCameraReader::GetCameraControl(const k4a_color_control_command_t command,
                                               k4a_color_control_mode_t *mode,
                                               int32_t *pValue)
{
    RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, mode == NULL);
    RETURN_VALUE_IF_ARG(K4A_RESULT_FAILED, pValue == NULL);
    if (!IsInitialized())
    {
        LOG_ERROR("Camera reader is not initialized", 0);
        return K4A_RESULT_FAILED;
    }
    uvc_error_t res = UVC_SUCCESS;
    *mode = K4A_COLOR_CONTROL_MODE_MANUAL;

    switch (command)
    {
    case K4A_COLOR_CONTROL_EXPOSURE_TIME_ABSOLUTE:
    {
        uint8_t ae_mode;
        uint32_t exposure_time;

        res = uvc_get_ae_mode(m_pDeviceHandle, &ae_mode, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get auto exposure mode: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }

        if (ae_mode == UVC_AUTO_EXPOSURE_MODE_MANUAL || ae_mode == UVC_AUTO_EXPOSURE_MODE_SHUTTER_PRIORITY)
        {
            *mode = K4A_COLOR_CONTROL_MODE_MANUAL;
        }
        else if (ae_mode == UVC_AUTO_EXPOSURE_MODE_AUTO || ae_mode == UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY)
        {
            *mode = K4A_COLOR_CONTROL_MODE_AUTO;
        }
        else
        {
            LOG_ERROR("Invalid auto exposure mode returned: %d", ae_mode);
            return K4A_RESULT_FAILED;
        }

        // Get current exposure time value
        res = uvc_get_exposure_abs(m_pDeviceHandle, &exposure_time, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get exposure time abs: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }

        *pValue = MapLinuxExposureToK4a((int32_t)exposure_time);
    }
    break;
    case K4A_COLOR_CONTROL_BRIGHTNESS:
    {
        int16_t brightness;
        res = uvc_get_brightness(m_pDeviceHandle, &brightness, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get brightness: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)brightness;
    }
    break;
    case K4A_COLOR_CONTROL_CONTRAST:
    {
        uint16_t contrast;
        res = uvc_get_contrast(m_pDeviceHandle, &contrast, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get contrast: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)contrast;
    }
    break;
    case K4A_COLOR_CONTROL_SATURATION:
    {
        uint16_t saturation;
        res = uvc_get_saturation(m_pDeviceHandle, &saturation, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get saturation: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)saturation;
    }
    break;
    case K4A_COLOR_CONTROL_SHARPNESS:
    {
        uint16_t sharpness;
        res = uvc_get_sharpness(m_pDeviceHandle, &sharpness, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get sharpness: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)sharpness;
    }
    break;
    case K4A_COLOR_CONTROL_WHITEBALANCE:
    {
        uint8_t wb_mode;
        uint16_t white_balance;

        res = uvc_get_white_balance_temperature_auto(m_pDeviceHandle, &wb_mode, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get auto white balance temperature mode: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }

        if (wb_mode == 0)
        {
            *mode = K4A_COLOR_CONTROL_MODE_MANUAL;
        }
        else if (wb_mode == 1)
        {
            *mode = K4A_COLOR_CONTROL_MODE_AUTO;
        }
        else
        {
            LOG_ERROR("Invalid auto white balance temperature mode returned: %d", wb_mode);
            return K4A_RESULT_FAILED;
        }

        // Get current white balance temperature value
        res = uvc_get_white_balance_temperature(m_pDeviceHandle, &white_balance, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get white balance temperature: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }

        *pValue = white_balance;
    }
    break;
    case K4A_COLOR_CONTROL_BACKLIGHT_COMPENSATION:
    {
        uint16_t backlight_compensation;
        res = uvc_get_backlight_compensation(m_pDeviceHandle, &backlight_compensation, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get backlight compensation: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)backlight_compensation;
    }
    break;
    case K4A_COLOR_CONTROL_GAIN:
    {
        uint16_t gain;
        res = uvc_get_gain(m_pDeviceHandle, &gain, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get gain: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)gain;
    }
    break;
    case K4A_COLOR_CONTROL_POWERLINE_FREQUENCY:
    {
        uint8_t powerline_freq;
        res = uvc_get_power_line_frequency(m_pDeviceHandle, &powerline_freq, UVC_GET_CUR);
        if (res < 0)
        {
            LOG_ERROR("Failed to get powerline frequency: %s", uvc_strerror(res));
            return K4A_RESULT_FAILED;
        }
        *pValue = (int32_t)powerline_freq;
    }
    break;
    case K4A_COLOR_CONTROL_AUTO_EXPOSURE_PRIORITY:
    {
        *pValue = 0;
        LOG_WARNING("K4A_COLOR_CONTROL_AUTO_EXPOSURE_PRIORITY is deprecated and does nothing.", 0);
    }
    break;
    default:
        LOG_ERROR("Unsupported control: %d\n", command);
        return K4A_RESULT_FAILED;
    }

    return K4A_RESULT_SUCCEEDED;
}