servodisc goodness

Dependencies:   mbed-dev-f303

Revision:
11:16d807d6b9c5
Parent:
6:1143996ac690
--- a/cube.cpp	Sun Feb 04 04:37:05 2018 +0000
+++ b/cube.cpp	Wed Mar 07 19:25:49 2018 +0000
@@ -173,6 +173,15 @@
     return 0;
 }
 
+int check_double_move(int f1, int f2)
+{
+    return (f1 == 0 && f2 == 3) ||
+           (f2 == 0 && f1 == 3) ||
+           (f2 == 1 && f1 == 4) ||
+           (f2 == 1 && f1 == 4) ||
+           (f2 == 2 && f1 == 5) ||
+           (f2 == 2 && f1 == 5);
+}
 
 int states[6] = {0,0,0,0,0,0};
 int move_counts[6] = {0,0,0,0,0,0};
@@ -207,7 +216,7 @@
         cmd->set_and(0,cmd->face); // and off
 
         // check if done
-        if(move_counts[cmd->face] > n_moves)
+        if(move_counts[cmd->face] >= n_moves)
         {
             printf("SEQUENCE DONE!\r\n");
             cmd->set_and(1,cmd->face);
@@ -216,7 +225,44 @@
             cmd->seq = NULL;
             return NULL;
         }
+        
+        // don't check if we're on the second to last move
+        int current_move = move_counts[cmd->face];
+        int second_to_last_move = n_moves - 1;
 
+
+        if( !(current_move >= second_to_last_move))
+        {
+            
+            // check the faces...
+            int face_1 =  cmd->seq->moves[move_counts[cmd->face]].face;
+            int face_2 =  cmd->seq->moves[move_counts[cmd->face] + 1].face;
+            
+            // if they don't match, go to old thing.
+            if(!check_double_move(face_1,face_2)) goto old_thing;
+            
+            // first check the current move...
+            if(cmd->face == face_1)
+                n_turns = cmd->seq->moves[move_counts[cmd->face]].n_turns;
+            else if(cmd->face == face_2)
+                n_turns = cmd->seq->moves[move_counts[cmd->face]+1].n_turns;
+            
+            move_counts[cmd->face]+=2;
+            
+            // rotate if needed
+            if(n_turns) 
+                cmd->rotate(n_turns,cmd->face);
+    
+            // wait...
+            states[cmd->face] = 1;
+            wait_counter[cmd->face] = 0;
+            return NULL;
+            
+        }
+
+old_thing:
+//***************** do the old thing ***************************
+        
         // check how many turns needed
         if(cmd->face == cmd->seq->moves[move_counts[cmd->face]].face)
         {
@@ -235,6 +281,7 @@
         states[cmd->face] = 1;
         wait_counter[cmd->face] = 0;
         return NULL;
+
     }
     else if(states[cmd->face] == 1)
     {