I got the structure of the code but did not get every details right

Files at this revision

API Documentation at this revision

Comitter:
hzsun
Date:
Fri Mar 06 02:13:24 2020 +0000
Parent:
6:d58d982c647a
Commit message:
zxzx

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Thu Mar 05 21:13:16 2020 +0000
+++ b/main.cpp	Fri Mar 06 02:13:24 2020 +0000
@@ -1,45 +1,76 @@
 #include "mbed.h"
 #include "MMA8451Q.h"
 
-  PinName const SDA = PTE25;
-  PinName const SCL = PTE24;
-  EventQueue queue;
-  Serial pc(USBTX, USBRX); // tx, rx
-  Thread eventThread ;
-  DigitalOut rled(LED1, 1); //red led
-    DigitalOut gled(LED2,1); //green led
-    DigitalOut bled(LED3,1); //blue led
-  EventFlags event_flags;
-  Timer x;
+PinName const SDA = PTE25;
+PinName const SCL = PTE24;
+EventQueue queue;
+Serial pc(USBTX, USBRX); // tx, rx
+  
+Thread eventThread ;
+DigitalOut rled(LED1); //red led
+DigitalOut gled(LED2); //green led
+DigitalOut bled(LED3); //blue led
+
+
+EventFlags event_flags;
+Timer A1;
 #define MMA8451_I2C_ADDRESS (0x1d<<1)
-  enum pos {up, down, left, right, flat, over, intermediate};
-  volatile int pos = intermediate;  
-  enum state {first, second, third, success, e};
-  volatile int state = first;
-  volatile uint32_t flags_read = 1;
-void seqchecking(){
-    switch(state){
+enum pos {up, down, left, right, flat, over, intermediate};
+volatile int pos = intermediate;
+enum state {first, second, third, success, e};
+volatile int state = first;
+volatile uint32_t flags_read = 1;
+
+void seqchecking()
+{
+    switch(state) {
         case first:
             flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,10000,true);
             pc.printf("flag_read:%d\r\n", flags_read);
             //if(flags_read == -2){rled = 1;}
             //else{
-                if(flags_read == 4){if(flags_read == -2){state = second; x.reset();pc.printf("turn right please");}}
-                if(flags_read != 4 && x.read() < 10){state = e;}
-                if((flags_read != 4 && flags_read != 6) && x.read() < 10){state = e;pc.printf("b");} 
+            if(flags_read == 4) {
+                if(flags_read == -2) {
+                    state = second;
+                    A1.reset();
+                    pc.printf("turn right please");
+                }
+            }
+            if(flags_read != 4 && A1.read() < 10) {
+                state = e;
+            }
+            if((flags_read != 4 && flags_read != 6) && A1.read() < 10) {
+                state = e;
+                pc.printf("b");
+            }
             //}
             break;
         case second:
             flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,6000,true);
-            if(flags_read != 3 && x.read() < 2){state = e;}
-            if(flags_read == 0 && x.read() >= 2){state = second; x.reset();pc.printf("turn upward please");}
-            if((flags_read != 3 && flags_read != 0)){state = e;}
+            if(flags_read != 3 && A1.read() < 2) {
+                state = e;
+            }
+            if(flags_read == 0 && A1.read() >= 2) {
+                state = second;
+                A1.reset();
+                pc.printf("turn upward please");
+            }
+            if((flags_read != 3 && flags_read != 0)) {
+                state = e;
+            }
             break;
         case third:
             flags_read = event_flags.wait_any(up|down|right|left|over|flat|intermediate,8000,true);
-            if(flags_read != 0 && x.read() < 4){state = e;}
-            if(flags_read == 4 && x.read() >= 4){state = second; x.reset();}
-            if((flags_read != 4 && flags_read != 0)){state = e;}
+            if(flags_read != 0 && A1.read() < 4) {
+                state = e;
+            }
+            if(flags_read == 4 && A1.read() >= 4) {
+                state = second;
+                A1.reset();
+            }
+            if((flags_read != 4 && flags_read != 0)) {
+                state = e;
+            }
             break;
         case success:
             gled = 0;
@@ -48,9 +79,9 @@
             rled = 0;
             event_flags.wait_any(flat, osWaitForever, true);
             state = first;
-            x.reset();
+            A1.reset();
             break;
-    } 
+    }
 }
 int main(void)
 {
@@ -110,157 +141,44 @@
                     event_flags.set(intermediate);
                 }
                 break;
-            case up:
-                if(x > 0.9){ //if the board is in position down
-                    pos = down;
-                    pc.printf("Down\n");// print down on pc screen
-                }
-                if(y > 0.9){ //if the board is in position left
-                    pos = left;
-                    pc.printf("left\n");// print left on pc screen
-                }
-                if(y < -0.9){ //if the board is in position right
-                    pos = right;
-                    pc.printf("right\n");// print right on pc screen
-                }
-                if(z > 0.9){ //if the board is in position flat
-                    pos = flat;
-                    pc.printf("Flat\n");// print flat on pc screen
-                }
-                if(z < -0.9){ //if the board is in position over
-                    pos = over; 
-                    pc.printf("Over\n");// print over on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+                        case flat:
+                if(z<0.8) {
+                    pos=intermediate;
                 }
                 break;
-            case down:
-                if(x < -0.9){ //if the board is in position up
-                    pos = up;
-                    pc.printf("Up\n"); // print up on pc screen
-                }
-                if(y > 0.9){ //if the board is in position left
-                    pos = left;
-                    pc.printf("left\n");// print left on pc screen
-                }
-                if(y < -0.9){ //if the board is in position right
-                    pos = right;
-                    pc.printf("right\n");// print right on pc screen
-                }
-                if(z > 0.9){ //if the board is in position flat
-                    pos = flat;
-                    pc.printf("Flat\n");// print flat on pc screen
-                }
-                if(z < -0.9){ //if the board is in position over
-                    pos = over; 
-                    pc.printf("Over\n");// print over on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+
+            case over:
+                if(z>-0.8) {
+                    pos=intermediate;
                 }
                 break;
+
             case left:
-                if(x > 0.9){ //if the board is in position down
-                    pos = down;
-                    pc.printf("Down\n"); // print up on pc screen
-                }
-                if(x < -0.9){ //if the board is in position up
-                    pos = up;
-                    pc.printf("Up\n");// print down on pc screen
-                }
-                if(y < -0.9){ //if the board is in position right
-                    pos = right;
-                    pc.printf("right\n");// print right on pc screen
-                }
-                if(z > 0.9){ //if the board is in position flat
-                    pos = flat;
-                    pc.printf("Flat\n");// print flat on pc screen
-                }
-                if(z < -0.9){ //if the board is in position over
-                    pos = over; 
-                    pc.printf("Over\n");// print over on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+                if(y>-0.8) {
+                    pos=intermediate;
                 }
                 break;
+
             case right:
-                if(x > 0.9){ //if the board is in position down
-                    pos = down;
-                    pc.printf("Down\n"); // print up on pc screen
-                }
-                if(x < -0.9){ //if the board is in position up
-                    pos = up;
-                    pc.printf("Up\n");// print down on pc screen
-                }
-                if(y > 0.9){ //if the board is in position left
-                    pos = left;
-                    pc.printf("left\n");// print left on pc screen
-                }
-                if(z > 0.9){ //if the board is in position flat
-                    pos = flat;
-                    pc.printf("Flat\n");// print flat on pc screen
-                }
-                if(z < -0.9){ //if the board is in position over
-                    pos = over; 
-                    pc.printf("Over\n");// print over on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+                if(y<0.8) {
+                    pos=intermediate;
                 }
                 break;
-            case flat:
-                if(x > 0.9){ //if the board is in position down
-                    pos = down;
-                    pc.printf("Down\n"); // print up on pc screen
-                }
-                if(x < -0.9){ //if the board is in position up
-                    pos = up;
-                    pc.printf("Up\n");// print down on pc screen
-                }
-                if(y > 0.9){ //if the board is in position left
-                    pos = left;
-                    pc.printf("left\n");// print left on pc screen
-                }
-                if(y < -0.9){ //if the board is in position right
-                    pos = right;
-                    pc.printf("right\n");// print right on pc screen
-                }
-                if(z < -0.9){ //if the board is in position over
-                    pos = over; 
-                    pc.printf("Over\n");// print over on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+
+            case up:
+                if(x<0.8) {
+                    pos=intermediate;
                 }
                 break;
-            case over:
-                if(x > 0.9){ //if the board is in position down
-                    pos = down;
-                    pc.printf("Down\n"); // print up on pc screen
-                }
-                if(x < -0.9){ //if the board is in position up
-                    pos = up;
-                    pc.printf("Up\n");// print down on pc screen
-                }
-                if(y > 0.9){ //if the board is in position left
-                    pos = left;
-                    pc.printf("left\n");// print left on pc screen
-                }
-                if(y < -0.9){ //if the board is in position right
-                    pos = right;
-                    pc.printf("right\n");// print right on pc screen
-                }
-                if(z > 0.9){ //if the board is in position flat
-                    pos = flat;
-                    pc.printf("Flat\n");// print flat on pc screen
-                }
-                if(z<0.8 && z>-0.8 && x<0.8 && x>-0.8 && y<0.8 && y>-0.8){
-                    pos = intermediate;
+
+            case down:
+                if(x>-0.8) {
+                    pos=intermediate;
                 }
                 break;
         }
+
+        }
             
     }
 }