Lely core libraries  1.9.2
driver.cpp
Go to the documentation of this file.
1 
24 #include "coapp.hpp"
25 
26 #if !LELY_NO_COAPP_MASTER
27 
28 #include <lely/coapp/driver.hpp>
29 
30 #if !LELY_NO_THREADS
31 #include <thread>
32 #endif
33 
34 namespace lely {
35 
36 namespace canopen {
37 
38 #if !LELY_NO_THREADS
39 struct LoopDriver::Impl_ {
41  explicit Impl_(LoopDriver* self);
42  ~Impl_();
43 
44  void Start();
45 
46  LoopDriver* self{nullptr};
47  ::std::thread thread;
48 };
49 #endif
50 
51 BasicDriver::BasicDriver(aio::LoopBase& loop, aio::ExecutorBase& exec,
52  BasicMaster& master_, uint8_t id)
53  : master(master_), loop_(loop), exec_(exec), id_(id) {
54  master.Insert(*this);
55 }
56 
57 BasicDriver::~BasicDriver() { master.Erase(*this); }
58 
59 uint8_t
60 BasicDriver::netid() const noexcept {
61  return master.netid();
62 }
63 
64 #if !LELY_NO_THREADS
65 
66 LoopDriver::LoopDriver(BasicMaster& master, uint8_t id)
67  : BasicDriver(loop, exec, master, id), impl_(new Impl_(this)) {}
68 
69 LoopDriver::~LoopDriver() = default;
70 
71 LoopDriver::Impl_::Impl_(LoopDriver* self_)
72  : self(self_), thread(&Impl_::Start, this) {}
73 
74 LoopDriver::Impl_::~Impl_() {
75  self->GetLoop().Stop();
76  thread.join();
77 }
78 
79 void
80 LoopDriver::Impl_::Start() {
81  auto loop = self->GetLoop();
82  auto exec = self->GetExecutor();
83 
84  // Start the event loop. Signal the existance of a fake task to
85  // prevent the loop for stopping early.
86  exec.OnTaskStarted();
87  loop.Run();
88  exec.OnTaskFinished();
89 
90  // Finish remaining tasks, but do not block.
91  loop.Restart();
92  loop.RunFor();
93 }
94 
95 #endif // !LELY_NO_THREADS
96 
97 } // namespace canopen
98 
99 } // namespace lely
100 
101 #endif // !LELY_NO_COAPP_MASTER
The CANopen master.
Definition: master.hpp:46
The base class for drivers for remote CANopen nodes.
Definition: driver.hpp:298
uint8_t netid() const noexcept
Returns the network-ID.
Definition: device.cpp:87
This header file is part of the C++ CANopen application library; it contains the remote node driver i...
uint8_t netid() const noexcept final override
Returns the network-ID.
Definition: driver.cpp:60
This is the internal header file of the C++ CANopen application library.
void Insert(DriverBase &driver)
Registers a driver for a remote CANopen node.
Definition: master.cpp:106
BasicMaster & master
A reference to the master with which this driver is registered.
Definition: driver.hpp:704
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
BasicDriver(aio::LoopBase &loop, aio::ExecutorBase &exec, BasicMaster &master, uint8_t id)
Creates a new driver for a remote CANopen node and registers it with the master.
Definition: driver.cpp:51
void Erase(DriverBase &driver)
Unregisters a driver for a remote CANopen node.
Definition: master.cpp:123