Motion Master
|
Base class for periodically refreshing a set of device parameters in a background thread. More...
#include <base.h>
Public Member Functions | |
DeviceParameterRefresher (Device &device, std::chrono::milliseconds intervalBetweenUploads) | |
Constructor initializes the refresher with a device reference and upload interval. More... | |
~DeviceParameterRefresher () | |
Destructor ensures the refresher thread is stopped cleanly. More... | |
void | add (uint16_t index, uint8_t subindex) |
Add a parameter identified by (index, subindex) to the refresh list. More... | |
void | remove (uint16_t index, uint8_t subindex) |
Remove a parameter identified by (index, subindex) from the refresh list. More... | |
void | clear () |
Remove all parameters from the refresh list. More... | |
void | start () |
Starts the refresher background thread if it is not already running. More... | |
void | stop () |
Stops the refresher background thread if it is running. More... | |
void | triggerManualRefresh (bool async=false) |
Triggers a manual refresh of device parameters. More... | |
Protected Member Functions | |
virtual void | refresh (const std::vector< Parameter::Address > &addresses)=0 |
Rereads the specified device parameters. More... | |
Protected Attributes | |
std::chrono::milliseconds | interval_ |
Time to wait between consecutive parameter uploads. More... | |
std::condition_variable | cv_ |
Condition variable used to notify the worker thread. More... | |
std::mutex | mutex_ |
Mutex protecting access to shared data members. More... | |
std::vector< Parameter::Address > | addresses_ |
List of parameter addresses to refresh. More... | |
Device & | device_ |
Reference to the device instance used for uploading. More... | |
bool | running_ |
Flag indicating whether the refresher thread is running. More... | |
std::thread | worker_ |
The background thread performing periodic uploads. More... | |
Base class for periodically refreshing a set of device parameters in a background thread.
This class manages a list of (index, subindex) parameter addresses and runs a background worker thread that periodically calls the refresh()
method with the current list of parameters.
Derived classes must implement the refresh()
method to define how parameters are uploaded or refreshed (e.g., individually for EtherCAT devices, or in bulk for Ethernet devices).
The class provides thread-safe methods to add, remove, and clear parameters, as well as to start and stop the background refresher thread.
The refresh cycle waits for a configurable interval between refreshes and can be stopped gracefully at any time.
mm::comm::base::DeviceParameterRefresher::DeviceParameterRefresher | ( | Device & | device, |
std::chrono::milliseconds | intervalBetweenUploads | ||
) |
Constructor initializes the refresher with a device reference and upload interval.
device | Reference to the device to upload parameters from. |
interval | Time to wait between consecutive parameter uploads. |
mm::comm::base::DeviceParameterRefresher::~DeviceParameterRefresher | ( | ) |
Destructor ensures the refresher thread is stopped cleanly.
void mm::comm::base::DeviceParameterRefresher::add | ( | uint16_t | index, |
uint8_t | subindex | ||
) |
Add a parameter identified by (index, subindex) to the refresh list.
index | The parameter index (uint16_t). |
subindex | The parameter subindex (uint8_t). |
Thread-safe: locks mutex while modifying the container.
< Notify in case the thread is waiting on empty parameters.
void mm::comm::base::DeviceParameterRefresher::clear | ( | ) |
Remove all parameters from the refresh list.
Thread-safe: locks mutex while clearing the container.
|
protectedpure virtual |
Rereads the specified device parameters.
This pure virtual function is called by the background thread loop with a snapshot of all parameters to refresh.
Derived classes must implement this to define how parameters are uploaded, for example one-by-one or in bulk.
This function must not handle waiting or stop checking — that is managed by the base class.
addresses | A snapshot of the current parameter addresses (index and subindex pairs) to be uploaded. |
Implemented in mm::comm::spoe::DeviceParameterRefresher.
void mm::comm::base::DeviceParameterRefresher::remove | ( | uint16_t | index, |
uint8_t | subindex | ||
) |
Remove a parameter identified by (index, subindex) from the refresh list.
index | The parameter index. |
subindex | The parameter subindex. |
Thread-safe: locks mutex while modifying the container.
void mm::comm::base::DeviceParameterRefresher::start | ( | ) |
Starts the refresher background thread if it is not already running.
Acquires the mutex to safely check and update the running_
flag. If the refresher is already running, this function does nothing. If not, it sets the running_
flag to true and launches the worker thread, which runs the internal run()
loop in the background.
This function is thread-safe.
< Lock to protect shared state
< If the refresher is already running, do nothing.
< Mark as running.
< Launch the worker thread.
void mm::comm::base::DeviceParameterRefresher::stop | ( | ) |
Stops the refresher background thread if it is running.
This method acquires the mutex to safely check and update the running_
flag. If the refresher is not running, the function does nothing. If it is running, it sets the running_
flag to false and notifies the condition variable to wake the worker thread if it is currently waiting. Finally, if the worker thread is joinable, it joins the thread to ensure clean shutdown.
This function is thread-safe.
< Acquire lock to protect shared state.
< If the refresher is not running, do nothing.
< Signal the thread to stop.
< Notify the condition variable to wake the thread if it is waiting.
< Check if the worker thread can be joined.
< Wait for the worker thread to finish.
void mm::comm::base::DeviceParameterRefresher::triggerManualRefresh | ( | bool | async = false | ) |
Triggers a manual refresh of device parameters.
If the refresher is already running, this call does nothing. If async
is true, the refresh will run in a detached thread. Otherwise, it runs synchronously in the calling thread.
async | If true, run refresh asynchronously in a new thread. |
|
protected |
List of parameter addresses to refresh.
|
protected |
Condition variable used to notify the worker thread.
|
protected |
Reference to the device instance used for uploading.
|
protected |
Time to wait between consecutive parameter uploads.
|
protected |
Mutex protecting access to shared data members.
|
protected |
Flag indicating whether the refresher thread is running.
|
protected |
The background thread performing periodic uploads.