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

flow sensors #1105

Merged
merged 7 commits into from
Jan 4, 2025
Merged
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
@@ -0,0 +1,34 @@
using Meadow.Hardware;

namespace Meadow.Foundation.Sensors;

public class SimulatedDigitalOutputPort : IDigitalOutputPort
{
private bool state;

public bool InitialState { get; }
public string Name { get; }

public SimulatedDigitalOutputPort(string name, bool initialState)
{
Name = name;
InitialState = state = initialState;
}

public virtual bool State
{
get => state;
set
{
state = value;
Resolver.Log.Info($"Output {Name} = {State}");
}
}

public IDigitalChannelInfo? Channel => null;

Check warning on line 28 in Source/Meadow.Foundation.Core/Simulation/Ports/SimulatedDigitalOutputPort.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in return type of 'IDigitalChannelInfo? SimulatedDigitalOutputPort.Channel.get' doesn't match implicitly implemented member 'IDigitalChannelInfo IPort<IDigitalChannelInfo>.Channel.get' (possibly because of nullability attributes).
public IPin? Pin => null;

Check warning on line 29 in Source/Meadow.Foundation.Core/Simulation/Ports/SimulatedDigitalOutputPort.cs

View workflow job for this annotation

GitHub Actions / build

Nullability of reference types in return type of 'IPin? SimulatedDigitalOutputPort.Pin.get' doesn't match implicitly implemented member 'IPin IPort<IDigitalChannelInfo>.Pin.get' (possibly because of nullability attributes).

public void Dispose()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Meadow.Hardware;
using Meadow.Units;

namespace Meadow.Foundation.Sensors;

public class SimulatedDigitalSignalAnalyzer : IDigitalSignalAnalyzer
{
private Frequency frequency;
private double dutyCycle = 0.5;
private ulong count;

public SimulatedDigitalSignalAnalyzer(Frequency frequency)
{
this.frequency = frequency;
}

public void SetDutyCycle(double dutyCycle)
{
this.dutyCycle = dutyCycle;
}

public double GetDutyCycle()
{
return dutyCycle;
}

public void SetFrequency(Frequency frequency)
{
this.frequency = frequency;
}

public Frequency GetFrequency()
{
return frequency;
}

public Frequency GetMeanFrequency()
{
return frequency;
}

public void SetCount(ulong count)
{
this.count = count;
}

public ulong GetCount()
{
return count;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,6 @@

namespace Meadow.Foundation.Sensors;

/*
/// <summary>
/// Represents a simulated tone generator that implements both IToneGenerator and ISimulatedSensor interfaces.
/// </summary>
public class SimulatedToneGenerator : IToneGenerator, ISimulatedSensor
{
}

/// <summary>
/// Represents a simulated RgbPwmLed that implements both IRgbPwmLed and ISimulatedSensor interfaces.
/// </summary>
public class SimulatedRgbPwmLed : IRgbPwmLed, ISimulatedSensor
{
}

/// <summary>
/// Represents a simulated barometric pressure sensor that implements both IBarometricPressureSensor and ISimulatedSensor interfaces.
/// </summary>
public class SimulatedBarometricPressureSensor : IBarometricPressureSensor, ISimulatedSensor
{
}

/// <summary>
/// Represents a simulated gas resistance sensor that implements both IGasResistanceSensor and ISimulatedSensor interfaces.
/// </summary>
public class SimulatedGasResistanceSensor : IGasResistanceSensor, ISimulatedSensor
{
}
*/

/// <summary>
/// A base class for simple simulated single-unit sensors
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using Meadow.Peripherals.Sensors;
using Meadow.Units;
using System.Threading.Tasks;

namespace Meadow.Foundation.Sensors;

public class SimulatedHallEffectFlowSensor : IVolumetricFlowSensor
{
private SimulatedDigitalSignalAnalyzer _analyzer;
private double _flowCoefficient;

public SimulatedHallEffectFlowSensor(Frequency simulatedPulseFrequency, double flowCoefficient = 80d)
{
_flowCoefficient = flowCoefficient;
_analyzer = new SimulatedDigitalSignalAnalyzer(simulatedPulseFrequency);
}

public void SetSignalFrequency(Frequency frequency)
{
_analyzer.SetFrequency(frequency);
}

public VolumetricFlow Flow => new VolumetricFlow(
_analyzer.GetFrequency().Hertz / _flowCoefficient,
VolumetricFlow.UnitType.LitersPerMinute);

public Task<VolumetricFlow> Read()
{
return Task.FromResult(Flow);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ public class ListBox : MicroLayout
/// Spacing, in pixels, between items
/// </summary>
public int ItemSpacing { get; } = 1;

/// <summary>
/// Spacing, in pixels, between the control left and the left of each item
/// </summary>
public int ItemLeftMargin { get; set; } = 2;

/// <summary>
/// Items to display in the ListBox
/// </summary>
Expand All @@ -53,11 +59,11 @@ public ListBox(int left, int top, int width, int height, IFont font)

private void CreateRowLabels(int rowCount)
{
var y = 0;
var y = 2;
for (var i = 0; i < rowCount; i++)
{
Controls.Add(
new Label(Left, Top + y, this.Width, _rowHeight)
new Label(ItemLeftMargin, y, this.Width, _rowHeight)
{
Font = _font,
TextColor = TextColor,
Expand Down Expand Up @@ -182,6 +188,7 @@ private void OnItemsCollectionChanged(object sender, NotifyCollectionChangedEven
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
case NotifyCollectionChangedAction.Replace:
// is the added item visible?
if (e.NewStartingIndex < TopIndex + Controls.Count)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -643,5 +643,11 @@ private PortBank GetPortBankForPin(IPin pin)
}
return PortBank.A;
}

/// <inheritdoc/>
public IDigitalSignalAnalyzer CreateDigitalSignalAnalyzer(IPin pin, bool captureDutyCycle)
{
return new SoftDigitalSignalAnalyzer(pin, captureDutyCycle: captureDutyCycle);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ public void AllOn()
/// <param name="state"><b>True</b> to set the pin state high, <b>False</b> to set it low</param>
protected abstract void SetPinState(IPin pin, bool state);

void WriteUint16(ushort value)
private void WriteUint16(ushort value)
{
Span<byte> buffer = stackalloc byte[2];
buffer[0] = (byte)value;
Expand All @@ -313,6 +313,12 @@ private void InterruptPortChanged(object sender, DigitalPortResult e)
}
}

/// <inheritdoc/>
public IDigitalSignalAnalyzer CreateDigitalSignalAnalyzer(IPin pin, bool captureDutyCycle)
{
return new SoftDigitalSignalAnalyzer(pin, captureDutyCycle: captureDutyCycle);
}

/// <summary>
/// Dispose of the object
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the GR-105 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 5.5 Hz per L/min
/// </remarks>
public class Gr105 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the GR-105 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public Gr105(IPin pin)
: base(pin, 5.5d)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the GR-201 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 7.5 Hz per L/min
/// </remarks>
public class Gr201 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the GR-201 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public Gr201(IPin pin)
: base(pin, 7.5d)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the GR-216 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 0.2 Hz per L/min
/// </remarks>
public class Gr216 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the GR-216 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public Gr216(IPin pin)
: base(pin, 0.2d)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the YF-B1 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 11.0 Hz per L/min
/// - Offset: 0 Hz
/// </remarks>
public class YfB1 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the YF-B9 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public YfB1(IPin pin)
: base(pin, 11.0)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the YF-B10 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 7.5 Hz per L/min
/// - Offset: 4 Hz
/// </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)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the YF-B1 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 11.0 Hz per L/min
/// - Offset: 0 Hz
/// </remarks>
public class YfB2 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the YF-B9 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public YfB2(IPin pin)
: base(pin, 11.0)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Meadow.Hardware;

namespace Meadow.Peripherals.Sensors.Flow;

/// <summary>
/// Driver for the YF-B3 Hall effect water flow sensor.
/// </summary>
/// <remarks>
/// Configures the sensor with its factory calibration values:
/// - Scale factor: 11.0 Hz per L/min
/// - Offset: 0 Hz
/// </remarks>
public class YfB3 : HallEffectBase
{
/// <summary>
/// Initializes a new instance of the YF-B9 flow sensor.
/// </summary>
/// <param name="pin">The digital input pin connected to the sensor's signal line.</param>
public YfB3(IPin pin)
: base(pin, 11.0)
{
}
}
Loading
Loading