using System; using System.Collections.Generic; using System.Linq; using System.Text; using Crestron.SimplSharp; using Crestron.SimplSharpPro; namespace PepperDash.Essentials.Core { /// /// A Feedback whose output is derived from the return value of a provided Func. /// public class BoolFeedback : Feedback { /// /// Returns the current value of the feedback, derived from the ValueFunc. The ValueFunc is /// evaluated whenever FireUpdate() is called /// public override bool BoolValue { get { return _BoolValue; } } bool _BoolValue; public override eCueType Type { get { return eCueType.Bool; } } /// /// Fake value to be used in test mode /// public bool TestValue { get; private set; } /// /// Func that evaluates on FireUpdate /// public Func ValueFunc { get; private set; } List LinkedInputSigs = new List(); List LinkedComplementInputSigs = new List(); public BoolFeedback(Func valueFunc) : this(null, valueFunc) { } public BoolFeedback(string key, Func valueFunc) : base(key) { ValueFunc = valueFunc; } //public BoolFeedback(Cue cue, Func valueFunc) // : base(cue) //{ // if (cue == null) throw new ArgumentNullException("cue"); // ValueFunc = valueFunc; //} public override void FireUpdate() { bool newValue = InTestMode ? TestValue : ValueFunc.Invoke(); if (newValue != _BoolValue) { _BoolValue = newValue; LinkedInputSigs.ForEach(s => UpdateSig(s)); LinkedComplementInputSigs.ForEach(s => UpdateComplementSig(s)); OnOutputChange(newValue); } } 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); } public override string ToString() { return (InTestMode ? "TEST -- " : "") + BoolValue.ToString(); } /// /// Puts this in test mode, sets the test value and fires an update. /// /// public void SetTestValue(bool value) { TestValue = value; InTestMode = true; FireUpdate(); } void UpdateSig(BoolInputSig sig) { sig.BoolValue = _BoolValue; } void UpdateComplementSig(BoolInputSig sig) { sig.BoolValue = !_BoolValue; } } }