Intel® RealSense™ Cross Platform API
Intel Realsense Cross-platform API
option.h
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2015 Intel Corporation. All Rights Reserved.
3 
4 #pragma once
5 
6 #include "backend.h"
7 #include "archive.h"
8 #include "hw-monitor.h"
9 #include "sensor.h"
10 #include "core/streaming.h"
11 
12 #include <chrono>
13 #include <memory>
14 #include <vector>
15 #include <cmath>
16 
17 namespace librealsense
18 {
19  class readonly_option : public option
20  {
21  public:
22  bool is_read_only() const override { return true; }
23 
24  void set(float) override
25  {
26  throw not_implemented_exception("This option is read-only!");
27  }
28 
29  void enable_recording(std::function<void(const option &)> record_action) override
30  {
31  //empty
32  }
33  };
34 
36  {
37  public:
38  const_value_option(std::string desc, float val)
39  : _val(lazy<float>([val]() {return val; })), _desc(std::move(desc)) {}
40 
41  const_value_option(std::string desc, lazy<float> val)
42  : _val(std::move(val)), _desc(std::move(desc)) {}
43 
44  float query() const override { return *_val; }
45  option_range get_range() const override { return { *_val, *_val, 0, *_val }; }
46  bool is_enabled() const override { return true; }
47 
48  const char* get_description() const override { return _desc.c_str(); }
49 
50  void update(std::shared_ptr<extension_snapshot> ext) override
51  {
52  if (auto opt = As<option>(ext))
53  {
54  auto new_val = opt->query();
55  _val = lazy<float>([new_val]() {return new_val; });
56  _desc = opt->get_description();
57  }
58  }
59  private:
60  lazy<float> _val;
61  std::string _desc;
62  };
63 
64  class option_base : public option
65  {
66  public:
67  option_base(const option_range& opt_range)
68  : _opt_range(opt_range)
69  {}
70 
71  bool is_valid(float value) const
72  {
73  if (!std::isnormal(_opt_range.step))
74  throw invalid_value_exception(to_string() << "is_valid(...) failed! step is not properly defined. (" << _opt_range.step << ")");
75 
76  if ((value < _opt_range.min) || (value > _opt_range.max))
77  return false;
78 
79  auto n = (value - _opt_range.min)/_opt_range.step;
80  return (fabs(fmod(n, 1)) < std::numeric_limits<float>::min());
81  }
82 
83  option_range get_range() const override
84  {
85  return _opt_range;
86  }
87  virtual void enable_recording(std::function<void(const option&)> recording_action) override
88  {
89  _recording_function = recording_action;
90  }
91  protected:
93  std::function<void(const option&)> _recording_function = [](const option&) {};
94 
95  };
96 
97  template<class T>
98  class ptr_option : public option_base
99  {
100  public:
101  ptr_option(T min, T max, T step, T def, T* value, const std::string& desc)
102  : option_base({ static_cast<float>(min),
103  static_cast<float>(max),
104  static_cast<float>(step),
105  static_cast<float>(def), }),
106  _min(min), _max(max), _step(step), _def(def), _value(value), _desc(desc)
107  {
108  static_assert((std::is_arithmetic<T>::value), "ptr_option class supports arithmetic built-in types only");
109  _on_set = [](float x) {};
110  }
111 
112  void set(float value) override
113  {
114  T val = static_cast<T>(value);
115  if ((_max < val) || (_min > val))
116  throw invalid_value_exception(to_string() << "Given value " << value << "is outside valid range!");
117  *_value = val;
118  _on_set(value);
119  }
120 
121  float query() const override
122  {
123  return static_cast<float>(*_value);
124  }
125 
126  option_range get_range() const override {
127  return{
128  (float)_min, (float)_max,
129  (float)_step, (float)_def };
130  }
131 
132  bool is_enabled() const override { return true; }
133 
134  void enable_recording(std::function<void(const option &)> record_action) override {}
135 
136  const char* get_description() const override { return _desc.c_str(); }
137 
138  const char* get_value_description(float val) const override
139  {
140  auto it = _item_desc.find(val);
141  if (it != _item_desc.end())
142  {
143  return it->second.c_str();
144  }
145  return nullptr;
146  }
147 
148  void set_description(float val, const std::string& desc)
149  {
150  _item_desc[val] = desc;
151  }
152 
153  void on_set(std::function<void(float)> on_set) { _on_set = on_set; }
154  private:
155  T _min, _max, _step, _def;
156  T* _value;
157  std::string _desc;
158  std::map<float, std::string> _item_desc;
159  std::function<void(float)> _on_set;
160  };
161 
162  class uvc_pu_option : public option
163  {
164  public:
165  void set(float value) override;
166 
167  float query() const override;
168 
169  option_range get_range() const override;
170 
171  bool is_enabled() const override
172  {
173  return true;
174  }
175 
177  : _ep(ep), _id(id)
178  {
179  }
180 
181  uvc_pu_option(uvc_sensor& ep, rs2_option id, const std::map<float, std::string>& description_per_value)
182  : _ep(ep), _id(id), _description_per_value(description_per_value)
183  {
184  }
185 
186  const char* get_description() const override;
187 
188  const char* get_value_description(float val) const override
189  {
190  if (_description_per_value.find(val) != _description_per_value.end())
191  return _description_per_value.at(val).c_str();
192  return nullptr;
193  }
194  void enable_recording(std::function<void(const option &)> record_action) override
195  {
196  _record = record_action;
197  }
198  private:
199  uvc_sensor& _ep;
200  rs2_option _id;
201  const std::map<float, std::string> _description_per_value;
202  std::function<void(const option &)> _record = [](const option &) {};
203  };
204 
205  template<typename T>
206  class uvc_xu_option : public option
207  {
208  public:
209  void set(float value) override
210  {
212  [this, value](platform::uvc_device& dev)
213  {
214  T t = static_cast<T>(value);
215  if (!dev.set_xu(_xu, _id, reinterpret_cast<uint8_t*>(&t), sizeof(T)))
216  throw invalid_value_exception(to_string() << "set_xu(id=" << std::to_string(_id) << ") failed!" << " Last Error: " << strerror(errno));
217  _recording_function(*this);
218  });
219  }
220 
221  float query() const override
222  {
223  return static_cast<float>(_ep.invoke_powered(
224  [this](platform::uvc_device& dev)
225  {
226  T t;
227  if (!dev.get_xu(_xu, _id, reinterpret_cast<uint8_t*>(&t), sizeof(T)))
228  throw invalid_value_exception(to_string() << "get_xu(id=" << std::to_string(_id) << ") failed!" << " Last Error: " << strerror(errno));
229 
230  return static_cast<float>(t);
231  }));
232  }
233 
234  option_range get_range() const override
235  {
236  auto uvc_range = _ep.invoke_powered(
237  [this](platform::uvc_device& dev)
238  {
239  return dev.get_xu_range(_xu, _id, sizeof(T));
240  });
241 
242  if (uvc_range.min.size() < sizeof(int32_t)) return option_range{0,0,1,0};
243 
244  auto min = *(reinterpret_cast<int32_t*>(uvc_range.min.data()));
245  auto max = *(reinterpret_cast<int32_t*>(uvc_range.max.data()));
246  auto step = *(reinterpret_cast<int32_t*>(uvc_range.step.data()));
247  auto def = *(reinterpret_cast<int32_t*>(uvc_range.def.data()));
248  return option_range{static_cast<float>(min),
249  static_cast<float>(max),
250  static_cast<float>(step),
251  static_cast<float>(def)};
252  }
253 
254  bool is_enabled() const override { return true; }
255 
256  uvc_xu_option(uvc_sensor& ep, platform::extension_unit xu, uint8_t id, std::string description)
257  : _ep(ep), _xu(xu), _id(id), _desciption(std::move(description))
258  {}
259 
260  const char* get_description() const override
261  {
262  return _desciption.c_str();
263  }
264  void enable_recording(std::function<void(const option &)> record_action) override
265  {
266  _recording_function = record_action;
267  }
268  protected:
271  uint8_t _id;
272  std::string _desciption;
273  std::function<void(const option&)> _recording_function = [](const option&) {};
274  };
275 
276  inline std::string hexify(unsigned char n)
277  {
278  std::string res;
279 
280  do
281  {
282  res += "0123456789ABCDEF"[n % 16];
283  n >>= 4;
284  } while (n);
285 
286  reverse(res.begin(), res.end());
287 
288  if (res.size() == 1)
289  {
290  res.insert(0, "0");
291  }
292 
293  return res;
294  }
295 
296  template<class T, class R, class W, class U>
298  {
299  public:
300  void set(float value) override
301  {
302  _struct_interface->set(_field, value);
303  _recording_function(*this);
304  }
305  float query() const override
306  {
307  return _struct_interface->get(_field);
308  }
309  option_range get_range() const override
310  {
311  return _range;
312  }
313  bool is_enabled() const override { return true; }
314 
316  U T::* field, const option_range& range)
317  : _struct_interface(struct_interface), _range(range), _field(field)
318  {
319  }
320 
321  const char* get_description() const override
322  {
323  return nullptr;
324  }
325 
326  void enable_recording(std::function<void(const option &)> record_action) override
327  {
328  _recording_function = record_action;
329  }
330  private:
331  std::shared_ptr<struct_interface<T, R, W>> _struct_interface;
332  option_range _range;
333  U T::* _field;
334  std::function<void(const option&)> _recording_function = [](const option&) {};
335  };
336 
337  template<class T, class R, class W, class U>
338  std::shared_ptr<struct_field_option<T, R, W, U>> make_field_option(
340  U T::* field, const option_range& range)
341  {
342  return std::make_shared<struct_field_option<T, R, W, U>>
343  (struct_interface, field, range);
344  }
345 
347  {
348  public:
349  std::vector<uint8_t> send_receive(const std::vector<uint8_t>& data, int, bool require_response) override;
350 
352  platform::extension_unit xu, uint8_t ctrl)
353  : _uvc(uvc), _xu(std::move(xu)), _ctrl(ctrl)
354  {}
355 
356  private:
357  uvc_sensor& _uvc;
359  uint8_t _ctrl;
360  };
361 
362  class polling_error_handler;
363 
365  {
366  public:
368  : _polling_error_handler(handler), _value(1)
369  {}
370 
371  void set(float value);
372 
373  float query() const;
374 
375  option_range get_range() const;
376 
377  bool is_enabled() const;
378 
379 
380  const char* get_description() const;
381 
382  const char* get_value_description(float value) const;
383  void enable_recording(std::function<void(const option &)> record_action) override
384  {
385  _recording_function = record_action;
386  }
387  private:
388  polling_error_handler* _polling_error_handler;
389  float _value;
390  std::function<void(const option&)> _recording_function = [](const option&) {};
391  };
392 
396  {
397  public:
398  const char* get_value_description(float val) const override
399  {
400  return _auto_disabling_control->get_value_description(val);
401  }
402  const char* get_description() const override
403  {
404  return _auto_disabling_control->get_description();
405  }
406  void set(float value) override
407  {
408  auto strong = _auto_exposure.lock();
409  assert(strong);
410 
411  auto move_to_manual = false;
412  auto val = strong->query();
413 
414  if (std::find(_move_to_manual_values.begin(),
415  _move_to_manual_values.end(), val) != _move_to_manual_values.end())
416  {
417  move_to_manual = true;
418  }
419 
420  if (strong && move_to_manual)
421  {
422  LOG_DEBUG("Move option to manual mode in order to set a value");
423  strong->set(_manual_value);
424  }
425  _auto_disabling_control->set(value);
426  _recording_function(*this);
427  }
428 
429  float query() const override
430  {
431  return _auto_disabling_control->query();
432  }
433 
434  option_range get_range() const override
435  {
436  return _auto_disabling_control->get_range();
437  }
438 
439  bool is_enabled() const override
440  {
441  return _auto_disabling_control->is_enabled();
442  }
443 
444  bool is_read_only() const override
445  {
446  return _auto_disabling_control->is_read_only();
447  }
448 
449  explicit auto_disabling_control(std::shared_ptr<option> auto_disabling,
450  std::shared_ptr<option> auto_exposure,
451  std::vector<float> move_to_manual_values = {1.f},
452  float manual_value = 0.f)
453 
454  : _auto_disabling_control(auto_disabling), _auto_exposure(auto_exposure),
455  _move_to_manual_values(move_to_manual_values), _manual_value(manual_value)
456  {}
457  void enable_recording(std::function<void(const option &)> record_action) override
458  {
459  _recording_function = record_action;
460  }
461  private:
462  std::shared_ptr<option> _auto_disabling_control;
463  std::weak_ptr<option> _auto_exposure;
464  std::vector<float> _move_to_manual_values;
465  float _manual_value;
466  std::function<void(const option&)> _recording_function = [](const option&) {};
467  };
468 }
Definition: error-handling.h:9
bool is_enabled() const override
Definition: option.h:313
const option_range _opt_range
Definition: option.h:92
auto invoke_powered(T action) -> decltype(action(*static_cast< platform::uvc_device *>(nullptr)))
Definition: sensor.h:194
option_range get_range() const override
Definition: option.h:309
const char * get_value_description(float value) const
bool is_enabled() const override
Definition: option.h:46
const char * get_description() const override
Definition: option.h:136
Definition: backend.h:351
rs2_option
Defines general configuration controls. These can generally be mapped to camera UVC controls...
Definition: rs_option.h:22
const char * get_value_description(float val) const override
Definition: option.h:188
option_range get_range() const override
Definition: option.h:234
bool is_read_only() const override
Definition: option.h:444
float query() const override
Definition: options.h:20
Definition: types.h:268
Definition: option.h:98
float query() const override
Definition: option.h:121
option_range get_range() const override
Definition: option.h:83
Definition: option.h:19
uvc_pu_option(uvc_sensor &ep, rs2_option id)
Definition: option.h:176
bool is_enabled() const override
Definition: option.h:439
std::string _desciption
Definition: option.h:272
float query() const override
Definition: option.h:221
Definition: stream.h:188
float max
Definition: options.h:15
uvc_xu_option(uvc_sensor &ep, platform::extension_unit xu, uint8_t id, std::string description)
Definition: option.h:256
option_range get_range() const override
Definition: option.h:45
auto_disabling_control class provided a control that disable auto-control when changing the auto disa...
Definition: option.h:395
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:134
bool is_read_only() const override
Definition: option.h:22
const char * get_value_description(float val) const override
Definition: option.h:138
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:383
const char * get_description() const override
Definition: option.h:402
Definition: option.h:297
virtual bool get_xu(const extension_unit &xu, uint8_t ctrl, uint8_t *data, int len) const =0
Definition: algo.h:16
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:457
struct_field_option(std::shared_ptr< struct_interface< T, R, W >> struct_interface, U T::*field, const option_range &range)
Definition: option.h:315
std::shared_ptr< struct_field_option< T, R, W, U > > make_field_option(std::shared_ptr< struct_interface< T, R, W >> struct_interface, U T::*field, const option_range &range)
Definition: option.h:338
const char * get_value_description(float val) const override
Definition: option.h:398
Definition: option.h:64
#define LOG_DEBUG(...)
Definition: types.h:107
uvc_sensor & _ep
Definition: option.h:269
void set_description(float val, const std::string &desc)
Definition: option.h:148
const char * get_description() const override
Definition: option.h:321
option_range get_range() const override
polling_errors_disable(polling_error_handler *handler)
Definition: option.h:367
virtual control_range get_xu_range(const extension_unit &xu, uint8_t ctrl, int len) const =0
const char * get_description() const
std::vector< uint8_t > send_receive(const std::vector< uint8_t > &data, int, bool require_response) override
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:194
Definition: option.h:35
uvc_pu_option(uvc_sensor &ep, rs2_option id, const std::map< float, std::string > &description_per_value)
Definition: option.h:181
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:29
auto_disabling_control(std::shared_ptr< option > auto_disabling, std::shared_ptr< option > auto_exposure, std::vector< float > move_to_manual_values={1.f}, float manual_value=0.f)
Definition: option.h:449
float query() const override
Definition: option.h:305
virtual void enable_recording(std::function< void(const option &)> recording_action) override
Definition: option.h:87
Definition: options.h:12
Definition: option.h:364
const_value_option(std::string desc, float val)
Definition: option.h:38
std::function< void(const option &)> _recording_function
Definition: option.h:93
Definition: types.h:54
Definition: option.h:206
float query() const override
Definition: option.h:44
ptr_option(T min, T max, T step, T def, T *value, const std::string &desc)
Definition: option.h:101
platform::extension_unit _xu
Definition: option.h:270
const char * get_description() const override
option_range get_range() const override
Definition: option.h:434
const_value_option(std::string desc, lazy< float > val)
Definition: option.h:41
command_transfer_over_xu(uvc_sensor &uvc, platform::extension_unit xu, uint8_t ctrl)
Definition: option.h:351
bool is_enabled() const override
Definition: option.h:132
option_range get_range() const override
Definition: option.h:126
bool is_valid(float value) const
Definition: option.h:71
std::string hexify(unsigned char n)
Definition: option.h:276
Definition: backend.h:387
std::function< void(const option &)> _recording_function
Definition: option.h:273
const char * get_description() const override
Definition: option.h:260
Definition: types.h:685
virtual bool set_xu(const extension_unit &xu, uint8_t ctrl, const uint8_t *data, int len)=0
void update(std::shared_ptr< extension_snapshot > ext) override
Definition: option.h:50
const char * get_description() const override
Definition: option.h:48
bool is_enabled() const override
Definition: option.h:171
uint8_t _id
Definition: option.h:271
float min
Definition: options.h:14
void on_set(std::function< void(float)> on_set)
Definition: option.h:153
Definition: extension.h:33
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:326
bool is_enabled() const override
Definition: option.h:254
Definition: option.h:162
float step
Definition: options.h:16
constexpr uint32_t auto_exposure
Definition: ros_file_format.h:623
option_base(const option_range &opt_range)
Definition: option.h:67
Definition: sensor.h:173
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:264
float query() const override
Definition: option.h:429