mirror of
https://github.com/PepperDash/Essentials.git
synced 2026-02-05 07:45:05 +00:00
Combining repos
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
public class BoolFeedbackPulse
|
||||
{
|
||||
public uint TimeoutMs { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Defaults to false
|
||||
/// </summary>
|
||||
public bool CanRetrigger { get; set; }
|
||||
|
||||
public BoolFeedback Feedback { get; private set; }
|
||||
CTimer Timer;
|
||||
|
||||
bool _BoolValue;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a non-retriggering one shot
|
||||
/// </summary>
|
||||
public BoolFeedbackPulse(uint timeoutMs)
|
||||
: this(timeoutMs, false)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Create a retriggerable one shot by setting canRetrigger true
|
||||
/// </summary>
|
||||
public BoolFeedbackPulse(uint timeoutMs, bool canRetrigger)
|
||||
{
|
||||
TimeoutMs = timeoutMs;
|
||||
CanRetrigger = canRetrigger;
|
||||
Feedback = new BoolFeedback(() => _BoolValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Starts the
|
||||
/// </summary>
|
||||
/// <param name="timeout"></param>
|
||||
public void Start()
|
||||
{
|
||||
if (Timer == null)
|
||||
{
|
||||
_BoolValue = true;
|
||||
Feedback.FireUpdate();
|
||||
Timer = new CTimer(o =>
|
||||
{
|
||||
_BoolValue = false;
|
||||
Feedback.FireUpdate();
|
||||
Timer = null;
|
||||
}, TimeoutMs);
|
||||
}
|
||||
// Timer is running, if retrigger is set, reset it.
|
||||
else if (CanRetrigger)
|
||||
Timer.Reset(TimeoutMs);
|
||||
}
|
||||
|
||||
public void Cancel()
|
||||
{
|
||||
if(Timer != null)
|
||||
Timer.Reset(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
/// <summary>
|
||||
/// A class that wraps a BoolFeedback with logic that extends it's true state for
|
||||
/// a time period after the value goes false.
|
||||
/// </summary>
|
||||
public class BoolFeedbackPulseExtender
|
||||
{
|
||||
public uint TimeoutMs { get; set; }
|
||||
public BoolFeedback Feedback { get; private set; }
|
||||
CTimer Timer;
|
||||
|
||||
/// <summary>
|
||||
/// When set to true, will cause Feedback to go high, and cancel the timer.
|
||||
/// When false, will start the timer, and after timeout, will go low and
|
||||
/// feedback will go low.
|
||||
/// </summary>
|
||||
public bool BoolValue
|
||||
{
|
||||
get { return _BoolValue; }
|
||||
set
|
||||
{
|
||||
if (value)
|
||||
{ // if Timer is running and the value goes high, cancel it.
|
||||
if (Timer != null)
|
||||
{
|
||||
Timer.Stop();
|
||||
Timer = null;
|
||||
}
|
||||
// if it's already true, don't fire again
|
||||
if (_BoolValue == true)
|
||||
return;
|
||||
_BoolValue = true;
|
||||
Feedback.FireUpdate();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Timer == null)
|
||||
Timer = new CTimer(o => ClearFeedback(), TimeoutMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
bool _BoolValue;
|
||||
|
||||
/// <summary>
|
||||
/// Constructor
|
||||
/// </summary>
|
||||
/// <param name="timeoutMs">The time which the true state will be extended after set to false</param>
|
||||
public BoolFeedbackPulseExtender(uint timeoutMs)
|
||||
{
|
||||
TimeoutMs = timeoutMs;
|
||||
Feedback = new BoolFeedback(() => this.BoolValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Forces the feedback to false regardless of timeout
|
||||
/// </summary>
|
||||
public void ClearNow()
|
||||
{
|
||||
if (Timer != null)
|
||||
Timer.Stop();
|
||||
ClearFeedback();
|
||||
}
|
||||
|
||||
void ClearFeedback()
|
||||
{
|
||||
_BoolValue = false;
|
||||
Feedback.FireUpdate();
|
||||
Timer = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
|
||||
|
||||
public abstract class BoolFeedbackLogic
|
||||
{
|
||||
/// <summary>
|
||||
/// Output representing the "and" value of all connected inputs
|
||||
/// </summary>
|
||||
public BoolFeedback Output { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// List of all connected outputs
|
||||
/// </summary>
|
||||
protected List<BoolFeedback> OutputsIn = new List<BoolFeedback>();
|
||||
|
||||
protected bool ComputedValue;
|
||||
|
||||
public BoolFeedbackLogic()
|
||||
{
|
||||
Output = new BoolFeedback(() => ComputedValue);
|
||||
}
|
||||
|
||||
public void AddOutputIn(BoolFeedback output)
|
||||
{
|
||||
// Don't double up outputs
|
||||
if(OutputsIn.Contains(output)) return;
|
||||
|
||||
OutputsIn.Add(output);
|
||||
output.OutputChange += AnyInput_OutputChange;
|
||||
Evaluate();
|
||||
}
|
||||
|
||||
public void AddOutputsIn(List<BoolFeedback> outputs)
|
||||
{
|
||||
foreach (var o in outputs)
|
||||
{
|
||||
// skip existing
|
||||
if (OutputsIn.Contains(o)) continue;
|
||||
|
||||
OutputsIn.Add(o);
|
||||
o.OutputChange += AnyInput_OutputChange;
|
||||
}
|
||||
Evaluate();
|
||||
}
|
||||
|
||||
public void RemoveOutputIn(BoolFeedback output)
|
||||
{
|
||||
// Don't double up outputs
|
||||
if (OutputsIn.Contains(output)) return;
|
||||
|
||||
OutputsIn.Remove(output);
|
||||
output.OutputChange -= AnyInput_OutputChange;
|
||||
Evaluate();
|
||||
}
|
||||
|
||||
public void RemoveOutputsIn(List<BoolFeedback> outputs)
|
||||
{
|
||||
foreach (var o in outputs)
|
||||
{
|
||||
OutputsIn.Remove(o);
|
||||
o.OutputChange -= AnyInput_OutputChange;
|
||||
}
|
||||
Evaluate();
|
||||
}
|
||||
|
||||
void AnyInput_OutputChange(object sender, EventArgs e)
|
||||
{
|
||||
Evaluate();
|
||||
}
|
||||
|
||||
protected abstract void Evaluate();
|
||||
}
|
||||
|
||||
public class BoolFeedbackAnd : BoolFeedbackLogic
|
||||
{
|
||||
protected override void Evaluate()
|
||||
{
|
||||
var prevValue = ComputedValue;
|
||||
var newValue = OutputsIn.All(o => o.BoolValue);
|
||||
if (newValue != prevValue)
|
||||
{
|
||||
ComputedValue = newValue;
|
||||
Output.FireUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BoolFeedbackOr : BoolFeedbackLogic
|
||||
{
|
||||
protected override void Evaluate()
|
||||
{
|
||||
var prevValue = ComputedValue;
|
||||
var newValue = OutputsIn.Any(o => o.BoolValue);
|
||||
if (newValue != prevValue)
|
||||
{
|
||||
ComputedValue = newValue;
|
||||
Output.FireUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public class BoolFeedbackLinq : BoolFeedbackLogic
|
||||
{
|
||||
Func<IEnumerable<BoolFeedback>, bool> Predicate;
|
||||
|
||||
public BoolFeedbackLinq(Func<IEnumerable<BoolFeedback>, bool> predicate)
|
||||
: base()
|
||||
{
|
||||
Predicate = predicate;
|
||||
}
|
||||
|
||||
protected override void Evaluate()
|
||||
{
|
||||
var prevValue = ComputedValue;
|
||||
var newValue = Predicate(OutputsIn);
|
||||
if (newValue != prevValue)
|
||||
{
|
||||
ComputedValue = newValue;
|
||||
Output.FireUpdate();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
220
Essentials Core/PepperDashEssentialsBase/Feedbacks/Feedbacks.cs
Normal file
220
Essentials Core/PepperDashEssentialsBase/Feedbacks/Feedbacks.cs
Normal file
@@ -0,0 +1,220 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using Crestron.SimplSharp;
|
||||
using Crestron.SimplSharpPro;
|
||||
|
||||
namespace PepperDash.Essentials.Core
|
||||
{
|
||||
public abstract class Feedback
|
||||
{
|
||||
public event EventHandler<EventArgs> OutputChange;
|
||||
|
||||
public virtual bool BoolValue { get { return false; } }
|
||||
public virtual int IntValue { get { return 0; } }
|
||||
public virtual string StringValue { get { return ""; } }
|
||||
|
||||
public Cue Cue { get; private set; }
|
||||
|
||||
public abstract eCueType Type { get; }
|
||||
|
||||
protected Feedback()
|
||||
{
|
||||
}
|
||||
|
||||
protected Feedback(Cue cue)
|
||||
{
|
||||
Cue = cue;
|
||||
}
|
||||
|
||||
public abstract void FireUpdate();
|
||||
|
||||
protected void OnOutputChange()
|
||||
{
|
||||
if (OutputChange != null) OutputChange(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A Feedback whose output is derived from the return value of a provided Func.
|
||||
/// </summary>
|
||||
public class BoolFeedback : Feedback
|
||||
{
|
||||
/// <summary>
|
||||
/// Returns the current value of the feedback, derived from the ValueFunc
|
||||
/// </summary>
|
||||
public override bool BoolValue { get { return _BoolValue; } }
|
||||
bool _BoolValue;
|
||||
|
||||
public override eCueType Type { get { return eCueType.Bool; } }
|
||||
|
||||
public Func<bool> ValueFunc { get; private set; }
|
||||
/// <summary>
|
||||
/// The last value delivered on FireUpdate
|
||||
/// </summary>
|
||||
//public bool PreviousValue { get; private set; }
|
||||
List<BoolInputSig> LinkedInputSigs = new List<BoolInputSig>();
|
||||
List<BoolInputSig> LinkedComplementInputSigs = new List<BoolInputSig>();
|
||||
|
||||
public BoolFeedback(Func<bool> valueFunc)
|
||||
: this(Cue.DefaultBoolCue, valueFunc)
|
||||
{
|
||||
}
|
||||
|
||||
public BoolFeedback(Cue cue, Func<bool> valueFunc)
|
||||
: base(cue)
|
||||
{
|
||||
if (cue == null) throw new ArgumentNullException("cue");
|
||||
ValueFunc = valueFunc;
|
||||
}
|
||||
|
||||
public override void FireUpdate()
|
||||
{
|
||||
var newValue = ValueFunc.Invoke();
|
||||
if (newValue != _BoolValue)
|
||||
{
|
||||
_BoolValue = newValue;
|
||||
LinkedInputSigs.ForEach(s => UpdateSig(s));
|
||||
LinkedComplementInputSigs.ForEach(s => UpdateComplementSig(s));
|
||||
OnOutputChange();
|
||||
}
|
||||
}
|
||||
|
||||
public void LinkInputSig(BoolInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Add(sig);
|
||||
UpdateSig(sig);
|
||||
}
|
||||
|
||||
public void UnlinkInputSig(BoolInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Remove(sig);
|
||||
}
|
||||
|
||||
public void LinkComplementInputSig(BoolInputSig sig)
|
||||
{
|
||||
LinkedComplementInputSigs.Add(sig);
|
||||
UpdateComplementSig(sig);
|
||||
}
|
||||
|
||||
public void UnlinkComplementInputSig(BoolInputSig sig)
|
||||
{
|
||||
LinkedComplementInputSigs.Remove(sig);
|
||||
}
|
||||
|
||||
void UpdateSig(BoolInputSig sig)
|
||||
{
|
||||
sig.BoolValue = _BoolValue;
|
||||
}
|
||||
|
||||
void UpdateComplementSig(BoolInputSig sig)
|
||||
{
|
||||
sig.BoolValue = !_BoolValue;
|
||||
}
|
||||
}
|
||||
|
||||
//******************************************************************************
|
||||
public class IntFeedback : Feedback
|
||||
{
|
||||
public override int IntValue { get { return _IntValue; } } // ValueFunc.Invoke(); } }
|
||||
int _IntValue;
|
||||
public ushort UShortValue { get { return (ushort)_IntValue; } }
|
||||
public override eCueType Type { get { return eCueType.Int; } }
|
||||
//public int PreviousValue { get; private set; }
|
||||
|
||||
Func<int> ValueFunc;
|
||||
List<UShortInputSig> LinkedInputSigs = new List<UShortInputSig>();
|
||||
|
||||
public IntFeedback(Func<int> valueFunc)
|
||||
: this(Cue.DefaultIntCue, valueFunc)
|
||||
{
|
||||
}
|
||||
|
||||
public IntFeedback(Cue cue, Func<int> valueFunc)
|
||||
: base(cue)
|
||||
{
|
||||
if (cue == null) throw new ArgumentNullException("cue");
|
||||
ValueFunc = valueFunc;
|
||||
}
|
||||
|
||||
public override void FireUpdate()
|
||||
{
|
||||
var newValue = ValueFunc.Invoke();
|
||||
if (newValue != _IntValue)
|
||||
{
|
||||
_IntValue = newValue;
|
||||
LinkedInputSigs.ForEach(s => UpdateSig(s));
|
||||
OnOutputChange();
|
||||
}
|
||||
}
|
||||
|
||||
public void LinkInputSig(UShortInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Add(sig);
|
||||
UpdateSig(sig);
|
||||
}
|
||||
|
||||
public void UnlinkInputSig(UShortInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Remove(sig);
|
||||
}
|
||||
|
||||
void UpdateSig(UShortInputSig sig)
|
||||
{
|
||||
sig.UShortValue = UShortValue;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//******************************************************************************
|
||||
public class StringFeedback : Feedback
|
||||
{
|
||||
public override string StringValue { get { return _StringValue; } } // ValueFunc.Invoke(); } }
|
||||
string _StringValue;
|
||||
public override eCueType Type { get { return eCueType.String; } }
|
||||
//public string PreviousValue { get; private set; }
|
||||
public Func<string> ValueFunc { get; private set; }
|
||||
List<StringInputSig> LinkedInputSigs = new List<StringInputSig>();
|
||||
|
||||
public StringFeedback(Func<string> valueFunc)
|
||||
: this(Cue.DefaultStringCue, valueFunc)
|
||||
{
|
||||
}
|
||||
|
||||
public StringFeedback(Cue cue, Func<string> valueFunc)
|
||||
: base(cue)
|
||||
{
|
||||
if (cue == null) throw new ArgumentNullException("cue");
|
||||
ValueFunc = valueFunc;
|
||||
|
||||
}
|
||||
|
||||
public override void FireUpdate()
|
||||
{
|
||||
var newValue = ValueFunc.Invoke();
|
||||
if (newValue != _StringValue)
|
||||
{
|
||||
_StringValue = newValue;
|
||||
LinkedInputSigs.ForEach(s => UpdateSig(s));
|
||||
OnOutputChange();
|
||||
}
|
||||
}
|
||||
|
||||
public void LinkInputSig(StringInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Add(sig);
|
||||
UpdateSig(sig);
|
||||
}
|
||||
|
||||
public void UnlinkInputSig(StringInputSig sig)
|
||||
{
|
||||
LinkedInputSigs.Remove(sig);
|
||||
}
|
||||
|
||||
void UpdateSig(StringInputSig sig)
|
||||
{
|
||||
sig.StringValue = _StringValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user