Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card

Dependencies:   BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed

Revision:
6:d1594fd2ec5a
Parent:
5:e36e0538a903
Child:
7:cca801103b86
--- a/main.cpp	Tue Sep 23 18:24:19 2014 +0000
+++ b/main.cpp	Tue Sep 23 23:42:32 2014 +0000
@@ -104,56 +104,16 @@
     //vector<LineData> param;
     vector<ConditionOp> paramCondition;
  
-/*
-    int val1, val2, val3, val4;
-    LineData temp;
-    temp.numWords = 3;
-    temp.word[1] = "val";
-    temp.word[2] = "1";
-    while (true){
-        
-        temp.word[0] = "pin1";
-        val1 = interpretCommand(selectedFile, temp);
-        
-        temp.word[0] = "pin2";
-        val2 = interpretCommand(selectedFile, temp);
-        
-        temp.word[0] = "pin3";
-        val3 = interpretCommand(selectedFile, temp);
-        
-        temp.word[0] = "pin4";
-        val4 = interpretCommand(selectedFile, temp);
-    
-        lcd.cls(); //clear the display 
-        lcd.setAddress(0,0);
-        lcd.printf("Pin1: %d", val1);
-        lcd.setAddress(0,1);
-        lcd.printf("Pin2: %d", val2);
-        lcd.setAddress(0,2);
-        lcd.printf("Pin3: %d", val3);
-        lcd.setAddress(0,3);
-        lcd.printf("Pin4: %d", val4);
-        wait(0.2);
-    }*/
     
     //Fill the param Vector with Line structs of each individual device, this way we can send the Line struct to the appropriate interpret function without modification within the function itself
     int i = 2, numParam = 0, paramNumWords = 0;
     for (i = 2; i < lineData.numWords; i++){
-           
-           lcd.setAddress(0,3);
-    lcd.printf("Test2");
-    
-        /*lcd.setAddress(0,2);
-        lcd.printf("i: %d, nW-1: %d ", i, (lineData.numWords - 1));
-        wait(2);*/
-        
+                       
         // if the word is not an AND or an OR, it must mean it's for the specific function
         // set the current parameter's next word to be equal to the current word we're checking
         // increase number of words that the parameter has
         if (lineData.word[i] != "AND" && lineData.word[i] != "xAND" && lineData.word[i] != "OR" && lineData.word[i] != "xOR"){
-//            lcd.setAddress(0,3);
-//            lcd.printf("%dlD[%d]: %s", numParam, i, lineData.word[i]);
-//            wait(2);
+
             //tempLineData.word[paramNumWords] = lineData.word[i];
             param[numParam].word[paramNumWords] = lineData.word[i];
             paramNumWords++;
@@ -197,7 +157,7 @@
 
     vector<ConditionOp> combinedCondition;
     ConditionOp tempCombinedCondition;
-    int j = 0, k = 0, returnValue = -1;
+    int j = 0, k = 0;
     for (j = 0; j < numParam; j++){
         paramCondition[j].value = interpretCommand(selectedFile, param[j]);
     }
@@ -207,33 +167,31 @@
     enum ConditionType prevCondition = NONE; 
     int first = 1, last = 0;
     for (k = 0; k < numParam; k++){
-/*
-lcd.setAddress(0,3);
-                    lcd.printf("k: %d       ", k);
-                    wait(1);*/
-                    
+
         if (k == numParam - 1)
             last = 1;
             
        if (!last && (prevCondition != AND || prevCondition != OR)){
             if (paramCondition[k].op != xAND && paramCondition[k].op != xOR && paramCondition[k + 1].op != xAND && paramCondition[k + 1].op != xOR){
+                lcd.setAddress(0,3);
+                lcd.printf("Test 0.1     ");
+                wait(1);
                 
-               /* lcd.setAddress(0,3);
-                    lcd.printf("Test 0.1     ");
-                    wait(1);*/
-                    
                 //AND
                 if (paramCondition[k].op == AND){
+                     lcd.setAddress(0,3);
+                    lcd.printf("Test 1.1     ");
+                    wait(1);
                     if (!first && prevCondition != xAND && prevCondition != xOR){
-                       /*   lcd.setAddress(0,3);
-                    lcd.printf("Test 1.6     ");
-                    wait(1);*/
+                    lcd.setAddress(0,3);
+                    lcd.printf("Test 1.11     ");
+                    wait(1);
                         combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value;
-                    }
+                        }
                     else if (first || prevCondition == xAND || prevCondition == xOR){
-                        /*  lcd.setAddress(0,3);
-                    lcd.printf("Test 1.5     ");
-                    wait(1);*/
+                        lcd.setAddress(0,3);
+                    lcd.printf("Test 1.12     ");
+                    wait(1);
                         tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; 
                         combinedCondition.push_back(tempCombinedCondition);
                         first = 0;
@@ -243,15 +201,19 @@
                 
                 //OR
                 else if (paramCondition[k].op == OR){
+                    lcd.setAddress(0,3);
+                    lcd.printf("Test 1.2     ");
+                    wait(1);
                     if (!first && prevCondition != xAND && prevCondition != xOR){
+                        lcd.setAddress(0,3);
+                    lcd.printf("Test 1.3     ");
+                    wait(1);
                         combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value;
-                        /*lcd.setAddress(0,3);
-                    lcd.printf("Test 1.1     ");
-                    wait(1);*/
-                    }else if (first || prevCondition == xAND || prevCondition == xOR){
-                       /* lcd.setAddress(0,3);
-                    lcd.printf("Test 1.2     ");
-                    wait(1);*/
+                        }
+                    else if (first || prevCondition == xAND || prevCondition == xOR){
+                        lcd.setAddress(0,3);
+                    lcd.printf("Test 1.4     ");
+                    wait(1);
                         tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; 
                         combinedCondition.push_back(tempCombinedCondition);
                         first = 0;
@@ -260,58 +222,68 @@
                 }
             }
             
+            // first value is something, not exclusive, but next values are exclusive
+            else if (first && (paramCondition[k].op == AND || paramCondition[k].op == OR) && (paramCondition[k + 1].op == xAND || paramCondition[k + 1].op == xOR)){
+                lcd.setAddress(0,3);
+                lcd.printf("Test 1     ");
+                wait(1);
+                tempCombinedCondition.value = paramCondition[k].value;
+                tempCombinedCondition.op = paramCondition[k].op;
+                combinedCondition.push_back(tempCombinedCondition);
+                prevCondition = paramCondition[k].op;
+                first = 0;
+                lcd.setAddress(0,3);
+                lcd.printf("1cCval: %d   ", combinedCondition.back().value);
+                wait(1);
+            }
+            
             else{   
-                      /*lcd.setAddress(0,3);
-                    lcd.printf("Test 0.2     ");
-                    wait(1);*/
+                /*lcd.setAddress(0,3);
+                lcd.printf("Test 0.2     ");
+                wait(1);*/
                 //xAND
                 if (paramCondition[k].op == xAND && prevCondition == xAND){
-                    /*lcd.setAddress(0,3);
-                    lcd.printf("Test 2     ");
-                    wait(1);*/
+                        lcd.setAddress(0,3);
+                    lcd.printf("Test 2.1     ");
+                    wait(1);
                     combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value;
                     prevCondition = xAND;
                 }
                 else if (paramCondition[k].op == xAND && prevCondition != xAND){
-                    /*lcd.setAddress(0,3);
-                    lcd.printf("Test 1     ");
-                    wait(1);*/
+                     lcd.setAddress(0,3);
+                    lcd.printf("Test 2.2    ");
+                    wait(1);
                     tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; 
                     combinedCondition.push_back(tempCombinedCondition);
                     prevCondition = xAND;
                 }
-                /*else if (paramCondition[k].op != xAND && prevCondition == xAND){
-                    combinedCondition.back().op = paramCondition[k].op;
-                    prevCondition = xAND;
-                }*/
                 
                 //xOR
                 else if (paramCondition[k].op == xOR && prevCondition == xOR){
-                      /*lcd.setAddress(0,3);
-                    lcd.printf("Test 1.3     ");
-                    wait(1);*/
+                     lcd.setAddress(0,3);
+                    lcd.printf("Test 2.3     ");
+                    wait(1);
                     combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value;
                     prevCondition = xOR;
+                    lcd.setAddress(0,3);
+                    lcd.printf("2.3cCval: %d   ", combinedCondition.back().value);
+                    wait(1);
                 }
                 else if (paramCondition[k].op == xOR && prevCondition != xOR){
-                     /* lcd.setAddress(0,3);
-                    lcd.printf("Test 1.4     ");
-                    wait(1);*/
+                     lcd.setAddress(0,3);
+                    lcd.printf("Test 2.4     ");
+                    wait(1);
                     tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; 
                     combinedCondition.push_back(tempCombinedCondition);
                     prevCondition = xOR;
+                    lcd.setAddress(0,3);
+                    lcd.printf("2.4cCval: %d   ", combinedCondition.back().value);
+                    wait(1);
                 }
-                /*else if (paramCondition[k].op != xOR && prevCondition == xOR){
-                    combinedCondition.back().op = paramCondition[k].op;
-                    prevCondition = xOR;
-                }*/
                 
                 // Since the k + 1 value is included in the xAND or xOR exclusively, skip checking that value, and add the appropriate AND / OR as the
                 // operator of this exclusive xAND / xOR set
                 if ((paramCondition[k + 1].op == AND || paramCondition[k + 1].op == OR) && (prevCondition == xAND || prevCondition == xOR)){
-                    /*lcd.setAddress(0,3);
-                    lcd.printf("Test 3     ");
-                    wait(1);*/
                     combinedCondition.back().op = paramCondition[k + 1].op;
                     k++;
                 }
@@ -319,12 +291,12 @@
             }
        }
         
+        //else if (last && (prevCondition != AND || prevCondition != OR)){
+            
+        
         //the last value was not included in any combination, since directly before the last value was an xAND / xOR set that
         // included the very last AND / OR as the set's operator
-        else{
-              /*lcd.setAddress(0,3);
-                lcd.printf("Test 5     ");
-                wait(1);*/
+        else if (last && (prevCondition != AND || prevCondition != OR)){
             tempCombinedCondition.value = paramCondition[k].value;
             tempCombinedCondition.op = NONE; 
             combinedCondition.push_back(tempCombinedCondition);
@@ -334,6 +306,11 @@
         tempCombinedCondition = ConditionOp();
     }
         
+        
+        lcd.setAddress(0,3);
+                lcd.printf("cCsize: %d   ", combinedCondition.size());
+                wait(1);
+                
     // run through all values in the combined Condition vector, AND'ing / OR'ing as appropriate
     // in the end, the last value in the array should be the final condition of the Condition statement... whether it was successful or failed
     for (i = 0; i < (combinedCondition.size() - 1); i++){
@@ -345,16 +322,14 @@
 
     int conditionSuccess = combinedCondition.back().value; //value is the success(1) or failure(0) of the condition statement
 
-
-
     int checkEnd = 0;
     if (!conditionSuccess){
-        lcd.setAddress(0,3);
-        lcd.printf("CONDITION FAILURE");
-        wait(1);
+
         while (checkEnd != 4){
             
-            getNextLine(selectedFile, lineData); 
+            getNextLine(selectedFile, lineData);
+            
+            // check if the first word is an end command (avoids interpreting functions that perform actions)
             if (lineData.word[0].compare("end") == 0)   
                 checkEnd = interpretCommand(selectedFile, lineData);
         
@@ -363,57 +338,12 @@
         }
     }
     
-    lcd.setAddress(0,3);
-    lcd.printf("CONDITION SUCCESS");
-    wait(1);
-    int returnValue2 = 1;
     // Return success as the function either met the condition and will continue from the next line, or
     // failed to meet the condition and ran through the lines inside the condition until "end condition" was found, therefore
     // the program will proceed from the line after the "end condition" line
-    return returnValue2;
+    return 1;
 }      
-    /*
-    for (k = 0; k < numParam; k++){
-        if (paramCondition[k].op == AND){
-            if (paramCondition[k + 1].op != xAND && paramCondition[k + 1].op != xOR)
-                tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; 
-            else
-                tempCombinedCondition.value = paramCondition[k].value; 
-            
-            tempCombinedCondition.op = AND;
-            combinedCondition.push_back(tempCombinedCondition);
-            prevCondition = AND;
-        }
-        
-        else if (paramCondition[k].op == OR){
-            if (paramCondition[k + 1].op != xAND && paramCondition[k + 1].op != xOR)
-                tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; 
-            else
-                tempCombinedCondition.value = paramCondition[k].value; 
-            
-            tempCombinedCondition.op = OR;
-            combinedCondition.push_back(tempCombinedCondition);
-            prevCondition = OR;
-        }
-        
-        else if (paramCondition[k].op == xAND){
-            if (prevCondition == xAND) // if previous one was also xAND, that means we already have a vector member for this (this last member)
-                combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value;
-            else{ // if the prevCondition was not xAND, then we need to add a new member to the vector
-                tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; 
-                combinedCondition.push_back(tempCombinedCondition);
-            }
-            prevCondition = xAND;
-        }
-        else if (paramCondition[k].op == xOR){
-            tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value;
-            prevCondition = xOR;
-        }
-    }
-    
-    
-    return 1;*/  
- 
+   
 
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
@@ -429,13 +359,17 @@
     sscanf(loopCondition.c_str(), "%d", &loopConditionValue);
     
     int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1;
-    
-    int counter = 0, checkEnd = 0;
-    while (counter < loopConditionValue){
+        
+    lcd.setAddress(0,0);
+    lcd.printf("Cycle 1 of %d", loopConditionValue);
         
-        /*lcd.setAddress(0,3);
-        lcd.printf("BcheckEnd: %d        ", checkEnd);
-        wait(1);*/
+    Timer cycleTimer;
+    float totalLoopTime = 0;
+    cycleTimer.reset();
+    cycleTimer.start();
+       
+    int counter = 1, checkEnd = 0;
+    while (counter <= loopConditionValue){
         
         getNextLine(selectedFile, lineData); 
         
@@ -450,26 +384,37 @@
         }
            
         checkEnd = interpretCommand(selectedFile, lineData);
-            
-        /*lcd.setAddress(0,3);
-        lcd.printf("AcheckEnd: %d        ", checkEnd);
-        wait(1);*/
-        
-        
-        /*
-        lcd.setAddress(0,3);
-        lcd.printf("checkEnd: %d        ", checkEnd);
-            wait(2);*/
+
         //Increase the loop counter and go back to the beginning of the loop
         if (checkEnd == 3){
-            counter++;
+            /*cycleTimer.stop();
+            lcd.setAddress(0,0);
+            lcd.printf("tLT: %d, tR", cycleTimer.read());
+            wait(2);*/
+            
+            //Output the Avg Cycle Time
+            cycleTimer.stop(); 
+            totalLoopTime += cycleTimer.read();
             
-            lcd.setAddress(0,2);
-            lcd.printf("Loop Cycle %d of %d", counter, loopConditionValue);
+           /* lcd.setAddress(0,0);
+            lcd.printf("tLT: %d, tR", cycleTimer.read());
+            wait(2);*/
+            
+            lcd.setAddress(0,1);
+            lcd.printf("Avg t(sec): %1.3f", (totalLoopTime / counter));
+            
+            //Output Cycle Number
+            counter++;
+            lcd.setAddress(0,0);
+            lcd.printf("Cycle %d of %d", counter, loopConditionValue);
             
             fseek(selectedFile, loopStartAddress, SEEK_SET);
             lineData.lineNumber = loopLineNumber - 2;
             checkEnd = 0;
+            
+            //Restart the timer for the next loop
+            cycleTimer.reset();
+            cycleTimer.start();
         }
     }
                 
@@ -583,7 +528,7 @@
     
     fullInit(); //Initialize anything that's required to run the code (LCD)
     
-    struct LineData lineData;
+    LineData lineData;
     resetLineData(lineData);
 
         /******************************************************************************/
@@ -606,7 +551,7 @@
         lcd.setAddress(0,2);
         lcd.printf("Num Programs = %d", numTextFiles);
         
-        uint8_t lastButState;
+        uint8_t lastButState = 0;
         lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on
     
         selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen
@@ -653,7 +598,7 @@
         
         lcd.cls(); //clear the display   
         lcd.setAddress(0,0);
-        lcd.printf("Program: %s", selectedFileName);
+        //lcd.printf("Program: %s", selectedFileName);
 
         
         /******************************************************************************/