Lely core libraries  1.9.2
slave.hpp
Go to the documentation of this file.
1 
22 #ifndef LELY_COCPP_SLAVE_HPP_
23 #define LELY_COCPP_SLAVE_HPP_
24 
25 #include <lely/coapp/node.hpp>
26 
27 namespace lely {
28 
29 namespace canopen {
30 
32 class BasicSlave : public Node {
33  public:
49  BasicSlave(aio::TimerBase& timer, aio::CanBusBase& bus,
50  const ::std::string& dcf_txt, const ::std::string& dcf_bin = "",
51  uint8_t id = 0xff);
52 
53  virtual ~BasicSlave();
54 
55  protected:
56  class Object;
57  class ConstObject;
58 
63  class SubObject {
64  friend class Object;
65 
66  public:
67  SubObject(const SubObject&) = default;
68  SubObject(SubObject&&) = default;
69 
70  SubObject& operator=(const SubObject&) = default;
71  SubObject& operator=(SubObject&&) = default;
72 
83  template <class T>
84  SubObject&
85  operator=(T&& value) {
86  Set(::std::forward<T>(value));
87  return *this;
88  }
89 
98  template <class T>
99  operator T() const {
100  return Get<T>();
101  }
102 
113  const ::std::type_info&
114  Type() const {
115  return slave_->Type(idx_, subidx_);
116  }
117 
130  const ::std::type_info&
131  Type(::std::error_code& ec) const {
132  return slave_->Type(idx_, subidx_, ec);
133  }
134 
145  template <class T>
146  T
147  Get() const {
148  return slave_->Get<T>(idx_, subidx_);
149  }
150 
163  template <class T>
164  T
165  Get(::std::error_code& ec) const {
166  return slave_->Get<T>(idx_, subidx_, ec);
167  }
168 
179  template <class T>
180  void
181  Set(T&& value) {
182  slave_->Set(idx_, subidx_, ::std::forward<T>(value));
183  }
184 
198  template <class T>
199  void
200  Set(T&& value, ::std::error_code& ec) {
201  slave_->Set(idx_, subidx_, ::std::forward<T>(value), ec);
202  }
203 
216  void
217  Set(const void* p, ::std::size_t n) {
218  slave_->Set(idx_, subidx_, p, n);
219  }
220 
232  void
233  Set(const void* p, ::std::size_t n, ::std::error_code& ec) {
234  slave_->Set(idx_, subidx_, p, n, ec);
235  }
236 
237  private:
238  SubObject(BasicSlave* slave, uint16_t idx, uint8_t subidx)
239  : slave_(slave), idx_(idx), subidx_(subidx) {}
240 
241  BasicSlave* slave_;
242  uint16_t idx_;
243  uint8_t subidx_;
244  };
245 
251  friend class Object;
252  friend class ConstObject;
253 
254  public:
263  template <class T>
264  operator T() const {
265  return Get<T>();
266  }
267 
278  const ::std::type_info&
279  Type() const {
280  return slave_->Type(idx_, subidx_);
281  }
282 
295  const ::std::type_info&
296  Type(::std::error_code& ec) const {
297  return slave_->Type(idx_, subidx_, ec);
298  }
299 
310  template <class T>
311  T
312  Get() const {
313  return slave_->Get<T>(idx_, subidx_);
314  }
315 
328  template <class T>
329  T
330  Get(::std::error_code& ec) const {
331  return slave_->Get<T>(idx_, subidx_, ec);
332  }
333 
334  private:
335  ConstSubObject(const BasicSlave* slave, uint16_t idx, uint8_t subidx)
336  : slave_(slave), idx_(idx), subidx_(subidx) {}
337 
338  const BasicSlave* slave_;
339  uint16_t idx_;
340  uint8_t subidx_;
341  };
342 
347  class Object {
348  friend class BasicSlave;
349 
350  public:
361  SubObject operator[](uint8_t subidx) {
362  return SubObject(slave_, idx_, subidx);
363  }
364 
375  ConstSubObject operator[](uint8_t subidx) const {
376  return ConstSubObject(slave_, idx_, subidx);
377  }
378 
379  private:
380  Object(BasicSlave* slave, uint16_t idx) : slave_(slave), idx_(idx) {}
381 
382  BasicSlave* slave_;
383  uint16_t idx_;
384  };
385 
390  class ConstObject {
391  friend class BasicSlave;
392 
393  public:
404  ConstSubObject operator[](uint8_t subidx) const {
405  return ConstSubObject(slave_, idx_, subidx);
406  }
407 
408  private:
409  ConstObject(const BasicSlave* slave, uint16_t idx)
410  : slave_(slave), idx_(idx) {}
411 
412  const BasicSlave* slave_;
413  uint16_t idx_;
414  };
415 
431  template <class T>
432  using OnReadSignature = ::std::error_code(uint16_t idx, uint8_t subdx,
433  T& value);
434 
452  template <class T>
453  using OnWriteSignature = typename ::std::conditional<
455  ::std::error_code(uint16_t idx, uint8_t subidx, T& new_val, T old_val),
456  ::std::error_code(uint16_t idx, uint8_t subidx, T& new_val)>::type;
457 
468  Object operator[](uint16_t idx) { return Object(this, idx); }
469 
480  ConstObject operator[](uint16_t idx) const { return ConstObject(this, idx); }
481 
494  template <class T>
495  typename ::std::enable_if<detail::IsCanopenType<T>::value>::type OnRead(
496  uint16_t idx, uint8_t subidx, ::std::function<OnReadSignature<T>> ind);
497 
509  template <class T>
510  typename ::std::enable_if<detail::IsCanopenType<T>::value>::type OnRead(
511  uint16_t idx, uint8_t subidx, ::std::function<OnReadSignature<T>> ind,
512  ::std::error_code& ec);
513 
527  template <class T>
528  typename ::std::enable_if<detail::IsCanopenType<T>::value>::type OnWrite(
529  uint16_t idx, uint8_t subidx, ::std::function<OnWriteSignature<T>> ind);
530 
543  template <class T>
544  typename ::std::enable_if<detail::IsCanopenType<T>::value>::type OnWrite(
545  uint16_t idx, uint8_t subidx, ::std::function<OnWriteSignature<T>> ind,
546  ::std::error_code& ec);
547 
548 #ifndef DOXYGEN_SHOULD_SKIP_THIS
549  private:
550 #endif
551 
560  virtual void
561  OnLifeGuarding(bool occurred) noexcept {
562  (void)occurred;
563  };
564 
565  private:
566  struct Impl_;
567  ::std::unique_ptr<Impl_> impl_;
568 };
569 
570 } // namespace canopen
571 
572 } // namespace lely
573 
574 #endif // LELY_COCPP_SLAVE_HPP_
void Set(const void *p, ::std::size_t n)
Writes an OCTET_STRING or DOMAIN value to the sub-object.
Definition: slave.hpp:217
BasicSlave(aio::TimerBase &timer, aio::CanBusBase &bus, const ::std::string &dcf_txt, const ::std::string &dcf_bin="", uint8_t id=0xff)
Creates a new CANopen slave.
Definition: slave.cpp:72
void Set(T &&value)
Writes a value to the sub-object.
Definition: slave.hpp:181
typename ::std::enable_if< detail::IsCanopenType< T >::value, T >::type Get(uint16_t idx, uint8_t subidx) const
Reads the value of a sub-object.
ConstObject operator[](uint16_t idx) const
Returns an accessor object that provides read-only access to the specified CANopen object in the loca...
Definition: slave.hpp:480
ConstSubObject operator[](uint8_t subidx) const
Returns an accessor object that provides read-only access to the specified CANopen sub-object in the ...
Definition: slave.hpp:404
const ::std::type_info & Type() const
Returns the type of the sub-object.
Definition: slave.hpp:279
ConstSubObject operator[](uint8_t subidx) const
Returns an accessor object that provides read-only access to the specified CANopen sub-object in the ...
Definition: slave.hpp:375
An accessor providing read-only access to a CANopen sub-object in a local object dictionary.
Definition: slave.hpp:250
Object operator[](uint16_t idx)
Returns a mutator object that provides read/write access to the specified CANopen object in the local...
Definition: slave.hpp:468
const ::std::type_info & Type() const
Returns the type of the sub-object.
Definition: slave.hpp:114
SubObject & operator=(T &&value)
Sets the value of the sub-object.
Definition: slave.hpp:85
void Set(const void *p, ::std::size_t n, ::std::error_code &ec)
Writes an OCTET_STRING or DOMAIN value to the sub-object.
Definition: slave.hpp:233
T Get() const
Reads the value of the sub-object.
Definition: slave.hpp:312
SubObject operator[](uint8_t subidx)
Returns a mutator object that provides read/write access to the specified CANopen sub-object in the l...
Definition: slave.hpp:361
::std::error_code(uint16_t idx, uint8_t subdx, T &value) OnReadSignature
The signature of the callback function invoked on read (SDO upload) access to the local object dictio...
Definition: slave.hpp:433
void Set(T &&value, ::std::error_code &ec)
Writes a value to the sub-object.
Definition: slave.hpp:200
T Get(::std::error_code &ec) const
Reads the value of the sub-object.
Definition: slave.hpp:330
const ::std::type_info & Type(uint16_t idx, uint8_t subidx) const
Returns the type of a sub-object.
Definition: device.cpp:392
If T is one of the CANopen basic types, provides the member constant value equal to true...
Definition: type_traits.hpp:43
The base class for CANopen slaves.
Definition: slave.hpp:32
typename ::std::conditional< detail::IsCanopenBasic< T >::value, ::std::error_code(uint16_t idx, uint8_t subidx, T &new_val, T old_val), ::std::error_code(uint16_t idx, uint8_t subidx, T &new_val)>::type OnWriteSignature
The signature of the callback function invoked on write (SDO download) access to the local object dic...
Definition: slave.hpp:456
const ::std::type_info & Type(::std::error_code &ec) const
Returns the type of the sub-object.
Definition: slave.hpp:296
virtual void OnLifeGuarding(bool occurred) noexcept
The function invoked when a life guarding event occurs or is resolved.
Definition: slave.hpp:561
typename ::std::enable_if< detail::IsCanopenType< T >::value >::type OnWrite(uint16_t idx, uint8_t subidx, ::std::function< OnWriteSignature< T >> ind)
Registers a callback function to be invoked on write (SDO download) access to the specified CANopen s...
A mutator providing read/write access to a CANopen object in a local object dictionary.
Definition: slave.hpp:347
This header file is part of the C++ CANopen application library; it contains the CANopen node declara...
T Get(::std::error_code &ec) const
Reads the value of the sub-object.
Definition: slave.hpp:165
typename ::std::enable_if< detail::IsCanopenType< T >::value >::type OnRead(uint16_t idx, uint8_t subidx, ::std::function< OnReadSignature< T >> ind)
Registers a callback function to be invoked on read (SDO upload) access to the specified CANopen sub-...
An accessor providing read-only access to a CANopen object in a local object dictionary.
Definition: slave.hpp:390
T Get() const
Reads the value of the sub-object.
Definition: slave.hpp:147
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
The base class for CANopen nodes.
Definition: node.hpp:109
typename ::std::enable_if< detail::IsCanopenBasic< T >::value >::type Set(uint16_t idx, uint8_t subidx, T value)
Writes a CANopen basic value to a sub-object.
const ::std::type_info & Type(::std::error_code &ec) const
Returns the type of the sub-object.
Definition: slave.hpp:131
A mutator providing read/write access to a CANopen sub-object in a local object dictionary.
Definition: slave.hpp:63