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 > Hmc5883 > Qmc5883
Namespace Meadow.Foundation.Sensors.Motion
Assembly Hmc5883.dll

Syntax

public class Hmc5883 : ByteCommsSensorBase<Vector>

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

Events

DirectionUpdated

Event to be raised when the compass changes

Declaration
public event EventHandler<IChangeResult<Vector>> DirectionUpdated

Event Type

Type Description
EventHandler<IChangeResult<Vector>>