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@2:b677fab4e408, 2018-11-10 (annotated)
- Committer:
- vicara
- Date:
- Sat Nov 10 15:22:00 2018 +0000
- Revision:
- 2:b677fab4e408
- Parent:
- 1:c29615f4e926
added dependencies
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vicara | 0:05f2173ffbfc | 1 | #include "mbed.h" |
vicara | 0:05f2173ffbfc | 2 | |
vicara | 0:05f2173ffbfc | 3 | // Sensors drivers present in the BSP library |
vicara | 0:05f2173ffbfc | 4 | #include "stm32l475e_iot01_tsensor.h" |
vicara | 0:05f2173ffbfc | 5 | #include "stm32l475e_iot01_hsensor.h" |
vicara | 0:05f2173ffbfc | 6 | #include "stm32l475e_iot01_psensor.h" |
vicara | 0:05f2173ffbfc | 7 | #include "stm32l475e_iot01_magneto.h" |
vicara | 0:05f2173ffbfc | 8 | #include "stm32l475e_iot01_gyro.h" |
vicara | 0:05f2173ffbfc | 9 | #include "stm32l475e_iot01_accelero.h" |
vicara | 0:05f2173ffbfc | 10 | |
vicara | 0:05f2173ffbfc | 11 | DigitalOut led(LED1); |
vicara | 0:05f2173ffbfc | 12 | InterruptIn button(USER_BUTTON); |
vicara | 0:05f2173ffbfc | 13 | int btn_status = 0; |
vicara | 0:05f2173ffbfc | 14 | |
vicara | 0:05f2173ffbfc | 15 | void toggle() { |
vicara | 0:05f2173ffbfc | 16 | btn_status = (btn_status + 1) % 6; |
vicara | 0:05f2173ffbfc | 17 | } |
vicara | 0:05f2173ffbfc | 18 | |
vicara | 0:05f2173ffbfc | 19 | int main() |
vicara | 0:05f2173ffbfc | 20 | { |
vicara | 0:05f2173ffbfc | 21 | float sensor_value = 0; |
vicara | 0:05f2173ffbfc | 22 | int16_t pDataXYZ[3] = {0}; |
vicara | 0:05f2173ffbfc | 23 | float pGyroDataXYZ[3] = {0}; |
vicara | 0:05f2173ffbfc | 24 | button.rise(&toggle); |
vicara | 0:05f2173ffbfc | 25 | |
vicara | 0:05f2173ffbfc | 26 | BSP_TSENSOR_Init(); |
vicara | 0:05f2173ffbfc | 27 | BSP_HSENSOR_Init(); |
vicara | 0:05f2173ffbfc | 28 | BSP_PSENSOR_Init(); |
vicara | 0:05f2173ffbfc | 29 | |
vicara | 0:05f2173ffbfc | 30 | BSP_MAGNETO_Init(); |
vicara | 0:05f2173ffbfc | 31 | BSP_GYRO_Init(); |
vicara | 0:05f2173ffbfc | 32 | BSP_ACCELERO_Init(); |
vicara | 0:05f2173ffbfc | 33 | |
vicara | 0:05f2173ffbfc | 34 | while(1) { |
vicara | 0:05f2173ffbfc | 35 | |
vicara | 0:05f2173ffbfc | 36 | led = 1; |
vicara | 0:05f2173ffbfc | 37 | switch(btn_status){ |
vicara | 0:05f2173ffbfc | 38 | case 0: |
vicara | 0:05f2173ffbfc | 39 | sensor_value = BSP_TSENSOR_ReadTemp(); |
vicara | 0:05f2173ffbfc | 40 | printf("TEMPERATURE = %.2f degC\n\n", sensor_value); |
vicara | 0:05f2173ffbfc | 41 | break; |
vicara | 0:05f2173ffbfc | 42 | case 1: |
vicara | 0:05f2173ffbfc | 43 | sensor_value = BSP_HSENSOR_ReadHumidity(); |
vicara | 0:05f2173ffbfc | 44 | printf("HUMIDITY = %.2f %%\n\n", sensor_value); |
vicara | 0:05f2173ffbfc | 45 | break; |
vicara | 0:05f2173ffbfc | 46 | case 2: |
vicara | 0:05f2173ffbfc | 47 | sensor_value = BSP_PSENSOR_ReadPressure(); |
vicara | 0:05f2173ffbfc | 48 | printf("PRESSURE is = %.2f mBar\n\n", sensor_value); |
vicara | 0:05f2173ffbfc | 49 | break; |
vicara | 0:05f2173ffbfc | 50 | case 3: |
vicara | 0:05f2173ffbfc | 51 | BSP_MAGNETO_GetXYZ(pDataXYZ); |
vicara | 0:05f2173ffbfc | 52 | printf("MAGNETO_X = %d\n", pDataXYZ[0]); |
vicara | 0:05f2173ffbfc | 53 | printf("MAGNETO_Y = %d\n", pDataXYZ[1]); |
vicara | 0:05f2173ffbfc | 54 | printf("MAGNETO_Z = %d\n\n", pDataXYZ[2]); |
vicara | 0:05f2173ffbfc | 55 | break; |
vicara | 0:05f2173ffbfc | 56 | case 4: |
vicara | 0:05f2173ffbfc | 57 | BSP_GYRO_GetXYZ(pGyroDataXYZ); |
vicara | 0:05f2173ffbfc | 58 | printf("GYRO_X = %.2f\n", pGyroDataXYZ[0]); |
vicara | 0:05f2173ffbfc | 59 | printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]); |
vicara | 0:05f2173ffbfc | 60 | printf("GYRO_Z = %.2f\n\n", pGyroDataXYZ[2]); |
vicara | 0:05f2173ffbfc | 61 | break; |
vicara | 0:05f2173ffbfc | 62 | case 5: |
vicara | 0:05f2173ffbfc | 63 | BSP_ACCELERO_AccGetXYZ(pDataXYZ); |
vicara | 0:05f2173ffbfc | 64 | printf("ACCELERO_X = %d\n", pDataXYZ[0]); |
vicara | 0:05f2173ffbfc | 65 | printf("ACCELERO_Y = %d\n", pDataXYZ[1]); |
vicara | 0:05f2173ffbfc | 66 | printf("ACCELERO_Z = %d\n\n", pDataXYZ[2]); |
vicara | 0:05f2173ffbfc | 67 | break; |
vicara | 0:05f2173ffbfc | 68 | default: |
vicara | 0:05f2173ffbfc | 69 | printf("\nThis shouldn't have happened!\n\n"); |
vicara | 0:05f2173ffbfc | 70 | break; |
vicara | 0:05f2173ffbfc | 71 | } |
vicara | 0:05f2173ffbfc | 72 | led = 0; |
vicara | 0:05f2173ffbfc | 73 | |
vicara | 0:05f2173ffbfc | 74 | wait(1); |
vicara | 0:05f2173ffbfc | 75 | } |
vicara | 0:05f2173ffbfc | 76 | } |