k4a_result_t UVCCameraReader::SetCameraControl()

in src/color/uvc_camerareader.cpp [896:1109]


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

    switch (command)
    {
    case K4A_COLOR_CONTROL_EXPOSURE_TIME_ABSOLUTE:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_ae_mode(m_pDeviceHandle, UVC_AUTO_EXPOSURE_MODE_MANUAL);
            if (res < 0)
            {
                LOG_ERROR("Failed to set auto exposure mode: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }

            res = uvc_set_exposure_abs(m_pDeviceHandle, (uint32_t)MapK4aExposureToLinux(newValue));
            if (res < 0)
            {
                LOG_ERROR("Failed to set exposure time abs: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else if (mode == K4A_COLOR_CONTROL_MODE_AUTO)
        {
            res = uvc_set_ae_mode(m_pDeviceHandle, UVC_AUTO_EXPOSURE_MODE_APERTURE_PRIORITY);
            if (res < 0)
            {
                LOG_ERROR("Failed to set auto exposure mode: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_BRIGHTNESS:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_brightness(m_pDeviceHandle, (int16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set brightness: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_CONTRAST:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_contrast(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set contrast: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_SATURATION:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_saturation(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set saturation: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_SHARPNESS:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_sharpness(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set sharpness: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_WHITEBALANCE:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_white_balance_temperature_auto(m_pDeviceHandle, 0);
            if (res < 0)
            {
                LOG_ERROR("Failed to set auto white balance temperature mode: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }

            res = uvc_set_white_balance_temperature(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set white balance temerature: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else if (mode == K4A_COLOR_CONTROL_MODE_AUTO)
        {
            res = uvc_set_white_balance_temperature_auto(m_pDeviceHandle, 1);
            if (res < 0)
            {
                LOG_ERROR("Failed to set auto white balance temperature mode: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_BACKLIGHT_COMPENSATION:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_backlight_compensation(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set backlight compensation: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_GAIN:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            res = uvc_set_gain(m_pDeviceHandle, (uint16_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set gain: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_POWERLINE_FREQUENCY:
        if (mode == K4A_COLOR_CONTROL_MODE_MANUAL)
        {
            if (newValue == 0)
            {
                // Even firmware does not able to disable power line frequncy control,
                // value zero is accepted.
                // Return failed unsupported value.
                LOG_ERROR("Can not disable Powerline Frequency Control", 0);
                return K4A_RESULT_FAILED;
            }

            res = uvc_set_power_line_frequency(m_pDeviceHandle, (uint8_t)newValue);
            if (res < 0)
            {
                LOG_ERROR("Failed to set powerline frequency: %s", uvc_strerror(res));
                return K4A_RESULT_FAILED;
            }
            else
            {
                m_using_60hz_power = (newValue == 2);
            }
        }
        else
        {
            LOG_ERROR("Invalid color control mode\n", 0);
            return K4A_RESULT_FAILED;
        }
        break;
    case K4A_COLOR_CONTROL_AUTO_EXPOSURE_PRIORITY:
    {
        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;
}