diff --git a/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs b/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs index 3a42b78f9..d98373a5b 100644 --- a/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs +++ b/Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs @@ -10,9 +10,7 @@ namespace Meadow.Foundation.Sensors; /// public class Potentiometer : IPotentiometer, IDisposable { - /// - public event EventHandler>? Changed; - + private EventHandler>? changedEvent; private IAnalogInputPort inputPort; private Voltage referenceVoltage; private bool portCreated = false; @@ -66,19 +64,40 @@ public Potentiometer(IPin inputPin, Resistance maxResistance, Voltage referenceV portCreated = true; } + /// + public event EventHandler>? Changed + { + add + { + if (changedEvent == null || changedEvent?.GetInvocationList().Length == 0) + { + inputPort.Updated += OnInputPortUpdated; + inputPort.StartUpdating(); + } + changedEvent += value; + } + remove + { + changedEvent -= value; + if (changedEvent?.GetInvocationList().Length == 0) + { + inputPort.StopUpdating(); + inputPort.Updated -= OnInputPortUpdated; + } + } + } + private void Initialize(IAnalogInputPort inputPort, Resistance maxResistance, Voltage refereceVoltage) { this.inputPort = inputPort; MaxResistance = maxResistance; referenceVoltage = inputPort.ReferenceVoltage; - - inputPort.Updated += OnInputPortUpdated; } private void OnInputPortUpdated(object sender, IChangeResult e) { var newValue = new Resistance(MaxResistance.Ohms * e.New.Volts / referenceVoltage.Volts); - Changed?.Invoke(this, new ChangeResult(newValue, oldValue)); + changedEvent?.Invoke(this, new ChangeResult(newValue, oldValue)); oldValue = newValue; } diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/Drivers/YfB10.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/Drivers/YfB10.cs index fc97fee67..eea073df6 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/Drivers/YfB10.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/Drivers/YfB10.cs @@ -7,8 +7,9 @@ namespace Meadow.Peripherals.Sensors.Flow; /// /// /// Configures the sensor with its factory calibration values: -/// - Scale factor: 7.5 Hz per L/min +/// - Scale factor: 8.0 Hz per L/min /// - Offset: 4 Hz +/// Note: Different data sheets differ on what the scale is on this device. Adjust the constructor parameters if required. /// public class YfB10 : HallEffectBase { @@ -16,8 +17,10 @@ public class YfB10 : HallEffectBase /// Initializes a new instance of the YF-B10 flow sensor. /// /// The digital input pin connected to the sensor's signal line. - public YfB10(IPin pin) - : base(pin, 7.5, 4) + /// The scale factor used to calculate flow. Defaults to 8.0 Hz per L/min. + /// The offset used to calculate flow. Defaults to 4hz + public YfB10(IPin pin, double scale = 8.0, double offset = 4) + : base(pin, scale, offset) { } } diff --git a/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/HallEffectBase.cs b/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/HallEffectBase.cs index 4d615bb98..839e356a3 100644 --- a/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/HallEffectBase.cs +++ b/Source/Meadow.Foundation.Peripherals/Sensors.Flow.HallEffect/Driver/HallEffectBase.cs @@ -62,6 +62,11 @@ private VolumetricFlow CalculateFlow(Frequency frequency, double scale, double o // First solve for Q (L/min): F = (S * Q - O) // F + O = S * Q // Q = (F + O) / S + if (frequency.Hertz == 0) + { + return VolumetricFlow.Zero; + } + double litersPerMinute = (frequency.Hertz + offset) / scale; return new VolumetricFlow(litersPerMinute, VolumetricFlow.UnitType.LitersPerMinute);