Meadow.Foundation.Sensors.Motion.Lis2Mdl
Lis2mdl | |
---|---|
Status | |
Source code | GitHub |
Datasheet(s) | GitHub |
NuGet package |
Code Example
Lis2mdl sensor;
public override Task Initialize()
{
Resolver.Log.Info("Initialize hardware...");
sensor = new Lis2mdl(Device.CreateI2cBus());
// classical .NET events can also be used:
sensor.Updated += HandleResult;
// Example that uses an IObservable subscription to only be notified when the filter is satisfied
var consumer = Lis2mdl.CreateObserver(handler: result => HandleResult(this, result),
filter: result => FilterResult(result));
sensor.Subscribe(consumer);
sensor.StartUpdating(TimeSpan.FromMilliseconds(2000));
return base.Initialize();
}
bool FilterResult(IChangeResult<MagneticField3D> result)
{
return result.New.Z > new MagneticField(0.1, MagneticField.UnitType.Gauss);
}
void HandleResult(object sender,
IChangeResult<MagneticField3D> result)
{
var mag = result.New;
Resolver.Log.Info($"MagX={mag.X.Gauss:0.##}gauss, MagY={mag.Y.Gauss:0.##}gauss, GyroZ={mag.Z.Gauss:0.##}gauss");
}
Sample project(s) available on GitHub
Class Lis2Mdl
Represents a LIS2MDL is a low-power, high-performance 3-axis magnetometer from STMicroelectronics with a fixed full range of ±50 gauss and a 16-bit resolution
Assembly: Lis2Mdl.dll
View Source
public class Lis2Mdl : PollingSensorBase<MagneticField3D>, IObservable<IChangeResult<MagneticField3D>>, IMagnetometer, ISamplingSensor<MagneticField3D>, ISensor<MagneticField3D>, ISensor, ISamplingSensor, II2cPeripheral
Inheritance: System.Object
-> Meadow.Foundation.ObservableBase<UNIT>
Implements:
System.IObservable<Meadow.IChangeResult<Meadow.Units.MagneticField3D>>
, Meadow.Peripherals.Sensors.Motion.IMagnetometer
, Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>
, Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>
, Meadow.Peripherals.Sensors.ISensor
, Meadow.Peripherals.Sensors.ISamplingSensor
, Meadow.Hardware.II2cPeripheral
Properties
MagneticField3D
Current Magnetic Field 3D
View Source
public MagneticField3D? MagneticField3D { get; }
DefaultI2cAddress
The default I2C address for the peripheral
View Source
public byte DefaultI2cAddress { get; }
Fields
i2cComms
I2C Communication bus used to communicate with the peripheral
View Source
protected readonly II2cCommunications i2cComms
Methods
ReadSensor()
Reads data from the sensor
View Source
protected override Task<MagneticField3D> ReadSensor()
Returns
System.Threading.Tasks.Task<Meadow.Units.MagneticField3D>
: The latest sensor reading### GetOutputDataRate()
Gets the output data rate (ODR) of the magnetometer.
View Source
public Lis2Mdl.OutputDataRate GetOutputDataRate()
Returns
Meadow.Foundation.Sensors.Motion.Lis2Mdl.OutputDataRate enum.### SetOutputDataRate(OutputDataRate) Sets the output data rate (ODR) of the magnetometer.
View Source
public void SetOutputDataRate(Lis2Mdl.OutputDataRate odr)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Foundation.Sensors.Motion.Lis2Mdl.OutputDataRate enum. |
GetOperatingMode()
Gets the operating mode of the magnetometer.
View Source
public Lis2Mdl.OperatingMode GetOperatingMode()
Returns
Meadow.Foundation.Sensors.Motion.Lis2Mdl.OperatingMode enum.### SetOperatingMode(OperatingMode) Sets the operating mode of the magnetometer.
View Source
public void SetOperatingMode(Lis2Mdl.OperatingMode mode)
Parameters
Type | Name | Description |
---|---|---|
Meadow.Foundation.Sensors.Motion.Lis2Mdl.OperatingMode enum. |
GetTemperatureCompensation()
Gets the status of the temperature compensation feature.
View Source
public bool GetTemperatureCompensation()
Returns
System.Boolean
: true if temperature compensation is enabled, false otherwise.### SetTemperatureCompensation(bool)
Sets the status of the temperature compensation feature.
View Source
public void SetTemperatureCompensation(bool enable)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enable | true to enable temperature compensation, false to disable it. |
GetFastRead()
Gets the status of the Fast Read feature.
View Source
public bool GetFastRead()
Returns
System.Boolean
: true if Fast Read is enabled, false otherwise.### SetFastRead(bool)
Sets the status of the Fast Read feature.
View Source
public void SetFastRead(bool enable)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enable | true to enable Fast Read, false to disable it. |
GetBlockDataUpdate()
Gets the status of the Block Data Update (BDU) feature.
View Source
public bool GetBlockDataUpdate()
Returns
System.Boolean
: true if BDU is enabled, false otherwise.### SetBlockDataUpdate(bool)
Sets the status of the Block Data Update (BDU) feature.
View Source
public void SetBlockDataUpdate(bool enable)
Parameters
Type | Name | Description |
---|---|---|
System.Boolean | enable | true to enable BDU, false to disable it. |
Implements
System.IObservable<Meadow.IChangeResult<Meadow.Units.MagneticField3D>>
Meadow.Peripherals.Sensors.Motion.IMagnetometer
Meadow.Peripherals.Sensors.ISamplingSensor<Meadow.Units.MagneticField3D>
Meadow.Peripherals.Sensors.ISensor<Meadow.Units.MagneticField3D>
Meadow.Peripherals.Sensors.ISensor
Meadow.Peripherals.Sensors.ISamplingSensor
Meadow.Hardware.II2cPeripheral