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  option_range get_range() const override;
74 
75  virtual void enable_recording(std::function<void(const option&)> recording_action) override;
76  protected:
78  std::function<void(const option&)> _recording_function = [](const option&) {};
79 
80  };
81 
82  template<class T>
83  class ptr_option : public option_base
84  {
85  public:
86  ptr_option(T min, T max, T step, T def, T* value, const std::string& desc)
87  : option_base({ static_cast<float>(min),
88  static_cast<float>(max),
89  static_cast<float>(step),
90  static_cast<float>(def), }),
91  _min(min), _max(max), _step(step), _def(def), _value(value), _desc(desc)
92  {
93  static_assert((std::is_arithmetic<T>::value), "ptr_option class supports arithmetic built-in types only");
94  _on_set = [](float x) {};
95  }
96 
97  void set(float value) override
98  {
99  T val = static_cast<T>(value);
100  if ((_max < val) || (_min > val))
101  throw invalid_value_exception(to_string() << "Given value " << value << "is outside valid range!");
102  *_value = val;
103  _on_set(value);
104  }
105 
106  float query() const override
107  {
108  return static_cast<float>(*_value);
109  }
110 
111  option_range get_range() const override {
112  return{
113  (float)_min, (float)_max,
114  (float)_step, (float)_def };
115  }
116 
117  bool is_enabled() const override { return true; }
118 
119  void enable_recording(std::function<void(const option &)> record_action) override {}
120 
121  const char* get_description() const override { return _desc.c_str(); }
122 
123  const char* get_value_description(float val) const override
124  {
125  auto it = _item_desc.find(val);
126  if (it != _item_desc.end())
127  {
128  return it->second.c_str();
129  }
130  return nullptr;
131  }
132 
133  void set_description(float val, const std::string& desc)
134  {
135  _item_desc[val] = desc;
136  }
137 
138  void on_set(std::function<void(float)> on_set) { _on_set = on_set; }
139  private:
140  T _min, _max, _step, _def;
141  T* _value;
142  std::string _desc;
143  std::map<float, std::string> _item_desc;
144  std::function<void(float)> _on_set;
145  };
146 
147  class uvc_pu_option : public option
148  {
149  public:
150  void set(float value) override;
151 
152  float query() const override;
153 
154  option_range get_range() const override;
155 
156  bool is_enabled() const override
157  {
158  return true;
159  }
160 
162  : _ep(ep), _id(id)
163  {
164  }
165 
166  uvc_pu_option(uvc_sensor& ep, rs2_option id, const std::map<float, std::string>& description_per_value)
167  : _ep(ep), _id(id), _description_per_value(description_per_value)
168  {
169  }
170 
171  const char* get_description() const override;
172 
173  const char* get_value_description(float val) const override
174  {
175  if (_description_per_value.find(val) != _description_per_value.end())
176  return _description_per_value.at(val).c_str();
177  return nullptr;
178  }
179  void enable_recording(std::function<void(const option &)> record_action) override
180  {
181  _record = record_action;
182  }
183  private:
184  uvc_sensor& _ep;
185  rs2_option _id;
186  const std::map<float, std::string> _description_per_value;
187  std::function<void(const option &)> _record = [](const option &) {};
188  };
189 
190  template<typename T>
191  class uvc_xu_option : public option
192  {
193  public:
194  void set(float value) override
195  {
196  _ep.invoke_powered(
197  [this, value](platform::uvc_device& dev)
198  {
199  T t = static_cast<T>(value);
200  if (!dev.set_xu(_xu, _id, reinterpret_cast<uint8_t*>(&t), sizeof(T)))
201  throw invalid_value_exception(to_string() << "set_xu(id=" << std::to_string(_id) << ") failed!" << " Last Error: " << strerror(errno));
202  _recording_function(*this);
203  });
204  }
205 
206  float query() const override
207  {
208  return static_cast<float>(_ep.invoke_powered(
209  [this](platform::uvc_device& dev)
210  {
211  T t;
212  if (!dev.get_xu(_xu, _id, reinterpret_cast<uint8_t*>(&t), sizeof(T)))
213  throw invalid_value_exception(to_string() << "get_xu(id=" << std::to_string(_id) << ") failed!" << " Last Error: " << strerror(errno));
214 
215  return static_cast<float>(t);
216  }));
217  }
218 
219  option_range get_range() const override
220  {
221  auto uvc_range = _ep.invoke_powered(
222  [this](platform::uvc_device& dev)
223  {
224  return dev.get_xu_range(_xu, _id, sizeof(T));
225  });
226 
227  if (uvc_range.min.size() < sizeof(int32_t)) return option_range{0,0,1,0};
228 
229  auto min = *(reinterpret_cast<int32_t*>(uvc_range.min.data()));
230  auto max = *(reinterpret_cast<int32_t*>(uvc_range.max.data()));
231  auto step = *(reinterpret_cast<int32_t*>(uvc_range.step.data()));
232  auto def = *(reinterpret_cast<int32_t*>(uvc_range.def.data()));
233  return option_range{static_cast<float>(min),
234  static_cast<float>(max),
235  static_cast<float>(step),
236  static_cast<float>(def)};
237  }
238 
239  bool is_enabled() const override { return true; }
240 
241  uvc_xu_option(uvc_sensor& ep, platform::extension_unit xu, uint8_t id, std::string description)
242  : _ep(ep), _xu(xu), _id(id), _desciption(std::move(description))
243  {}
244 
245  const char* get_description() const override
246  {
247  return _desciption.c_str();
248  }
249  void enable_recording(std::function<void(const option &)> record_action) override
250  {
251  _recording_function = record_action;
252  }
253  protected:
256  uint8_t _id;
257  std::string _desciption;
258  std::function<void(const option&)> _recording_function = [](const option&) {};
259  };
260 
261  inline std::string hexify(unsigned char n)
262  {
263  std::string res;
264 
265  do
266  {
267  res += "0123456789ABCDEF"[n % 16];
268  n >>= 4;
269  } while (n);
270 
271  reverse(res.begin(), res.end());
272 
273  if (res.size() == 1)
274  {
275  res.insert(0, "0");
276  }
277 
278  return res;
279  }
280 
281  template<class T, class R, class W, class U>
283  {
284  public:
285  void set(float value) override
286  {
287  _struct_interface->set(_field, value);
288  _recording_function(*this);
289  }
290  float query() const override
291  {
292  return _struct_interface->get(_field);
293  }
294  option_range get_range() const override
295  {
296  return _range;
297  }
298  bool is_enabled() const override { return true; }
299 
301  U T::* field, const option_range& range)
302  : _struct_interface(struct_interface), _range(range), _field(field)
303  {
304  }
305 
306  const char* get_description() const override
307  {
308  return nullptr;
309  }
310 
311  void enable_recording(std::function<void(const option &)> record_action) override
312  {
313  _recording_function = record_action;
314  }
315  private:
316  std::shared_ptr<struct_interface<T, R, W>> _struct_interface;
317  option_range _range;
318  U T::* _field;
319  std::function<void(const option&)> _recording_function = [](const option&) {};
320  };
321 
322  template<class T, class R, class W, class U>
323  std::shared_ptr<struct_field_option<T, R, W, U>> make_field_option(
325  U T::* field, const option_range& range)
326  {
327  return std::make_shared<struct_field_option<T, R, W, U>>
328  (struct_interface, field, range);
329  }
330 
332  {
333  public:
334  std::vector<uint8_t> send_receive(const std::vector<uint8_t>& data, int, bool require_response) override;
335 
337  platform::extension_unit xu, uint8_t ctrl)
338  : _uvc(uvc), _xu(std::move(xu)), _ctrl(ctrl)
339  {}
340 
341  private:
342  uvc_sensor& _uvc;
344  uint8_t _ctrl;
345  };
346 
347  class polling_error_handler;
348 
350  {
351  public:
353  : _polling_error_handler(handler), _value(1)
354  {}
355 
356  void set(float value);
357 
358  float query() const;
359 
360  option_range get_range() const;
361 
362  bool is_enabled() const;
363 
364 
365  const char* get_description() const;
366 
367  const char* get_value_description(float value) const;
368  void enable_recording(std::function<void(const option &)> record_action) override
369  {
370  _recording_function = record_action;
371  }
372  private:
373  polling_error_handler* _polling_error_handler;
374  float _value;
375  std::function<void(const option&)> _recording_function = [](const option&) {};
376  };
377 
381  {
382  public:
383  const char* get_value_description(float val) const override
384  {
385  return _auto_disabling_control->get_value_description(val);
386  }
387  const char* get_description() const override
388  {
389  return _auto_disabling_control->get_description();
390  }
391  void set(float value) override
392  {
393  auto strong = _auto_exposure.lock();
394  assert(strong);
395 
396  auto move_to_manual = false;
397  auto val = strong->query();
398 
399  if (std::find(_move_to_manual_values.begin(),
400  _move_to_manual_values.end(), val) != _move_to_manual_values.end())
401  {
402  move_to_manual = true;
403  }
404 
405  if (strong && move_to_manual)
406  {
407  LOG_DEBUG("Move option to manual mode in order to set a value");
408  strong->set(_manual_value);
409  }
410  _auto_disabling_control->set(value);
411  _recording_function(*this);
412  }
413 
414  float query() const override
415  {
416  return _auto_disabling_control->query();
417  }
418 
419  option_range get_range() const override
420  {
421  return _auto_disabling_control->get_range();
422  }
423 
424  bool is_enabled() const override
425  {
426  return _auto_disabling_control->is_enabled();
427  }
428 
429  bool is_read_only() const override
430  {
431  return _auto_disabling_control->is_read_only();
432  }
433 
434  explicit auto_disabling_control(std::shared_ptr<option> auto_disabling,
435  std::shared_ptr<option> auto_exposure,
436  std::vector<float> move_to_manual_values = {1.f},
437  float manual_value = 0.f)
438 
439  : _auto_disabling_control(auto_disabling), _auto_exposure(auto_exposure),
440  _move_to_manual_values(move_to_manual_values), _manual_value(manual_value)
441  {}
442  void enable_recording(std::function<void(const option &)> record_action) override
443  {
444  _recording_function = record_action;
445  }
446  private:
447  std::shared_ptr<option> _auto_disabling_control;
448  std::weak_ptr<option> _auto_exposure;
449  std::vector<float> _move_to_manual_values;
450  float _manual_value;
451  std::function<void(const option&)> _recording_function = [](const option&) {};
452  };
453 }
Definition: error-handling.h:9
bool is_enabled() const override
Definition: option.h:298
const option_range _opt_range
Definition: option.h:77
option_range get_range() const override
Definition: option.h:294
bool is_enabled() const override
Definition: option.h:46
virtual const char * get_description() const =0
const char * get_description() const override
Definition: option.h:121
Definition: backend.h:380
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:173
option_range get_range() const override
Definition: option.h:219
bool is_read_only() const override
Definition: option.h:429
virtual void set(float value)=0
Definition: options.h:20
Definition: types.h:269
Definition: option.h:83
float query() const override
Definition: option.h:106
Definition: option.h:19
uvc_pu_option(uvc_sensor &ep, rs2_option id)
Definition: option.h:161
bool is_enabled() const override
Definition: option.h:424
std::string _desciption
Definition: option.h:257
float query() const override
Definition: option.h:206
Definition: stream.h:188
uvc_xu_option(uvc_sensor &ep, platform::extension_unit xu, uint8_t id, std::string description)
Definition: option.h:241
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:380
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:119
virtual option_range get_range() const =0
bool is_read_only() const override
Definition: option.h:22
const char * get_value_description(float val) const override
Definition: option.h:123
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:368
virtual float query() const =0
const char * get_description() const override
Definition: option.h:387
Definition: option.h:282
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:442
struct_field_option(std::shared_ptr< struct_interface< T, R, W >> struct_interface, U T::*field, const option_range &range)
Definition: option.h:300
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:323
const char * get_value_description(float val) const override
Definition: option.h:383
Definition: option.h:64
#define LOG_DEBUG(...)
Definition: types.h:108
uvc_sensor & _ep
Definition: option.h:254
void set_description(float val, const std::string &desc)
Definition: option.h:133
const char * get_description() const override
Definition: option.h:306
virtual const char * get_value_description(float) const
Definition: options.h:29
virtual bool is_enabled() const =0
polling_errors_disable(polling_error_handler *handler)
Definition: option.h:352
virtual control_range get_xu_range(const extension_unit &xu, uint8_t ctrl, int len) const =0
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:179
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:166
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:434
float query() const override
Definition: option.h:290
Definition: options.h:12
Definition: option.h:349
const_value_option(std::string desc, float val)
Definition: option.h:38
Definition: types.h:55
Definition: option.h:191
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:86
platform::extension_unit _xu
Definition: option.h:255
option_range get_range() const override
Definition: option.h:419
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:336
bool is_enabled() const override
Definition: option.h:117
option_range get_range() const override
Definition: option.h:111
std::string hexify(unsigned char n)
Definition: option.h:261
Definition: backend.h:413
const char * get_description() const override
Definition: option.h:245
Definition: types.h:735
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:156
uint8_t _id
Definition: option.h:256
void on_set(std::function< void(float)> on_set)
Definition: option.h:138
Definition: extension.h:33
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:311
bool is_enabled() const override
Definition: option.h:239
Definition: option.h:147
constexpr uint32_t auto_exposure
Definition: ros_file_format.h:624
option_base(const option_range &opt_range)
Definition: option.h:67
Definition: sensor.h:175
void enable_recording(std::function< void(const option &)> record_action) override
Definition: option.h:249
float query() const override
Definition: option.h:414