use super::Rs2Exception;
use num_derive::{FromPrimitive, ToPrimitive};
use realsense_sys as sys;
use std::{ffi::CStr, fmt::Display};
use thiserror::Error;
#[derive(Error, Debug)]
pub enum OptionSetError {
#[error("Option not supported on this sensor.")]
OptionNotSupported,
#[error("Option is read only.")]
OptionIsReadOnly,
#[error("Could not set option. Type: {0}; Reason: {1}")]
CouldNotSetOption(Rs2Exception, String),
}
#[repr(i32)]
#[derive(FromPrimitive, ToPrimitive, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Rs2Option {
BacklightCompensation = sys::rs2_option_RS2_OPTION_BACKLIGHT_COMPENSATION as i32,
Brightness = sys::rs2_option_RS2_OPTION_BRIGHTNESS as i32,
Contrast = sys::rs2_option_RS2_OPTION_CONTRAST as i32,
Exposure = sys::rs2_option_RS2_OPTION_EXPOSURE as i32,
Gain = sys::rs2_option_RS2_OPTION_GAIN as i32,
Gamma = sys::rs2_option_RS2_OPTION_GAMMA as i32,
Hue = sys::rs2_option_RS2_OPTION_HUE as i32,
Saturation = sys::rs2_option_RS2_OPTION_SATURATION as i32,
Sharpness = sys::rs2_option_RS2_OPTION_SHARPNESS as i32,
WhiteBalance = sys::rs2_option_RS2_OPTION_WHITE_BALANCE as i32,
EnableAutoExposure = sys::rs2_option_RS2_OPTION_ENABLE_AUTO_EXPOSURE as i32,
EnableAutoWhiteBalance = sys::rs2_option_RS2_OPTION_ENABLE_AUTO_WHITE_BALANCE as i32,
VisualPreset = sys::rs2_option_RS2_OPTION_VISUAL_PRESET as i32,
LaserPower = sys::rs2_option_RS2_OPTION_LASER_POWER as i32,
Accuracy = sys::rs2_option_RS2_OPTION_ACCURACY as i32,
MotionRange = sys::rs2_option_RS2_OPTION_MOTION_RANGE as i32,
FilterOption = sys::rs2_option_RS2_OPTION_FILTER_OPTION as i32,
ConfidenceThreshold = sys::rs2_option_RS2_OPTION_CONFIDENCE_THRESHOLD as i32,
EmitterEnabled = sys::rs2_option_RS2_OPTION_EMITTER_ENABLED as i32,
FramesQueueSize = sys::rs2_option_RS2_OPTION_FRAMES_QUEUE_SIZE as i32,
TotalFrameDrops = sys::rs2_option_RS2_OPTION_TOTAL_FRAME_DROPS as i32,
AutoExposureMode = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_MODE as i32,
PowerLineFrequency = sys::rs2_option_RS2_OPTION_POWER_LINE_FREQUENCY as i32,
AsicTemperature = sys::rs2_option_RS2_OPTION_ASIC_TEMPERATURE as i32,
ErrorPollingEnabled = sys::rs2_option_RS2_OPTION_ERROR_POLLING_ENABLED as i32,
ProjectorTemperature = sys::rs2_option_RS2_OPTION_PROJECTOR_TEMPERATURE as i32,
OutputTriggerEnabled = sys::rs2_option_RS2_OPTION_OUTPUT_TRIGGER_ENABLED as i32,
MotionModuleTemperature = sys::rs2_option_RS2_OPTION_MOTION_MODULE_TEMPERATURE as i32,
DepthUnits = sys::rs2_option_RS2_OPTION_DEPTH_UNITS as i32,
EnableMotionCorrection = sys::rs2_option_RS2_OPTION_ENABLE_MOTION_CORRECTION as i32,
AutoExposurePriority = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_PRIORITY as i32,
ColorScheme = sys::rs2_option_RS2_OPTION_COLOR_SCHEME as i32,
HistogramEqualizationEnabled = sys::rs2_option_RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED as i32,
MinDistance = sys::rs2_option_RS2_OPTION_MIN_DISTANCE as i32,
MaxDistance = sys::rs2_option_RS2_OPTION_MAX_DISTANCE as i32,
TextureSource = sys::rs2_option_RS2_OPTION_TEXTURE_SOURCE as i32,
FilterMagnitude = sys::rs2_option_RS2_OPTION_FILTER_MAGNITUDE as i32,
FilterSmoothAlpha = sys::rs2_option_RS2_OPTION_FILTER_SMOOTH_ALPHA as i32,
FilterSmoothDelta = sys::rs2_option_RS2_OPTION_FILTER_SMOOTH_DELTA as i32,
HolesFill = sys::rs2_option_RS2_OPTION_HOLES_FILL as i32,
StereoBaseline = sys::rs2_option_RS2_OPTION_STEREO_BASELINE as i32,
AutoExposureConvergeStep = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_CONVERGE_STEP as i32,
InterCamSyncMode = sys::rs2_option_RS2_OPTION_INTER_CAM_SYNC_MODE as i32,
StreamFilter = sys::rs2_option_RS2_OPTION_STREAM_FILTER as i32,
StreamFormatFilter = sys::rs2_option_RS2_OPTION_STREAM_FORMAT_FILTER as i32,
StreamIndexFilter = sys::rs2_option_RS2_OPTION_STREAM_INDEX_FILTER as i32,
EmitterOnOff = sys::rs2_option_RS2_OPTION_EMITTER_ON_OFF as i32,
LldTemperature = sys::rs2_option_RS2_OPTION_LLD_TEMPERATURE as i32,
McTemperature = sys::rs2_option_RS2_OPTION_MC_TEMPERATURE as i32,
MaTemperature = sys::rs2_option_RS2_OPTION_MA_TEMPERATURE as i32,
HardwarePreset = sys::rs2_option_RS2_OPTION_HARDWARE_PRESET as i32,
GlobalTimeEnabled = sys::rs2_option_RS2_OPTION_GLOBAL_TIME_ENABLED as i32,
ApdTemperature = sys::rs2_option_RS2_OPTION_APD_TEMPERATURE as i32,
EnableMapping = sys::rs2_option_RS2_OPTION_ENABLE_MAPPING as i32,
EnableRelocalization = sys::rs2_option_RS2_OPTION_ENABLE_RELOCALIZATION as i32,
EnablePoseJumping = sys::rs2_option_RS2_OPTION_ENABLE_POSE_JUMPING as i32,
EnableDynamicCalibration = sys::rs2_option_RS2_OPTION_ENABLE_DYNAMIC_CALIBRATION as i32,
DepthOffset = sys::rs2_option_RS2_OPTION_DEPTH_OFFSET as i32,
LedPower = sys::rs2_option_RS2_OPTION_LED_POWER as i32,
EnableMapPreservation = sys::rs2_option_RS2_OPTION_ENABLE_MAP_PRESERVATION as i32,
FreefallDetectionEnabled = sys::rs2_option_RS2_OPTION_FREEFALL_DETECTION_ENABLED as i32,
AvalanchePhotoDiode = sys::rs2_option_RS2_OPTION_AVALANCHE_PHOTO_DIODE as i32,
PostProcessingSharpening = sys::rs2_option_RS2_OPTION_POST_PROCESSING_SHARPENING as i32,
PreProcessingSharpening = sys::rs2_option_RS2_OPTION_PRE_PROCESSING_SHARPENING as i32,
NoiseFiltering = sys::rs2_option_RS2_OPTION_NOISE_FILTERING as i32,
InvalidationBypass = sys::rs2_option_RS2_OPTION_INVALIDATION_BYPASS as i32,
DigitalGain = sys::rs2_option_RS2_OPTION_DIGITAL_GAIN as i32,
SensoeMode = sys::rs2_option_RS2_OPTION_SENSOR_MODE as i32,
EmitterAlwaysOn = sys::rs2_option_RS2_OPTION_EMITTER_ALWAYS_ON as i32,
ThermalCompensation = sys::rs2_option_RS2_OPTION_THERMAL_COMPENSATION as i32,
HostPerformance = sys::rs2_option_RS2_OPTION_HOST_PERFORMANCE as i32,
HdrEnabled = sys::rs2_option_RS2_OPTION_HDR_ENABLED as i32,
SequenceName = sys::rs2_option_RS2_OPTION_SEQUENCE_NAME as i32,
SequenceSize = sys::rs2_option_RS2_OPTION_SEQUENCE_SIZE as i32,
SequenceId = sys::rs2_option_RS2_OPTION_SEQUENCE_ID as i32,
HumidityTemperature = sys::rs2_option_RS2_OPTION_HUMIDITY_TEMPERATURE as i32,
EnableMaxUsableRange = sys::rs2_option_RS2_OPTION_ENABLE_MAX_USABLE_RANGE as i32,
AlternateIr = sys::rs2_option_RS2_OPTION_ALTERNATE_IR as i32,
NoiseEstimation = sys::rs2_option_RS2_OPTION_NOISE_ESTIMATION as i32,
EnableIrReflectivity = sys::rs2_option_RS2_OPTION_ENABLE_IR_REFLECTIVITY as i32,
AutoExposureLimit = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_LIMIT as i32,
AutoGainLimit = sys::rs2_option_RS2_OPTION_AUTO_GAIN_LIMIT as i32,
AutoReceiverSensitivity = sys::rs2_option_RS2_OPTION_AUTO_RX_SENSITIVITY as i32,
TransmitterFrequency = sys::rs2_option_RS2_OPTION_TRANSMITTER_FREQUENCY as i32,
VerticalBinning = sys::rs2_option_RS2_OPTION_VERTICAL_BINNING as i32,
ReceiverSensitivity = sys::rs2_option_RS2_OPTION_RECEIVER_SENSITIVITY as i32,
AutoExposureLimitToggle = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_LIMIT_TOGGLE as i32,
AutoGainLimitToggle = sys::rs2_option_RS2_OPTION_AUTO_GAIN_LIMIT_TOGGLE as i32,
EmitterFrequency = sys::rs2_option_RS2_OPTION_EMITTER_FREQUENCY as i32,
DepthAutoExposureMode = sys::rs2_option_RS2_OPTION_DEPTH_AUTO_EXPOSURE_MODE as i32,
OhmTemperature = sys::rs2_option_RS2_OPTION_OHM_TEMPERATURE as i32,
SocPvtTemperature = sys::rs2_option_RS2_OPTION_SOC_PVT_TEMPERATURE as i32,
GyroSensitivity = sys::rs2_option_RS2_OPTION_GYRO_SENSITIVITY as i32,
RegionOfInterest = sys::rs2_option_RS2_OPTION_REGION_OF_INTEREST as i32,
Rotation = sys::rs2_option_RS2_OPTION_ROTATION as i32,
}
impl Rs2Option {
pub fn to_cstr(self) -> &'static CStr {
unsafe {
let ptr = sys::rs2_option_to_string(self as sys::rs2_option);
CStr::from_ptr(ptr)
}
}
pub fn to_str(self) -> &'static str {
self.to_cstr().to_str().unwrap()
}
}
impl Display for Rs2Option {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.to_str())
}
}
pub struct Rs2OptionRange {
pub min: f32,
pub max: f32,
pub step: f32,
pub default: f32,
}
#[cfg(test)]
mod tests {
use super::*;
use num_traits::FromPrimitive;
#[test]
fn all_variants_exist() {
let deprecated_options = [
sys::rs2_option_RS2_OPTION_ZERO_ORDER_POINT_X as i32,
sys::rs2_option_RS2_OPTION_ZERO_ORDER_POINT_Y as i32,
sys::rs2_option_RS2_OPTION_ZERO_ORDER_ENABLED as i32,
sys::rs2_option_RS2_OPTION_AMBIENT_LIGHT as i32,
sys::rs2_option_RS2_OPTION_TRIGGER_CAMERA_ACCURACY_HEALTH as i32,
sys::rs2_option_RS2_OPTION_RESET_CAMERA_ACCURACY_HEALTH as i32,
];
for i in 0..sys::rs2_option_RS2_OPTION_COUNT as i32 {
if deprecated_options.iter().any(|x| x == &i) {
continue;
}
assert!(
Rs2Option::from_i32(i).is_some(),
"Rs2Option variant for ordinal {} does not exist.",
i,
);
}
}
}