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
34namespace lely {
35
36namespace canopen {
37
38#if !LELY_NO_THREADS
41 explicit Impl_(LoopDriver* self);
42 ~Impl_();
43
44 void Start();
45
46 LoopDriver* self{nullptr};
47 ::std::thread thread;
48};
49#endif
50
51BasicDriver::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
57BasicDriver::~BasicDriver() { master.Erase(*this); }
58
59uint8_t
60BasicDriver::netid() const noexcept {
61 return master.netid();
62}
63
64#if !LELY_NO_THREADS
65
66LoopDriver::LoopDriver(BasicMaster& master, uint8_t id)
67 : BasicDriver(loop, exec, master, id), impl_(new Impl_(this)) {}
68
69LoopDriver::~LoopDriver() = default;
70
71LoopDriver::Impl_::Impl_(LoopDriver* self_)
72 : self(self_), thread(&Impl_::Start, this) {}
73
74LoopDriver::Impl_::~Impl_() {
75 self->GetLoop().Stop();
76 thread.join();
77}
78
79void
80LoopDriver::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 base class for drivers for remote CANopen nodes.
Definition: driver.hpp:298
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
uint8_t netid() const noexcept final override
Returns the network-ID.
Definition: driver.cpp:60
aio::LoopBase GetLoop() const noexcept
Returns the event loop used to create promises and futures.
Definition: driver.hpp:324
BasicMaster & master
A reference to the master with which this driver is registered.
Definition: driver.hpp:704
The CANopen master.
Definition: master.hpp:46
void Erase(DriverBase &driver)
Unregisters a driver for a remote CANopen node.
Definition: master.cpp:123
void Insert(DriverBase &driver)
Registers a driver for a remote CANopen node.
Definition: master.cpp:106
uint8_t netid() const noexcept
Returns the network-ID.
Definition: device.cpp:87
A CANopen driver running its own dedicated event loop in a separate thread.
Definition: driver.hpp:796
This header file is part of the C++ CANopen application library; it contains the remote node driver i...
Global namespace for the Lely Industries N.V. libraries.
Definition: buf.hpp:32
This is the internal header file of the C++ CANopen application library.
The internal implementation of lely::canopen::LoopDriver.
Definition: driver.cpp:40