Skip to content

Commit

Permalink
Merge pull request #267 from WildernessLabs/feature/analog-units
Browse files Browse the repository at this point in the history
Analog change effects
  • Loading branch information
ctacke authored Jan 25, 2022
2 parents 00a672e + 33ce743 commit bb8d592
Show file tree
Hide file tree
Showing 17 changed files with 111 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Meadow.Devices;
using Meadow.Foundation.Sensors.Hid;
using Meadow.Peripherals.Sensors.Hid;
using Meadow.Units;

namespace MeadowApp
{
Expand Down Expand Up @@ -33,8 +34,8 @@ void Initialize()
// these are pretty fast updates (40ms in total), if you need more time to process, you can
// increase the sample interval duration and/or standby duration.
joystick = new AnalogJoystick(
Device.CreateAnalogInputPort(Device.Pins.A01, 1, 10),
Device.CreateAnalogInputPort(Device.Pins.A00, 1, 10),
Device.CreateAnalogInputPort(Device.Pins.A01, 1, TimeSpan.FromMilliseconds(10), new Voltage(3.3)),
Device.CreateAnalogInputPort(Device.Pins.A00, 1, TimeSpan.FromMilliseconds(10), new Voltage(3.3)),
null, false);

Console.WriteLine("Hardware initialization complete.");
Expand Down
5 changes: 5 additions & 0 deletions Source/Meadow.Foundation.Core/ByteCommsSensorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ protected ByteCommsSensorBase(
Init(readBufferSize, writeBufferSize);
}

/// <summary>
/// ByteCommsSensorBase abstract ctor with no bus
/// </summary>
/// <param name="readBufferSize">Read buffer size</param>
/// <param name="writeBufferSize">Write buffer size</param>
protected ByteCommsSensorBase(
int readBufferSize = 8, int writeBufferSize = 8)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ public AnalogWaterLevel(
IAnalogInputController device,
IPin analogPin,
Calibration? calibration = null,
int updateIntervalMs = 1000)
: this(device.CreateAnalogInputPort(analogPin), calibration)
TimeSpan? updateInterval = null)
: this(device.CreateAnalogInputPort(analogPin, 5, TimeSpan.FromMilliseconds(40), new Voltage(3.3, Voltage.UnitType.Volts)), calibration)
{
base.UpdateInterval = TimeSpan.FromMilliseconds(updateIntervalMs);
base.UpdateInterval = updateInterval ?? TimeSpan.FromSeconds(1000);
}

/// <summary>
Expand Down
29 changes: 20 additions & 9 deletions Source/Meadow.Foundation.Core/Sensors/Hid/AnalogJoystick.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,32 @@ public DigitalJoystickPosition? DigitalPosition {
/// <param name="verticalPin"></param>
/// <param name="calibration">Calibration for the joystick.</param>
/// <param name="isInverted">Whether or not the vertical component is inverted.</param>
/// <param name="updateIntervalMs">The time, in milliseconds, to wait
/// between sets of sample readings. This value determines how often
/// `Changed` events are raised and `IObservable` consumers are notified.</param>
public AnalogJoystick(
IAnalogInputController device, IPin horizontalPin, IPin verticalPin,
JoystickCalibration? calibration = null, bool isInverted = false)
: this(device, horizontalPin, verticalPin, calibration, isInverted, 5, TimeSpan.FromMilliseconds(40))
{ }

/// <summary>
/// Creates a 2-axis analog joystick
/// </summary>
/// <param name="device">The `IAnalogInputController` to create the port on.</param>
/// <param name="horizontalPin"></param>
/// <param name="verticalPin"></param>
/// <param name="calibration">Calibration for the joystick.</param>
/// <param name="isInverted">Whether or not the vertical component is inverted.</param>
/// <param name="sampleCount">How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.</param>
/// <param name="sampleIntervalMs">The time, in milliseconds,
/// <param name="sampleInterval">The time, in milliseconds,
/// to wait in between samples during a reading.</param>
public AnalogJoystick(
IAnalogInputController device, IPin horizontalPin, IPin verticalPin,
JoystickCalibration? calibration = null, bool isInverted = false,
int updateIntervalMs = 1000,
int sampleCount = 5, int sampleIntervalMs = 40)
JoystickCalibration? calibration, bool isInverted,
int sampleCount,
TimeSpan sampleInterval)
: this(
device.CreateAnalogInputPort(horizontalPin, updateIntervalMs, sampleCount, sampleIntervalMs),
device.CreateAnalogInputPort(verticalPin, updateIntervalMs, sampleCount, sampleIntervalMs),
device.CreateAnalogInputPort(horizontalPin, sampleCount, sampleInterval, new Voltage(3.3, VU.Volts)),
device.CreateAnalogInputPort(verticalPin, sampleCount, sampleInterval, new Voltage(3.3, VU.Volts)),
calibration, isInverted)
{ }

Expand Down
26 changes: 6 additions & 20 deletions Source/Meadow.Foundation.Core/Sensors/Light/AnalogLightSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,6 @@ public partial class AnalogLightSensor
/// </summary>
protected IAnalogInputPort AnalogInputPort { get; }

/// <summary>
/// How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.
/// </summary>
protected int sampleCount = 5;
/// <summary>
/// The time, in milliseconds, to wait
/// between sets of sample readings. This value determines how often
/// `Changed` events are raised and `IObservable` consumers are notified
/// </summary>
protected int sampleIntervalMs = 40;

/// <summary>
/// Raised when the value of the reading changes.
/// </summary>
Expand All @@ -52,24 +40,22 @@ public partial class AnalogLightSensor
/// <param name="device">The `IAnalogInputController` to create the port on.</param>
/// <param name="analogPin">Analog pin the sensor is connected to.</param>
/// <param name="calibration">Calibration for the analog sensor.</param>
/// <param name="updateIntervalMs">The time, in milliseconds, to wait
/// <param name="updateInterval">The time, in milliseconds, to wait
/// between sets of sample readings. This value determines how often
/// `Changed` events are raised and `IObservable` consumers are notified.</param>
/// <param name="sampleCount">How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.</param>
/// <param name="sampleIntervalMs">The time, in milliseconds,
/// <param name="sampleInterval">The time, in milliseconds,
/// to wait in between samples during a reading.</param>
public AnalogLightSensor(
IAnalogInputController device,
IPin analogPin,
Calibration? calibration = null,
int updateIntervalMs = 1000,
int sampleCount = 5, int sampleIntervalMs = 40)
: this(device.CreateAnalogInputPort(analogPin), calibration)
TimeSpan? updateInterval = null,
int sampleCount = 5, TimeSpan? sampleInterval = null)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleInterval ?? new TimeSpan(0, 0, 40), new Voltage(3.3)), calibration)
{
base.UpdateInterval = TimeSpan.FromMilliseconds(updateIntervalMs);
this.sampleCount = sampleCount;
this.sampleIntervalMs = sampleIntervalMs;
base.UpdateInterval = updateInterval ?? new TimeSpan(0, 0, 10);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ public enum KnownSensorType
/// <param name="calibration">Calibration for the analog temperature sensor. Only used if sensorType is set to Custom.</param>
/// <param name="sampleCount">How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.</param>
/// <param name="sampleIntervalMs">The time, in milliseconds,
/// <param name="sampleInterval">The time,
/// to wait in between samples during a reading.</param>
public AnalogTemperature(
IAnalogInputController device, IPin analogPin,
KnownSensorType sensorType, Calibration? calibration = null,
int sampleCount = 5, int sampleIntervalMs = 40)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleIntervalMs),
int sampleCount = 5, TimeSpan? sampleInterval = null)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), new Voltage(3.3, Voltage.UnitType.Volts)),
sensorType, calibration)
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public class Gp2d12 : SensorBase<Length>, IRangeFinder
/// <summary>
/// Create a new Gp2d12 object with an IO Device
/// </summary>
public Gp2d12(IAnalogInputController device, IPin analogInputPin)
public Gp2d12(IAnalogInputController device,
IPin analogInputPin,
int sampleCount = 5,
TimeSpan? sampleInterval = null,
Voltage? voltage = null)
{
AnalogInputPort = device.CreateAnalogInputPort(analogInputPin);
AnalogInputPort = device.CreateAnalogInputPort(analogInputPin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3));

// wire up our observable
// have to convert from voltage to length units for our consumers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Meadow.Devices;
using Meadow.Hardware;
using Meadow.Units;
using System;
using System.Threading.Tasks;

namespace Meadow.Foundation.Sensors.Distance
Expand All @@ -19,9 +20,13 @@ public MaxBotix(IAnalogInputPort analogIntputPort, SensorType sensor)
AnalogInitialize();
}

public MaxBotix(IMeadowDevice device, IPin analogIntputPin,
SensorType sensor) :
this(device.CreateAnalogInputPort(analogIntputPin), sensor)
public MaxBotix(SensorType sensor,
IMeadowDevice device,
IPin analogInputPin,
int sampleCount = 5,
TimeSpan? sampleInterval = null,
Voltage? voltage = null) :
this(device.CreateAnalogInputPort(analogInputPin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3)), sensor)
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class Alspt19315C : SensorBase<Voltage>
/// Create a new light sensor object using a static reference voltage.
/// </summary>
/// <param name="pin">AnalogChannel connected to the sensor.</param>
public Alspt19315C(IAnalogInputController device, IPin pin)
: this(device.CreateAnalogInputPort(pin))
public Alspt19315C(IAnalogInputController device, IPin pin, int sampleCount = 5, TimeSpan? sampleInterval = null, Voltage? voltage = null)
: this(device.CreateAnalogInputPort(pin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3)))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public class AnalogSolarGauge : SensorBase<float>,
public event EventHandler<IChangeResult<float>> SolarIntensityUpdated = delegate { };

//==== internals
protected IAnalogInputPort analogIn;
protected int sampleCount = 5;
protected int sampleIntervalMs = 40;
protected IAnalogInputPort analogInputPort;

//==== properties
public Voltage MinVoltageReference { get; protected set; } = new Voltage(0, VU.Volts);
Expand All @@ -39,24 +37,28 @@ public class AnalogSolarGauge : SensorBase<float>,
/// <param name="analogPin">Analog pin the temperature sensor is connected to.</param>
/// <param name="minVoltageReference">The minimum voltage expected when the solar panel isn't receiving light. Default is 0.</param>
/// <param name="maxVoltageReference">The maxmimu voltage expected when the solar panel is in full sun. Default is 3.3V.</param>
/// <param name="updateIntervalMs">The time, in milliseconds, to wait
/// <param name="updateInterval">The time to wait
/// between sets of sample readings. This value determines how often
/// `Changed` events are raised and `IObservable` consumers are notified.</param>
/// <param name="sampleCount">How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.</param>
/// <param name="sampleIntervalMs">The time, in milliseconds,
/// <param name="sampleInterval">The time,
/// to wait in between samples during a reading.</param>
public AnalogSolarGauge(
IAnalogInputController device, IPin analogPin,
Voltage? minVoltageReference = null, Voltage? maxVoltageReference = null,
int updateIntervalMs = 10000,
int sampleCount = 5, int sampleIntervalMs = 40)
: this(device.CreateAnalogInputPort(analogPin, updateIntervalMs, sampleCount, sampleIntervalMs),
IAnalogInputController device,
IPin analogPin,
Voltage? minVoltageReference = null,
Voltage? maxVoltageReference = null,
TimeSpan? updateInterval = null,
int sampleCount = 5,
TimeSpan? sampleInterval = null)
: this(device.CreateAnalogInputPort(analogPin,
sampleCount,
sampleInterval ?? new TimeSpan(0, 0, 0, 40),
maxVoltageReference ?? new Voltage(3.3)),
minVoltageReference, maxVoltageReference)
{
base.UpdateInterval = TimeSpan.FromMilliseconds(updateIntervalMs);
this.sampleCount = sampleCount;
this.sampleIntervalMs = sampleIntervalMs;
base.UpdateInterval = updateInterval ?? new TimeSpan(0, 0, 10);
}

/// <summary>
Expand All @@ -73,7 +75,7 @@ public AnalogSolarGauge(
if (maxVoltageReference is { } maxV) { this.MaxVoltageReference = maxV; }

// TODO: input port validation if any (is it constructed all right?)
this.analogIn = analogIn;
this.analogInputPort = analogIn;
Init();
}

Expand All @@ -93,13 +95,13 @@ protected void Init()
RaiseEventsAndNotify(changeResult);
},
null);
analogIn.Subscribe(observer);
analogInputPort.Subscribe(observer);
}

protected override async Task<float> ReadSensor()
{
// read the voltage
Voltage voltage = await analogIn.Read();
Voltage voltage = await analogInputPort.Read();

// convert the voltage
var newSolarIntensity = ConvertVoltageToIntensity(voltage);
Expand All @@ -123,15 +125,15 @@ protected override async Task<float> ReadSensor()
/// The default is 5 seconds.</param>
public void StartUpdating(TimeSpan updateInterval)
{
analogIn.StartUpdating(updateInterval);
analogInputPort.StartUpdating(updateInterval);
}

/// <summary>
/// Stops sampling the solar intensity.
/// </summary>
public void StopUpdating()
{
analogIn.StopUpdating();
analogInputPort.StopUpdating();
}

protected override void RaiseEventsAndNotify(IChangeResult<float> changeResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MeadowApp : App<F7MicroV2, MeadowApp>
public MeadowApp()
{
Console.WriteLine("Initialize hardware...");
solarGauge = new AnalogSolarGauge(Device, Device.Pins.A02, updateIntervalMs: 1000);
solarGauge = new AnalogSolarGauge(Device, Device.Pins.A02, updateInterval: TimeSpan.FromSeconds(1));

//==== classic .NET Event
solarGauge.SolarIntensityUpdated += (s, result) => Console.WriteLine($"SolarIntensityUpdated: {result.New * 100:n2}%");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public class Temt6000 : SensorBase<Voltage>
/// Creates a new Temt6000 driver
/// </summary>
/// <param name="pin">AnalogChannel connected to the sensor.</param>
public Temt6000(IAnalogInputController device, IPin pin)
: this(device.CreateAnalogInputPort(pin))
public Temt6000(IAnalogInputController device, IPin pin, int sampleCount = 5, TimeSpan? sampleInterval = null, Voltage? voltage = null)
: this(device.CreateAnalogInputPort(pin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), voltage ?? new Voltage(3.3)))
{
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,21 @@ public class Capacitive : SensorBase<double>, IMoistureSensor
/// </summary>
/// <param name="device">The `IAnalogInputController` to create the port on.</param>
/// <param name="analogPin">Analog pin the temperature sensor is connected to.</param>
/// <param name="updateIntervalMs">The time, in milliseconds, to wait
/// between sets of sample readings. This value determines how often
/// `Changed` events are raised and `IObservable` consumers are notified.</param>
/// <param name="updateInterval">The time, to wait between sets of sample readings.
/// This value determines how often`Changed` events are raised and `IObservable` consumers are notified.</param>
/// <param name="sampleCount">How many samples to take during a given
/// reading. These are automatically averaged to reduce noise.</param>
/// <param name="sampleIntervalMs">The time, in milliseconds,
/// to wait in between samples during a reading.</param>
/// <param name="sampleInterval">The time, to wait in between samples during a reading.</param>
public Capacitive(
IAnalogInputController device, IPin analogPin,
Voltage? minimumVoltageCalibration, Voltage? maximumVoltageCalibration,
int sampleCount = 5, int sampleIntervalMs = 40)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleIntervalMs),
TimeSpan? updateInterval = null,
int sampleCount = 5, TimeSpan? sampleInterval = null)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleInterval ?? TimeSpan.FromMilliseconds(40), new Voltage(3.3)),
minimumVoltageCalibration, maximumVoltageCalibration)
{ }
{
this.UpdateInterval = updateInterval ?? TimeSpan.FromSeconds(5);
}

/// <summary>
/// Creates a Capacitive soil moisture sensor object with the especified AnalogInputPort.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,18 @@ public class Fc28 : SensorBase<double>, IMoistureSensor
/// <summary>
/// Creates a FC28 soil moisture sensor object with the especified analog pin, digital pin and IO device.
/// </summary>
/// <param name="analogPort"></param>
/// <param name="digitalPort"></param>
/// <param name="analogPin"></param>
/// <param name="digitalPin"></param>
public Fc28(
IMeadowDevice device, IPin analogPin, IPin digitalPin,
Voltage? minimumVoltageCalibration, Voltage? maximumVoltageCalibration,
int updateIntervalMs = 1000,
int sampleCount = 5, int sampleIntervalMs = 40)
: this(device.CreateAnalogInputPort(analogPin, updateIntervalMs, sampleCount, sampleIntervalMs),
TimeSpan? updateInterval = null,
int sampleCount = 5, TimeSpan? sampleInterval = null)
: this(device.CreateAnalogInputPort(analogPin, sampleCount, sampleInterval ?? new TimeSpan(0, 0, 0, 40), new Voltage(3.3)),
device.CreateDigitalOutputPort(digitalPin), minimumVoltageCalibration, maximumVoltageCalibration)
{ }
{
this.UpdateInterval = updateInterval ?? new TimeSpan(0, 0, 1);
}

/// <summary>
/// Creates a FC28 soil moisture sensor object with the especified analog pin and digital pin.
Expand Down
Loading

0 comments on commit bb8d592

Please sign in to comment.