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
main.cpp@4:86d0d04cc055, 2014-09-19 (annotated)
- Committer:
- mehatfie
- Date:
- Fri Sep 19 01:54:00 2014 +0000
- Revision:
- 4:86d0d04cc055
- Parent:
- 3:078e9a1e8be3
- Child:
- 5:e36e0538a903
- fixed bug where "deviceFound" was not matching correct value; "if (lineData.word[2].compare(DeviceNames[i]) == 0)"... forgot the "== 0"; ; - Added Device PinIN, works as expected for current "wait" function
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| mehatfie | 0:22618cf06f45 | 1 | |
| mehatfie | 0:22618cf06f45 | 2 | #include "mbed.h" |
| mehatfie | 0:22618cf06f45 | 3 | #include "LocalPinNames.h" |
| mehatfie | 0:22618cf06f45 | 4 | #include "BridgeDriver.h" |
| mehatfie | 0:22618cf06f45 | 5 | #include "FrontPanelButtons.h" |
| mehatfie | 0:22618cf06f45 | 6 | #include "TextLCD.h" |
| mehatfie | 0:22618cf06f45 | 7 | #include "SDFileSystem.h" |
| mehatfie | 0:22618cf06f45 | 8 | #include "Initialization.hpp" |
| mehatfie | 0:22618cf06f45 | 9 | //#include "DeviceClasses.h" |
| mehatfie | 2:3e7baa3e3fec | 10 | //#include "Device.hpp" |
| mehatfie | 2:3e7baa3e3fec | 11 | #include "Motor.hpp" |
| mehatfie | 2:3e7baa3e3fec | 12 | #include "VoltageDriver.hpp" |
| mehatfie | 4:86d0d04cc055 | 13 | #include "PinIN.hpp" |
| mehatfie | 0:22618cf06f45 | 14 | #include "TextFile.h" |
| mehatfie | 0:22618cf06f45 | 15 | #include <stdio.h> |
| mehatfie | 0:22618cf06f45 | 16 | #include <string> |
| mehatfie | 0:22618cf06f45 | 17 | #include <stdlib.h> |
| mehatfie | 0:22618cf06f45 | 18 | #include <fstream> |
| mehatfie | 0:22618cf06f45 | 19 | #include <vector> |
| mehatfie | 0:22618cf06f45 | 20 | using std::string; |
| mehatfie | 0:22618cf06f45 | 21 | |
| mehatfie | 0:22618cf06f45 | 22 | //const int MAX_LINE_LENGTH = 100; |
| mehatfie | 2:3e7baa3e3fec | 23 | //struct Line lineData; |
| mehatfie | 2:3e7baa3e3fec | 24 | |
| mehatfie | 2:3e7baa3e3fec | 25 | |
| mehatfie | 2:3e7baa3e3fec | 26 | //DeviceData devices[15]; |
| mehatfie | 0:22618cf06f45 | 27 | |
| mehatfie | 1:5731f31f96be | 28 | FrontPanelButtons buttons(&i2c); |
| mehatfie | 1:5731f31f96be | 29 | |
| mehatfie | 0:22618cf06f45 | 30 | //extern "C" void mbed_reset(); //enable software reset of code |
| mehatfie | 0:22618cf06f45 | 31 | /* |
| mehatfie | 0:22618cf06f45 | 32 | http://stackoverflow.com/questions/3081289/how-to-read-a-line-from-a-text-file-in-c-c |
| mehatfie | 0:22618cf06f45 | 33 | http://stackoverflow.com/questions/12475915/rewind-stream-pointer-to-the-line-back |
| mehatfie | 0:22618cf06f45 | 34 | |
| mehatfie | 0:22618cf06f45 | 35 | recurrsive |
| mehatfie | 0:22618cf06f45 | 36 | */ |
| mehatfie | 0:22618cf06f45 | 37 | |
| mehatfie | 1:5731f31f96be | 38 | |
| mehatfie | 1:5731f31f96be | 39 | |
| mehatfie | 2:3e7baa3e3fec | 40 | int interpretCommand(FILE *, Line &); |
| mehatfie | 2:3e7baa3e3fec | 41 | int loopCommand(FILE *, Line &); |
| mehatfie | 1:5731f31f96be | 42 | |
| mehatfie | 1:5731f31f96be | 43 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 44 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 45 | /*************************** <FUNCTION: resetLineData> **************************/ |
| mehatfie | 1:5731f31f96be | 46 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 47 | /**********************************************************************************************************************************/ |
| mehatfie | 2:3e7baa3e3fec | 48 | void resetLineData(Line &lineData){ |
| mehatfie | 0:22618cf06f45 | 49 | |
| mehatfie | 2:3e7baa3e3fec | 50 | //lineData.fullLine.clear(); |
| mehatfie | 0:22618cf06f45 | 51 | lineData.lineNumber = 0; |
| mehatfie | 0:22618cf06f45 | 52 | lineData.numWords = 0; |
| mehatfie | 2:3e7baa3e3fec | 53 | lineData.lineAddress = 0; |
| mehatfie | 0:22618cf06f45 | 54 | } |
| mehatfie | 0:22618cf06f45 | 55 | |
| mehatfie | 0:22618cf06f45 | 56 | |
| mehatfie | 0:22618cf06f45 | 57 | |
| mehatfie | 1:5731f31f96be | 58 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 59 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 60 | /************************ <FUNCTION: cyclePrograms> *****************************/ |
| mehatfie | 1:5731f31f96be | 61 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 62 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 63 | |
| mehatfie | 2:3e7baa3e3fec | 64 | int cyclePrograms(vector<string> files, int SIZE, int currIndex, int direction){ |
| mehatfie | 0:22618cf06f45 | 65 | |
| mehatfie | 0:22618cf06f45 | 66 | int nextIndex = 0; |
| mehatfie | 0:22618cf06f45 | 67 | switch(direction){ |
| mehatfie | 0:22618cf06f45 | 68 | case 0: //Cycle Back one File |
| mehatfie | 0:22618cf06f45 | 69 | if ((currIndex - 1) < 0) |
| mehatfie | 0:22618cf06f45 | 70 | nextIndex = SIZE - 1; |
| mehatfie | 0:22618cf06f45 | 71 | else |
| mehatfie | 0:22618cf06f45 | 72 | nextIndex = currIndex - 1; |
| mehatfie | 0:22618cf06f45 | 73 | break; |
| mehatfie | 0:22618cf06f45 | 74 | case 1: //Cycle Forward one File |
| mehatfie | 0:22618cf06f45 | 75 | if ((currIndex + 1) >= SIZE) |
| mehatfie | 0:22618cf06f45 | 76 | nextIndex = 0; |
| mehatfie | 0:22618cf06f45 | 77 | else |
| mehatfie | 0:22618cf06f45 | 78 | nextIndex = currIndex + 1; |
| mehatfie | 0:22618cf06f45 | 79 | break; |
| mehatfie | 0:22618cf06f45 | 80 | case -1: //set the selectedFile to the currIndex (used for initialization) |
| mehatfie | 0:22618cf06f45 | 81 | nextIndex = currIndex; |
| mehatfie | 0:22618cf06f45 | 82 | break; |
| mehatfie | 0:22618cf06f45 | 83 | } |
| mehatfie | 0:22618cf06f45 | 84 | |
| mehatfie | 0:22618cf06f45 | 85 | //Output file on Display |
| mehatfie | 0:22618cf06f45 | 86 | lcd.setAddress(0,3); |
| mehatfie | 0:22618cf06f45 | 87 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
| mehatfie | 0:22618cf06f45 | 88 | wait(.2); |
| mehatfie | 0:22618cf06f45 | 89 | lcd.setAddress(0,3); |
| mehatfie | 4:86d0d04cc055 | 90 | lcd.printf("%s", files[nextIndex]); |
| mehatfie | 0:22618cf06f45 | 91 | |
| mehatfie | 0:22618cf06f45 | 92 | return nextIndex; // Return the file index in the Array |
| mehatfie | 0:22618cf06f45 | 93 | } |
| mehatfie | 0:22618cf06f45 | 94 | |
| mehatfie | 0:22618cf06f45 | 95 | |
| mehatfie | 0:22618cf06f45 | 96 | |
| mehatfie | 1:5731f31f96be | 97 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 98 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 99 | /************************** <FUNCTION: loopCommand> *****************************/ |
| mehatfie | 1:5731f31f96be | 100 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 101 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 102 | |
| mehatfie | 2:3e7baa3e3fec | 103 | int loopCommand(FILE *selectedFile, Line &lineData){ |
| mehatfie | 1:5731f31f96be | 104 | |
| mehatfie | 2:3e7baa3e3fec | 105 | //Get the Condition value for number of times to loop |
| mehatfie | 2:3e7baa3e3fec | 106 | string loopCondition = lineData.word[1]; |
| mehatfie | 2:3e7baa3e3fec | 107 | int loopConditionValue = 0; |
| mehatfie | 2:3e7baa3e3fec | 108 | sscanf(loopCondition.c_str(), "%d", &loopConditionValue); |
| mehatfie | 1:5731f31f96be | 109 | |
| mehatfie | 2:3e7baa3e3fec | 110 | int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1; |
| mehatfie | 2:3e7baa3e3fec | 111 | |
| mehatfie | 2:3e7baa3e3fec | 112 | int counter = 0, checkEnd = 0; |
| mehatfie | 1:5731f31f96be | 113 | while (counter < loopConditionValue){ |
| mehatfie | 1:5731f31f96be | 114 | |
| mehatfie | 2:3e7baa3e3fec | 115 | getNextLine(selectedFile, lineData); |
| mehatfie | 2:3e7baa3e3fec | 116 | checkEnd = interpretCommand(selectedFile, lineData); |
| mehatfie | 1:5731f31f96be | 117 | |
| mehatfie | 1:5731f31f96be | 118 | if (firstLineOfLoop){ |
| mehatfie | 1:5731f31f96be | 119 | loopStartAddress = lineData.lineAddress; //Save the Line Address |
| mehatfie | 1:5731f31f96be | 120 | loopLineNumber = lineData.lineNumber; //Save the Line Number |
| mehatfie | 1:5731f31f96be | 121 | firstLineOfLoop = 0; |
| mehatfie | 1:5731f31f96be | 122 | } |
| mehatfie | 1:5731f31f96be | 123 | |
| mehatfie | 1:5731f31f96be | 124 | //Increase the loop counter and go back to the beginning of the loop |
| mehatfie | 1:5731f31f96be | 125 | if (checkEnd == 1){ |
| mehatfie | 2:3e7baa3e3fec | 126 | counter++; |
| mehatfie | 2:3e7baa3e3fec | 127 | |
| mehatfie | 1:5731f31f96be | 128 | lcd.setAddress(0,2); |
| mehatfie | 2:3e7baa3e3fec | 129 | lcd.printf("Loop Cycle %d of %d", counter, loopConditionValue); |
| mehatfie | 2:3e7baa3e3fec | 130 | |
| mehatfie | 1:5731f31f96be | 131 | fseek(selectedFile, loopStartAddress, SEEK_SET); |
| mehatfie | 2:3e7baa3e3fec | 132 | lineData.lineNumber = loopLineNumber - 2; |
| mehatfie | 1:5731f31f96be | 133 | } |
| mehatfie | 1:5731f31f96be | 134 | } |
| mehatfie | 2:3e7baa3e3fec | 135 | |
| mehatfie | 1:5731f31f96be | 136 | return 1; |
| mehatfie | 1:5731f31f96be | 137 | } |
| mehatfie | 1:5731f31f96be | 138 | |
| mehatfie | 1:5731f31f96be | 139 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 140 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 141 | /************************* <FUNCTION: interpretCommand> *************************/ |
| mehatfie | 1:5731f31f96be | 142 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 143 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 144 | |
| mehatfie | 2:3e7baa3e3fec | 145 | int interpretCommand(FILE *selectedFile, Line &lineData){ |
| mehatfie | 2:3e7baa3e3fec | 146 | |
| mehatfie | 2:3e7baa3e3fec | 147 | if (lineData.word[0].compare("device") == 0){ |
| mehatfie | 2:3e7baa3e3fec | 148 | |
| mehatfie | 1:5731f31f96be | 149 | int i = 0, deviceFound = -1; |
| mehatfie | 2:3e7baa3e3fec | 150 | for (i = 0; i < numDevices; i++){ |
| mehatfie | 4:86d0d04cc055 | 151 | if (lineData.word[2].compare(DeviceNames[i]) == 0){ |
| mehatfie | 1:5731f31f96be | 152 | deviceFound = i; |
| mehatfie | 4:86d0d04cc055 | 153 | } |
| mehatfie | 2:3e7baa3e3fec | 154 | } |
| mehatfie | 1:5731f31f96be | 155 | |
| mehatfie | 1:5731f31f96be | 156 | //if the device type does not match any known type, error out |
| mehatfie | 1:5731f31f96be | 157 | if (deviceFound == -1){ |
| mehatfie | 1:5731f31f96be | 158 | //Error Out since the device Name was not matched with anything ************************* |
| mehatfie | 1:5731f31f96be | 159 | } |
| mehatfie | 1:5731f31f96be | 160 | |
| mehatfie | 1:5731f31f96be | 161 | //Add device to the array of devices and initialize it |
| mehatfie | 2:3e7baa3e3fec | 162 | else{ |
| mehatfie | 2:3e7baa3e3fec | 163 | devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData)); |
| mehatfie | 2:3e7baa3e3fec | 164 | devices.back()->name = lineData.word[1]; |
| mehatfie | 2:3e7baa3e3fec | 165 | } |
| mehatfie | 1:5731f31f96be | 166 | } |
| mehatfie | 1:5731f31f96be | 167 | |
| mehatfie | 2:3e7baa3e3fec | 168 | else if (lineData.word[0].compare("delay") == 0){ |
| mehatfie | 2:3e7baa3e3fec | 169 | string duration = lineData.word[1]; |
| mehatfie | 2:3e7baa3e3fec | 170 | int durationValue = 0; |
| mehatfie | 2:3e7baa3e3fec | 171 | sscanf(duration.c_str(), "%d", &durationValue); |
| mehatfie | 1:5731f31f96be | 172 | |
| mehatfie | 1:5731f31f96be | 173 | if (durationValue){ |
| mehatfie | 1:5731f31f96be | 174 | timer.reset(); |
| mehatfie | 1:5731f31f96be | 175 | timer.start(); |
| mehatfie | 1:5731f31f96be | 176 | while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration |
| mehatfie | 1:5731f31f96be | 177 | timer.stop(); //Stop the Timer |
| mehatfie | 1:5731f31f96be | 178 | } |
| mehatfie | 1:5731f31f96be | 179 | else{ |
| mehatfie | 1:5731f31f96be | 180 | //Error Out |
| mehatfie | 1:5731f31f96be | 181 | return -1; |
| mehatfie | 1:5731f31f96be | 182 | } |
| mehatfie | 1:5731f31f96be | 183 | } |
| mehatfie | 1:5731f31f96be | 184 | |
| mehatfie | 2:3e7baa3e3fec | 185 | else if (lineData.word[0].compare("loop") == 0){ |
| mehatfie | 2:3e7baa3e3fec | 186 | int checkLoopEnd = loopCommand(selectedFile, lineData); |
| mehatfie | 1:5731f31f96be | 187 | if (checkLoopEnd == 1) |
| mehatfie | 1:5731f31f96be | 188 | return 1; |
| mehatfie | 1:5731f31f96be | 189 | } |
| mehatfie | 1:5731f31f96be | 190 | |
| mehatfie | 2:3e7baa3e3fec | 191 | else if (lineData.word[0].compare("end") == 0){ |
| mehatfie | 2:3e7baa3e3fec | 192 | if (lineData.word[1].compare("program") == 0){ |
| mehatfie | 1:5731f31f96be | 193 | return 0; |
| mehatfie | 2:3e7baa3e3fec | 194 | } |
| mehatfie | 2:3e7baa3e3fec | 195 | else if (lineData.word[1].compare("loop") == 0){ |
| mehatfie | 1:5731f31f96be | 196 | return 1; |
| mehatfie | 2:3e7baa3e3fec | 197 | } |
| mehatfie | 1:5731f31f96be | 198 | } |
| mehatfie | 1:5731f31f96be | 199 | |
| mehatfie | 1:5731f31f96be | 200 | //not a keyword so check if it's a localName for a device |
| mehatfie | 1:5731f31f96be | 201 | else{ |
| mehatfie | 1:5731f31f96be | 202 | |
| mehatfie | 1:5731f31f96be | 203 | int i = 0, deviceFound = -1; |
| mehatfie | 2:3e7baa3e3fec | 204 | for (i = 0; i < devices.size(); i++){ |
| mehatfie | 2:3e7baa3e3fec | 205 | if (lineData.word[0].compare(devices[i]->name) == 0) |
| mehatfie | 1:5731f31f96be | 206 | deviceFound = i; |
| mehatfie | 1:5731f31f96be | 207 | } |
| mehatfie | 1:5731f31f96be | 208 | |
| mehatfie | 1:5731f31f96be | 209 | //no device was found that matched the local name, and this is also the last error check, meaning it can match no other potential keywords |
| mehatfie | 1:5731f31f96be | 210 | if (deviceFound == -1){ |
| mehatfie | 1:5731f31f96be | 211 | lcd.setAddress(0,3); |
| mehatfie | 1:5731f31f96be | 212 | lcd.printf("Final ERROR!"); |
| mehatfie | 1:5731f31f96be | 213 | wait(10); |
| mehatfie | 1:5731f31f96be | 214 | } |
| mehatfie | 1:5731f31f96be | 215 | |
| mehatfie | 1:5731f31f96be | 216 | //Local Name matches a device, send line to that device in order to process the functionality |
| mehatfie | 2:3e7baa3e3fec | 217 | else{ |
| mehatfie | 2:3e7baa3e3fec | 218 | //addDevice(deviceFound); |
| mehatfie | 2:3e7baa3e3fec | 219 | devices[deviceFound]->interpret(lineData); |
| mehatfie | 2:3e7baa3e3fec | 220 | } |
| mehatfie | 1:5731f31f96be | 221 | } |
| mehatfie | 1:5731f31f96be | 222 | |
| mehatfie | 1:5731f31f96be | 223 | return -1; |
| mehatfie | 1:5731f31f96be | 224 | } |
| mehatfie | 1:5731f31f96be | 225 | |
| mehatfie | 1:5731f31f96be | 226 | |
| mehatfie | 1:5731f31f96be | 227 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 228 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 229 | /****************************** <FUNCTION: main> ********************************/ |
| mehatfie | 1:5731f31f96be | 230 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 231 | /**********************************************************************************************************************************/ |
| mehatfie | 1:5731f31f96be | 232 | |
| mehatfie | 0:22618cf06f45 | 233 | int main() { |
| mehatfie | 0:22618cf06f45 | 234 | |
| mehatfie | 0:22618cf06f45 | 235 | fullInit(); //Initialize anything that's required to run the code (LCD) |
| mehatfie | 0:22618cf06f45 | 236 | |
| mehatfie | 2:3e7baa3e3fec | 237 | struct Line lineData; |
| mehatfie | 2:3e7baa3e3fec | 238 | resetLineData(lineData); |
| mehatfie | 2:3e7baa3e3fec | 239 | |
| mehatfie | 0:22618cf06f45 | 240 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 241 | /*** <Get all the Potential Programs> ***/ |
| mehatfie | 0:22618cf06f45 | 242 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 243 | int numTextFiles = 0; |
| mehatfie | 2:3e7baa3e3fec | 244 | vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card |
| mehatfie | 2:3e7baa3e3fec | 245 | vector<string> filenames = readFileNames("/sd"); |
| mehatfie | 2:3e7baa3e3fec | 246 | numTextFiles = getFileNamesWithoutExt(textFiles, filenames); |
| mehatfie | 0:22618cf06f45 | 247 | |
| mehatfie | 0:22618cf06f45 | 248 | |
| mehatfie | 0:22618cf06f45 | 249 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 250 | /*** <Select the Program txt File> ***/ |
| mehatfie | 0:22618cf06f45 | 251 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 252 | int fileSelected = 0, selectedFileIndex = 0; |
| mehatfie | 0:22618cf06f45 | 253 | |
| mehatfie | 0:22618cf06f45 | 254 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 255 | lcd.setAddress(0,1); |
| mehatfie | 0:22618cf06f45 | 256 | lcd.printf("Select Your Program"); |
| mehatfie | 0:22618cf06f45 | 257 | lcd.setAddress(0,2); |
| mehatfie | 0:22618cf06f45 | 258 | lcd.printf("Num Programs = %d", numTextFiles); |
| mehatfie | 0:22618cf06f45 | 259 | |
| mehatfie | 0:22618cf06f45 | 260 | uint8_t lastButState; |
| mehatfie | 0:22618cf06f45 | 261 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
| mehatfie | 2:3e7baa3e3fec | 262 | |
| mehatfie | 0:22618cf06f45 | 263 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen |
| mehatfie | 0:22618cf06f45 | 264 | while(!fileSelected) { |
| mehatfie | 0:22618cf06f45 | 265 | |
| mehatfie | 0:22618cf06f45 | 266 | uint8_t curButState = buttons.readBus(); |
| mehatfie | 0:22618cf06f45 | 267 | if(curButState != lastButState){ |
| mehatfie | 0:22618cf06f45 | 268 | lastButState = curButState; |
| mehatfie | 0:22618cf06f45 | 269 | if(buttons.readRight()) |
| mehatfie | 0:22618cf06f45 | 270 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 1); |
| mehatfie | 0:22618cf06f45 | 271 | else if(buttons.readLeft()) |
| mehatfie | 0:22618cf06f45 | 272 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 0); |
| mehatfie | 0:22618cf06f45 | 273 | else if(buttons.readSel()) |
| mehatfie | 0:22618cf06f45 | 274 | fileSelected = 1; |
| mehatfie | 0:22618cf06f45 | 275 | } |
| mehatfie | 0:22618cf06f45 | 276 | } |
| mehatfie | 0:22618cf06f45 | 277 | |
| mehatfie | 0:22618cf06f45 | 278 | char selectedFileName[50]; |
| mehatfie | 0:22618cf06f45 | 279 | strcpy(selectedFileName, textFiles[selectedFileIndex].c_str()); |
| mehatfie | 0:22618cf06f45 | 280 | |
| mehatfie | 0:22618cf06f45 | 281 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 282 | /*** <Open the Program txt File> ***/ |
| mehatfie | 0:22618cf06f45 | 283 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 284 | |
| mehatfie | 0:22618cf06f45 | 285 | //Create the string of the full directory and path to the program txt file |
| mehatfie | 0:22618cf06f45 | 286 | char selectedFileFullName[100] = "/sd/"; //Assuming that no directory and file name will be longer than 100 characters |
| mehatfie | 0:22618cf06f45 | 287 | strcat(selectedFileFullName, selectedFileName); |
| mehatfie | 0:22618cf06f45 | 288 | strcat(selectedFileFullName, ".txt"); |
| mehatfie | 0:22618cf06f45 | 289 | |
| mehatfie | 0:22618cf06f45 | 290 | FILE *selectedFile = fopen(selectedFileFullName, "r"); |
| mehatfie | 2:3e7baa3e3fec | 291 | |
| mehatfie | 0:22618cf06f45 | 292 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 293 | |
| mehatfie | 0:22618cf06f45 | 294 | if(selectedFile == NULL) { |
| mehatfie | 0:22618cf06f45 | 295 | lcd.setAddress(0,0); |
| mehatfie | 0:22618cf06f45 | 296 | lcd.printf("Invalid"); |
| mehatfie | 0:22618cf06f45 | 297 | wait(10); |
| mehatfie | 0:22618cf06f45 | 298 | } |
| mehatfie | 0:22618cf06f45 | 299 | |
| mehatfie | 0:22618cf06f45 | 300 | |
| mehatfie | 0:22618cf06f45 | 301 | while(1){ |
| mehatfie | 0:22618cf06f45 | 302 | |
| mehatfie | 2:3e7baa3e3fec | 303 | resetLineData(lineData); |
| mehatfie | 0:22618cf06f45 | 304 | |
| mehatfie | 0:22618cf06f45 | 305 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 306 | lcd.setAddress(0,0); |
| mehatfie | 0:22618cf06f45 | 307 | lcd.printf("Program: %s", selectedFileName); |
| mehatfie | 0:22618cf06f45 | 308 | |
| mehatfie | 0:22618cf06f45 | 309 | |
| mehatfie | 0:22618cf06f45 | 310 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 311 | /*** <Start Running through the Program txt File Lines> ***/ |
| mehatfie | 0:22618cf06f45 | 312 | /******************************************************************************/ |
| mehatfie | 0:22618cf06f45 | 313 | |
| mehatfie | 0:22618cf06f45 | 314 | int endOfFile = 0; |
| mehatfie | 0:22618cf06f45 | 315 | |
| mehatfie | 0:22618cf06f45 | 316 | while (!endOfFile){ |
| mehatfie | 0:22618cf06f45 | 317 | |
| mehatfie | 2:3e7baa3e3fec | 318 | getNextLine(selectedFile, lineData); |
| mehatfie | 2:3e7baa3e3fec | 319 | int checkEnd = interpretCommand(selectedFile, lineData); |
| mehatfie | 2:3e7baa3e3fec | 320 | |
| mehatfie | 1:5731f31f96be | 321 | if (checkEnd == 0) |
| mehatfie | 1:5731f31f96be | 322 | endOfFile = 1; |
| mehatfie | 0:22618cf06f45 | 323 | } |
| mehatfie | 0:22618cf06f45 | 324 | |
| mehatfie | 0:22618cf06f45 | 325 | |
| mehatfie | 0:22618cf06f45 | 326 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 327 | lcd.setAddress(0,0); |
| mehatfie | 0:22618cf06f45 | 328 | lcd.printf("END OF PROGRAM"); |
| mehatfie | 0:22618cf06f45 | 329 | lcd.setAddress(0,2); |
| mehatfie | 0:22618cf06f45 | 330 | lcd.printf("To Restart..."); |
| mehatfie | 0:22618cf06f45 | 331 | lcd.setAddress(0,3); |
| mehatfie | 0:22618cf06f45 | 332 | lcd.printf("Press BACK"); |
| mehatfie | 0:22618cf06f45 | 333 | |
| mehatfie | 0:22618cf06f45 | 334 | while(!buttons.readBack()); |
| mehatfie | 0:22618cf06f45 | 335 | |
| mehatfie | 0:22618cf06f45 | 336 | lcd.setAddress(0,1); |
| mehatfie | 2:3e7baa3e3fec | 337 | //rewind(selectedFile); |
| mehatfie | 0:22618cf06f45 | 338 | } |
| mehatfie | 0:22618cf06f45 | 339 | |
| mehatfie | 2:3e7baa3e3fec | 340 | } |
| mehatfie | 2:3e7baa3e3fec | 341 | |
| mehatfie | 2:3e7baa3e3fec | 342 | |
| mehatfie | 2:3e7baa3e3fec | 343 | |
| mehatfie | 2:3e7baa3e3fec | 344 | |
| mehatfie | 2:3e7baa3e3fec | 345 | |
| mehatfie | 2:3e7baa3e3fec | 346 | |
| mehatfie | 2:3e7baa3e3fec | 347 | |
| mehatfie | 2:3e7baa3e3fec | 348 | |
| mehatfie | 2:3e7baa3e3fec | 349 | |
| mehatfie | 2:3e7baa3e3fec | 350 | |
| mehatfie | 2:3e7baa3e3fec | 351 | |
| mehatfie | 2:3e7baa3e3fec | 352 | |
| mehatfie | 2:3e7baa3e3fec | 353 | |
| mehatfie | 2:3e7baa3e3fec | 354 | |
| mehatfie | 2:3e7baa3e3fec | 355 | |
| mehatfie | 2:3e7baa3e3fec | 356 | |
| mehatfie | 2:3e7baa3e3fec | 357 | |
| mehatfie | 2:3e7baa3e3fec | 358 | |
| mehatfie | 2:3e7baa3e3fec | 359 | |
| mehatfie | 2:3e7baa3e3fec | 360 | |
| mehatfie | 0:22618cf06f45 | 361 | /***** Cycle through txt lines and remember last lines ******/ |
| mehatfie | 0:22618cf06f45 | 362 | /* |
| mehatfie | 0:22618cf06f45 | 363 | int running = 0, selectedFile = 0; |
| mehatfie | 0:22618cf06f45 | 364 | int locCount = 0, tempSpot = 0; |
| mehatfie | 0:22618cf06f45 | 365 | int loc[4]; |
| mehatfie | 0:22618cf06f45 | 366 | char line[32]; |
| mehatfie | 0:22618cf06f45 | 367 | uint8_t lastButState; |
| mehatfie | 0:22618cf06f45 | 368 | |
| mehatfie | 0:22618cf06f45 | 369 | |
| mehatfie | 0:22618cf06f45 | 370 | while(1){ |
| mehatfie | 0:22618cf06f45 | 371 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
| mehatfie | 0:22618cf06f45 | 372 | while(!running) { |
| mehatfie | 0:22618cf06f45 | 373 | uint8_t curButState = buttons.readBus(); |
| mehatfie | 0:22618cf06f45 | 374 | |
| mehatfie | 0:22618cf06f45 | 375 | if(curButState != lastButState){ |
| mehatfie | 0:22618cf06f45 | 376 | switch(lastButState = curButState){ |
| mehatfie | 0:22618cf06f45 | 377 | case 0x1F: //right |
| mehatfie | 0:22618cf06f45 | 378 | |
| mehatfie | 0:22618cf06f45 | 379 | loc[locCount] = ftell(fp); |
| mehatfie | 0:22618cf06f45 | 380 | |
| mehatfie | 0:22618cf06f45 | 381 | if (locCount >= 3) |
| mehatfie | 0:22618cf06f45 | 382 | locCount = 0; |
| mehatfie | 0:22618cf06f45 | 383 | else |
| mehatfie | 0:22618cf06f45 | 384 | locCount++; |
| mehatfie | 0:22618cf06f45 | 385 | |
| mehatfie | 0:22618cf06f45 | 386 | //tempSpot = ftell(fp); |
| mehatfie | 0:22618cf06f45 | 387 | fgets(line, 32, fp); |
| mehatfie | 0:22618cf06f45 | 388 | |
| mehatfie | 0:22618cf06f45 | 389 | lcd.setAddress(0,1); |
| mehatfie | 0:22618cf06f45 | 390 | lcd.printf("L: %s", line); |
| mehatfie | 0:22618cf06f45 | 391 | wait(0.2); |
| mehatfie | 0:22618cf06f45 | 392 | break; |
| mehatfie | 0:22618cf06f45 | 393 | case 0x2F: //left |
| mehatfie | 0:22618cf06f45 | 394 | |
| mehatfie | 0:22618cf06f45 | 395 | if (locCount == 0) { |
| mehatfie | 0:22618cf06f45 | 396 | locCount = 3; |
| mehatfie | 0:22618cf06f45 | 397 | fseek(fp, loc[locCount], SEEK_SET); |
| mehatfie | 0:22618cf06f45 | 398 | } |
| mehatfie | 0:22618cf06f45 | 399 | else { |
| mehatfie | 0:22618cf06f45 | 400 | fseek(fp, loc[locCount - 1], SEEK_SET); |
| mehatfie | 0:22618cf06f45 | 401 | locCount--; |
| mehatfie | 0:22618cf06f45 | 402 | } |
| mehatfie | 0:22618cf06f45 | 403 | fgets(line, 32, fp); |
| mehatfie | 0:22618cf06f45 | 404 | lcd.setAddress(0,1); |
| mehatfie | 0:22618cf06f45 | 405 | lcd.printf("L: %s", line); |
| mehatfie | 0:22618cf06f45 | 406 | wait(0.2); |
| mehatfie | 0:22618cf06f45 | 407 | break; |
| mehatfie | 0:22618cf06f45 | 408 | } |
| mehatfie | 0:22618cf06f45 | 409 | } |
| mehatfie | 0:22618cf06f45 | 410 | } |
| mehatfie | 0:22618cf06f45 | 411 | } |
| mehatfie | 0:22618cf06f45 | 412 | */ |
| mehatfie | 2:3e7baa3e3fec | 413 | |
| mehatfie | 0:22618cf06f45 | 414 | |
| mehatfie | 0:22618cf06f45 | 415 | |
| mehatfie | 0:22618cf06f45 | 416 | /******* Select the Program txt File ***********/ |
| mehatfie | 0:22618cf06f45 | 417 | /* |
| mehatfie | 0:22618cf06f45 | 418 | int numTextFiles = 0; |
| mehatfie | 0:22618cf06f45 | 419 | string tempTextFiles[25]; //Assuming Maximum of 25 txt files will be on the SD Card |
| mehatfie | 0:22618cf06f45 | 420 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 421 | readFileNames("/sd"); |
| mehatfie | 0:22618cf06f45 | 422 | numTextFiles = getFileNamesWithoutExt(tempTextFiles); |
| mehatfie | 0:22618cf06f45 | 423 | |
| mehatfie | 0:22618cf06f45 | 424 | string *textFiles = resize_StringArr(tempTextFiles, numTextFiles); //Resize Array |
| mehatfie | 0:22618cf06f45 | 425 | //delete [] tempTextFiles; //free previous array |
| mehatfie | 0:22618cf06f45 | 426 | |
| mehatfie | 0:22618cf06f45 | 427 | |
| mehatfie | 0:22618cf06f45 | 428 | int running = 0, selectedFile = 0; |
| mehatfie | 0:22618cf06f45 | 429 | |
| mehatfie | 0:22618cf06f45 | 430 | lcd.cls(); //clear the display |
| mehatfie | 0:22618cf06f45 | 431 | lcd.setAddress(0,1); |
| mehatfie | 0:22618cf06f45 | 432 | lcd.printf("Select Your Program"); |
| mehatfie | 0:22618cf06f45 | 433 | lcd.setAddress(0,2); |
| mehatfie | 0:22618cf06f45 | 434 | lcd.printf("Num Programs = %d", numTextFiles); |
| mehatfie | 0:22618cf06f45 | 435 | |
| mehatfie | 0:22618cf06f45 | 436 | uint8_t lastButState; |
| mehatfie | 0:22618cf06f45 | 437 | while(1){ |
| mehatfie | 0:22618cf06f45 | 438 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
| mehatfie | 0:22618cf06f45 | 439 | while(!running) { |
| mehatfie | 0:22618cf06f45 | 440 | uint8_t curButState = buttons.readBus(); |
| mehatfie | 0:22618cf06f45 | 441 | |
| mehatfie | 0:22618cf06f45 | 442 | if(curButState != lastButState){ |
| mehatfie | 0:22618cf06f45 | 443 | switch(lastButState = curButState){ |
| mehatfie | 0:22618cf06f45 | 444 | case 0x1F: //right |
| mehatfie | 0:22618cf06f45 | 445 | selectedFile = cyclePrograms(textFiles, numTextFiles, selectedFile, 1); |
| mehatfie | 0:22618cf06f45 | 446 | break; |
| mehatfie | 0:22618cf06f45 | 447 | case 0x2F: //left |
| mehatfie | 0:22618cf06f45 | 448 | selectedFile = cyclePrograms(textFiles, numTextFiles, selectedFile, 0); |
| mehatfie | 0:22618cf06f45 | 449 | break; |
| mehatfie | 0:22618cf06f45 | 450 | } |
| mehatfie | 0:22618cf06f45 | 451 | } |
| mehatfie | 0:22618cf06f45 | 452 | } |
| mehatfie | 0:22618cf06f45 | 453 | } |
| mehatfie | 0:22618cf06f45 | 454 | |
| mehatfie | 0:22618cf06f45 | 455 | */ |
| mehatfie | 0:22618cf06f45 | 456 | |
| mehatfie | 0:22618cf06f45 | 457 | /*float speed = 0.5; |
| mehatfie | 0:22618cf06f45 | 458 | while(1){ |
| mehatfie | 0:22618cf06f45 | 459 | bridges.drive(1, -1*speed); |
| mehatfie | 0:22618cf06f45 | 460 | wait(2); |
| mehatfie | 0:22618cf06f45 | 461 | bridges.drive(1, speed); |
| mehatfie | 0:22618cf06f45 | 462 | wait(2); |
| mehatfie | 0:22618cf06f45 | 463 | }*/ |
| mehatfie | 0:22618cf06f45 | 464 | |
| mehatfie | 0:22618cf06f45 | 465 | //BridgeDriver::MOTOR_A |