Skip to content

Commit

Permalink
Merge pull request #1109 from WildernessLabs/bug/pot
Browse files Browse the repository at this point in the history
Bug/pot
  • Loading branch information
adrianstevens authored Jan 5, 2025
2 parents dd70a05 + 5e2e8d0 commit bb6a2a2
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ namespace Meadow.Foundation.Sensors;
/// </summary>
public class Potentiometer : IPotentiometer, IDisposable
{
/// <inheritdoc/>
public event EventHandler<IChangeResult<Resistance>>? Changed;

private EventHandler<IChangeResult<Resistance>>? changedEvent;
private IAnalogInputPort inputPort;
private Voltage referenceVoltage;
private bool portCreated = false;
Expand Down Expand Up @@ -66,19 +64,40 @@ public Potentiometer(IPin inputPin, Resistance maxResistance, Voltage referenceV
portCreated = true;
}

/// <inheritdoc/>
public event EventHandler<IChangeResult<Resistance>>? 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<Voltage> e)
{
var newValue = new Resistance(MaxResistance.Ohms * e.New.Volts / referenceVoltage.Volts);
Changed?.Invoke(this, new ChangeResult<Resistance>(newValue, oldValue));
changedEvent?.Invoke(this, new ChangeResult<Resistance>(newValue, oldValue));
oldValue = newValue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ namespace Meadow.Peripherals.Sensors.Flow;
/// </summary>
/// <remarks>
/// 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.
/// </remarks>
public class YfB10 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the YF-B10 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public YfB10(IPin pin)
: base(pin, 7.5, 4)
/// <param name="scale">The scale factor used to calculate flow. Defaults to 8.0 Hz per L/min.</param>
/// <param name="offset">The offset used to calculate flow. Defaults to 4hz</param>
public YfB10(IPin pin, double scale = 8.0, double offset = 4)
: base(pin, scale, offset)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bb6a2a2

Please sign in to comment.