Remarks

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

Code Example

Hmc5883 sensor;

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

    sensor = new Hmc5883(Device.CreateI2cBus());

    // classical .NET events can also be used:
    sensor.Updated += (sender, result) => {
        Resolver.Log.Info($"Direction: [X:{result.New.X:N2}," +
            $"Y:{result.New.Y:N2}," +
            $"Z:{result.New.Z:N2}]");

        Resolver.Log.Info($"Heading: [{Hmc5883.DirectionToHeading(result.New).DecimalDegrees:N2}] degrees");
    };

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Hmc5883.CreateObserver(
        handler: result => Resolver.Log.Info($"Observer: [x] changed by threshold; new [x]: X:{Hmc5883.DirectionToHeading(result.New):N2}," +
                $" old: X:{((result.Old != null) ? Hmc5883.DirectionToHeading(result.Old.Value) : "n/a"):N2} degrees"),
        // only notify if there's a greater than 5° of heading change
        filter: result => {
            if (result.Old is { } old) { //c# 8 pattern match syntax. checks for !null and assigns var.
                return (Hmc5883.DirectionToHeading(result.New - old) > new Azimuth(5));
            }
            return false;
        });

    sensor.Subscribe(consumer);

    return Task.CompletedTask;
}

public async override Task Run()
{
    var result = await sensor.Read();
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info($"Direction: [X:{result.X:N2}," +
        $"Y:{result.Y:N2}," +
        $"Z:{result.Z:N2}]");

    Resolver.Log.Info($"Heading: [{Hmc5883.DirectionToHeading(result).DecimalDegrees:N2}] degrees");

    sensor.StartUpdating(TimeSpan.FromMilliseconds(1000));
}

Sample project(s) available on GitHub

Characteristic Locus
Inheritance System.Object ObservableBase<Vector> SamplingSensorBase<Vector> PollingSensorBase<Vector> ByteCommsSensorBase<Vector> > Hmc5883 > Qmc5883
Implements ISamplingSensor<Vector> ISamplingSensor<Vector> IDisposable
Inherited Members ByteCommsSensorBase<Vector>.Peripheral ByteCommsSensorBase<Vector>.ReadBuffer ByteCommsSensorBase<Vector>.WriteBuffer ByteCommsSensorBase<Vector>.Init(Int32, Int32) ByteCommsSensorBase<Vector>.Dispose(Boolean) ByteCommsSensorBase<Vector>.Dispose() PollingSensorBase<Vector>.StartUpdating(Nullable<TimeSpan>) PollingSensorBase<Vector>.StopUpdating() SamplingSensorBase<Vector>.samplingLock SamplingSensorBase<Vector>.Updated SamplingSensorBase<Vector>.SamplingTokenSource SamplingSensorBase<Vector>.Conditions SamplingSensorBase<Vector>.IsSampling SamplingSensorBase<Vector>.UpdateInterval SamplingSensorBase<Vector>.RaiseEventsAndNotify(IChangeResult<>) SamplingSensorBase<Vector>.Read() ObservableBase<Vector>.observers ObservableBase<Vector>.NotifyObservers(IChangeResult<>) Meadow.Foundation.ObservableBase<Meadow.Foundation.Spatial.Vector>.Subscribe(IObserver<>) Meadow.Foundation.ObservableBase<Meadow.Foundation.Spatial.Vector>.CreateObserver(Action<>, System.Nullable<Predicate<IChangeResult<UNIT>>>)
Namespace Meadow.Foundation.Sensors.Motion
Assembly Hmc5883.dll

Syntax

public class Hmc5883 : ByteCommsSensorBase<Vector>, ISamplingSensor<Vector>, ISamplingSensor<Vector>, IDisposable

Constructors

Hmc5883(II2cBus, Byte, Hmc5883.GainLevels, Hmc5883.MeasuringModes, Hmc5883.DataOutputRates, Hmc5883.SampleAmounts, Hmc5883.MeasurementConfigurations)

Create a new Hmc5883 object

Declaration
public Hmc5883(II2cBus i2cBus, byte address = null, Hmc5883.GainLevels gain = default(Hmc5883.GainLevels), Hmc5883.MeasuringModes measuringMode = default(Hmc5883.MeasuringModes), Hmc5883.DataOutputRates outputRate = default(Hmc5883.DataOutputRates), Hmc5883.SampleAmounts samplesAmount = default(Hmc5883.SampleAmounts), Hmc5883.MeasurementConfigurations measurementConfig = default(Hmc5883.MeasurementConfigurations))

Parameters

Type Name Description
II2cBus i2cBus

The I2C bus

System.Byte address

The I2C address

Hmc5883.GainLevels gain

Gain

Hmc5883.MeasuringModes measuringMode

Measuring mode

Hmc5883.DataOutputRates outputRate

Output rate

Hmc5883.SampleAmounts samplesAmount

Samples amount

Hmc5883.MeasurementConfigurations measurementConfig

Measurement configuration

Properties

DeviceStatus

HMC5883L Status

Declaration
public Hmc5883.Statuses DeviceStatus { get; }

Property Value

Type Description
Hmc5883.Statuses

Direction

HMC5883L Direction as a Vector

Declaration
public Vector? Direction { get; }

Property Value

Type Description
System.Nullable<Vector>

Heading

HMC5883L Heading (DEG)

Declaration
public Azimuth? Heading { get; }

Property Value

Type Description
System.Nullable<Azimuth>

Methods

DirectionToHeading(Vector)

Calculate heading

Declaration
public static Azimuth DirectionToHeading(Vector direction)

Parameters

Type Name Description
Vector direction

HMC5883L Direction

Returns

Type Description
Azimuth

Heading (DEG)

Initialize()

Initalize the sensor

Declaration
protected virtual void Initialize()

RaiseEventsAndNotify(IChangeResult<Vector>)

Raise events for subcribers and notify of value changes

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

Parameters

Type Name Description
IChangeResult<Vector> changeResult

The updated sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<Vector> ReadSensor()

Returns

Type Description
Task<Vector>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<Meadow.Foundation.Spatial.Vector>.ReadSensor()

Events

DirectionUpdated

Event to be raised when the compass changes

Declaration
public event EventHandler<IChangeResult<Vector>> DirectionUpdated

Event Type

Type Description
EventHandler<IChangeResult<Vector>>