boost::signals2::signal - is a "decimated" version available?

Hi, I'm using boost::signals2::signal to notify a dialog when the state of an underlying object has changed. The signal is sometimes is generated at 100Hz. Other times it is generated at 1Hz. The dialog provides a "slot" for this signal that updates the controls on the screen. I'd like to limit the "slot rate" to 10Hz. By "slot rate" I mean the rate at which the slot receives notification from the signal. This will prevent the screen updates from happening faster than can be seen. Has this been done already? If not, I'm going to implement something like the pseudocode below. Currently I'm not sure how I to specify "arguments" nor how I am going to specify the function "SlotCallback". Thank you, Chris === Pseudocode: float MaxPeriod = 0.001; // 10Hz class SignalDecimator { public: void Register(signal Signal, slot Slot) { Signal.connect(&SignalDecimator::SlotCallback, this, _1, _2, _3, _4...); mSlot = Slot; } private: void SlotCallback(arguments Arguments) { if (TimeSinceLastCall() > MaxPeriod) { mSlot(Arguments); } else { mArguments = Arguments; if (!TimerIsRunning()) { StartTimer(MaxPeriod - TimeSinceLastCall()); } } } void TimerCallback() { mSlot(mArguments); } slot mSlot; timer mTimer; arguments mArguments; }

The signal is sometimes is generated at 100Hz. Other times it is generated at 1Hz. The dialog provides a "slot" for this signal that updates the controls on the screen. I'd like to limit the "slot rate" to 10Hz. By "slot rate" I mean the rate at which the slot receives notification from the signal. This will prevent the screen updates from happening faster than can be seen.
Has this been done already? If not, I'm going to implement something like the pseudocode below. Currently I'm not sure how I to specify "arguments" nor how I am going to specify the function "SlotCallback". <...> Pseudocode:
float MaxPeriod = 0.001; // 10Hz
class SignalDecimator {
public:
void Register(signal Signal, slot Slot) { Signal.connect(&SignalDecimator::SlotCallback, this, _1, _2, _3, _4...);
mSlot = Slot; }
private:
void SlotCallback(arguments Arguments) { if (TimeSinceLastCall() > MaxPeriod) { mSlot(Arguments); } else { mArguments = Arguments;
if (!TimerIsRunning()) { StartTimer(MaxPeriod - TimeSinceLastCall()); } } }
Do you really need to store arguments and re-signal with timer? If all you need is just to ignore too frequent signals, you can do this in a much simpler way: void SlotCallback(arguments Arguments) { auto now = boost::posix_time::microsec_clock::universal_time(); if (now - lastNotified > boost::posix_time::millisec(100)) mSlot(Arguments); else lastNotified = now; }

On Monday, May 21, 2012, Igor R wrote:
Do you really need to store arguments and re-signal with timer? If all you need is just to ignore too frequent signals, you can do this in a much simpler way:
Igor, Thank you for your reply. Consider the case where two signals are generated 1 ms apart. With your implementation, the observer will end up with the arguments from the first signal. With my more complicated implementation, the observer will end up with the most recent signal arguments. Chris

Le 22/05/12 02:01, Chris Stankevitz a écrit :
Hi,
I'm using boost::signals2::signal to notify a dialog when the state of an underlying object has changed.
The signal is sometimes is generated at 100Hz. Other times it is generated at 1Hz. The dialog provides a "slot" for this signal that updates the controls on the screen. I'd like to limit the "slot rate" to 10Hz. By "slot rate" I mean the rate at which the slot receives notification from the signal. This will prevent the screen updates from happening faster than can be seen.
Has this been done already? If not, I'm going to implement something like the pseudocode below. Currently I'm not sure how I to specify "arguments" nor how I am going to specify the function "SlotCallback".
Hi, I don't think this exist in Boost. I would define instead a functor that takes care of the timing constraint. This functor could be use in other contexts, as Boost.Signal, or whatever library requesting callbacks. Best, Vicente
participants (3)
-
Chris Stankevitz
-
Igor R
-
Vicente J. Botet Escriba