Remarks

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

Code Example

Mmc5603 sensor;

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

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

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

    // Example that uses an IObservable subscription to only be notified when the filter is satisfied
    var consumer = Mmc5603.CreateObserver(
        handler: result => Resolver.Log.Info($"Observer: [x] changed by threshold; new [x]: X:{result.New.X.MicroTesla:N2}, old: X:{result.Old?.X.MicroTesla:N2}"),
        // only notify if there's a greater than 1 micro tesla on the Y axis
        filter: result => 
        {
            if (result.Old is { } old) 
            { //c# 8 pattern match syntax. checks for !null and assigns var
                return (result.New - old).Y > new MagneticField(1, MU.MicroTesla);
            }
            return false;
        });

    sensor.Subscribe(consumer);

    return Task.CompletedTask;
}

public async override Task Run()
{
    Resolver.Log.Loglevel = Meadow.Logging.LogLevel.Trace;

    //Read from sensor
    var result = await sensor.Read();

    //output initial readings text to console
    Resolver.Log.Info("Initial Readings:");
    Resolver.Log.Info(
        $"Mangetic field: [X:{result.X.MicroTesla:N2}," +
        $"Y:{result.Y.MicroTesla:N2}," +
        $"Z:{result.Z.MicroTesla:N2} (MicroTeslas)]");

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

Sample project(s) available on GitHub

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

Syntax

public class Mmc5603 : ByteCommsSensorBase<MagneticField3D>, ISamplingSensor<MagneticField3D>, ISamplingSensor<MagneticField3D>, IDisposable, IMagnetometer

Constructors

Mmc5603(II2cBus, Byte)

Create a new Mmc5603 object using the default parameters for the component

Declaration
public Mmc5603(II2cBus i2cBus, byte address = null)

Parameters

Type Name Description
II2cBus i2cBus

I2C bus object - default = 400 KHz

System.Byte address

Address of the Mmc5603

Properties

ContinuousModeEnabled

Get/set continuous sensor reading mode

Declaration
public bool ContinuousModeEnabled { get; set; }

Property Value

Type Description
System.Boolean

MagneticField3d

The current magnetic field value

Declaration
public MagneticField3D? MagneticField3d { get; }

Property Value

Type Description
System.Nullable<MagneticField3D>

Methods

RaiseEventsAndNotify(IChangeResult<MagneticField3D>)

Raise events for subcribers and notify of value changes

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

Parameters

Type Name Description
IChangeResult<MagneticField3D> changeResult

The updated sensor data

ReadSensor()

Reads data from the sensor

Declaration
protected override Task<MagneticField3D> ReadSensor()

Returns

Type Description
Task<MagneticField3D>

The latest sensor reading

Overrides

Meadow.Foundation.SamplingSensorBase<MagneticField3D>.ReadSensor()

ReadTemperature()

Read the sensor temperature Doesn't work in continuous mode

Declaration
public Task<Units.Temperature> ReadTemperature()

Returns

Type Description
Task<Units.Temperature>

Reset()

Reset the sensor

Declaration
public void Reset()

Events

MagneticField3dUpdated

Raised when the magnetic field value changes

Declaration
public event EventHandler<IChangeResult<MagneticField3D>> MagneticField3dUpdated

Event Type

Type Description
EventHandler<IChangeResult<MagneticField3D>>