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

Revision:
1:8681eb2b971d
Parent:
0:56acdbca3ea1
Child:
2:527f86cab491
--- a/main.cpp	Sun Nov 11 15:50:33 2018 +0000
+++ b/main.cpp	Wed Nov 14 16:31:32 2018 +0000
@@ -6,7 +6,6 @@
 DigitalOut led2(LED2);
 DigitalOut led3(LED3);
 
-
 InterruptIn button(USER_BUTTON);
 int16_t pDataXYZ[3] = {0};
 
@@ -88,39 +87,41 @@
     return is_X_inRange && is_Y_inRange && is_Z_inRange;
 }
 
+void blink_led(int led1_status, int led2_status, int led3_status, DigitalOut led){
+    printf("%d - %d - %d\n", led1_status, led2_status, led3_status);
+    led1 = led1_status;
+    led2 = led2_status;
+    led3 = led3_status;
+    wait(1);
+    led = !led;
+}
+
+void blink_all(){
+    led1 = 1;
+    led2 = 1;
+    led3 = 1;
+    wait(1);
+    led1 = 0;
+    led2 = 0;
+    led3 = 0;
+}
+
 int main(){
     button.rise(&toggle);
     BSP_ACCELERO_Init();
-    
     while(true){
         BSP_ACCELERO_AccGetXYZ(pDataXYZ);
-        
         if((!is_board_vertical_long() && !is_board_vertical_short() && !is_board_horizontal())){
-//            printf("NONE\n");
-            led1 = 1;
-            led2 = 1;
-            led3 = 1;
+            blink_all();
         } else {
             if(is_board_horizontal() && !is_board_vertical_short() && !is_board_vertical_long()){
-//            printf("Horizontal\n");
-                led1 = 1;
-            } else {
-                led1 = 0;
-            }
-            
-            if(!is_board_horizontal() && is_board_vertical_short() && !is_board_vertical_long()){
-//                printf("Vertical short\n");
-                led2 = 1;
-            } else {
-                led2 = 0;
-            }
-            
-            if(!is_board_horizontal() && !is_board_vertical_short() && is_board_vertical_long()){
-//                printf("Vertical long\n");
-                led3 = 1;
-            } else {
-                led3 = 0;
+                blink_led(1,0,0, led1);
+            } else if(!is_board_horizontal() && is_board_vertical_short() && !is_board_vertical_long()){
+                blink_led(0,1,0, led2);
+            } else if(!is_board_horizontal() && !is_board_vertical_short() && is_board_vertical_long()){
+                blink_led(0,0,1, led3);
             }
         }
+        wait(1);
     }
 }
\ No newline at end of file