Skip to content

Commit

Permalink
add rs2_set_option_value
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel committed Feb 29, 2024
1 parent 614e4d7 commit 4dc9011
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 3 deletions.
8 changes: 8 additions & 0 deletions include/librealsense2/h/rs_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,14 @@ extern "C" {
*/
void rs2_set_option(const rs2_options* options, rs2_option option, float value, rs2_error** error);

/**
* write new value to sensor option
* \param[in] options the options container
* \param[in] option_value option id, type, and value to be set
* \param[out] error if non-null, receives any error that occurs during this call, otherwise, errors are ignored
*/
void rs2_set_option_value( rs2_options const * options, rs2_option_value const * option_value, rs2_error ** error );

/**
* get the list of supported options of options container
* \param[in] options the options container
Expand Down
47 changes: 44 additions & 3 deletions include/librealsense2/hpp/rs_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,44 @@ namespace rs2
std::shared_ptr< const rs2_option_value > _value;

public:
option_value( rs2_option_value const * handle )
explicit option_value( rs2_option_value const * handle )
: _value( handle, rs2_delete_option_value )
{
}
option_value( option_value const & ) = default;
option_value( option_value && ) = default;
option_value() = default;

enum invalid_t { invalid };
option_value( rs2_option option_id, invalid_t )
: _value( new rs2_option_value{ option_id, false, RS2_OPTION_TYPE_COUNT } ) {}

option_value( rs2_option option_id, int64_t as_integer )
: _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_INTEGER } )
{
const_cast< rs2_option_value * >( _value.get() )->as_integer = as_integer;
}
option_value( rs2_option option_id, float as_float )
: _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_FLOAT } )
{
const_cast< rs2_option_value * >( _value.get() )->as_float = as_float;
}
option_value( rs2_option option_id, char const * as_string )
: _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_STRING } )
{
const_cast< rs2_option_value * >( _value.get() )->as_string = as_string;
}
option_value( rs2_option option_id, bool as_boolean )
: _value( new rs2_option_value{ option_id, true, RS2_OPTION_TYPE_BOOLEAN } )
{
const_cast<rs2_option_value *>(_value.get())->as_integer = as_boolean;
}

option_value & operator=( option_value const & ) = default;
option_value & operator=( option_value && ) = default;

rs2_option_value const * operator->() const { return _value.get(); }
operator rs2_option_value const *() const { return _value.get(); }
};

class options_list
Expand Down Expand Up @@ -53,7 +82,7 @@ namespace rs2
rs2_error * e = nullptr;
auto value = rs2_get_option_value_from_list( _list.get(), static_cast< int >( index ), &e );
error::handle( e );
return value;
return option_value( value );
}

size_t size() const { return _size; }
Expand Down Expand Up @@ -202,7 +231,7 @@ namespace rs2
rs2_error * e = nullptr;
auto value = rs2_get_option_value( _options, option_id, &e );
error::handle( e );
return value;
return option_value( value );
}

/**
Expand Down Expand Up @@ -231,6 +260,18 @@ namespace rs2
error::handle(e);
}

/**
* write new value to the option
* \param[in] option option id to be queried
* \param[in] value option (id,type,is_valid,new value)
*/
void set_option_value( option_value const & value ) const
{
rs2_error * e = nullptr;
rs2_set_option_value( _options, value, &e );
error::handle( e );
}

/**
* check if particular option is read-only
* \param[in] option option id to be checked
Expand Down
1 change: 1 addition & 0 deletions src/realsense.def
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ EXPORTS
rs2_get_option_value
rs2_delete_option_value
rs2_set_option
rs2_set_option_value
rs2_supports_option
rs2_get_option_range
rs2_get_option_description
Expand Down
41 changes: 41 additions & 0 deletions src/rs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,47 @@ void rs2_set_option(const rs2_options* options, rs2_option option, float value,
}
HANDLE_EXCEPTIONS_AND_RETURN(, options, option, value)

void rs2_set_option_value( rs2_options const * options, rs2_option_value const * option_value, rs2_error ** error ) BEGIN_API_CALL
{
VALIDATE_NOT_NULL( options );
VALIDATE_NOT_NULL( option_value );
auto & option = options->options->get_option( option_value->id ); // throws
if( ! option_value->is_valid )
{
option.set_value( rsutils::null_json );
return;
}
rs2_option_type const option_type = option.get_value_type();
if( option_value->type != option_type )
throw invalid_value_exception( "expected " + get_string( option_type ) + " type" );
auto range = option.get_range();
switch( option_type )
{
case RS2_OPTION_TYPE_FLOAT:
VALIDATE_RANGE( option_value->as_float, range.min, range.max );
option.set_value( option_value->as_float );
break;

case RS2_OPTION_TYPE_INTEGER:
VALIDATE_RANGE( option_value->as_integer, range.min, range.max );
option.set_value( option_value->as_integer );
break;

case RS2_OPTION_TYPE_BOOLEAN:
VALIDATE_RANGE( option_value->as_integer, range.min, range.max );
option.set_value( (bool)option_value->as_integer );
break;

case RS2_OPTION_TYPE_STRING:
option.set_value( option_value->as_string );
break;

default:
throw not_implemented_exception( "unexpected option type " + get_string( option_type ) );
}
}
HANDLE_EXCEPTIONS_AND_RETURN( , options, option_value )

rs2_options_list* rs2_get_options_list(const rs2_options* options, rs2_error** error) BEGIN_API_CALL
{
VALIDATE_NOT_NULL(options);
Expand Down
2 changes: 2 additions & 0 deletions unit-tests/dds/test-librs-options.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@
test.check_equal( s.get_option( eo ), 0. )
s.set_option( eo, 2. )
test.check_equal( s.get_option_value( eo ).value, 'Everything' )
s.set_option_value( eo, 'Last' )
test.check_equal( s.get_option( eo ), 1. )

with test.closure( 'All done' ):
dev = None
Expand Down
35 changes: 35 additions & 0 deletions wrappers/python/pyrs_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#include "pyrealsense2.h"
#include <librealsense2/hpp/rs_options.hpp>

using rsutils::json;


void init_options(py::module &m) {
/** rs_options.hpp **/

Expand Down Expand Up @@ -78,6 +81,38 @@ void init_options(py::module &m) {
.def( "get_option_value",
[]( rs2::options const & self, rs2_option option_id ) -> option_value
{ return self.get_option_value( option_id ); } )
.def( "set_option_value",
[]( rs2::options const & self, rs2_option option_id, json value )
{
rs2::option_value rs2_value;
switch( value.type() )
{
case json::value_t::null:
rs2_value = rs2::option_value( option_id, rs2::option_value::invalid );
break;

case json::value_t::string:
rs2_value = rs2::option_value( option_id, value.string_ref().c_str() );
break;

case json::value_t::number_float:
rs2_value = rs2::option_value( option_id, value.get< float >() );
break;

case json::value_t::number_unsigned:
case json::value_t::number_integer:
rs2_value = rs2::option_value( option_id, value.get< int64_t >() );
break;

case json::value_t::boolean:
rs2_value = rs2::option_value( option_id, value.get< bool >() );
break;

default:
throw std::runtime_error( "invalid value type: " + value.dump() );
}
self.set_option_value( rs2_value );
} )
.def("get_option_range", &rs2::options::get_option_range, "Retrieve the available range of values "
"of a supported option", "option"_a, py::call_guard<py::gil_scoped_release>())
.def("set_option", &rs2::options::set_option, "Write new value to device option", "option"_a, "value"_a, py::call_guard<py::gil_scoped_release>())
Expand Down

0 comments on commit 4dc9011

Please sign in to comment.