BlinkPosition
Dependencies: BSP_B-L475E-IOT01 mbed
Revision 2:d51d7f26a4e1, committed 2018-11-16
- Comitter:
- alessioburatti
- Date:
- Fri Nov 16 11:56:52 2018 +0000
- Parent:
- 1:dce931d77528
- Commit message:
- Done
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Fri Nov 09 15:39:17 2018 +0000 +++ b/main.cpp Fri Nov 16 11:56:52 2018 +0000 @@ -1,67 +1,73 @@ #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); +#define X_VALUE pDataXYZ[0] +#define Y_VALUE pDataXYZ[1] +#define Z_VALUE pDataXYZ[2] -int main() -{ - float sensor_value = 0; - int16_t pDataXYZ[3] = {0}; - float pGyroDataXYZ[3] = {0}; +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); - BSP_TSENSOR_Init(); - BSP_HSENSOR_Init(); - - BSP_PSENSOR_Init(); +int16_t pDataXYZ[3] = {0}; + +bool is_in_range_100(int val) { + return val < 100 && val > -100; +} - BSP_MAGNETO_Init(); - BSP_GYRO_Init(); - BSP_ACCELERO_Init(); +bool is_in_range_900_1000(int val) { + return (val < 1050 && val > 950) || (val < -950 && val > -1050); +} - while(1) { +bool board_is_horizontal() { + return is_in_range_100(X_VALUE) && is_in_range_100(Y_VALUE) && is_in_range_900_1000(Z_VALUE); +} - led = 1; +bool board_is_vertical_short() { + return is_in_range_900_1000(X_VALUE) && is_in_range_100(Y_VALUE) && is_in_range_100(Z_VALUE); +} - sensor_value = BSP_TSENSOR_ReadTemp(); - printf("\nTEMPERATURE = %.2f degC\n", sensor_value); - - sensor_value = BSP_HSENSOR_ReadHumidity(); - printf("HUMIDITY = %.2f %%\n", sensor_value); +bool board_is_vertical_long() { + return is_in_range_100(X_VALUE) && is_in_range_900_1000(Y_VALUE) && is_in_range_100(Z_VALUE); +} - sensor_value = BSP_PSENSOR_ReadPressure(); - printf("PRESSURE is = %.2f mBar\n", sensor_value); - - led = 0; - - wait(1); +void blink_led(DigitalOut * led){ + led1 = 0; + led2 = 0; + led3 = 0; + *led = 1; + wait(1); + *led = 0; +} - led = 1; - - BSP_MAGNETO_GetXYZ(pDataXYZ); - printf("\nMAGNETO_X = %d\n", pDataXYZ[0]); - printf("MAGNETO_Y = %d\n", pDataXYZ[1]); - printf("MAGNETO_Z = %d\n", pDataXYZ[2]); +void blink_all(){ + led1 = 1; + led2 = 1; + led3 = 1; + wait(1); + led1 = 0; + led2 = 0; + led3 = 0; +} - BSP_GYRO_GetXYZ(pGyroDataXYZ); - printf("\nGYRO_X = %.2f\n", pGyroDataXYZ[0]); - printf("GYRO_Y = %.2f\n", pGyroDataXYZ[1]); - printf("GYRO_Z = %.2f\n", pGyroDataXYZ[2]); - +int main(){ + BSP_ACCELERO_Init(); + while(true){ BSP_ACCELERO_AccGetXYZ(pDataXYZ); - printf("\nACCELERO_X = %d\n", pDataXYZ[0]); - printf("ACCELERO_Y = %d\n", pDataXYZ[1]); - printf("ACCELERO_Z = %d\n", pDataXYZ[2]); - - led = 0; - + bool is_vertical_long = board_is_vertical_long(); + bool is_vertical_short = board_is_vertical_short(); + bool is_horizontal = board_is_horizontal(); + + if (is_vertical_long && !is_vertical_short && !is_horizontal) { + blink_led(&led1); + } else if (!is_vertical_long && is_vertical_short && !is_horizontal) { + blink_led(&led2); + } else if (!is_vertical_long && !is_vertical_short && is_horizontal) { + blink_led(&led3); + } else { + blink_all(); + } wait(1); - } }