AtlasScientificGravityDOMeter sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize...");
sensor = new AtlasScientificGravityDOMeter(Device.Pins.A01);
sensor.CalibrationInAir = new Voltage(0.04, Voltage.UnitType.Volts);
var consumer = AtlasScientificGravityDOMeter.CreateObserver(
handler: result =>
{
string oldValue = (result.Old is { } old) ? $"{old * 100:n1}" : "n/a";
string newValue = $"{result.New * 100:n1}";
Resolver.Log.Info($"New: {newValue}%, Old: {oldValue}%");
},
filter: null
);
sensor.Subscribe(consumer);
sensor.Updated += (sender, result) =>
{
string oldValue = (result.Old is { } old) ? $"{old * 100:n0}%" : "n/a";
Resolver.Log.Info($"Updated - New: {result.New * 100:n0}%, Old: {oldValue}");
};
return Task.CompletedTask;
}
public override async Task Run()
{
Resolver.Log.Info("Run...");
await ReadSensor();
var calibrationVoltage = await sensor.GetCurrentVoltage();
sensor.CalibrationInAir = calibrationVoltage;
Resolver.Log.Info($"Calibration voltage: {calibrationVoltage.Volts}V");
sensor.StartUpdating(TimeSpan.FromSeconds(2));
}
protected async Task ReadSensor()
{
var saturation = await sensor.Read();
Resolver.Log.Info($"Initial saturation: {saturation * 100:N1}%");
}