Skip to content

Commit

Permalink
Add on option changed to python wrapper. Add options-watcher unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
OhadMeir committed Dec 28, 2023
1 parent 08f25fb commit 8a431c8
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
5 changes: 5 additions & 0 deletions include/librealsense2/hpp/rs_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ namespace rs2
{
}

options_list()
: _list( nullptr )
{
}

options_list & operator=( std::shared_ptr< rs2_options_list > list )
{
_list = std::move( list );
Expand Down
51 changes: 51 additions & 0 deletions unit-tests/live/options/test-options-watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2023 Intel Corporation. All Rights Reserved.

# test:device each(D400*)

import pyrealsense2 as rs
from rspy import test
from rspy import log
import time

dev = test.find_first_device_or_exit()
depth_sensor = dev.first_depth_sensor()
product_line = dev.get_info(rs.camera_info.product_line)

changed_options = 0

def notification_callback( opt_list ):
global changed_options
log.d( "notification_callback called with {} options".format( len( opt_list ) ) )
for opt in opt_list:
log.d( " Changed option {}".format( opt ) )
changed_options = changed_options + 1

depth_sensor.on_options_changed( notification_callback )

with test.closure( 'disabling auto exposure' ): # Need to disable or changing gain/exposure might automatically disable it
depth_sensor.set_option( rs.option.enable_auto_exposure, 0 )
test.check_equal( depth_sensor.get_option( rs.option.enable_auto_exposure ), 0.0 )
time.sleep( 1.5 ) # default options-watcher update interval is 1 second

with test.closure( 'set one option' ):
changed_options = 0
current_gain = depth_sensor.get_option( rs.option.gain )
depth_sensor.set_option( rs.option.gain , current_gain + 1 )
test.check_equal( depth_sensor.get_option( rs.option.gain ), current_gain + 1 )
time.sleep( 1.5 ) # default options-watcher update interval is 1 second
test.check_equal( changed_options, 1 )
changed_options = 0

with test.closure( 'set multiple options' ):
current_gain = depth_sensor.get_option( rs.option.gain )
depth_sensor.set_option( rs.option.gain , current_gain + 1 )
test.check_equal( depth_sensor.get_option( rs.option.gain ), current_gain + 1 )
current_exposure = depth_sensor.get_option( rs.option.exposure )
depth_sensor.set_option( rs.option.exposure , current_exposure + 1 )
test.check_equal( depth_sensor.get_option( rs.option.exposure ), current_exposure + 1 )
time.sleep( 2.5 ) # default options-watcher update interval is 1 second, multiple options might be updated on different intervals
test.check_equal( changed_options, 2 )
changed_options = 0

test.print_results_and_exit()
23 changes: 22 additions & 1 deletion wrappers/python/pyrs_options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ Copyright(c) 2017 Intel Corporation. All Rights Reserved. */

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

py::class_< rs2::options_list > options_list( m, "options_list" ); // No docstring in C++
options_list.def( py::init<>() )
.def( "__getitem__",
[]( const rs2::options_list & self, size_t i )
{
if( i >= self.size() )
throw py::index_error();
return self[size_t( i )];
} )
.def( "__len__", &rs2::options_list::size )
.def( "size", &rs2::options_list::size ) // No docstring in C++
.def( "__iter__",
[]( const rs2::options_list & self ) { return py::make_iterator( self.begin(), self.end() ); },
py::keep_alive< 0, 1 >() )
.def( "front", &rs2::options_list::front ) // No docstring in C++
.def( "back", &rs2::options_list::back ); // No docstring in C++

py::class_<rs2::options> options(m, "options", "Base class for options interface. Should be used via sensor or processing_block."); // No docstring in C++
options.def("is_option_read_only", &rs2::options::is_option_read_only, "Check if particular option "
"is read only.", "option"_a)
Expand All @@ -18,6 +36,9 @@ void init_options(py::module &m) {
.def("get_option_description", &rs2::options::get_option_description, "Get option description.", "option"_a)
.def("get_option_value_description", &rs2::options::get_option_value_description, "Get option value description "
"(In case a specific option value holds special meaning)", "option"_a, "value"_a)
.def("get_supported_options", &rs2::options::get_supported_options, "Retrieve list of supported options"); // No docstring in C++
.def("get_supported_options", &rs2::options::get_supported_options, "Retrieve list of supported options") // No docstring in C++
.def( "on_options_changed", &rs2::options::on_options_changed,
"Sets a callback to notify in case options in this container change value", "callback"_a );

/** end rs_options.hpp **/
}
2 changes: 2 additions & 0 deletions wrappers/python/pyrs_sensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ void init_sensor(py::module &m) {
"Check if specific camera info is supported.", "info")
.def("supports", (bool (rs2::sensor::*)(rs2_option) const) &rs2::options::supports,
"Check if specific camera info is supported.", "info")
.def( "on_options_changed", &rs2::options::on_options_changed,
"Sets a callback to notify in case options in this container change value", "callback"_a )
.def("get_info", &rs2::sensor::get_info, "Retrieve camera specific information, "
"like versions of various internal components.", "info"_a)
.def("set_notifications_callback", [](const rs2::sensor& self, std::function<void(rs2::notification)> callback) {
Expand Down

0 comments on commit 8a431c8

Please sign in to comment.