Modify the DISCO-Sensors example so that the press of the button changes the sensor being read

Dependencies:   BSP_B-L475E-IOT01 mbed

main.cpp

Committer:
vicara
Date:
2018-11-10
Revision:
2:b677fab4e408
Parent:
1:c29615f4e926

File content as of revision 2:b677fab4e408:

#include "mbed.h"

// Sensors drivers present in the BSP library
#include "stm32l475e_iot01_tsensor.h"
#include "stm32l475e_iot01_hsensor.h"
#include "stm32l475e_iot01_psensor.h"
#include "stm32l475e_iot01_magneto.h"
#include "stm32l475e_iot01_gyro.h"
#include "stm32l475e_iot01_accelero.h"

DigitalOut led(LED1);
InterruptIn button(USER_BUTTON);
int btn_status = 0;

void toggle() {
    btn_status = (btn_status + 1) % 6;
}

int main()
{
    float sensor_value = 0;
    int16_t pDataXYZ[3] = {0};
    float pGyroDataXYZ[3] = {0};
    button.rise(&toggle);

    BSP_TSENSOR_Init();
    BSP_HSENSOR_Init();
    BSP_PSENSOR_Init();

    BSP_MAGNETO_Init();
    BSP_GYRO_Init();
    BSP_ACCELERO_Init();

    while(1) {

        led = 1;
        switch(btn_status){
            case 0:
                sensor_value = BSP_TSENSOR_ReadTemp();
                printf("TEMPERATURE = %.2f degC\n\n", sensor_value);
                break;
            case 1:
                sensor_value = BSP_HSENSOR_ReadHumidity();
                printf("HUMIDITY    = %.2f %%\n\n", sensor_value);
                break;
            case 2:
                sensor_value = BSP_PSENSOR_ReadPressure();
                printf("PRESSURE is = %.2f mBar\n\n", sensor_value);
                break;
            case 3:
                BSP_MAGNETO_GetXYZ(pDataXYZ);
                printf("MAGNETO_X = %d\n", pDataXYZ[0]);
                printf("MAGNETO_Y = %d\n", pDataXYZ[1]);
                printf("MAGNETO_Z = %d\n\n", pDataXYZ[2]);
                break;
            case 4:
                BSP_GYRO_GetXYZ(pGyroDataXYZ);                     
                printf("GYRO_X = %.2f\n", pGyroDataXYZ[0]);
                printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]);
                printf("GYRO_Z = %.2f\n\n", pGyroDataXYZ[2]);
                break;
            case 5:
                 BSP_ACCELERO_AccGetXYZ(pDataXYZ);
                printf("ACCELERO_X = %d\n", pDataXYZ[0]);
                printf("ACCELERO_Y = %d\n", pDataXYZ[1]);
                printf("ACCELERO_Z = %d\n\n", pDataXYZ[2]);
                break;
            default:
                printf("\nThis shouldn't have happened!\n\n");   
                break;
        }
        led = 0;

        wait(1);
    }
}