Skip to content

Commit

Permalink
Merge pull request #464 from WildernessLabs/feature/ISamplingSensor
Browse files Browse the repository at this point in the history
Updates for ISamplingSensor
  • Loading branch information
adrianstevens authored Nov 15, 2022
2 parents e2c7cf1 + 3a41e5a commit 000e79b
Show file tree
Hide file tree
Showing 38 changed files with 208 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public override Task Initialize()
protected async Task ReadIlluminance()
{
var illuminance = await analogLightSensor.Read();
Console.WriteLine($"Initial lux: {illuminance.Lux:N2} lux");
Console.WriteLine($"Initial lux: {illuminance.Value.Lux:N2} lux");
}

//<!=SNOP=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public override Task Initialize()
protected async Task ReadTemp()
{
var temperature = await analogTemperature.Read();
Console.WriteLine($"Initial temp: {temperature.Celsius:N2}C");
Console.WriteLine($"Initial temp: {temperature.Value.Celsius:N2}C");
}

//<!=SNOP=>
Expand Down
6 changes: 3 additions & 3 deletions Source/Meadow.Foundation.Core/SamplingSensorBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.Collections.Generic;
using Meadow.Peripherals.Sensors;
using System;
using System.Threading;
using System.Threading.Tasks;

Expand All @@ -10,7 +10,7 @@ namespace Meadow.Foundation
/// </summary>
/// <typeparam name="UNIT"></typeparam>
public abstract class SamplingSensorBase<UNIT>
: SensorBase<UNIT>
: SensorBase<UNIT>, ISamplingSensor<UNIT>
where UNIT : struct
{
/// <summary>
Expand Down
1 change: 0 additions & 1 deletion Source/Meadow.Foundation.Core/SensorBase.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

Expand Down
17 changes: 9 additions & 8 deletions Source/Meadow.Foundation.Core/Sensors/Light/AnalogLightSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,22 @@ public AnalogLightSensor(IAnalogInputPort analogInputPort,
)
);
}


/// <summary>
/// Convenience method to get the current sensor readings. For frequent reads, use
/// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
/// </summary>
public async Task<Illuminance?> Read()
=> new Illuminance?(await ReadSensor());

/// <summary>
/// Convenience method to get the current luminance. For frequent reads, use
/// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
/// </summary>
protected override async Task<Illuminance> ReadSensor()
{
// read the voltage
Voltage voltage = await AnalogInputPort.Read();

// convert and save to our temp property for later retreival
illuminance = VoltageToLuminance(voltage);

// return
return illuminance;
return illuminance = VoltageToLuminance(voltage);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,13 @@ public AnalogTemperature(IAnalogInputPort analogInputPort,
);
}

/// <summary>
/// Convenience method to get the current sensor readings. For frequent reads, use
/// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
/// </summary>
public async Task<Units.Temperature?> Read()
=> new Units.Temperature?(await ReadSensor());

/// <summary>
/// Convenience method to get the current temperature. For frequent reads, use
/// StartSampling() and StopSampling() in conjunction with the SampleBuffer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,5 +135,8 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Pressure? Pressure,
return conditions;
});
}

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await ReadSensor()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -439,5 +439,14 @@ public override void StartUpdating(TimeSpan? updateInterval = null)

base.StartUpdating(updateInterval);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -582,5 +582,14 @@ byte CalculateHeaterDuration(TimeSpan duration)
}
return durationValue;
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,11 @@ private short ReadShort(byte address)

return (short)((ReadBuffer.Span[0] << 8) | ReadBuffer.Span[1]);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,11 @@ private short ReadShort(byte address)

return (short)((ReadBuffer.Span[0] << 8) | ReadBuffer.Span[1]);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,8 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Concentration? Co2,

base.RaiseEventsAndNotify(changeResult);
}

async Task<Concentration> ISamplingSensor<Concentration>.Read()
=> (await Read()).Voc.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T

return Task.FromResult(conditions);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T

return conditions;
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Meadow.Peripherals.Sensors;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace Meadow.Foundation.Sensors.Atmospheric
{
Expand All @@ -24,7 +25,7 @@ public enum Addresses : byte
/// Abstract HTDx1D base class for HTU21D and HTU31D
/// </summary>
public abstract class Htux1dBase :
ByteCommsSensorBase<(Meadow.Units.Temperature? Temperature, RelativeHumidity? Humidity)>,
ByteCommsSensorBase<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>,
ITemperatureSensor, IHumiditySensor
{
/// <summary>
Expand Down Expand Up @@ -84,5 +85,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T
}
base.RaiseEventsAndNotify(changeResult);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,5 +163,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T
return conditions;
});
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,5 +211,11 @@ public void Reset()
data |= 0x04;
Peripheral?.WriteRegister(Registers.Control1, data);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,11 @@ int ReadPressure()

return result;
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<Pressure> ISamplingSensor<Pressure>.Read()
=> (await Read()).Pressure.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,5 +84,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T

return Task.FromResult<(Units.Temperature? Temperature, RelativeHumidity? Humidity)>(conditions);
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,11 @@ protected int GetDelayForPrecision(Precision precision)
return conditions;
});
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -214,5 +214,11 @@ void SetResolution(SensorResolution resolution)
//Request a write to user register
Peripheral?.WriteRegister((byte)Register.USER_REG_1, register); //Write the new resolution bits
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,11 @@ protected override void RaiseEventsAndNotify(IChangeResult<(Units.Temperature? T
return conditions;
});
}

async Task<Units.Temperature> ISamplingSensor<Units.Temperature>.Read()
=> (await Read()).Temperature.Value;

async Task<RelativeHumidity> ISamplingSensor<RelativeHumidity>.Read()
=> (await Read()).Humidity.Value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Meadow.Foundation.Sensors.Distance
{
/// <summary>
/// HCSR04 Distance Sensor
/// HCSR04 Distance Sensor - driver not complete
/// </summary>
public class Hcsr04 : SensorBase<Length>, IRangeFinder
{
Expand Down Expand Up @@ -142,5 +142,16 @@ protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

public void StartUpdating(TimeSpan? updateInterval = null)
{
//ToDo
throw new NotImplementedException();
}

public void StopUpdating()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Meadow.Foundation.Sensors.Distance
{
/// <summary>
/// HYSRF05 Distance Sensor
/// HYSRF05 Distance Sensor- driver not complete
/// </summary>
public class Hysrf05: SensorBase<Length>, IRangeFinder
{
Expand Down Expand Up @@ -140,5 +140,16 @@ protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

public void StartUpdating(TimeSpan? updateInterval = null)
{
//ToDo
throw new NotImplementedException();
}

public void StopUpdating()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Meadow.Foundation.Sensors.Distance
{
/// <summary>
/// Sfsr02 Distance Sensor
/// Sfsr02 Distance Sensor- driver not complete
/// </summary>
public class Sfsr02: SensorBase<Length>, IRangeFinder
{
Expand Down Expand Up @@ -136,5 +136,16 @@ protected override void RaiseEventsAndNotify(IChangeResult<Length> changeResult)
DistanceUpdated?.Invoke(this, changeResult);
base.RaiseEventsAndNotify(changeResult);
}

public void StartUpdating(TimeSpan? updateInterval = null)
{
//ToDo
throw new NotImplementedException();
}

public void StopUpdating()
{
throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace Meadow.Foundation.Sensors.Light
/// Driver to measure solar panel input
/// </summary>
public class AnalogSolarGauge : SensorBase<float>,
ISolarIntensityGauge, ISensor
ISolarIntensityGauge
{
/// <summary>
/// Raised when the solar intensity changes
Expand Down
Loading

0 comments on commit 000e79b

Please sign in to comment.