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()
{
    Console.WriteLine("Initialize...");

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

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

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

    //==== IObservable example
    var observer = SwitchingAnemometer.CreateObserver(
        handler: result => {
            Console.WriteLine($"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();
    Console.WriteLine("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> SensorBase<Speed> SamplingSensorBase<Speed> > SwitchingAnemometer
Implements IAnemometer
Inherited Members SensorBase<Speed>.Updated SensorBase<Speed>.samplingLock SensorBase<Speed>.SamplingTokenSource SensorBase<Speed>.Conditions SensorBase<Speed>.IsSampling SensorBase<Speed>.UpdateInterval SensorBase<Speed>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<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 : SamplingSensorBase<Speed>, IAnemometer

Constructors

SwitchingAnemometer(IDigitalInputController, IPin)

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

Declaration
public SwitchingAnemometer(IDigitalInputController device, IPin digitalInputPin)

Parameters

Type Name Description
IDigitalInputController device
IPin digitalInputPin

SwitchingAnemometer(IDigitalInputPort)

Creates a new switching anemometer using the specific IDigitalInputPort.

Declaration
public SwitchingAnemometer(IDigitalInputPort inputPort)

Parameters

Type Name Description
IDigitalInputPort inputPort

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

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

HandleInputPortChange(Object, DigitalPortResult)

Declaration
protected void HandleInputPortChange(object sender, DigitalPortResult result)

Parameters

Type Name Description
System.Object sender
DigitalPortResult result

RaiseEventsAndNotify(IChangeResult<Speed>)

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

Parameters

Type Name Description
IChangeResult<Speed> changeResult

ReadSensor()

Declaration
protected override Task<Speed> ReadSensor()

Returns

Type Description
Task<Speed>

Overrides

Meadow.Foundation.SensorBase<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.SamplingSensorBase<Speed>.StopUpdating()

SubscribeToInputPortEvents()

Declaration
protected void SubscribeToInputPortEvents()

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

UnsubscribeToInputPortEvents()

Declaration
protected void UnsubscribeToInputPortEvents()

Events

WindSpeedUpdated

Raised when the speed of the wind changes

Declaration
public event EventHandler<IChangeResult<Speed>> WindSpeedUpdated

Event Type

Type Description
EventHandler<IChangeResult<Speed>>