Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Options: add BOOLEAN & ENUM; rs2_set_option_value #12708

Merged
merged 17 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions common/subdevice-model.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2017 Intel Corporation. All Rights Reserved.
// Copyright(c) 2024 Intel Corporation. All Rights Reserved.

#include "post-processing-filters-list.h"
#include "post-processing-block-model.h"
Expand Down Expand Up @@ -89,7 +89,10 @@ namespace rs2
if( it != options_metadata.end() && ! _destructing ) // Callback runs in different context, check options_metadata still valid
{
if( RS2_OPTION_TYPE_FLOAT == changed_option->type )
it->second.value = changed_option->as_float;
{
if( changed_option->is_valid )
it->second.value = changed_option->as_float;
}
}
}
} );
Expand Down
6 changes: 4 additions & 2 deletions include/librealsense2/h/rs_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ extern "C" {
RS2_OPTION_TYPE_INTEGER, /**< 64-bit signed integer value */
RS2_OPTION_TYPE_FLOAT,
RS2_OPTION_TYPE_STRING,
RS2_OPTION_TYPE_BOOLEAN,

RS2_OPTION_TYPE_COUNT

Expand All @@ -163,11 +164,12 @@ extern "C" {
typedef struct rs2_option_value
{
rs2_option id;
rs2_option_type type; /**< RS2_OPTION_TYPE_COUNT if no value is available */
int is_valid; /**< 0 if no value available; 1 otherwise */
rs2_option_type type;
union {
char const * as_string; /**< valid only while rs2_option_value is alive! */
float as_float;
int64_t as_integer;
int64_t as_integer; /**< including boolean value */
};
} rs2_option_value;

Expand Down
13 changes: 11 additions & 2 deletions src/core/option-interface.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2023 Intel Corporation. All Rights Reserved.
// Copyright(c) 2024 Intel Corporation. All Rights Reserved.
#pragma once

#include "extension.h"

#include <src/basics.h>
#include <librealsense2/h/rs_option.h>
#include <rsutils/json-fwd.h>


namespace librealsense {
Expand All @@ -23,6 +24,14 @@ class LRS_EXTENSION_API option : public recordable< option >
public:
virtual void set( float value ) = 0;
virtual float query() const = 0;

// Return the value currently assigned to the option
// By default, this is the result of query()
virtual rsutils::json get_value() const noexcept;

// Return the type of the option value
virtual rs2_option_type get_value_type() const noexcept;

virtual option_range get_range() const = 0;
virtual bool is_enabled() const = 0;
virtual bool is_read_only() const { return false; }
Expand Down
9 changes: 2 additions & 7 deletions src/dds/rs-dds-depth-sensor-proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ namespace librealsense {
float dds_depth_sensor_proxy::get_depth_scale() const
{
if( auto opt = get_option_handler( RS2_OPTION_DEPTH_UNITS ) )
{
// We don't want to do a long control-reply cycle on a value that gets updated automatically
if( auto dds_option = std::dynamic_pointer_cast< rs_dds_option >( opt ) )
return dds_option->get_last_known_value();
else
return opt->query();
}
return opt->get_value();

// Rather than throwing an exception, we try to be a little more helpful: without depth units, the depth image will
// show black, prompting bug reports. The D400 units min/max are taken from the HW, but the default is set to:
return 0.001f;
Expand Down
45 changes: 44 additions & 1 deletion src/dds/rs-dds-option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "rs-dds-option.h"

#include <realdds/dds-option.h>
#include <rsutils/json.h>

using rsutils::json;


namespace librealsense {
Expand All @@ -19,15 +22,38 @@ static option_range range_from_realdds( std::shared_ptr< realdds::dds_option > c
dds_opt->get_stepping(),
dds_opt->get_default_value() };
}
if( std::dynamic_pointer_cast< realdds::dds_boolean_option >( dds_opt ) )
{
return { 0, 1, 1, bool( dds_opt->get_default_value() ) ? 1.f : 0.f };
}
if( auto e = std::dynamic_pointer_cast< realdds::dds_enum_option >( dds_opt ) )
{
return { 0, float( e->get_choices().size() - 1 ), 1, (float)e->get_value_index( e->get_default_value() ) };
OhadMeir marked this conversation as resolved.
Show resolved Hide resolved
}
return { 0, 0, 0, 0 };
}


static rs2_option_type rs_type_from_dds_option( std::shared_ptr< realdds::dds_option > const & dds_opt )
{
if( std::dynamic_pointer_cast< realdds::dds_float_option >( dds_opt ) )
return RS2_OPTION_TYPE_FLOAT;
if( std::dynamic_pointer_cast< realdds::dds_string_option >( dds_opt ) )
return RS2_OPTION_TYPE_STRING;
if( std::dynamic_pointer_cast< realdds::dds_boolean_option >( dds_opt ) )
return RS2_OPTION_TYPE_BOOLEAN;
if( std::dynamic_pointer_cast< realdds::dds_integer_option >( dds_opt ) )
return RS2_OPTION_TYPE_INTEGER;
return RS2_OPTION_TYPE_COUNT;
OhadMeir marked this conversation as resolved.
Show resolved Hide resolved
}


rs_dds_option::rs_dds_option( const std::shared_ptr< realdds::dds_option > & dds_opt,
set_option_callback set_opt_cb,
query_option_callback query_opt_cb )
: option_base( range_from_realdds( dds_opt ) )
, _dds_opt( dds_opt )
, _rs_type( rs_type_from_dds_option( dds_opt ) )
, _set_opt_cb( set_opt_cb )
, _query_opt_cb( query_opt_cb )
{
Expand All @@ -52,7 +78,7 @@ float rs_dds_option::query() const
}


float rs_dds_option::get_last_known_value() const
json rs_dds_option::get_value() const noexcept
{
return _dds_opt->get_value();
}
Expand All @@ -76,4 +102,21 @@ const char * rs_dds_option::get_description() const
}


const char * rs_dds_option::get_value_description( float v ) const
{
auto e = std::dynamic_pointer_cast< realdds::dds_enum_option >( _dds_opt );
if( ! e )
return nullptr;
if( v < 0.f )
return nullptr;
auto & choices = e->get_choices();
auto i = size_t( v + 0.005 );
OhadMeir marked this conversation as resolved.
Show resolved Hide resolved
if( fabs( v - i ) > 0.01f )
return nullptr;
if( i >= choices.size() )
return nullptr;
return choices[i].c_str();
}


} // namespace librealsense
8 changes: 5 additions & 3 deletions src/dds/rs-dds-option.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2024 Intel Corporation. All Rights Reserved.

#pragma once

#include <src/option.h>
Expand All @@ -21,6 +20,7 @@ namespace librealsense {
class rs_dds_option : public option_base
{
std::shared_ptr< realdds::dds_option > _dds_opt;
rs2_option_type const _rs_type;

public:
typedef std::function< void( const std::string & name, float value ) > set_option_callback;
Expand All @@ -35,15 +35,17 @@ class rs_dds_option : public option_base
set_option_callback set_opt_cb,
query_option_callback query_opt_cb );

void set( float value ) override;
rsutils::json get_value() const noexcept override;
rs2_option_type get_value_type() const noexcept override { return _rs_type; }

float get_last_known_value() const;
void set( float value ) override;

float query() const override;

bool is_read_only() const override;
bool is_enabled() const override;
const char * get_description() const override;
const char * get_value_description( float ) const override;
};


Expand Down
16 changes: 16 additions & 0 deletions src/option.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "sensor.h"

#include <rsutils/string/from.h>
#include <rsutils/json.h>

using rsutils::json;


namespace librealsense {
Expand Down Expand Up @@ -36,6 +39,19 @@ void option_base::enable_recording(std::function<void(const option&)> recording_
_recording_function = recording_action;
}

json option::get_value() const noexcept
{
return query();
}


rs2_option_type option::get_value_type() const noexcept
{
// By default, all options are floats
return RS2_OPTION_TYPE_FLOAT;
}


void option::create_snapshot(std::shared_ptr<option>& snapshot) const
{
snapshot = std::make_shared<const_value_option>(get_description(), query());
Expand Down
Loading
Loading