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@0:22618cf06f45, 2014-09-16 (annotated)
- Committer:
- mehatfie
- Date:
- Tue Sep 16 15:28:59 2014 +0000
- Revision:
- 0:22618cf06f45
- Child:
- 1:5731f31f96be
- Initial Commit; - Code is a mess (in mid transition of general code architecture); - Functionality work to drive motors in both direction and turn off; - Delay functionality working; - Able to read a program from a txt file and perform functionality
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 | 0:22618cf06f45 | 77 | char newLine[MAX_LINE_LENGTH]; |
mehatfie | 0:22618cf06f45 | 78 | fgets(newLine, MAX_LINE_LENGTH, selectedFile); |
mehatfie | 0:22618cf06f45 | 79 | |
mehatfie | 0:22618cf06f45 | 80 | lineData.fullLine = newLine; |
mehatfie | 0:22618cf06f45 | 81 | lineData.lineNumber++; |
mehatfie | 0:22618cf06f45 | 82 | |
mehatfie | 0:22618cf06f45 | 83 | //Create a copy of the line, so that we don't effect the full line when we're extracting the words |
mehatfie | 0:22618cf06f45 | 84 | char cpyLine[MAX_LINE_LENGTH]; |
mehatfie | 0:22618cf06f45 | 85 | strcpy(cpyLine, newLine); |
mehatfie | 0:22618cf06f45 | 86 | |
mehatfie | 0:22618cf06f45 | 87 | char *tempWord; |
mehatfie | 0:22618cf06f45 | 88 | int numWords = 0; |
mehatfie | 0:22618cf06f45 | 89 | tempWord = strtok(cpyLine, " "); |
mehatfie | 0:22618cf06f45 | 90 | while (tempWord != NULL){ |
mehatfie | 0:22618cf06f45 | 91 | strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array |
mehatfie | 0:22618cf06f45 | 92 | 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 | 93 | tempWord = strtok(NULL, " "); |
mehatfie | 0:22618cf06f45 | 94 | } |
mehatfie | 0:22618cf06f45 | 95 | |
mehatfie | 0:22618cf06f45 | 96 | lineData.numWords = numWords; //Record the number of words in the line |
mehatfie | 0:22618cf06f45 | 97 | |
mehatfie | 0:22618cf06f45 | 98 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 99 | lcd.printf("Current Line#: %d", lineData.lineNumber); |
mehatfie | 0:22618cf06f45 | 100 | /* |
mehatfie | 0:22618cf06f45 | 101 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 102 | lcd.setAddress(0,0); |
mehatfie | 0:22618cf06f45 | 103 | lcd.printf("wrd1: %s", lineData.word[0]); |
mehatfie | 0:22618cf06f45 | 104 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 105 | lcd.printf("wrd2: %s", lineData.word[1]); |
mehatfie | 0:22618cf06f45 | 106 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 107 | lcd.printf("wrd3: %s", lineData.word[2]); |
mehatfie | 0:22618cf06f45 | 108 | lcd.setAddress(0,3); |
mehatfie | 0:22618cf06f45 | 109 | lcd.printf("wrd4: %s", lineData.word[3]); |
mehatfie | 0:22618cf06f45 | 110 | wait(5);*/ |
mehatfie | 0:22618cf06f45 | 111 | } |
mehatfie | 0:22618cf06f45 | 112 | |
mehatfie | 0:22618cf06f45 | 113 | |
mehatfie | 0:22618cf06f45 | 114 | |
mehatfie | 0:22618cf06f45 | 115 | |
mehatfie | 0:22618cf06f45 | 116 | |
mehatfie | 0:22618cf06f45 | 117 | |
mehatfie | 0:22618cf06f45 | 118 | |
mehatfie | 0:22618cf06f45 | 119 |