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:
2:3e7baa3e3fec
Parent:
1:5731f31f96be
Child:
3:078e9a1e8be3
--- a/main.cpp	Tue Sep 16 18:27:41 2014 +0000
+++ b/main.cpp	Thu Sep 18 23:38:11 2014 +0000
@@ -7,22 +7,22 @@
 #include "SDFileSystem.h"
 #include "Initialization.hpp"
 //#include "DeviceClasses.h"
-#include "Device.hpp"
-//#include "Motor.hpp"
-//#include "VoltageDriver.hpp"
+//#include "Device.hpp"
+#include "Motor.hpp"
+#include "VoltageDriver.hpp"
 #include "TextFile.h"
 #include <stdio.h>
 #include <string>
 #include <stdlib.h>
-#include <iostream>
 #include <fstream>
 #include <vector>
 using std::string;
 
 //const int MAX_LINE_LENGTH = 100;
-struct Line lineData;
-//Device devices[15];
-DeviceData devices[15];
+//struct Line lineData;
+
+
+//DeviceData devices[15];
 
 FrontPanelButtons buttons(&i2c);
 
@@ -36,19 +36,20 @@
 
 
 
-int interpretCommand(FILE *);
-int loopCommand(FILE *);
+int interpretCommand(FILE *, Line &);
+int loopCommand(FILE *, Line &);
 
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
 /***************************                        <FUNCTION: resetLineData>                            **************************/
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
-void resetLineData(){
+void resetLineData(Line &lineData){
     
-    lineData.fullLine = NULL;
+    //lineData.fullLine.clear();
     lineData.lineNumber = 0;
     lineData.numWords = 0;
+    lineData.lineAddress = 0;
 }
 
     
@@ -59,7 +60,7 @@
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
    
-int cyclePrograms(string files[], int SIZE, int currIndex, int direction){
+int cyclePrograms(vector<string> files, int SIZE, int currIndex, int direction){
     
     int nextIndex = 0;
     switch(direction){
@@ -112,84 +113,41 @@
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
 
-int loopCommand(FILE *selectedFile){
+int loopCommand(FILE *selectedFile, Line &lineData){
         
-    char *loopCondition = lineData.word[1];
-    int loopConditionValue = atoi(loopCondition);
-    int loopStartAddress = 0, loopLineNumber = 0;
-    int firstLineOfLoop = 1;
+    //Get the Condition value for number of times to loop
+    string loopCondition = lineData.word[1];
+    int loopConditionValue = 0;
+    sscanf(loopCondition.c_str(), "%d", &loopConditionValue);
     
-    int counter = 0;
+    int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1;
+    
+    int counter = 0, checkEnd = 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);*/
+        getNextLine(selectedFile, lineData);    
+        checkEnd = interpretCommand(selectedFile, lineData);
             
         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){
+            counter++;
+            
             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++;
+            lcd.printf("Loop Cycle %d of %d", counter, loopConditionValue);
+            
             fseek(selectedFile, loopStartAddress, SEEK_SET);
-            lineData.lineNumber = loopLineNumber - 1;
+            lineData.lineNumber = loopLineNumber - 2;
         }
     }
-    
-    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
-    
-    
-    */
 
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
@@ -197,14 +155,15 @@
 /**********************************************************************************************************************************/
 /**********************************************************************************************************************************/
 
-int interpretCommand(FILE *selectedFile){
-    
-    if (strncmp(lineData.word[0],"device", 6) == 0){
-    
+int interpretCommand(FILE *selectedFile, Line &lineData){
+        
+    if (lineData.word[0].compare("device") == 0){
+
         int i = 0, deviceFound = -1;
-        for (i = 0; i < numDevices; i++)
-            if (strncmp(lineData.word[2], DeviceNames[i], strlen(DeviceNames[i])) == 0)
+        for (i = 0; i < numDevices; i++){
+            if (lineData.word[2].compare(DeviceNames[i]))
                 deviceFound = i;
+        }
         
         //if the device type does not match any known type, error out
         if (deviceFound == -1){
@@ -212,13 +171,18 @@
         }
         
         //Add device to the array of devices and initialize it
-        else
-            addDevice(deviceFound);    
+        else{
+            //For some reason the enum is indexed weirdly, device-- to send correct value
+            deviceFound--;
+            devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData));
+            devices.back()->name = lineData.word[1];             
+        }
     }
     
-    else if (strncmp(lineData.word[0],"delay", 5) == 0){
-        char *duration = lineData.word[1];
-        int durationValue = atoi(duration);
+    else if (lineData.word[0].compare("delay") == 0){
+        string duration = lineData.word[1];
+        int durationValue = 0;
+        sscanf(duration.c_str(), "%d", &durationValue);
         
         if (durationValue){
             timer.reset();
@@ -232,27 +196,27 @@
         }
     }
     
-    else if (strncmp(lineData.word[0],"loop", 4) == 0){
-        int checkLoopEnd = loopCommand(selectedFile);
-        lcd.setAddress(0,3);
-        lcd.printf("TEST2   ");
+    else if (lineData.word[0].compare("loop") == 0){
+        int checkLoopEnd = loopCommand(selectedFile, lineData);
         if (checkLoopEnd == 1)
             return 1;
     }
             
-    else if (strncmp(lineData.word[0],"end", 3) == 0){
-        if (strncmp(lineData.word[1],"program", 7) == 0)
+    else if (lineData.word[0].compare("end") == 0){
+        if (lineData.word[1].compare("program") == 0){
             return 0;
-        else if (strncmp(lineData.word[1],"loop", 4) == 0) 
+        }
+        else if (lineData.word[1].compare("loop") == 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)
+        for (i = 0; i < devices.size(); i++){
+            if (lineData.word[0].compare(devices[i]->name) == 0)
                 deviceFound = i;
         }
                 
@@ -264,8 +228,10 @@
         }
         
         //Local Name matches a device, send line to that device in order to process the functionality
-        else
-            addDevice(deviceFound);//devices[deviceFound].interpret(); 
+        else{
+            //addDevice(deviceFound);
+            devices[deviceFound]->interpret(lineData);
+        }
     }  
     
     return -1;
@@ -282,18 +248,16 @@
     
     fullInit(); //Initialize anything that's required to run the code (LCD)
     
+    struct Line lineData;
+    resetLineData(lineData);
+
         /******************************************************************************/
         /***                 <Get all the Potential Programs>                       ***/
         /******************************************************************************/
         int numTextFiles = 0;
-        string tempTextFiles[25]; //Assuming Maximum of 25 txt files will be on the SD Card
-        lcd.cls(); //clear the display
-        
-        vector<string> filenames = readFileNames("/sd");
-        numTextFiles = getFileNamesWithoutExt(tempTextFiles, filenames);
-        string *textFiles = resize_StringArr(tempTextFiles, numTextFiles); //Resize Array
-        
-        //delete[] tempTextFiles; //free previous array
+        vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card        
+        vector<string> filenames = readFileNames("/sd");    
+        numTextFiles = getFileNamesWithoutExt(textFiles, filenames);
             
     
         /******************************************************************************/
@@ -309,7 +273,7 @@
         
         uint8_t lastButState;
         lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on
-        
+    
         selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen
         while(!fileSelected) {
             
@@ -338,6 +302,7 @@
         strcat(selectedFileFullName, ".txt");
         
         FILE *selectedFile = fopen(selectedFileFullName, "r");
+
         lcd.cls(); //clear the display 
 
         if(selectedFile == NULL) {
@@ -349,7 +314,7 @@
         
     while(1){  
             
-        resetLineData();
+        resetLineData(lineData);
         
         lcd.cls(); //clear the display   
         lcd.setAddress(0,0);
@@ -364,12 +329,9 @@
         
         while (!endOfFile){
         
-            getNextLine(selectedFile);
-            int checkEnd = interpretCommand(selectedFile);   
-            
-            lcd.setAddress(0,3);
-            lcd.printf("TEST3   ");
-        
+            getNextLine(selectedFile, lineData);
+            int checkEnd = interpretCommand(selectedFile, lineData);   
+                    
             if (checkEnd == 0)
                 endOfFile = 1;
         }
@@ -386,10 +348,30 @@
         while(!buttons.readBack());
         
         lcd.setAddress(0,1);
-        rewind(selectedFile);
+        //rewind(selectedFile);
     }
 
-    
+ } 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+ 
+   
     /***** Cycle through txt lines and remember last lines ******/
     /*
     int running = 0, selectedFile = 0;
@@ -442,9 +424,7 @@
         } 
     } 
         */   
-    
-    
-    
+  
     
     
     /******* Select the Program txt File ***********/
@@ -487,19 +467,6 @@
     }
     
     */
-    
-    
-    
-    
- }  
-      
-    
-    
-    
-    
-    
-    
-
 
     /*float speed = 0.5;
     while(1){