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:
1:5731f31f96be
Parent:
0:22618cf06f45
Child:
2:3e7baa3e3fec
--- a/main.cpp	Tue Sep 16 15:28:59 2014 +0000
+++ b/main.cpp	Tue Sep 16 18:27:41 2014 +0000
@@ -24,6 +24,8 @@
 //Device devices[15];
 DeviceData devices[15];
 
+FrontPanelButtons buttons(&i2c);
+
 //extern "C" void mbed_reset(); //enable software reset of code
 /*
 http://stackoverflow.com/questions/3081289/how-to-read-a-line-from-a-text-file-in-c-c
@@ -32,6 +34,16 @@
 recurrsive
 */
 
+
+
+int interpretCommand(FILE *);
+int loopCommand(FILE *);
+
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/***************************                        <FUNCTION: resetLineData>                            **************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
 void resetLineData(){
     
     lineData.fullLine = NULL;
@@ -39,38 +51,14 @@
     lineData.numWords = 0;
 }
 
-FrontPanelButtons buttons(&i2c);
-
-
-/*
-    Make LOOP function
-    - All you need to do is remember (val = ftell(fp)) the beginning of the line you're currently reading.
-    - If it turns out to be a LOOP call, then you simply send that remembered value to the LOOP function, and it will continue to LOOP back to that position until the condition is met.
-    
-    
-    func LOOP (FILE *fp, int loopStart){
-        
-        string conditions;
-        fseek(fp, loopStart, SEEK_SET);
-        fgets(conditions, MAX_LINE_LENGTH, fp);
-        
-        int numCycles = interpretLoopCondtions();
-        
-        int i = 0;
-        int checkEndLoop = 0;
-        while (i < numCycles) {
-            
-            checkEndLoop = interpretLine(fp); //if end Loop return ____ (1?), return 0 on success
-            if (checkEndLoop) //if it's not the end of the loop, the counter will not increase and we'll just interpret the next line
-                i++;
-        }
-    } //loop conditions met, return to main code
-    
-    
-    */
     
 
-    
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/************************                        <FUNCTION: cyclePrograms>                            *****************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+   
 int cyclePrograms(string files[], int SIZE, int currIndex, int direction){
     
     int nextIndex = 0;
@@ -102,7 +90,13 @@
     return nextIndex; // Return the file index in the Array         
 }
             
-            
+
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/**************************                        <FUNCTION: resize_StringArr>                            ************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+          
 string* resize_StringArr(string oldArr[], int newSize) {
     
     string newArr[newSize];
@@ -112,6 +106,178 @@
 }
     
 
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/**************************                        <FUNCTION: loopCommand>                            *****************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+
+int loopCommand(FILE *selectedFile){
+        
+    char *loopCondition = lineData.word[1];
+    int loopConditionValue = atoi(loopCondition);
+    int loopStartAddress = 0, loopLineNumber = 0;
+    int firstLineOfLoop = 1;
+    
+    int counter = 0;
+    while (counter < loopConditionValue){
+        
+        getNextLine(selectedFile);
+        /*lcd.setAddress(0,2);
+        lcd.printf("wrd1: %s", lineData.word[0]);
+        lcd.setAddress(0,3);
+        lcd.printf("TEST012   ");*/
+    
+        int checkEnd = interpretCommand(selectedFile);
+        /*lcd.setAddress(0,3);
+        lcd.printf("TEST022   ");
+        wait(2);*/
+            
+        if (firstLineOfLoop){
+            loopStartAddress = lineData.lineAddress; //Save the Line Address
+            loopLineNumber = lineData.lineNumber;    //Save the Line Number
+            firstLineOfLoop = 0;
+           /* lcd.setAddress(0,2);
+            lcd.printf("ldLN: %d, ldLA: %d", lineData.lineNumber, lineData.lineAddress);
+            lcd.setAddress(0,3);
+            lcd.printf("lln: %d, lsa: %d", loopLineNumber, loopStartAddress);
+            wait(3);*/
+        }
+        
+        //Increase the loop counter and go back to the beginning of the loop
+        if (checkEnd == 1){
+            lcd.setAddress(0,2);
+            lcd.printf("%dldLN: %d, ldLA: %d", counter, lineData.lineNumber, lineData.lineAddress);
+            lcd.setAddress(0,3);
+            lcd.printf("%dlln: %d, lsa: %d", counter, loopLineNumber, loopStartAddress);
+            wait(3);
+        
+            counter++;
+            fseek(selectedFile, loopStartAddress, SEEK_SET);
+            lineData.lineNumber = loopLineNumber - 1;
+        }
+    }
+    
+    lcd.setAddress(0,3);
+    lcd.printf("TEST1   ");
+            
+    return 1;
+ }   
+    
+    /*
+    Make LOOP function
+    - All you need to do is remember (val = ftell(fp)) the beginning of the line you're currently reading.
+    - If it turns out to be a LOOP call, then you simply send that remembered value to the LOOP function, and it will continue to LOOP back to that position until the condition is met.
+    
+    
+    func LOOP (FILE *fp, int loopStart){
+        
+        string conditions;
+        fseek(fp, loopStart, SEEK_SET);
+        fgets(conditions, MAX_LINE_LENGTH, fp);
+        
+        int numCycles = interpretLoopCondtions();
+        
+        int i = 0;
+        int checkEndLoop = 0;
+        while (i < numCycles) {
+            
+            checkEndLoop = interpretLine(fp); //if end Loop return ____ (1?), return 0 on success
+            if (checkEndLoop) //if it's not the end of the loop, the counter will not increase and we'll just interpret the next line
+                i++;
+        }
+    } //loop conditions met, return to main code
+    
+    
+    */
+
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/*************************                        <FUNCTION: interpretCommand>                            *************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+
+int interpretCommand(FILE *selectedFile){
+    
+    if (strncmp(lineData.word[0],"device", 6) == 0){
+    
+        int i = 0, deviceFound = -1;
+        for (i = 0; i < numDevices; i++)
+            if (strncmp(lineData.word[2], DeviceNames[i], strlen(DeviceNames[i])) == 0)
+                deviceFound = i;
+        
+        //if the device type does not match any known type, error out
+        if (deviceFound == -1){
+            //Error Out since the device Name was not matched with anything *************************   
+        }
+        
+        //Add device to the array of devices and initialize it
+        else
+            addDevice(deviceFound);    
+    }
+    
+    else if (strncmp(lineData.word[0],"delay", 5) == 0){
+        char *duration = lineData.word[1];
+        int durationValue = atoi(duration);
+        
+        if (durationValue){
+            timer.reset();
+            timer.start();
+            while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration
+            timer.stop(); //Stop the Timer
+        }
+        else{
+            //Error Out
+            return -1;
+        }
+    }
+    
+    else if (strncmp(lineData.word[0],"loop", 4) == 0){
+        int checkLoopEnd = loopCommand(selectedFile);
+        lcd.setAddress(0,3);
+        lcd.printf("TEST2   ");
+        if (checkLoopEnd == 1)
+            return 1;
+    }
+            
+    else if (strncmp(lineData.word[0],"end", 3) == 0){
+        if (strncmp(lineData.word[1],"program", 7) == 0)
+            return 0;
+        else if (strncmp(lineData.word[1],"loop", 4) == 0) 
+            return 1;
+    }
+    
+    //not a keyword so check if it's a localName for a device
+    else{
+    
+        int i = 0, deviceFound = -1;
+        for (i = 0; i < currNumDevices; i++){
+            if (strncmp(lineData.word[0], devices[i].name, strlen(devices[i].name)) == 0)
+                deviceFound = i;
+        }
+                
+        //no device was found that matched the local name, and this is also the last error check, meaning it can match no other potential keywords
+        if (deviceFound == -1){
+            lcd.setAddress(0,3);
+            lcd.printf("Final ERROR!");
+            wait(10);
+        }
+        
+        //Local Name matches a device, send line to that device in order to process the functionality
+        else
+            addDevice(deviceFound);//devices[deviceFound].interpret(); 
+    }  
+    
+    return -1;
+}
+
+
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+/******************************                        <FUNCTION: main>                            ********************************/
+/**********************************************************************************************************************************/
+/**********************************************************************************************************************************/
+
 int main() {
     
     fullInit(); //Initialize anything that's required to run the code (LCD)
@@ -127,7 +293,7 @@
         numTextFiles = getFileNamesWithoutExt(tempTextFiles, filenames);
         string *textFiles = resize_StringArr(tempTextFiles, numTextFiles); //Resize Array
         
-        //delete [] tempTextFiles; //free previous array
+        //delete[] tempTextFiles; //free previous array
             
     
         /******************************************************************************/
@@ -199,68 +365,13 @@
         while (!endOfFile){
         
             getNextLine(selectedFile);
-                    
-            if (strncmp(lineData.word[0],"device", 6) == 0){
-    
-                int i = 0, deviceFound = -1;
-                for (i = 0; i < numDevices; i++)
-                    if (strncmp(lineData.word[2], DeviceNames[i], strlen(DeviceNames[i])) == 0)
-                        deviceFound = i;
-                
-                //if the device type does not match any known type, error out
-                if (deviceFound == -1){
-                    //Error Out since the device Name was not matched with anything *************************   
-                }
-                
-                //Add device to the array of devices and initialize it
-                else
-                    addDevice(deviceFound);    
-            }
+            int checkEnd = interpretCommand(selectedFile);   
             
-            else if (strncmp(lineData.word[0],"delay", 5) == 0){
-                char *duration = lineData.word[1];
-                int durationValue = atoi(duration);
-                
-                if (durationValue){
-                    timer.reset();
-                    timer.start();
-                    while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration
-                    timer.stop(); //Stop the Timer
-                }
-                else{
-                    //Error Out
-                }
-            }
-            
-            else if (strncmp(lineData.word[0],"loop", 4) == 0){
-                //Do something for looping     
-            }
-                    
-            else if (strncmp(lineData.word[0],"end", 3) == 0){
-                if (strncmp(lineData.word[1],"program", 7) == 0) 
-                    endOfFile = 1;
-            }
-            
-            //not a keyword so check if it's a localName for a device
-            else{
-    
-                int i = 0, deviceFound = -1;
-                for (i = 0; i < currNumDevices; i++){
-                    if (strncmp(lineData.word[0], devices[i].name, strlen(devices[i].name)) == 0)
-                        deviceFound = i;
-                }
-                        
-                //no device was found that matched the local name, and this is also the last error check, meaning it can match no other potential keywords
-                if (deviceFound == -1){
-                    lcd.setAddress(0,3);
-                    lcd.printf("Final ERROR!");
-                    wait(10);
-                }
-                
-                //Local Name matches a device, send line to that device in order to process the functionality
-                else
-                    addDevice(deviceFound);//devices[deviceFound].interpret(); 
-            }  
+            lcd.setAddress(0,3);
+            lcd.printf("TEST3   ");
+        
+            if (checkEnd == 0)
+                endOfFile = 1;
         }