Skip to main content

Meadow.Foundation.Sensors.Distance.A02yyuw

A02yyuw
StatusStatus badge: working
Source codeGitHub
Datasheet(s)GitHub
NuGet packageNuGet Gallery for Meadow.Foundation.Sensors.Distance.A02yyuw

Code Example

A02yyuw a02yyuw;

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

a02yyuw = new A02yyuw(Device, Device.PlatformOS.GetSerialPortName("COM4"));

var consumer = A02yyuw.CreateObserver(
handler: result =>
{
Resolver.Log.Info($"Observer: Distance changed by threshold; new distance: {result.New.Centimeters:N1}cm, old: {result.Old?.Centimeters:N1}cm");
},
filter: result =>
{
if (result.Old is { } old)
{
return Math.Abs((result.New - old).Centimeters) > 5.0;
}
return false;
}
);
a02yyuw.Subscribe(consumer);

a02yyuw.Updated += A02yyuw_DistanceUpdated;

return Task.CompletedTask;
}

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

var distance = await a02yyuw.Read();
Resolver.Log.Info($"Initial distance is: {distance.Centimeters:N1}cm / {distance.Inches:N1}in");

a02yyuw.StartUpdating(TimeSpan.FromSeconds(2));
}

private void A02yyuw_DistanceUpdated(object sender, IChangeResult<Length> e)
{
Resolver.Log.Info($"Distance: {e.New.Centimeters:N1}cm / {e.New.Inches:N1}in");
}

Sample project(s) available on GitHub

Class A02yyuw

Represents the A02YYUW serial distance sensor

Assembly: A02yyuw.dll
View Source
Declaration
public class A02yyuw : PollingSensorBase<Length>, IObservable<IChangeResult<Length>>, IRangeFinder, ISamplingSensor<Length>, ISensor<Length>, ISensor, ISamplingSensor, ISleepAwarePeripheral, IDisposable

Inheritance: System.Object -> Meadow.Foundation.ObservableBase<UNIT>

Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.Length>>, Meadow.Peripherals.Sensors.Distance.IRangeFinder, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Length>, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Length>, Meadow.Peripherals.Sensors.ISensor, Meadow.Peripherals.Sensors.ISamplingSensor, Meadow.ISleepAwarePeripheral, System.IDisposable

Properties

Distance

Distance from sensor to object

View Source
Declaration
public Length? Distance { get; }

OutOfRangeValue

Value returned when the sensor cannot determine the distance

View Source
Declaration
public Length OutOfRangeValue { get; }

SensorReadTimeOut

The maximum time to wait for a sensor reading

View Source
Declaration
public TimeSpan SensorReadTimeOut { get; set; }

Methods

MeasureDistance()

Start a distance measurement

View Source
Declaration
public void MeasureDistance()

Read()

Convenience method to get the current sensor reading

View Source
Declaration
public override Task<Length> Read()
Returns

System.Threading.Tasks.Task<Meadow.Units.Length>

ReadSensor()

Read the distance from the sensor

View Source
Declaration
protected override Task<Length> ReadSensor()
Returns

System.Threading.Tasks.Task<Meadow.Units.Length>

StartUpdating(TimeSpan?)

Start updating distances

View Source
Declaration
public override void StartUpdating(TimeSpan? updateInterval)
Parameters
TypeNameDescription
System.Nullable<System.TimeSpan>updateIntervalThe interval used to notify external subscribers

StopUpdating()

Stop sampling

View Source
Declaration
public override void StopUpdating()

BeforeSleep(CancellationToken)

Called before the platform goes into Sleep state

View Source
Declaration
public Task BeforeSleep(CancellationToken cancellationToken)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
System.Threading.CancellationTokencancellationToken

AfterWake(CancellationToken)

Called after the platform returns to Wake state

View Source
Declaration
public Task AfterWake(CancellationToken cancellationToken)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
System.Threading.CancellationTokencancellationToken

Dispose(bool)

Dispose of the object

View Source
Declaration
protected virtual void Dispose(bool disposing)
Parameters
TypeNameDescription
System.BooleandisposingIs disposing

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

View Source
Declaration
public void Dispose()

Implements

  • System.IObservable<Meadow.IChangeResult<Meadow.Units.Length>>
  • Meadow.Peripherals.Sensors.Distance.IRangeFinder
  • Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.Length>
  • Meadow.Peripherals.Sensors.ISensor<Meadow.Units.Length>
  • Meadow.Peripherals.Sensors.ISensor
  • Meadow.Peripherals.Sensors.ISamplingSensor
  • Meadow.ISleepAwarePeripheral
  • System.IDisposable