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:
Thu Sep 18 23:38:11 2014 +0000
Revision:
2:3e7baa3e3fec
Parent:
1:5731f31f96be
Child:
5:e36e0538a903
- Revamped whole system and now works without stalling or freezing mid program; - now Object Oriented Based in terms of Devices (inheritance of functions)

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