Remarks

SwitchingAnemometer
Status Status badge: working
Source code GitHub
Datasheet(s) GitHub
NuGet package NuGet Gallery for Meadow.Foundation.Sensors.Weather.SwitchingAnemometer

Code Example

RgbPwmLed onboardLed;
SwitchingAnemometer anemometer;

public override Task Initialize()
{
    Resolver.Log.Info("Initialize...");

    //==== onboard LED
    onboardLed = new RgbPwmLed(
        redPwmPin: Device.Pins.OnboardLedRed,
        greenPwmPin: Device.Pins.OnboardLedGreen,
        bluePwmPin: Device.Pins.OnboardLedBlue,
        CommonType.CommonAnode);

    //==== create the anemometer
    anemometer = new SwitchingAnemometer(Device.Pins.A01);

    //==== classic events example
    anemometer.WindSpeedUpdated += (sender, result) =>
    {
        Resolver.Log.Info($"new speed: {result.New.KilometersPerHour:n1}kmh, old: {result.Old?.KilometersPerHour:n1}kmh");
        OutputWindSpeed(result.New);
    };

    //==== IObservable example
    var observer = SwitchingAnemometer.CreateObserver(
        handler: result =>
        {
            Resolver.Log.Info($"new speed (from observer): {result.New.KilometersPerHour:n1}kmh, old: {result.Old?.KilometersPerHour:n1}kmh");
        },
        null
        );
    anemometer.Subscribe(observer);

    return Task.CompletedTask;
}

public override Task Run()
{
    // start raising updates
    anemometer.StartUpdating();
    Resolver.Log.Info("Hardware initialized.");

    return Task.CompletedTask;
}

/// <summary>
/// Displays the windspeed on the onboard LED as full red @ >= `10km/h`,
/// blue @ `0km/h`, and a proportional mix, in between those speeds.
/// </summary>
/// <param name="windspeed"></param>
void OutputWindSpeed(Speed windspeed)
{
    // `0.0` - `10kmh`
    int r = (int)windspeed.KilometersPerHour.Map(0f, 10f, 0f, 255f);
    int b = (int)windspeed.KilometersPerHour.Map(0f, 10f, 255f, 0f);

    var wspeedColor = Color.FromRgb(r, 0, b);
    onboardLed.SetColor(wspeedColor);
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<Speed> SamplingSensorBase<Speed> PollingSensorBase<Speed> > SwitchingAnemometer
Implements ISamplingSensor<Speed> ISamplingSensor<Speed> IAnemometer
Inherited Members SamplingSensorBase<Speed>.samplingLock SamplingSensorBase<Speed>.Updated SamplingSensorBase<Speed>.SamplingTokenSource SamplingSensorBase<Speed>.Conditions SamplingSensorBase<Speed>.IsSampling SamplingSensorBase<Speed>.UpdateInterval SamplingSensorBase<Speed>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<Speed>.Read() ObservableBase<Speed>.observers ObservableBase<Speed>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Speed>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Speed>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Weather
Assembly SwitchingAnemometer.dll

Syntax

public class SwitchingAnemometer : PollingSensorBase<Speed>, ISamplingSensor<Speed>, ISamplingSensor<Speed>, IAnemometer

Constructors

SwitchingAnemometer(IDigitalInputPort)

Creates a new switching anemometer using the specific IDigitalInputPort.

Declaration
public SwitchingAnemometer(IDigitalInputPort inputPort)

Parameters

Type Name Description
IDigitalInputPort inputPort

SwitchingAnemometer(IPin)

Creates a new SwitchingAnemometer using the specific digital input on the device.

Declaration
public SwitchingAnemometer(IPin digitalInputPin)

Parameters

Type Name Description
IPin digitalInputPin

Properties

KmhPerSwitchPerSecond

Calibration for how fast the wind speed is when the switch is hit once per second. Used to calculate the wind speed based on the time duration between switch events. Default is 2.4kmh.

Declaration
public float KmhPerSwitchPerSecond { get; set; }

Property Value

Type Description
System.Single

NoWindTimeout

Time to wait if no events come in to register a zero speed wind

Declaration
public TimeSpan NoWindTimeout { get; set; }

Property Value

Type Description
TimeSpan

OneTimeReadDuration

Time to capture samples for a one time Read if IsSampling is false

Declaration
public TimeSpan OneTimeReadDuration { get; set; }

Property Value

Type Description
TimeSpan

SampleCount

Number of samples to take for a reading

Declaration
public int SampleCount { get; set; }

Property Value

Type Description
System.Int32

WindSpeed

The current wind speed

Declaration
public Speed? WindSpeed { get; protected set; }

Property Value

Type Description
System.Nullable<Speed>

Methods

RaiseEventsAndNotify(IChangeResult<Speed>)

Raise events for subcribers and notify of value changes

Declaration
protected override void RaiseEventsAndNotify(IChangeResult<Speed> changeResult)

Parameters

Type Name Description
IChangeResult<Speed> changeResult

The updated sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<Speed> ReadSensor()

Returns

Type Description
Task<Speed>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<Speed>.ReadSensor()

StartUpdating(Nullable<TimeSpan>)

Starts continuously sampling the sensor

This method also starts raising Updated events and IObservable subscribers getting notified. Use the standbyDuration parameter to specify how often events and notifications are raised/sent.

Declaration
public override void StartUpdating(TimeSpan? updateInterval = null)

Parameters

Type Name Description
System.Nullable<TimeSpan> updateInterval

StopUpdating()

Stops the driver from raising wind speed events

Declaration
public override void StopUpdating()

Overrides

Meadow.Foundation.PollingSensorBase<Speed>.StopUpdating()

SwitchIntervalToKmh(TimeSpan)

A wind speed of 2.4km/h causes the switch to close once per second

Declaration
protected float SwitchIntervalToKmh(TimeSpan interval)

Parameters

Type Name Description
TimeSpan interval

The interval between signals

Returns

Type Description
System.Single

Events

WindSpeedUpdated

Raised when the speed of the wind changes

Declaration
public event EventHandler<IChangeResult<Speed>> WindSpeedUpdated

Event Type

Type Description
EventHandler<IChangeResult<Speed>>