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

Committer:
mehatfie
Date:
Tue Sep 23 23:42:32 2014 +0000
Revision:
6:d1594fd2ec5a
Parent:
5:e36e0538a903
Child:
9:5a0c4c6e39c7
- System Functional for Door_Latch_Test; - Fixed issue with Motors, needed to initialize the Motors for PWM; - Confirmed code works to read from SD Card that has spaces and tabs

Who changed what in which revision?

UserRevisionLine numberNew 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 6:d1594fd2ec5a 35 lcd.setAddress(0,1);
mehatfie 6:d1594fd2ec5a 36 lcd.printf("TEST1");
mehatfie 6:d1594fd2ec5a 37
mehatfie 0:22618cf06f45 38 vector<string> filenames;
mehatfie 0:22618cf06f45 39 DIR *dp;
mehatfie 0:22618cf06f45 40 struct dirent *dirp;
mehatfie 6:d1594fd2ec5a 41
mehatfie 6:d1594fd2ec5a 42 lcd.setAddress(0,2);
mehatfie 6:d1594fd2ec5a 43 lcd.printf("TEST2");
mehatfie 6:d1594fd2ec5a 44
mehatfie 0:22618cf06f45 45 dp = opendir(dir);
mehatfie 6:d1594fd2ec5a 46
mehatfie 6:d1594fd2ec5a 47 lcd.setAddress(0,3);
mehatfie 6:d1594fd2ec5a 48 lcd.printf("TEST3");
mehatfie 6:d1594fd2ec5a 49
mehatfie 0:22618cf06f45 50 //read all directory and file names in current directory into filename vector
mehatfie 0:22618cf06f45 51 while((dirp = readdir(dp)) != NULL) {
mehatfie 0:22618cf06f45 52 filenames.push_back(string(dirp->d_name));
mehatfie 0:22618cf06f45 53 }
mehatfie 0:22618cf06f45 54 closedir(dp);
mehatfie 0:22618cf06f45 55 return filenames;
mehatfie 0:22618cf06f45 56 }
mehatfie 0:22618cf06f45 57
mehatfie 0:22618cf06f45 58
mehatfie 0:22618cf06f45 59
mehatfie 0:22618cf06f45 60 /******************************************************************************/
mehatfie 0:22618cf06f45 61 /*** <getFileNamesWithoutExt> ***/
mehatfie 0:22618cf06f45 62 /******************************************************************************/
mehatfie 0:22618cf06f45 63 // save filename strings to array from vector using an iterator
mehatfie 2:3e7baa3e3fec 64 int getFileNamesWithoutExt(vector<string> &textFiles, vector<string> filenames) {
mehatfie 0:22618cf06f45 65
mehatfie 0:22618cf06f45 66 //Cycle through all files listed in the directoy (strings in the vector list)
mehatfie 0:22618cf06f45 67 int n = 0;
mehatfie 0:22618cf06f45 68 for(vector<string>::iterator it=filenames.begin(); it < filenames.end(); it++)
mehatfie 0:22618cf06f45 69 {
mehatfie 2:3e7baa3e3fec 70
mehatfie 2:3e7baa3e3fec 71 vector<string> fileName; //filename[0] = filename, filename[1] = extension
mehatfie 2:3e7baa3e3fec 72
mehatfie 2:3e7baa3e3fec 73 stringstream ss;
mehatfie 2:3e7baa3e3fec 74 ss << (*it);
mehatfie 0:22618cf06f45 75
mehatfie 2:3e7baa3e3fec 76 //parse the array based on a '.' delimeter, effectively getting the file name and extension
mehatfie 2:3e7baa3e3fec 77 string item;
mehatfie 2:3e7baa3e3fec 78 while (getline(ss, item, '.')) {
mehatfie 2:3e7baa3e3fec 79 fileName.push_back(item);
mehatfie 2:3e7baa3e3fec 80 }
mehatfie 2:3e7baa3e3fec 81
mehatfie 2:3e7baa3e3fec 82 // 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 83 if (fileName.size() == 2 && fileName[1].compare("txt") == 0) {
mehatfie 2:3e7baa3e3fec 84 textFiles.push_back(fileName[0]);
mehatfie 0:22618cf06f45 85 n++;
mehatfie 2:3e7baa3e3fec 86 }
mehatfie 0:22618cf06f45 87 }
mehatfie 0:22618cf06f45 88
mehatfie 0:22618cf06f45 89 return n; //Return the number of txt files that were found in the directory
mehatfie 0:22618cf06f45 90 }
mehatfie 0:22618cf06f45 91
mehatfie 0:22618cf06f45 92 /******************************************************************************/
mehatfie 0:22618cf06f45 93 /*** <getNextLine> ***/
mehatfie 0:22618cf06f45 94 /******************************************************************************/
mehatfie 0:22618cf06f45 95 //
mehatfie 0:22618cf06f45 96
mehatfie 5:e36e0538a903 97 void getNextLine(FILE *selectedFile, LineData &lineData) {
mehatfie 0:22618cf06f45 98
mehatfie 1:5731f31f96be 99 lineData.lineAddress = ftell(selectedFile);
mehatfie 1:5731f31f96be 100
mehatfie 2:3e7baa3e3fec 101 //char *newLine;
mehatfie 2:3e7baa3e3fec 102 //getline(selectedFile, newLine);
mehatfie 2:3e7baa3e3fec 103 char newLine[MAX_LINE_LENGTH];
mehatfie 1:5731f31f96be 104
mehatfie 0:22618cf06f45 105 fgets(newLine, MAX_LINE_LENGTH, selectedFile);
mehatfie 1:5731f31f96be 106
mehatfie 2:3e7baa3e3fec 107 string tempString(newLine);
mehatfie 2:3e7baa3e3fec 108 //lineData.fullLine = newLine;
mehatfie 2:3e7baa3e3fec 109 lineData.lineNumber++;
mehatfie 2:3e7baa3e3fec 110
mehatfie 2:3e7baa3e3fec 111 stringstream newLineStrm(newLine);
mehatfie 2:3e7baa3e3fec 112 //string tempWord = strtok(cpyLine, " ");
mehatfie 0:22618cf06f45 113
mehatfie 2:3e7baa3e3fec 114 istream_iterator<string> it(newLineStrm);
mehatfie 2:3e7baa3e3fec 115 istream_iterator<string> end;
mehatfie 2:3e7baa3e3fec 116 vector<string> results(it, end);
mehatfie 2:3e7baa3e3fec 117 copy(results.begin(), results.end(), lineData.word);
mehatfie 0:22618cf06f45 118
mehatfie 2:3e7baa3e3fec 119 lineData.numWords = results.size(); //Record the number of words in the line
mehatfie 1:5731f31f96be 120
mehatfie 2:3e7baa3e3fec 121 results.erase(results.begin(), results.end());
mehatfie 2:3e7baa3e3fec 122
mehatfie 2:3e7baa3e3fec 123 /*
mehatfie 2:3e7baa3e3fec 124 int numWords = 0;
mehatfie 0:22618cf06f45 125 while (tempWord != NULL){
mehatfie 0:22618cf06f45 126 strcpy(lineData.word[numWords], tempWord); //Record the word in the lineData array
mehatfie 0:22618cf06f45 127 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 128 tempWord = strtok(NULL, " ");
mehatfie 2:3e7baa3e3fec 129 }*/
mehatfie 0:22618cf06f45 130
mehatfie 1:5731f31f96be 131
mehatfie 1:5731f31f96be 132
mehatfie 6:d1594fd2ec5a 133 lcd.setAddress(0,2);
mehatfie 1:5731f31f96be 134 lcd.printf("Current Line#: %d ", lineData.lineNumber);
mehatfie 1:5731f31f96be 135
mehatfie 6:d1594fd2ec5a 136 lcd.setAddress(0,3);
mehatfie 6:d1594fd2ec5a 137 lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters
mehatfie 6:d1594fd2ec5a 138 lcd.setAddress(0,3);
mehatfie 6:d1594fd2ec5a 139 lcd.printf("cmd/dvc: %s", lineData.word[0]);
mehatfie 6:d1594fd2ec5a 140
mehatfie 0:22618cf06f45 141 /*
mehatfie 0:22618cf06f45 142 lcd.cls(); //clear the display
mehatfie 0:22618cf06f45 143 lcd.setAddress(0,0);
mehatfie 0:22618cf06f45 144 lcd.printf("wrd1: %s", lineData.word[0]);
mehatfie 0:22618cf06f45 145 lcd.setAddress(0,1);
mehatfie 0:22618cf06f45 146 lcd.printf("wrd2: %s", lineData.word[1]);
mehatfie 0:22618cf06f45 147 lcd.setAddress(0,2);
mehatfie 0:22618cf06f45 148 lcd.printf("wrd3: %s", lineData.word[2]);
mehatfie 0:22618cf06f45 149 lcd.setAddress(0,3);
mehatfie 0:22618cf06f45 150 lcd.printf("wrd4: %s", lineData.word[3]);
mehatfie 5:e36e0538a903 151 wait(2);*/
mehatfie 0:22618cf06f45 152 }
mehatfie 0:22618cf06f45 153
mehatfie 0:22618cf06f45 154
mehatfie 2:3e7baa3e3fec 155 #endif
mehatfie 0:22618cf06f45 156
mehatfie 0:22618cf06f45 157
mehatfie 0:22618cf06f45 158
mehatfie 0:22618cf06f45 159
mehatfie 0:22618cf06f45 160