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

refactored Y4000 driver #1084

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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,15 @@
using Meadow.Peripherals.Sensors;
using Meadow.Units;

namespace Meadow.Foundation.Sensors.Environmental;

/// <summary>
/// Represents a sensor for measuring oxidation/reduction potential
/// </summary>
public interface IRedoxPotentialSensor : ISamplingSensor<Voltage>
{
/// <summary>
/// Last value read from the sensor
/// </summary>
Voltage? Potential { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Meadow.Peripherals.Sensors;

namespace Meadow.Foundation.Sensors.Environmental;

/// <summary>
/// Represents a sensor for measuring water quality concentrations
/// </summary>
public interface IWaterQualityConcentrationsSensor : ISamplingSensor<WaterQualityConcentrations>
{
/// <summary>
/// Last value read from the sensor
/// </summary>
WaterQualityConcentrations? Concentrations { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Meadow.Units;

namespace Meadow.Foundation.Sensors.Environmental;

/// <summary>
/// Represents concentrations indicating water quality
/// </summary>
public struct WaterQualityConcentrations
{
/// <summary>
/// Concentration of dissolved Oxygen in water
/// </summary>
public ConcentrationInWater? DissolvedOxygen;
/// <summary>
/// Chlorophyll Concentration (CHL)
/// </summary>
public ConcentrationInWater? Chlorophyl;
/// <summary>
/// Salination (SAL)
/// </summary>
public ConcentrationInWater? BlueGreenAlgae;
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public partial class Y4000
//check for infinity and switch to zero
//populate all of the properties

enum Measurement
private enum Measurement
{
DissolvedOxygen, //DO
Turbidity,
Expand All @@ -27,11 +27,6 @@ enum Measurement
/// </summary>
public struct Measurements
{
/// <summary>
/// Concentration of dissolved Oxygen in water
/// </summary>
public ConcentrationInWater DissolvedOxygen { get; private set; }

/// <summary>
/// Turbidity (NTU)
/// </summary>
Expand All @@ -54,14 +49,9 @@ public struct Measurements
public Voltage OxidationReductionPotential { get; private set; }

/// <summary>
/// Chlorophyll Concentration (CHL)
/// </summary>
public ConcentrationInWater Chlorophyl { get; private set; }

/// <summary>
/// Salination (SAL)
/// Water quality concentraions
/// </summary>
public ConcentrationInWater BlueGreenAlgae { get; private set; }
public WaterQualityConcentrations Concentrations { get; private set; }

/// <summary>
/// Temperature
Expand All @@ -79,8 +69,8 @@ public Measurements(float[] data)
throw new ArgumentException($"Measurements record expects 8 values, received {data.Length}");
}

float value = Normalize(data[(int)Measurement.DissolvedOxygen]);
DissolvedOxygen = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MilligramsPerLiter);
var value = Normalize(data[(int)Measurement.Temperature]);
Temperature = new Units.Temperature(value, Units.Temperature.UnitType.Celsius);

value = Normalize(data[(int)Measurement.Turbidity]);
Turbidity = new Turbidity(value);
Expand All @@ -95,16 +85,23 @@ public Measurements(float[] data)
OxidationReductionPotential = new Voltage(value, Voltage.UnitType.Volts);

value = Normalize(data[(int)Measurement.Chlorophyl]);
Chlorophyl = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MicrogramsPerLiter);
var chlorophyl = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MicrogramsPerLiter);

value = Normalize(data[(int)Measurement.BlueGreenAlgae]);
BlueGreenAlgae = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MilligramsPerLiter);
var blueGreenAlgae = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MilligramsPerLiter);

value = Normalize(data[(int)Measurement.Temperature]);
Temperature = new Units.Temperature(value, Units.Temperature.UnitType.Celsius);
value = Normalize(data[(int)Measurement.DissolvedOxygen]);
var dissolvedOxygen = new ConcentrationInWater(value, ConcentrationInWater.UnitType.MilligramsPerLiter);

Concentrations = new WaterQualityConcentrations
{
Chlorophyl = chlorophyl,
BlueGreenAlgae = blueGreenAlgae,
DissolvedOxygen = dissolvedOxygen
};
}

static float Normalize(float value) => float.IsNormal(value) ? value : 0;
private static float Normalize(float value) => float.IsNormal(value) ? value : 0;

/// <summary>
/// Returns a string that represents the current Y4000 measurement data.
Expand All @@ -113,13 +110,13 @@ public Measurements(float[] data)
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.AppendLine($"DissolvedOxygen: {DissolvedOxygen} mg/L");
sb.AppendLine($"DissolvedOxygen: {Concentrations.DissolvedOxygen} mg/L");
sb.AppendLine($"Turbidity: {Turbidity} NTU");
sb.AppendLine($"ElectricalConductivity: {ElectricalConductivity.MilliSiemensPerCentimeter} mS/cm");
sb.AppendLine($"PH: {PH}");
sb.AppendLine($"OxidationReductionPotential: {OxidationReductionPotential.Millivolts} mV");
sb.AppendLine($"Chlorophyll: {Chlorophyl.MicrogramsPerLiter} ug/L");
sb.AppendLine($"BlueGreenAlgae: {BlueGreenAlgae.PartsPerMillion} ppm");
sb.AppendLine($"Chlorophyll: {Concentrations.Chlorophyl.Value.MicrogramsPerLiter} ug/L");
sb.AppendLine($"BlueGreenAlgae: {Concentrations.BlueGreenAlgae.Value.PartsPerMillion} ppm");
sb.AppendLine($"Temperature: {Temperature.Celsius} C");

return sb.ToString();
Expand Down
Loading
Loading