Motion Master
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
mm::comm::base::DeviceParameterRefresher Class Referenceabstract

Base class for periodically refreshing a set of device parameters in a background thread. More...

#include <base.h>

Inheritance diagram for mm::comm::base::DeviceParameterRefresher:
Inheritance graph
[legend]
Collaboration diagram for mm::comm::base::DeviceParameterRefresher:
Collaboration graph
[legend]

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::Addressaddresses_
 List of parameter addresses to refresh. More...
 
Devicedevice_
 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...
 

Detailed Description

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.

Note
This class is intended to be used as a base class. Do not instantiate it directly.

Constructor & Destructor Documentation

◆ DeviceParameterRefresher()

mm::comm::base::DeviceParameterRefresher::DeviceParameterRefresher ( Device device,
std::chrono::milliseconds  intervalBetweenUploads 
)

Constructor initializes the refresher with a device reference and upload interval.

Parameters
deviceReference to the device to upload parameters from.
intervalTime to wait between consecutive parameter uploads.

◆ ~DeviceParameterRefresher()

mm::comm::base::DeviceParameterRefresher::~DeviceParameterRefresher ( )

Destructor ensures the refresher thread is stopped cleanly.

Member Function Documentation

◆ add()

void mm::comm::base::DeviceParameterRefresher::add ( uint16_t  index,
uint8_t  subindex 
)

Add a parameter identified by (index, subindex) to the refresh list.

Parameters
indexThe parameter index (uint16_t).
subindexThe parameter subindex (uint8_t).

Thread-safe: locks mutex while modifying the container.

< Notify in case the thread is waiting on empty parameters.

◆ clear()

void mm::comm::base::DeviceParameterRefresher::clear ( )

Remove all parameters from the refresh list.

Thread-safe: locks mutex while clearing the container.

◆ refresh()

virtual void mm::comm::base::DeviceParameterRefresher::refresh ( const std::vector< Parameter::Address > &  addresses)
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.

Parameters
addressesA snapshot of the current parameter addresses (index and subindex pairs) to be uploaded.

Implemented in mm::comm::spoe::DeviceParameterRefresher.

◆ remove()

void mm::comm::base::DeviceParameterRefresher::remove ( uint16_t  index,
uint8_t  subindex 
)

Remove a parameter identified by (index, subindex) from the refresh list.

Parameters
indexThe parameter index.
subindexThe parameter subindex.

Thread-safe: locks mutex while modifying the container.

◆ start()

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.

◆ stop()

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.

◆ triggerManualRefresh()

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.

Parameters
asyncIf true, run refresh asynchronously in a new thread.

Member Data Documentation

◆ addresses_

std::vector<Parameter::Address> mm::comm::base::DeviceParameterRefresher::addresses_
protected

List of parameter addresses to refresh.

◆ cv_

std::condition_variable mm::comm::base::DeviceParameterRefresher::cv_
protected

Condition variable used to notify the worker thread.

◆ device_

Device& mm::comm::base::DeviceParameterRefresher::device_
protected

Reference to the device instance used for uploading.

◆ interval_

std::chrono::milliseconds mm::comm::base::DeviceParameterRefresher::interval_
protected

Time to wait between consecutive parameter uploads.

◆ mutex_

std::mutex mm::comm::base::DeviceParameterRefresher::mutex_
protected

Mutex protecting access to shared data members.

◆ running_

bool mm::comm::base::DeviceParameterRefresher::running_
protected

Flag indicating whether the refresher thread is running.

◆ worker_

std::thread mm::comm::base::DeviceParameterRefresher::worker_
protected

The background thread performing periodic uploads.


The documentation for this class was generated from the following files: