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

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

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

        Console.WriteLine($"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 => Console.WriteLine($"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();
    Console.WriteLine("Initial Readings:");
    Console.WriteLine($"Direction: [X:{result.X:N2}," +
        $"Y:{result.Y:N2}," +
        $"Z:{result.Z:N2}]");

    Console.WriteLine($"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> SensorBase<Vector> SamplingSensorBase<Vector> ByteCommsSensorBase<Vector> > Hmc5883 > Qmc5883
Implements IDisposable
Inherited Members ByteCommsSensorBase<Vector>.Peripheral ByteCommsSensorBase<Vector>.ReadBuffer ByteCommsSensorBase<Vector>.WriteBuffer ByteCommsSensorBase<Vector>.Init(Int32, Int32) ByteCommsSensorBase<Vector>.Dispose(Boolean) ByteCommsSensorBase<Vector>.Dispose() SamplingSensorBase<Vector>.StartUpdating(Nullable<TimeSpan>) SamplingSensorBase<Vector>.StopUpdating() SensorBase<Vector>.Updated SensorBase<Vector>.samplingLock SensorBase<Vector>.SamplingTokenSource SensorBase<Vector>.Conditions SensorBase<Vector>.IsSampling SensorBase<Vector>.UpdateInterval SensorBase<Vector>.RaiseEventsAndNotify(IChangeResult<>) SensorBase<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>, IDisposable

Constructors

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

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
System.Byte address
Hmc5883.GainLevels gain
Hmc5883.MeasuringModes measuringMode
Hmc5883.DataOutputRates outputRate
Hmc5883.SampleAmounts samplesAmount
Hmc5883.MeasurementConfigurations measurementConfig

Fields

gain

Declaration
protected byte gain

Field Value

Type Description
System.Byte

measurementConfig

Declaration
protected byte measurementConfig

Field Value

Type Description
System.Byte

measuringMode

Declaration
protected byte measuringMode

Field Value

Type Description
System.Byte

outputRate

Declaration
protected byte outputRate

Field Value

Type Description
System.Byte

sampleAmount

Declaration
protected byte sampleAmount

Field Value

Type Description
System.Byte

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()

Declaration
protected virtual void Initialize()

RaiseEventsAndNotify(IChangeResult<Vector>)

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

Parameters

Type Name Description
IChangeResult<Vector> changeResult

ReadSensor()

Declaration
protected override Task<Vector> ReadSensor()

Returns

Type Description
Task<Vector>

Overrides

Meadow.Foundation.SensorBase<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>>