Write an Mbed application that blinks – LED1 when the board is horizontal – LED2 when the board is lying on the long edge – LED3 when the board is lying on the short edge – All LEDs when the board is in none of those positions
Dependencies: BSP_B-L475E-IOT01 mbed
main.cpp
00001 #include "mbed.h" 00002 00003 #include "stm32l475e_iot01_accelero.h" 00004 00005 DigitalOut led1(LED1); 00006 DigitalOut led2(LED2); 00007 DigitalOut led3(LED3); 00008 00009 InterruptIn button(USER_BUTTON); 00010 int16_t pDataXYZ[3] = {0}; 00011 00012 void toggle() { 00013 printf("ACCELERO_X = %d\n", pDataXYZ[0]); 00014 printf("ACCELERO_Y = %d\n", pDataXYZ[1]); 00015 printf("ACCELERO_Z = %d\n\n", pDataXYZ[2]); 00016 } 00017 00018 bool is_board_horizontal() { 00019 return (pDataXYZ[2] < 1030 && pDataXYZ[2] > 950) || (pDataXYZ[2] < -950 && pDataXYZ[2] > -1030); 00020 } 00021 00022 bool is_board_vertical_short() { 00023 return (pDataXYZ[0] < -950 && pDataXYZ[0] > -1030) || (pDataXYZ[0] < 1030 && pDataXYZ[0] > 950); 00024 } 00025 00026 bool is_board_vertical_long() { 00027 return (pDataXYZ[1] < 1030 && pDataXYZ[1] > 950) || (pDataXYZ[1] < -950 && pDataXYZ[1] > -1030); 00028 } 00029 00030 void blink_led(int led1_status, int led2_status, int led3_status, DigitalOut led){ 00031 printf("%d - %d - %d\n", led1_status, led2_status, led3_status); 00032 led1 = led1_status; 00033 led2 = led2_status; 00034 led3 = led3_status; 00035 wait(1); 00036 led = !led; 00037 } 00038 00039 void blink_all(){ 00040 led1 = 1; 00041 led2 = 1; 00042 led3 = 1; 00043 wait(1); 00044 led1 = 0; 00045 led2 = 0; 00046 led3 = 0; 00047 } 00048 00049 int main(){ 00050 button.rise(&toggle); 00051 BSP_ACCELERO_Init(); 00052 while(true){ 00053 BSP_ACCELERO_AccGetXYZ(pDataXYZ); 00054 if((!is_board_vertical_long() && !is_board_vertical_short() && !is_board_horizontal())){ 00055 blink_all(); 00056 } else { 00057 if(is_board_horizontal() && !is_board_vertical_short() && !is_board_vertical_long()){ 00058 blink_led(1,0,0, led1); 00059 } else if(!is_board_horizontal() && is_board_vertical_short() && !is_board_vertical_long()){ 00060 blink_led(0,1,0, led2); 00061 } else if(!is_board_horizontal() && !is_board_vertical_short() && is_board_vertical_long()){ 00062 blink_led(0,0,1, led3); 00063 } 00064 } 00065 wait(1); 00066 } 00067 }
Generated on Fri Aug 12 2022 21:30:01 by 1.7.2