Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/pot #1109

Merged
merged 2 commits into from
Jan 5, 2025
Merged

Bug/pot #1109

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@
/// </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 All @@ -33,7 +31,7 @@
/// </summary>
/// <param name="inputPort">The input port to read the potentiometer value from.</param>
/// <param name="maxResistance">The maximum resistance value of the potentiometer.</param>
public Potentiometer(IAnalogInputPort inputPort, Resistance maxResistance)

Check warning on line 34 in Source/Meadow.Foundation.Core/Sensors/Potentiometer/Potentiometer.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'inputPort' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
{
Initialize(inputPort, maxResistance, inputPort.ReferenceVoltage);
portCreated = false;
Expand Down Expand Up @@ -66,19 +64,40 @@
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
Loading