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.