.. highlight:: java

BMM150 Magnetometer
===================

The `Bmm150Magnetometer <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.html>`_ class controls the magnetometer on the MetaWear C Pro boards.

Magnetic Field Sampling
-----------------------

The sensor measures magnetic field strength in µT for XYZ directions, and comes with 4 presets that control the operating frequency, current draw, and noise.  Check out which `PowerPreset <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.PowerPreset.html>`_ setting best fits your needs and call `setPowerPreset <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.html#setPowerPreset(com.mbientlab.metawear.module.Bmm150Magnetometer.PowerPreset)>`_ to write your choice to the board.  For most applications, it is recommended that the low power preset be chosen.

::

    import com.mbientlab.metawear.module.Bmm150Magnetometer;
    import com.mbientlab.metawear.module.Bmm150Magnetometer.PowerPreset;
    
    Bmm150Magnetometer magModule= mwBoard.getModule(Bmm150Magnetometer.class);
    //Set to low power mode
    magModule.setPowerPreset(PowerPreset.LOW_POWER);
    magModule.enableBFieldSampling();
    
    //Stream rotation data around the XYZ axes from the gyro sensor
    magModule.routeData().fromBField().stream("mag_stream").commit()
        .onComplete(new CompletionHandler<RouteManager>() {
            @Override
            public void success(RouteManager result) {
                result.subscribe("mag_stream", new RouteManager.MessageHandler() {
                    @Override
                    public void process(Message msg) {
                        final CartesianFloat bField = msg.getData(CartesianFloat.class);
    
                        Log.i("MainActivity", bField.toString());
                    }
                });
    
                magModule.start();
            }
        });

Threshold Detection
-------------------

In addition to simply sensing magnetic field strength, the BMM150 also has some monitoring features that triggers interrupts when the field strength crosses a threshold.  Users can configure separate high and low threshold levels with the `ThresholdDetectionConfigEditor <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.ThresholdDetectionConfigEditor.html>`_ interface and enable detection on specific axes when calling `enableThresholdDetection <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.html#enableThresholdDetection(com.mbientlab.metawear.module.Bmm150Magnetometer.ThresholdDetectionType...)>`_.

A threshold interrupt is represented by either the `ThresholdInterrupt <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.ThresholdInterrupt.html>`_ interface or a boolean array where the array is indexed by the `ThresholdDetectionType <https://mbientlab.com/docs/metawear/android/latest/com/mbientlab/metawear/module/Bmm150Magnetometer.ThresholdDetectionType.html>`_ enum values.  ::

    import static com.mbientlab.metawear.module.Bmm150Magnetometer.ThresholdDetectionType.*;
    import com.mbientlab.metawear.module.Bmm150Magnetometer.ThresholdInterrupt;

    final Bmm150Magnetometer mag= mwBoard.getModule(Bmm150Magnetometer.class);
    final String THS_INTERRUPT= "mag_ths";

    mag.enableThresholdDetection(LOW_X, HIGH_Y);
    mag.routeData().fromThreshold().stream(THS_INTERRUPT).commit()
        .onComplete(new AsyncOperation.CompletionHandler<RouteManager>() {
            @Override
            public void success(RouteManager result) {
                result.subscribe(THS_INTERRUPT, new RouteManager.MessageHandler() {
                    @Override
                    public void process(Message msg) {
                        Log.i("MainActivity", msg.getData(ThresholdInterrupt.class).toString());
                    }
                });
                mag.start();
            }
        });