![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
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@11:bc9cd2869f95, 2014-10-01 (annotated)
- Committer:
- mehatfie
- Date:
- Wed Oct 01 18:11:38 2014 +0000
- Revision:
- 11:bc9cd2869f95
- Parent:
- 9:5a0c4c6e39c7
- Latest revision is not a working copy, simply publishing all previous working copies to the server
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 | 11:bc9cd2869f95 | 64 | for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++){ |
mehatfie | 2:3e7baa3e3fec | 65 | |
mehatfie | 2:3e7baa3e3fec | 66 | vector<string> fileName; //filename[0] = filename, filename[1] = extension |
mehatfie | 2:3e7baa3e3fec | 67 | |
mehatfie | 2:3e7baa3e3fec | 68 | stringstream ss; |
mehatfie | 2:3e7baa3e3fec | 69 | ss << (*it); |
mehatfie | 0:22618cf06f45 | 70 | |
mehatfie | 2:3e7baa3e3fec | 71 | //parse the array based on a '.' delimeter, effectively getting the file name and extension |
mehatfie | 2:3e7baa3e3fec | 72 | string item; |
mehatfie | 2:3e7baa3e3fec | 73 | while (getline(ss, item, '.')) { |
mehatfie | 2:3e7baa3e3fec | 74 | fileName.push_back(item); |
mehatfie | 2:3e7baa3e3fec | 75 | } |
mehatfie | 2:3e7baa3e3fec | 76 | |
mehatfie | 2:3e7baa3e3fec | 77 | // 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 | 78 | if (fileName.size() == 2 && fileName[1].compare("txt") == 0) { |
mehatfie | 2:3e7baa3e3fec | 79 | textFiles.push_back(fileName[0]); |
mehatfie | 0:22618cf06f45 | 80 | n++; |
mehatfie | 2:3e7baa3e3fec | 81 | } |
mehatfie | 0:22618cf06f45 | 82 | } |
mehatfie | 0:22618cf06f45 | 83 | |
mehatfie | 0:22618cf06f45 | 84 | return n; //Return the number of txt files that were found in the directory |
mehatfie | 0:22618cf06f45 | 85 | } |
mehatfie | 0:22618cf06f45 | 86 | |
mehatfie | 0:22618cf06f45 | 87 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 88 | /*** <getNextLine> ***/ |
mehatfie | 0:22618cf06f45 | 89 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 90 | // |
mehatfie | 0:22618cf06f45 | 91 | |
mehatfie | 9:5a0c4c6e39c7 | 92 | int getNextLine(FILE *selectedFile, LineData &lineData) { |
mehatfie | 0:22618cf06f45 | 93 | |
mehatfie | 11:bc9cd2869f95 | 94 | int validLine = 0; |
mehatfie | 11:bc9cd2869f95 | 95 | while (!validLine){ |
mehatfie | 11:bc9cd2869f95 | 96 | |
mehatfie | 11:bc9cd2869f95 | 97 | lineData.lineAddress = ftell(selectedFile); |
mehatfie | 11:bc9cd2869f95 | 98 | |
mehatfie | 11:bc9cd2869f95 | 99 | if (lineData.lineAddress == -1L){ |
mehatfie | 11:bc9cd2869f95 | 100 | ErrorOut("Unable to get address of line, SD Card Removed?", lineData.lineNumber + 1); |
mehatfie | 11:bc9cd2869f95 | 101 | return -1; |
mehatfie | 11:bc9cd2869f95 | 102 | } |
mehatfie | 11:bc9cd2869f95 | 103 | |
mehatfie | 11:bc9cd2869f95 | 104 | //char *newLine; |
mehatfie | 11:bc9cd2869f95 | 105 | //getline(selectedFile, newLine); |
mehatfie | 11:bc9cd2869f95 | 106 | char newLine[MAX_LINE_LENGTH]; |
mehatfie | 11:bc9cd2869f95 | 107 | |
mehatfie | 11:bc9cd2869f95 | 108 | fgets(newLine, MAX_LINE_LENGTH, selectedFile); |
mehatfie | 11:bc9cd2869f95 | 109 | |
mehatfie | 11:bc9cd2869f95 | 110 | if (newLine == NULL){ |
mehatfie | 11:bc9cd2869f95 | 111 | ErrorOut("Unable to get the next line, SD Card Removed?", lineData.lineNumber + 1); |
mehatfie | 11:bc9cd2869f95 | 112 | return -1; |
mehatfie | 11:bc9cd2869f95 | 113 | } |
mehatfie | 11:bc9cd2869f95 | 114 | |
mehatfie | 11:bc9cd2869f95 | 115 | lineData.lineNumber++; // new line successfully found, increase lineNumber |
mehatfie | 11:bc9cd2869f95 | 116 | |
mehatfie | 11:bc9cd2869f95 | 117 | //pull out each individual word (separated by any white space), and place it in the vector |
mehatfie | 11:bc9cd2869f95 | 118 | stringstream newLineStrm(newLine); |
mehatfie | 11:bc9cd2869f95 | 119 | istream_iterator<string> it(newLineStrm); |
mehatfie | 11:bc9cd2869f95 | 120 | istream_iterator<string> end; |
mehatfie | 11:bc9cd2869f95 | 121 | vector<string> results(it, end); |
mehatfie | 11:bc9cd2869f95 | 122 | |
mehatfie | 11:bc9cd2869f95 | 123 | //Get the size of the results vector, if it is 0 then while loop will loop again, |
mehatfie | 11:bc9cd2869f95 | 124 | //if it has a value, then it will accept the line and end looping cycles |
mehatfie | 11:bc9cd2869f95 | 125 | validLine = results.size(); |
mehatfie | 1:5731f31f96be | 126 | |
mehatfie | 11:bc9cd2869f95 | 127 | //copy the vector of results into the array of words |
mehatfie | 11:bc9cd2869f95 | 128 | copy(results.begin(), results.end(), lineData.word); |
mehatfie | 11:bc9cd2869f95 | 129 | |
mehatfie | 11:bc9cd2869f95 | 130 | lineData.numWords = results.size(); //Record the number of words in the line |
mehatfie | 11:bc9cd2869f95 | 131 | |
mehatfie | 11:bc9cd2869f95 | 132 | results.erase(results.begin(), results.end()); //remove the results vector from memory |
mehatfie | 9:5a0c4c6e39c7 | 133 | } |
mehatfie | 2:3e7baa3e3fec | 134 | |
mehatfie | 9:5a0c4c6e39c7 | 135 | //Update the Current Line Number |
mehatfie | 6:d1594fd2ec5a | 136 | lcd.setAddress(0,2); |
mehatfie | 1:5731f31f96be | 137 | lcd.printf("Current Line#: %d ", lineData.lineNumber); |
mehatfie | 1:5731f31f96be | 138 | |
mehatfie | 9:5a0c4c6e39c7 | 139 | //Update the cmd/dvc label for the line so that the user has an idea of what's going on |
mehatfie | 6:d1594fd2ec5a | 140 | lcd.setAddress(0,3); |
mehatfie | 9:5a0c4c6e39c7 | 141 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
mehatfie | 6:d1594fd2ec5a | 142 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 143 | lcd.printf("cmd/dvc: %s", lineData.word[0]); |
mehatfie | 11:bc9cd2869f95 | 144 | |
mehatfie | 0:22618cf06f45 | 145 | /* |
mehatfie | 0:22618cf06f45 | 146 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 147 | lcd.setAddress(0,0); |
mehatfie | 0:22618cf06f45 | 148 | lcd.printf("wrd1: %s", lineData.word[0]); |
mehatfie | 0:22618cf06f45 | 149 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 150 | lcd.printf("wrd2: %s", lineData.word[1]); |
mehatfie | 0:22618cf06f45 | 151 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 152 | lcd.printf("wrd3: %s", lineData.word[2]); |
mehatfie | 0:22618cf06f45 | 153 | lcd.setAddress(0,3); |
mehatfie | 0:22618cf06f45 | 154 | lcd.printf("wrd4: %s", lineData.word[3]); |
mehatfie | 11:bc9cd2869f95 | 155 | wait(1);*/ |
mehatfie | 0:22618cf06f45 | 156 | } |
mehatfie | 0:22618cf06f45 | 157 | |
mehatfie | 0:22618cf06f45 | 158 | |
mehatfie | 2:3e7baa3e3fec | 159 | #endif |
mehatfie | 0:22618cf06f45 | 160 | |
mehatfie | 0:22618cf06f45 | 161 | |
mehatfie | 0:22618cf06f45 | 162 | |
mehatfie | 0:22618cf06f45 | 163 | |
mehatfie | 0:22618cf06f45 | 164 |