using System.Timers;
namespace PepperDash.Essentials.Core
{
///
/// Represents a BoolFeedbackPulse
///
public class BoolFeedbackPulse
{
///
/// Gets or sets the TimeoutMs
///
public uint TimeoutMs { get; set; }
///
/// Gets or sets the CanRetrigger
///
public bool CanRetrigger { get; set; }
///
/// Gets or sets the Feedback
///
public BoolFeedback Feedback { get; private set; }
Timer Timer;
bool _BoolValue;
///
/// Creates a non-retriggering one shot
///
public BoolFeedbackPulse(uint timeoutMs)
: this(timeoutMs, false)
{
}
///
/// Create a retriggerable one shot by setting canRetrigger true
///
public BoolFeedbackPulse(uint timeoutMs, bool canRetrigger)
{
TimeoutMs = timeoutMs;
CanRetrigger = canRetrigger;
Feedback = new BoolFeedback(() => _BoolValue);
}
///
/// Start method
///
public void Start()
{
if (Timer == null)
{
_BoolValue = true;
Feedback.FireUpdate();
Timer = new Timer(TimeoutMs) { AutoReset = false };
Timer.Elapsed += (s, e) =>
{
_BoolValue = false;
Feedback.FireUpdate();
Timer = null;
};
Timer.Start();
}
// Timer is running, if retrigger is set, reset it.
else if (CanRetrigger)
{
Timer.Stop();
Timer.Interval = TimeoutMs;
Timer.Start();
}
}
///
/// Cancel method
///
public void Cancel()
{
if (Timer != null)
{
Timer.Stop();
Timer.Interval = 1;
Timer.Start();
}
}
}
}