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
TextFile.h@1:5731f31f96be, 2014-09-16 (annotated)
- Committer:
- mehatfie
- Date:
- Tue Sep 16 18:27:41 2014 +0000
- Revision:
- 1:5731f31f96be
- Parent:
- 0:22618cf06f45
- Child:
- 2:3e7baa3e3fec
- 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
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mehatfie | 0:22618cf06f45 | 1 | #include "mbed.h" |
| mehatfie | 0:22618cf06f45 | 2 | #include "TextLCD.h" |
| mehatfie | 0:22618cf06f45 | 3 | #include "SDFileSystem.h" |
| mehatfie | 0:22618cf06f45 | 4 | #include <stdio.h> |
| mehatfie | 0:22618cf06f45 | 5 | #include <string> |
| mehatfie | 0:22618cf06f45 | 6 | #include <stdlib.h> |
| mehatfie | 0:22618cf06f45 | 7 | #include <vector> |
| mehatfie | 0:22618cf06f45 | 8 | using std::string; |
| mehatfie | 0:22618cf06f45 | 9 | |
| mehatfie | 0:22618cf06f45 | 10 | |
| mehatfie | 0:22618cf06f45 | 11 | //vector<string> filenames; //filenames are stored in a vector string |
| mehatfie | 0:22618cf06f45 | 12 | |
| mehatfie | 0:22618cf06f45 | 13 | vector<string> readFileNames(char *); |
| mehatfie | 0:22618cf06f45 | 14 | int getFileNamesWithoutExt(string[], vector<string> &); |
| mehatfie | 0:22618cf06f45 | 15 | string getPreviousLine(istream &); |
| mehatfie | 0:22618cf06f45 | 16 | |
| mehatfie | 0:22618cf06f45 | 17 | |
| mehatfie | 0:22618cf06f45 | 18 | |
| mehatfie | 0:22618cf06f45 | 19 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 20 | /*** <readFileNames> ***/ |
| mehatfie | 0:22618cf06f45 | 21 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 22 | // read file names into vector of strings |
| mehatfie | 0:22618cf06f45 | 23 | vector<string> readFileNames(char *dir) { |
| mehatfie | 0:22618cf06f45 | 24 | |
| mehatfie | 0:22618cf06f45 | 25 | vector<string> filenames; |
| mehatfie | 0:22618cf06f45 | 26 | DIR *dp; |
| mehatfie | 0:22618cf06f45 | 27 | struct dirent *dirp; |
| mehatfie | 0:22618cf06f45 | 28 | dp = opendir(dir); |
| mehatfie | 0:22618cf06f45 | 29 | //read all directory and file names in current directory into filename vector |
| mehatfie | 0:22618cf06f45 | 30 | while((dirp = readdir(dp)) != NULL) { |
| mehatfie | 0:22618cf06f45 | 31 | filenames.push_back(string(dirp->d_name)); |
| mehatfie | 0:22618cf06f45 | 32 | } |
| mehatfie | 0:22618cf06f45 | 33 | closedir(dp); |
| mehatfie | 0:22618cf06f45 | 34 | return filenames; |
| mehatfie | 0:22618cf06f45 | 35 | } |
| mehatfie | 0:22618cf06f45 | 36 | |
| mehatfie | 0:22618cf06f45 | 37 | |
| mehatfie | 0:22618cf06f45 | 38 | |
| mehatfie | 0:22618cf06f45 | 39 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 40 | /*** <getFileNamesWithoutExt> ***/ |
| mehatfie | 0:22618cf06f45 | 41 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 42 | // save filename strings to array from vector using an iterator |
| mehatfie | 0:22618cf06f45 | 43 | int getFileNamesWithoutExt(string fileNames[], vector<string> &filenames) { |
| mehatfie | 0:22618cf06f45 | 44 | |
| mehatfie | 0:22618cf06f45 | 45 | //Cycle through all files listed in the directoy (strings in the vector list) |
| mehatfie | 0:22618cf06f45 | 46 | int n = 0; |
| mehatfie | 0:22618cf06f45 | 47 | for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) |
| mehatfie | 0:22618cf06f45 | 48 | { |
| mehatfie | 0:22618cf06f45 | 49 | //Convert the Vector found String to a char for use in the strtok function |
| mehatfie | 0:22618cf06f45 | 50 | char temp[50]; |
| mehatfie | 0:22618cf06f45 | 51 | if((*it).size() < 100) |
| mehatfie | 0:22618cf06f45 | 52 | strcpy(temp, (*it).c_str()); |
| mehatfie | 0:22618cf06f45 | 53 | |
| mehatfie | 0:22618cf06f45 | 54 | // Retrieve the fileName and fileExtension by splitting the string |
| mehatfie | 0:22618cf06f45 | 55 | char *fileName; |
| mehatfie | 0:22618cf06f45 | 56 | char *fileExtension; |
| mehatfie | 0:22618cf06f45 | 57 | fileName = strtok(temp, "."); |
| mehatfie | 0:22618cf06f45 | 58 | fileExtension = strtok(NULL, "."); |
| mehatfie | 0:22618cf06f45 | 59 | |
| mehatfie | 0:22618cf06f45 | 60 | // if the file is a .txt file, then save it to the array |
| mehatfie | 0:22618cf06f45 | 61 | if (strncmp(fileExtension,"txt", 3) == 0) { |
| mehatfie | 0:22618cf06f45 | 62 | fileNames[n] = fileName; |
| mehatfie | 0:22618cf06f45 | 63 | n++; |
| mehatfie | 0:22618cf06f45 | 64 | } |
| mehatfie | 0:22618cf06f45 | 65 | } |
| mehatfie | 0:22618cf06f45 | 66 | |
| mehatfie | 0:22618cf06f45 | 67 | return n; //Return the number of txt files that were found in the directory |
| mehatfie | 0:22618cf06f45 | 68 | } |
| mehatfie | 0:22618cf06f45 | 69 | |
| mehatfie | 0:22618cf06f45 | 70 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 71 | /*** <getNextLine> ***/ |
| mehatfie | 0:22618cf06f45 | 72 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 73 | // |
| mehatfie | 0:22618cf06f45 | 74 | |
| mehatfie | 0:22618cf06f45 | 75 | void getNextLine(FILE *selectedFile) { |
| mehatfie | 0:22618cf06f45 | 76 | |
| mehatfie | 1:5731f31f96be | 77 | lineData.lineAddress = ftell(selectedFile); |
| mehatfie | 1:5731f31f96be | 78 | |
| mehatfie | 1:5731f31f96be | 79 | char *newLine; |
| mehatfie | 1:5731f31f96be | 80 | newLine = new char[MAX_LINE_LENGTH]; |
| mehatfie | 1:5731f31f96be | 81 | |
| mehatfie | 0:22618cf06f45 | 82 | fgets(newLine, MAX_LINE_LENGTH, selectedFile); |
| mehatfie | 1:5731f31f96be | 83 | |
| mehatfie | 0:22618cf06f45 | 84 | lineData.fullLine = newLine; |
| mehatfie | 0:22618cf06f45 | 85 | lineData.lineNumber++; |
| mehatfie | 0:22618cf06f45 | 86 | |
| mehatfie | 0:22618cf06f45 | 87 | //Create a copy of the line, so that we don't effect the full line when we're extracting the words |
| mehatfie | 1:5731f31f96be | 88 | char *cpyLine; |
| mehatfie | 1:5731f31f96be | 89 | cpyLine = new char[MAX_LINE_LENGTH]; |
| mehatfie | 0:22618cf06f45 | 90 | strcpy(cpyLine, newLine); |
| mehatfie | 1:5731f31f96be | 91 | |
| mehatfie | 1:5731f31f96be | 92 | char *tempWord; |
| mehatfie | 1:5731f31f96be | 93 | tempWord = new char; |
| mehatfie | 0:22618cf06f45 | 94 | |
| mehatfie | 0:22618cf06f45 | 95 | int numWords = 0; |
| mehatfie | 1:5731f31f96be | 96 | |
| mehatfie | 0:22618cf06f45 | 97 | tempWord = strtok(cpyLine, " "); |
| mehatfie | 0:22618cf06f45 | 98 | while (tempWord != NULL){ |
| mehatfie | 0:22618cf06f45 | 99 | strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array |
| mehatfie | 0:22618cf06f45 | 100 | numWords++; //add the number of words first, since we've already got one word, this helps for the way the while loop works too |
| mehatfie | 0:22618cf06f45 | 101 | tempWord = strtok(NULL, " "); |
| mehatfie | 0:22618cf06f45 | 102 | } |
| mehatfie | 0:22618cf06f45 | 103 | |
| mehatfie | 1:5731f31f96be | 104 | |
| mehatfie | 1:5731f31f96be | 105 | |
| mehatfie | 0:22618cf06f45 | 106 | lineData.numWords = numWords; //Record the number of words in the line |
| mehatfie | 0:22618cf06f45 | 107 | |
| mehatfie | 0:22618cf06f45 | 108 | lcd.setAddress(0,1); |
| mehatfie | 1:5731f31f96be | 109 | lcd.printf("Current Line#: %d ", lineData.lineNumber); |
| mehatfie | 1:5731f31f96be | 110 | |
| mehatfie | 1:5731f31f96be | 111 | delete[] tempWord; |
| mehatfie | 1:5731f31f96be | 112 | delete[] newLine; |
| mehatfie | 1:5731f31f96be | 113 | delete[] cpyLine; |
| mehatfie | 1:5731f31f96be | 114 | |
| mehatfie | 0:22618cf06f45 | 115 | /* |
| mehatfie | 0:22618cf06f45 | 116 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 117 | lcd.setAddress(0,0); |
| mehatfie | 0:22618cf06f45 | 118 | lcd.printf("wrd1: %s", lineData.word[0]); |
| mehatfie | 0:22618cf06f45 | 119 | lcd.setAddress(0,1); |
| mehatfie | 0:22618cf06f45 | 120 | lcd.printf("wrd2: %s", lineData.word[1]); |
| mehatfie | 0:22618cf06f45 | 121 | lcd.setAddress(0,2); |
| mehatfie | 0:22618cf06f45 | 122 | lcd.printf("wrd3: %s", lineData.word[2]); |
| mehatfie | 0:22618cf06f45 | 123 | lcd.setAddress(0,3); |
| mehatfie | 0:22618cf06f45 | 124 | lcd.printf("wrd4: %s", lineData.word[3]); |
| mehatfie | 0:22618cf06f45 | 125 | wait(5);*/ |
| mehatfie | 0:22618cf06f45 | 126 | } |
| mehatfie | 0:22618cf06f45 | 127 | |
| mehatfie | 0:22618cf06f45 | 128 | |
| mehatfie | 0:22618cf06f45 | 129 | |
| mehatfie | 0:22618cf06f45 | 130 | |
| mehatfie | 0:22618cf06f45 | 131 | |
| mehatfie | 0:22618cf06f45 | 132 | |
| mehatfie | 0:22618cf06f45 | 133 | |
| mehatfie | 0:22618cf06f45 | 134 |