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, committed 2014-09-16
- Comitter:
- mehatfie
- Date:
- Tue Sep 16 18:27:41 2014 +0000
- Parent:
- 0:22618cf06f45
- Child:
- 2:3e7baa3e3fec
- Commit message:
- - Noticed having issues with code in terms of memory(?); - Loop functionality works fine; - Depending on the number of cycles, the code will randomly freeze up. Same thing happens if you extend the number of tasks within the loop, and decrease count
Changed in this revision
--- a/DeviceClasses.h Tue Sep 16 15:28:59 2014 +0000
+++ b/DeviceClasses.h Tue Sep 16 18:27:41 2014 +0000
@@ -246,6 +246,12 @@
wait(3);
*/
+ lcd.setAddress(0,2);
+ lcd.printf("wrd1: %s", lineData.word[0]);
+ lcd.setAddress(0,3);
+ lcd.printf("TEST1 ");
+ wait(1);
+
char *firstWord = lineData.word[0]; //splits the str based on a space delimeter
if (strncmp(firstWord,"device", 6) == 0){
@@ -320,6 +326,12 @@
/******************************************************************************/
else if (strncmp(func,"drive", 5) == 0){
+ lcd.setAddress(0,2);
+ lcd.printf("wrd2: %s", lineData.word[1]);
+ lcd.setAddress(0,3);
+ lcd.printf("TEST2 ");
+ wait(1);
+
if (lineData.numWords != 4){
//Error Check, incorrect number of parameter, error out
return 0;
@@ -329,17 +341,29 @@
char *speed = lineData.word[2];
char *dir = lineData.word[3];
+ lcd.setAddress(0,3);
+ lcd.printf("TEST3 ");
+
//Initialize Convertion Variables if needed
float speedValue;
int dirValue = 0;
+ lcd.setAddress(0,3);
+ lcd.printf("TEST4 ");
+
//Convert string to usable values
//NOTE both atof and atoi functions return 0 if no valid conversion could be performed
speedValue = atof(speed) / 100;
+ lcd.setAddress(0,3);
+ lcd.printf("TEST5 ");
+
if (speedValue <= 0)
return 0; //Error Out because a value gives no functionality or is wrong
+ lcd.setAddress(0,3);
+ lcd.printf("TEST6 ");
+
if (strncmp(dir,"CC", 2) == 0 || strncmp(dir,"cc", 2) == 0)
dirValue = -1; //Turn Clockwise
else if (strncmp(dir,"C", 1) == 0 || strncmp(dir,"c", 1) == 0)
@@ -347,13 +371,26 @@
else
return 0; //Error Out since the parameter is incorrect
+ lcd.setAddress(0,3);
+ lcd.printf("TEST7 ");
+
bridges.drive(getMotor(), dirValue, speedValue); //Turn on the Motor
+
+ lcd.setAddress(0,3);
+ lcd.printf("TEST8 ");
+
}
/******************************************************************************/
/**** <Func: off> ****/
/******************************************************************************/
else if (strncmp(func,"off", 3) == 0){
+ lcd.setAddress(0,2);
+ lcd.printf("wrd2: %s", lineData.word[1]);
+ lcd.setAddress(0,3);
+ lcd.printf("TEST2 ");
+ wait(1);
+
if (lineData.numWords != 2){
//Error Check, incorrect number of parameter, error out
return 0;
--- a/Initialization.hpp Tue Sep 16 15:28:59 2014 +0000
+++ b/Initialization.hpp Tue Sep 16 18:27:41 2014 +0000
@@ -46,6 +46,7 @@
char word[15][MAX_LINE_LENGTH]; //array of words from the line of text, assuming no more than 15 words will be in any given line
//in this initialization there are 15 string (pointers) of size MAX_LINE_LENGTH each
int numWords; //Number of words in the given line
+ int lineAddress;
};
extern struct Line lineData;
--- a/TextFile.h Tue Sep 16 15:28:59 2014 +0000
+++ b/TextFile.h Tue Sep 16 18:27:41 2014 +0000
@@ -74,18 +74,26 @@
void getNextLine(FILE *selectedFile) {
- char newLine[MAX_LINE_LENGTH];
+ lineData.lineAddress = ftell(selectedFile);
+
+ char *newLine;
+ newLine = new char[MAX_LINE_LENGTH];
+
fgets(newLine, MAX_LINE_LENGTH, selectedFile);
-
+
lineData.fullLine = newLine;
lineData.lineNumber++;
//Create a copy of the line, so that we don't effect the full line when we're extracting the words
- char cpyLine[MAX_LINE_LENGTH];
+ char *cpyLine;
+ cpyLine = new char[MAX_LINE_LENGTH];
strcpy(cpyLine, newLine);
+
+ char *tempWord;
+ tempWord = new char;
- char *tempWord;
int numWords = 0;
+
tempWord = strtok(cpyLine, " ");
while (tempWord != NULL){
strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array
@@ -93,10 +101,17 @@
tempWord = strtok(NULL, " ");
}
+
+
lineData.numWords = numWords; //Record the number of words in the line
lcd.setAddress(0,1);
- lcd.printf("Current Line#: %d", lineData.lineNumber);
+ lcd.printf("Current Line#: %d ", lineData.lineNumber);
+
+ delete[] tempWord;
+ delete[] newLine;
+ delete[] cpyLine;
+
/*
lcd.cls(); //clear the display
lcd.setAddress(0,0);
--- 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;
}