C

This code base on language C have the propose to get information about this sensor and display it.

First part that we need to write on our code is the libraries that we are going to use. In this case we are going to use

XdkSensorHandle.h

/************************************************************
*   MagneticSensor.c
************************************************************/

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

#include "XdkSensorHandle.h"

/* own header files */

A storage struct for every axis’ magnetic field data is declared. The data is then read by the Magnetometer_readXyzTeslaData() function and then stored in a passed reference of the storage struct.

// Function that read and print the sensor data of the BMM150 to the console of the XDK-Workbench
static void readMagnetometer(xTimerHandle xTimer){

    (void) xTimer;
    Retcode_T returnValue = RETCODE_FAILURE;

    /* read and print BMM150 magnetometer data */

    Magnetometer_XyzData_T bmm150 = {INT32_C(0), INT32_C(0), INT32_C(0), INT32_C(0)};

    returnValue = Magnetometer_readXyzTeslaData(xdkMagnetometer_BMM150_Handle, &bmm150);

    if (RETCODE_OK == returnValue) {
    printf("BMM150 Magnetic Data: x =%ld mT y =%ld mT z =%ld mT \n\r",
          (long int) bmm150.xAxisData, (long int) bmm150.yAxisData, (long int) bmm150.zAxisData);
    }
}

Then we declared the function to initialized the sensor and variable. We need to use the function of Magnetometerinit(xdkMagnetometer_BBM150_Handle)

// Function that initializes the magnetometer with the BMM150 handler and with additional presettings
static void initMagnetometer(void){

    Retcode_T returnValue = RETCODE_FAILURE;
    Retcode_T returnDataRateValue = RETCODE_FAILURE;
    Retcode_T returnPresetModeValue = RETCODE_FAILURE;

    /* initialize magnetometer */

    returnValue = Magnetometer_init(xdkMagnetometer_BMM150_Handle);

    if(RETCODE_OK != returnValue){
        printf("BMM150 Magnetometer initialization failed \n\r");
    }

    returnDataRateValue = Magnetometer_setDataRate(xdkMagnetometer_BMM150_Handle,
            MAGNETOMETER_BMM150_DATARATE_10HZ);
    if (RETCODE_OK != returnDataRateValue) {
    printf("Configuring data rate failed \n\r");
    }
    returnPresetModeValue = Magnetometer_setPresetMode(xdkMagnetometer_BMM150_Handle,
            MAGNETOMETER_BMM150_PRESETMODE_REGULAR);
    if (RETCODE_OK != returnPresetModeValue) {
    printf("Configuring preset mode failed \n\r");
    }
}

Finally we declared the main part of the program where we need to call all the function that we previously defined. Also define the timers.

void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);

    uint32_t timerBlockTime = UINT32_MAX;
    uint32_t oneSecondDelay = UINT32_C(1000);
    uint32_t timerAutoReloadOn = UINT32_C(1);

    xTimerHandle magnetometerHandle = NULL;

    initMagnetometer();

    magnetometerHandle = xTimerCreate((const char *) "readMagnetometer", oneSecondDelay,timerAutoReloadOn, NULL, readMagnetometer);

    xTimerStart(magnetometerHandle,timerBlockTime);
}

Last updated

Was this helpful?