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@9:5a0c4c6e39c7, 2014-09-24 (annotated)
- Committer:
- mehatfie
- Date:
- Wed Sep 24 22:23:00 2014 +0000
- Revision:
- 9:5a0c4c6e39c7
- Parent:
- 6:d1594fd2ec5a
- Child:
- 11:bc9cd2869f95
- System error checking and syntax checking should be completed; --- Compiled code still functions correctly; --- All error checking has not been checked that they indeed error out / not error out and function as planned
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehatfie | 2:3e7baa3e3fec | 1 | #ifndef TEXTFILE_HPP |
mehatfie | 2:3e7baa3e3fec | 2 | #define TEXTFILE_HPP |
mehatfie | 2:3e7baa3e3fec | 3 | |
mehatfie | 0:22618cf06f45 | 4 | #include "mbed.h" |
mehatfie | 0:22618cf06f45 | 5 | #include "TextLCD.h" |
mehatfie | 0:22618cf06f45 | 6 | #include "SDFileSystem.h" |
mehatfie | 0:22618cf06f45 | 7 | #include <stdio.h> |
mehatfie | 0:22618cf06f45 | 8 | #include <string> |
mehatfie | 0:22618cf06f45 | 9 | #include <stdlib.h> |
mehatfie | 0:22618cf06f45 | 10 | #include <vector> |
mehatfie | 0:22618cf06f45 | 11 | using std::string; |
mehatfie | 0:22618cf06f45 | 12 | |
mehatfie | 2:3e7baa3e3fec | 13 | #include <iostream> |
mehatfie | 2:3e7baa3e3fec | 14 | #include <istream> |
mehatfie | 2:3e7baa3e3fec | 15 | #include <ostream> |
mehatfie | 2:3e7baa3e3fec | 16 | #include <iterator> |
mehatfie | 2:3e7baa3e3fec | 17 | #include <sstream> |
mehatfie | 2:3e7baa3e3fec | 18 | |
mehatfie | 2:3e7baa3e3fec | 19 | |
mehatfie | 0:22618cf06f45 | 20 | |
mehatfie | 0:22618cf06f45 | 21 | //vector<string> filenames; //filenames are stored in a vector string |
mehatfie | 0:22618cf06f45 | 22 | |
mehatfie | 2:3e7baa3e3fec | 23 | vector<string> readFileNames(char []); |
mehatfie | 0:22618cf06f45 | 24 | int getFileNamesWithoutExt(string[], vector<string> &); |
mehatfie | 0:22618cf06f45 | 25 | string getPreviousLine(istream &); |
mehatfie | 0:22618cf06f45 | 26 | |
mehatfie | 0:22618cf06f45 | 27 | |
mehatfie | 0:22618cf06f45 | 28 | |
mehatfie | 0:22618cf06f45 | 29 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 30 | /*** <readFileNames> ***/ |
mehatfie | 0:22618cf06f45 | 31 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 32 | // read file names into vector of strings |
mehatfie | 0:22618cf06f45 | 33 | vector<string> readFileNames(char *dir) { |
mehatfie | 0:22618cf06f45 | 34 | |
mehatfie | 0:22618cf06f45 | 35 | vector<string> filenames; |
mehatfie | 0:22618cf06f45 | 36 | DIR *dp; |
mehatfie | 0:22618cf06f45 | 37 | struct dirent *dirp; |
mehatfie | 6:d1594fd2ec5a | 38 | |
mehatfie | 9:5a0c4c6e39c7 | 39 | |
mehatfie | 0:22618cf06f45 | 40 | dp = opendir(dir); |
mehatfie | 6:d1594fd2ec5a | 41 | |
mehatfie | 9:5a0c4c6e39c7 | 42 | //if no directory was found, don't try and get the fileNames and return an empty vector of 0 |
mehatfie | 9:5a0c4c6e39c7 | 43 | if (dp != NULL){ |
mehatfie | 9:5a0c4c6e39c7 | 44 | |
mehatfie | 9:5a0c4c6e39c7 | 45 | //read all directory and file names in current directory into filename vector |
mehatfie | 9:5a0c4c6e39c7 | 46 | while((dirp = readdir(dp)) != NULL) { |
mehatfie | 9:5a0c4c6e39c7 | 47 | filenames.push_back(string(dirp->d_name)); |
mehatfie | 9:5a0c4c6e39c7 | 48 | } |
mehatfie | 9:5a0c4c6e39c7 | 49 | closedir(dp); |
mehatfie | 0:22618cf06f45 | 50 | } |
mehatfie | 0:22618cf06f45 | 51 | return filenames; |
mehatfie | 0:22618cf06f45 | 52 | } |
mehatfie | 0:22618cf06f45 | 53 | |
mehatfie | 0:22618cf06f45 | 54 | |
mehatfie | 0:22618cf06f45 | 55 | |
mehatfie | 0:22618cf06f45 | 56 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 57 | /*** <getFileNamesWithoutExt> ***/ |
mehatfie | 0:22618cf06f45 | 58 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 59 | // save filename strings to array from vector using an iterator |
mehatfie | 2:3e7baa3e3fec | 60 | int getFileNamesWithoutExt(vector<string> &textFiles, vector<string> filenames) { |
mehatfie | 0:22618cf06f45 | 61 | |
mehatfie | 0:22618cf06f45 | 62 | //Cycle through all files listed in the directoy (strings in the vector list) |
mehatfie | 0:22618cf06f45 | 63 | int n = 0; |
mehatfie | 0:22618cf06f45 | 64 | for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++) |
mehatfie | 0:22618cf06f45 | 65 | { |
mehatfie | 2:3e7baa3e3fec | 66 | |
mehatfie | 2:3e7baa3e3fec | 67 | vector<string> fileName; //filename[0] = filename, filename[1] = extension |
mehatfie | 2:3e7baa3e3fec | 68 | |
mehatfie | 2:3e7baa3e3fec | 69 | stringstream ss; |
mehatfie | 2:3e7baa3e3fec | 70 | ss << (*it); |
mehatfie | 0:22618cf06f45 | 71 | |
mehatfie | 2:3e7baa3e3fec | 72 | //parse the array based on a '.' delimeter, effectively getting the file name and extension |
mehatfie | 2:3e7baa3e3fec | 73 | string item; |
mehatfie | 2:3e7baa3e3fec | 74 | while (getline(ss, item, '.')) { |
mehatfie | 2:3e7baa3e3fec | 75 | fileName.push_back(item); |
mehatfie | 2:3e7baa3e3fec | 76 | } |
mehatfie | 2:3e7baa3e3fec | 77 | |
mehatfie | 2:3e7baa3e3fec | 78 | // if the fileName vector has two items (a name and an extension) and is a .txt file, then save it to the array |
mehatfie | 2:3e7baa3e3fec | 79 | if (fileName.size() == 2 && fileName[1].compare("txt") == 0) { |
mehatfie | 2:3e7baa3e3fec | 80 | textFiles.push_back(fileName[0]); |
mehatfie | 0:22618cf06f45 | 81 | n++; |
mehatfie | 2:3e7baa3e3fec | 82 | } |
mehatfie | 0:22618cf06f45 | 83 | } |
mehatfie | 0:22618cf06f45 | 84 | |
mehatfie | 0:22618cf06f45 | 85 | return n; //Return the number of txt files that were found in the directory |
mehatfie | 0:22618cf06f45 | 86 | } |
mehatfie | 0:22618cf06f45 | 87 | |
mehatfie | 0:22618cf06f45 | 88 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 89 | /*** <getNextLine> ***/ |
mehatfie | 0:22618cf06f45 | 90 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 91 | // |
mehatfie | 0:22618cf06f45 | 92 | |
mehatfie | 9:5a0c4c6e39c7 | 93 | int getNextLine(FILE *selectedFile, LineData &lineData) { |
mehatfie | 0:22618cf06f45 | 94 | |
mehatfie | 1:5731f31f96be | 95 | lineData.lineAddress = ftell(selectedFile); |
mehatfie | 1:5731f31f96be | 96 | |
mehatfie | 9:5a0c4c6e39c7 | 97 | if (lineData.lineAddress == -1L){ |
mehatfie | 9:5a0c4c6e39c7 | 98 | ErrorOut("Unable to get address of line, SD Card Removed?", lineData.lineNumber + 1); |
mehatfie | 9:5a0c4c6e39c7 | 99 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 100 | } |
mehatfie | 9:5a0c4c6e39c7 | 101 | |
mehatfie | 2:3e7baa3e3fec | 102 | //char *newLine; |
mehatfie | 2:3e7baa3e3fec | 103 | //getline(selectedFile, newLine); |
mehatfie | 2:3e7baa3e3fec | 104 | char newLine[MAX_LINE_LENGTH]; |
mehatfie | 1:5731f31f96be | 105 | |
mehatfie | 0:22618cf06f45 | 106 | fgets(newLine, MAX_LINE_LENGTH, selectedFile); |
mehatfie | 1:5731f31f96be | 107 | |
mehatfie | 9:5a0c4c6e39c7 | 108 | if (newLine == NULL){ |
mehatfie | 9:5a0c4c6e39c7 | 109 | ErrorOut("Unable to get the next line, SD Card Removed?", lineData.lineNumber + 1); |
mehatfie | 9:5a0c4c6e39c7 | 110 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 111 | } |
mehatfie | 2:3e7baa3e3fec | 112 | |
mehatfie | 9:5a0c4c6e39c7 | 113 | lineData.lineNumber++; // new line successfully found, increase lineNumber |
mehatfie | 0:22618cf06f45 | 114 | |
mehatfie | 9:5a0c4c6e39c7 | 115 | //pull out each individual word (separated by any white space), and place it in the vector |
mehatfie | 9:5a0c4c6e39c7 | 116 | stringstream newLineStrm(newLine); |
mehatfie | 2:3e7baa3e3fec | 117 | istream_iterator<string> it(newLineStrm); |
mehatfie | 2:3e7baa3e3fec | 118 | istream_iterator<string> end; |
mehatfie | 2:3e7baa3e3fec | 119 | vector<string> results(it, end); |
mehatfie | 9:5a0c4c6e39c7 | 120 | |
mehatfie | 9:5a0c4c6e39c7 | 121 | //copy the vector of results into the array of words |
mehatfie | 2:3e7baa3e3fec | 122 | copy(results.begin(), results.end(), lineData.word); |
mehatfie | 0:22618cf06f45 | 123 | |
mehatfie | 2:3e7baa3e3fec | 124 | lineData.numWords = results.size(); //Record the number of words in the line |
mehatfie | 1:5731f31f96be | 125 | |
mehatfie | 9:5a0c4c6e39c7 | 126 | results.erase(results.begin(), results.end()); //remove the results vector from memory |
mehatfie | 2:3e7baa3e3fec | 127 | |
mehatfie | 9:5a0c4c6e39c7 | 128 | //Update the Current Line Number |
mehatfie | 6:d1594fd2ec5a | 129 | lcd.setAddress(0,2); |
mehatfie | 1:5731f31f96be | 130 | lcd.printf("Current Line#: %d ", lineData.lineNumber); |
mehatfie | 1:5731f31f96be | 131 | |
mehatfie | 9:5a0c4c6e39c7 | 132 | //Update the cmd/dvc label for the line so that the user has an idea of what's going on |
mehatfie | 6:d1594fd2ec5a | 133 | lcd.setAddress(0,3); |
mehatfie | 9:5a0c4c6e39c7 | 134 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
mehatfie | 6:d1594fd2ec5a | 135 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 136 | lcd.printf("cmd/dvc: %s", lineData.word[0]); |
mehatfie | 6:d1594fd2ec5a | 137 | |
mehatfie | 0:22618cf06f45 | 138 | /* |
mehatfie | 0:22618cf06f45 | 139 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 140 | lcd.setAddress(0,0); |
mehatfie | 0:22618cf06f45 | 141 | lcd.printf("wrd1: %s", lineData.word[0]); |
mehatfie | 0:22618cf06f45 | 142 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 143 | lcd.printf("wrd2: %s", lineData.word[1]); |
mehatfie | 0:22618cf06f45 | 144 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 145 | lcd.printf("wrd3: %s", lineData.word[2]); |
mehatfie | 0:22618cf06f45 | 146 | lcd.setAddress(0,3); |
mehatfie | 0:22618cf06f45 | 147 | lcd.printf("wrd4: %s", lineData.word[3]); |
mehatfie | 5:e36e0538a903 | 148 | wait(2);*/ |
mehatfie | 0:22618cf06f45 | 149 | } |
mehatfie | 0:22618cf06f45 | 150 | |
mehatfie | 0:22618cf06f45 | 151 | |
mehatfie | 2:3e7baa3e3fec | 152 | #endif |
mehatfie | 0:22618cf06f45 | 153 | |
mehatfie | 0:22618cf06f45 | 154 | |
mehatfie | 0:22618cf06f45 | 155 | |
mehatfie | 0:22618cf06f45 | 156 | |
mehatfie | 0:22618cf06f45 | 157 |