![](/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
main.cpp@7:cca801103b86, 2014-09-23 (annotated)
- Committer:
- mehatfie
- Date:
- Tue Sep 23 23:44:16 2014 +0000
- Revision:
- 7:cca801103b86
- Parent:
- 6:d1594fd2ec5a
- Child:
- 8:e9f836163229
- testing "make working copy" function of revisions
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 "TextFile.h" |
mehatfie | 0:22618cf06f45 | 10 | #include <stdio.h> |
mehatfie | 0:22618cf06f45 | 11 | #include <string> |
mehatfie | 0:22618cf06f45 | 12 | #include <stdlib.h> |
mehatfie | 0:22618cf06f45 | 13 | #include <fstream> |
mehatfie | 0:22618cf06f45 | 14 | #include <vector> |
mehatfie | 0:22618cf06f45 | 15 | using std::string; |
mehatfie | 0:22618cf06f45 | 16 | |
mehatfie | 1:5731f31f96be | 17 | FrontPanelButtons buttons(&i2c); |
mehatfie | 1:5731f31f96be | 18 | |
mehatfie | 0:22618cf06f45 | 19 | //extern "C" void mbed_reset(); //enable software reset of code |
mehatfie | 0:22618cf06f45 | 20 | |
mehatfie | 1:5731f31f96be | 21 | |
mehatfie | 1:5731f31f96be | 22 | |
mehatfie | 5:e36e0538a903 | 23 | int interpretCommand(FILE *, LineData &); |
mehatfie | 5:e36e0538a903 | 24 | int loopCommand(FILE *, LineData &); |
mehatfie | 1:5731f31f96be | 25 | |
mehatfie | 1:5731f31f96be | 26 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 27 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 28 | /*************************** <FUNCTION: resetLineData> **************************/ |
mehatfie | 1:5731f31f96be | 29 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 30 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 31 | void resetLineData(LineData &lineData){ |
mehatfie | 0:22618cf06f45 | 32 | |
mehatfie | 2:3e7baa3e3fec | 33 | //lineData.fullLine.clear(); |
mehatfie | 0:22618cf06f45 | 34 | lineData.lineNumber = 0; |
mehatfie | 0:22618cf06f45 | 35 | lineData.numWords = 0; |
mehatfie | 2:3e7baa3e3fec | 36 | lineData.lineAddress = 0; |
mehatfie | 0:22618cf06f45 | 37 | } |
mehatfie | 0:22618cf06f45 | 38 | |
mehatfie | 0:22618cf06f45 | 39 | |
mehatfie | 0:22618cf06f45 | 40 | |
mehatfie | 1:5731f31f96be | 41 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 42 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 43 | /************************ <FUNCTION: cyclePrograms> *****************************/ |
mehatfie | 1:5731f31f96be | 44 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 45 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 46 | |
mehatfie | 2:3e7baa3e3fec | 47 | int cyclePrograms(vector<string> files, int SIZE, int currIndex, int direction){ |
mehatfie | 0:22618cf06f45 | 48 | |
mehatfie | 0:22618cf06f45 | 49 | int nextIndex = 0; |
mehatfie | 0:22618cf06f45 | 50 | switch(direction){ |
mehatfie | 0:22618cf06f45 | 51 | case 0: //Cycle Back one File |
mehatfie | 0:22618cf06f45 | 52 | if ((currIndex - 1) < 0) |
mehatfie | 0:22618cf06f45 | 53 | nextIndex = SIZE - 1; |
mehatfie | 0:22618cf06f45 | 54 | else |
mehatfie | 0:22618cf06f45 | 55 | nextIndex = currIndex - 1; |
mehatfie | 0:22618cf06f45 | 56 | break; |
mehatfie | 0:22618cf06f45 | 57 | case 1: //Cycle Forward one File |
mehatfie | 0:22618cf06f45 | 58 | if ((currIndex + 1) >= SIZE) |
mehatfie | 0:22618cf06f45 | 59 | nextIndex = 0; |
mehatfie | 0:22618cf06f45 | 60 | else |
mehatfie | 0:22618cf06f45 | 61 | nextIndex = currIndex + 1; |
mehatfie | 0:22618cf06f45 | 62 | break; |
mehatfie | 0:22618cf06f45 | 63 | case -1: //set the selectedFile to the currIndex (used for initialization) |
mehatfie | 0:22618cf06f45 | 64 | nextIndex = currIndex; |
mehatfie | 0:22618cf06f45 | 65 | break; |
mehatfie | 0:22618cf06f45 | 66 | } |
mehatfie | 0:22618cf06f45 | 67 | |
mehatfie | 0:22618cf06f45 | 68 | //Output file on Display |
mehatfie | 0:22618cf06f45 | 69 | lcd.setAddress(0,3); |
mehatfie | 0:22618cf06f45 | 70 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
mehatfie | 0:22618cf06f45 | 71 | wait(.2); |
mehatfie | 0:22618cf06f45 | 72 | lcd.setAddress(0,3); |
mehatfie | 4:86d0d04cc055 | 73 | lcd.printf("%s", files[nextIndex]); |
mehatfie | 0:22618cf06f45 | 74 | |
mehatfie | 0:22618cf06f45 | 75 | return nextIndex; // Return the file index in the Array |
mehatfie | 0:22618cf06f45 | 76 | } |
mehatfie | 0:22618cf06f45 | 77 | |
mehatfie | 0:22618cf06f45 | 78 | |
mehatfie | 5:e36e0538a903 | 79 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 80 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 81 | /************************** <FUNCTION: conditionCommand> *****************************/ |
mehatfie | 5:e36e0538a903 | 82 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 83 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 84 | |
mehatfie | 5:e36e0538a903 | 85 | //Create an enum map of the positble conditions |
mehatfie | 5:e36e0538a903 | 86 | enum ConditionType{xAND, AND, xOR, OR, NONE}; |
mehatfie | 5:e36e0538a903 | 87 | //static const enum ConditionType Condition_Map[] = {xAND, AND, xOR, OR}; |
mehatfie | 5:e36e0538a903 | 88 | |
mehatfie | 5:e36e0538a903 | 89 | struct ConditionOp{ |
mehatfie | 5:e36e0538a903 | 90 | |
mehatfie | 5:e36e0538a903 | 91 | int value; //returned value of the interpret function: 1 = meets criteria, 0 = criteria not met, -1 = failed to interpret |
mehatfie | 5:e36e0538a903 | 92 | ConditionType op; //operator that follows the given parameter: x val 2 AND y val 3, if this ConditionOp is for x, then the value will be AND |
mehatfie | 5:e36e0538a903 | 93 | }; |
mehatfie | 5:e36e0538a903 | 94 | |
mehatfie | 5:e36e0538a903 | 95 | int conditionCommand(FILE *selectedFile, LineData &lineData){ |
mehatfie | 5:e36e0538a903 | 96 | |
mehatfie | 5:e36e0538a903 | 97 | //Get the number of condition parameters |
mehatfie | 5:e36e0538a903 | 98 | string numConditionVals = lineData.word[1]; |
mehatfie | 5:e36e0538a903 | 99 | int numConditionValues = 0; |
mehatfie | 5:e36e0538a903 | 100 | sscanf(numConditionVals.c_str(), "%d", &numConditionValues); |
mehatfie | 5:e36e0538a903 | 101 | |
mehatfie | 5:e36e0538a903 | 102 | //LineData tempLineData; |
mehatfie | 5:e36e0538a903 | 103 | LineData param[15]; |
mehatfie | 5:e36e0538a903 | 104 | //vector<LineData> param; |
mehatfie | 5:e36e0538a903 | 105 | vector<ConditionOp> paramCondition; |
mehatfie | 5:e36e0538a903 | 106 | |
mehatfie | 5:e36e0538a903 | 107 | |
mehatfie | 5:e36e0538a903 | 108 | //Fill the param Vector with Line structs of each individual device, this way we can send the Line struct to the appropriate interpret function without modification within the function itself |
mehatfie | 5:e36e0538a903 | 109 | int i = 2, numParam = 0, paramNumWords = 0; |
mehatfie | 5:e36e0538a903 | 110 | for (i = 2; i < lineData.numWords; i++){ |
mehatfie | 6:d1594fd2ec5a | 111 | |
mehatfie | 5:e36e0538a903 | 112 | // if the word is not an AND or an OR, it must mean it's for the specific function |
mehatfie | 5:e36e0538a903 | 113 | // set the current parameter's next word to be equal to the current word we're checking |
mehatfie | 5:e36e0538a903 | 114 | // increase number of words that the parameter has |
mehatfie | 5:e36e0538a903 | 115 | if (lineData.word[i] != "AND" && lineData.word[i] != "xAND" && lineData.word[i] != "OR" && lineData.word[i] != "xOR"){ |
mehatfie | 6:d1594fd2ec5a | 116 | |
mehatfie | 5:e36e0538a903 | 117 | //tempLineData.word[paramNumWords] = lineData.word[i]; |
mehatfie | 5:e36e0538a903 | 118 | param[numParam].word[paramNumWords] = lineData.word[i]; |
mehatfie | 5:e36e0538a903 | 119 | paramNumWords++; |
mehatfie | 5:e36e0538a903 | 120 | |
mehatfie | 5:e36e0538a903 | 121 | //if this is the last word in the line.... |
mehatfie | 5:e36e0538a903 | 122 | if(i == (lineData.numWords - 1)){ |
mehatfie | 5:e36e0538a903 | 123 | param[numParam].numWords = paramNumWords; |
mehatfie | 5:e36e0538a903 | 124 | paramCondition[numParam].op = NONE; |
mehatfie | 5:e36e0538a903 | 125 | numParam++; |
mehatfie | 5:e36e0538a903 | 126 | } |
mehatfie | 5:e36e0538a903 | 127 | |
mehatfie | 5:e36e0538a903 | 128 | } |
mehatfie | 5:e36e0538a903 | 129 | |
mehatfie | 5:e36e0538a903 | 130 | // if the word is an AND or an OR, it must mean the last function has been completely identified |
mehatfie | 5:e36e0538a903 | 131 | // set the parameters number of Words value to the calculated value |
mehatfie | 5:e36e0538a903 | 132 | // increase the number of Parameters (the current parameter function we're filling) |
mehatfie | 5:e36e0538a903 | 133 | else if (lineData.word[i].compare("AND") == 0 || lineData.word[i].compare("xAND") == 0 || lineData.word[i].compare("OR") == 0 || lineData.word[i].compare("xOR") == 0){ |
mehatfie | 5:e36e0538a903 | 134 | |
mehatfie | 5:e36e0538a903 | 135 | //tempLineData.numWords = paramNumWords; |
mehatfie | 5:e36e0538a903 | 136 | param[numParam].numWords = paramNumWords; |
mehatfie | 5:e36e0538a903 | 137 | |
mehatfie | 5:e36e0538a903 | 138 | paramCondition.push_back(ConditionOp()); |
mehatfie | 5:e36e0538a903 | 139 | if (lineData.word[i].compare("AND") == 0) |
mehatfie | 5:e36e0538a903 | 140 | paramCondition[numParam].op = AND; |
mehatfie | 5:e36e0538a903 | 141 | else if (lineData.word[i].compare("xAND") == 0) |
mehatfie | 5:e36e0538a903 | 142 | paramCondition[numParam].op = xAND; |
mehatfie | 5:e36e0538a903 | 143 | else if (lineData.word[i].compare("OR") == 0) |
mehatfie | 5:e36e0538a903 | 144 | paramCondition[numParam].op = OR; |
mehatfie | 5:e36e0538a903 | 145 | else if (lineData.word[i].compare("xOR") == 0) |
mehatfie | 5:e36e0538a903 | 146 | paramCondition[numParam].op = xOR; |
mehatfie | 5:e36e0538a903 | 147 | |
mehatfie | 5:e36e0538a903 | 148 | //param.push_back(LineData()); |
mehatfie | 5:e36e0538a903 | 149 | //param[numParam] = tempLineData; |
mehatfie | 5:e36e0538a903 | 150 | //param.push_back(tempLineData); //add it to the vector list of parameters |
mehatfie | 5:e36e0538a903 | 151 | //tempLineData = LineData(); //reset |
mehatfie | 5:e36e0538a903 | 152 | numParam++; // increase the index of param |
mehatfie | 5:e36e0538a903 | 153 | paramNumWords = 0; // reset the number of words |
mehatfie | 5:e36e0538a903 | 154 | } |
mehatfie | 5:e36e0538a903 | 155 | } |
mehatfie | 5:e36e0538a903 | 156 | |
mehatfie | 5:e36e0538a903 | 157 | |
mehatfie | 5:e36e0538a903 | 158 | vector<ConditionOp> combinedCondition; |
mehatfie | 5:e36e0538a903 | 159 | ConditionOp tempCombinedCondition; |
mehatfie | 6:d1594fd2ec5a | 160 | int j = 0, k = 0; |
mehatfie | 5:e36e0538a903 | 161 | for (j = 0; j < numParam; j++){ |
mehatfie | 5:e36e0538a903 | 162 | paramCondition[j].value = interpretCommand(selectedFile, param[j]); |
mehatfie | 5:e36e0538a903 | 163 | } |
mehatfie | 5:e36e0538a903 | 164 | |
mehatfie | 5:e36e0538a903 | 165 | //create the combined Condition vector (take care of this xAND and xOR statements and combine them into one so that the whole vector is just AND's and OR's) |
mehatfie | 5:e36e0538a903 | 166 | //this should make the xAND's / xOR's into a single member of the combinedCondition vector |
mehatfie | 5:e36e0538a903 | 167 | enum ConditionType prevCondition = NONE; |
mehatfie | 5:e36e0538a903 | 168 | int first = 1, last = 0; |
mehatfie | 5:e36e0538a903 | 169 | for (k = 0; k < numParam; k++){ |
mehatfie | 6:d1594fd2ec5a | 170 | |
mehatfie | 5:e36e0538a903 | 171 | if (k == numParam - 1) |
mehatfie | 5:e36e0538a903 | 172 | last = 1; |
mehatfie | 5:e36e0538a903 | 173 | |
mehatfie | 5:e36e0538a903 | 174 | if (!last && (prevCondition != AND || prevCondition != OR)){ |
mehatfie | 5:e36e0538a903 | 175 | if (paramCondition[k].op != xAND && paramCondition[k].op != xOR && paramCondition[k + 1].op != xAND && paramCondition[k + 1].op != xOR){ |
mehatfie | 6:d1594fd2ec5a | 176 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 177 | lcd.printf("Test 0.1 "); |
mehatfie | 6:d1594fd2ec5a | 178 | wait(1); |
mehatfie | 5:e36e0538a903 | 179 | |
mehatfie | 5:e36e0538a903 | 180 | //AND |
mehatfie | 5:e36e0538a903 | 181 | if (paramCondition[k].op == AND){ |
mehatfie | 6:d1594fd2ec5a | 182 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 183 | lcd.printf("Test 1.1 "); |
mehatfie | 6:d1594fd2ec5a | 184 | wait(1); |
mehatfie | 5:e36e0538a903 | 185 | if (!first && prevCondition != xAND && prevCondition != xOR){ |
mehatfie | 6:d1594fd2ec5a | 186 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 187 | lcd.printf("Test 1.11 "); |
mehatfie | 6:d1594fd2ec5a | 188 | wait(1); |
mehatfie | 5:e36e0538a903 | 189 | combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value; |
mehatfie | 6:d1594fd2ec5a | 190 | } |
mehatfie | 5:e36e0538a903 | 191 | else if (first || prevCondition == xAND || prevCondition == xOR){ |
mehatfie | 6:d1594fd2ec5a | 192 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 193 | lcd.printf("Test 1.12 "); |
mehatfie | 6:d1594fd2ec5a | 194 | wait(1); |
mehatfie | 5:e36e0538a903 | 195 | tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 196 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 5:e36e0538a903 | 197 | first = 0; |
mehatfie | 5:e36e0538a903 | 198 | } |
mehatfie | 5:e36e0538a903 | 199 | prevCondition = AND; |
mehatfie | 5:e36e0538a903 | 200 | } |
mehatfie | 5:e36e0538a903 | 201 | |
mehatfie | 5:e36e0538a903 | 202 | //OR |
mehatfie | 5:e36e0538a903 | 203 | else if (paramCondition[k].op == OR){ |
mehatfie | 6:d1594fd2ec5a | 204 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 205 | lcd.printf("Test 1.2 "); |
mehatfie | 6:d1594fd2ec5a | 206 | wait(1); |
mehatfie | 5:e36e0538a903 | 207 | if (!first && prevCondition != xAND && prevCondition != xOR){ |
mehatfie | 6:d1594fd2ec5a | 208 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 209 | lcd.printf("Test 1.3 "); |
mehatfie | 6:d1594fd2ec5a | 210 | wait(1); |
mehatfie | 5:e36e0538a903 | 211 | combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value; |
mehatfie | 6:d1594fd2ec5a | 212 | } |
mehatfie | 6:d1594fd2ec5a | 213 | else if (first || prevCondition == xAND || prevCondition == xOR){ |
mehatfie | 6:d1594fd2ec5a | 214 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 215 | lcd.printf("Test 1.4 "); |
mehatfie | 6:d1594fd2ec5a | 216 | wait(1); |
mehatfie | 5:e36e0538a903 | 217 | tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 218 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 5:e36e0538a903 | 219 | first = 0; |
mehatfie | 5:e36e0538a903 | 220 | } |
mehatfie | 5:e36e0538a903 | 221 | prevCondition = OR; |
mehatfie | 5:e36e0538a903 | 222 | } |
mehatfie | 5:e36e0538a903 | 223 | } |
mehatfie | 5:e36e0538a903 | 224 | |
mehatfie | 6:d1594fd2ec5a | 225 | // first value is something, not exclusive, but next values are exclusive |
mehatfie | 6:d1594fd2ec5a | 226 | else if (first && (paramCondition[k].op == AND || paramCondition[k].op == OR) && (paramCondition[k + 1].op == xAND || paramCondition[k + 1].op == xOR)){ |
mehatfie | 6:d1594fd2ec5a | 227 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 228 | lcd.printf("Test 1 "); |
mehatfie | 6:d1594fd2ec5a | 229 | wait(1); |
mehatfie | 6:d1594fd2ec5a | 230 | tempCombinedCondition.value = paramCondition[k].value; |
mehatfie | 6:d1594fd2ec5a | 231 | tempCombinedCondition.op = paramCondition[k].op; |
mehatfie | 6:d1594fd2ec5a | 232 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 6:d1594fd2ec5a | 233 | prevCondition = paramCondition[k].op; |
mehatfie | 6:d1594fd2ec5a | 234 | first = 0; |
mehatfie | 6:d1594fd2ec5a | 235 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 236 | lcd.printf("1cCval: %d ", combinedCondition.back().value); |
mehatfie | 6:d1594fd2ec5a | 237 | wait(1); |
mehatfie | 6:d1594fd2ec5a | 238 | } |
mehatfie | 6:d1594fd2ec5a | 239 | |
mehatfie | 5:e36e0538a903 | 240 | else{ |
mehatfie | 6:d1594fd2ec5a | 241 | /*lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 242 | lcd.printf("Test 0.2 "); |
mehatfie | 6:d1594fd2ec5a | 243 | wait(1);*/ |
mehatfie | 5:e36e0538a903 | 244 | //xAND |
mehatfie | 5:e36e0538a903 | 245 | if (paramCondition[k].op == xAND && prevCondition == xAND){ |
mehatfie | 6:d1594fd2ec5a | 246 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 247 | lcd.printf("Test 2.1 "); |
mehatfie | 6:d1594fd2ec5a | 248 | wait(1); |
mehatfie | 5:e36e0538a903 | 249 | combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 250 | prevCondition = xAND; |
mehatfie | 5:e36e0538a903 | 251 | } |
mehatfie | 5:e36e0538a903 | 252 | else if (paramCondition[k].op == xAND && prevCondition != xAND){ |
mehatfie | 6:d1594fd2ec5a | 253 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 254 | lcd.printf("Test 2.2 "); |
mehatfie | 6:d1594fd2ec5a | 255 | wait(1); |
mehatfie | 5:e36e0538a903 | 256 | tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 257 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 5:e36e0538a903 | 258 | prevCondition = xAND; |
mehatfie | 5:e36e0538a903 | 259 | } |
mehatfie | 5:e36e0538a903 | 260 | |
mehatfie | 5:e36e0538a903 | 261 | //xOR |
mehatfie | 5:e36e0538a903 | 262 | else if (paramCondition[k].op == xOR && prevCondition == xOR){ |
mehatfie | 6:d1594fd2ec5a | 263 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 264 | lcd.printf("Test 2.3 "); |
mehatfie | 6:d1594fd2ec5a | 265 | wait(1); |
mehatfie | 5:e36e0538a903 | 266 | combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 267 | prevCondition = xOR; |
mehatfie | 6:d1594fd2ec5a | 268 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 269 | lcd.printf("2.3cCval: %d ", combinedCondition.back().value); |
mehatfie | 6:d1594fd2ec5a | 270 | wait(1); |
mehatfie | 5:e36e0538a903 | 271 | } |
mehatfie | 5:e36e0538a903 | 272 | else if (paramCondition[k].op == xOR && prevCondition != xOR){ |
mehatfie | 6:d1594fd2ec5a | 273 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 274 | lcd.printf("Test 2.4 "); |
mehatfie | 6:d1594fd2ec5a | 275 | wait(1); |
mehatfie | 5:e36e0538a903 | 276 | tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; |
mehatfie | 5:e36e0538a903 | 277 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 5:e36e0538a903 | 278 | prevCondition = xOR; |
mehatfie | 6:d1594fd2ec5a | 279 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 280 | lcd.printf("2.4cCval: %d ", combinedCondition.back().value); |
mehatfie | 6:d1594fd2ec5a | 281 | wait(1); |
mehatfie | 5:e36e0538a903 | 282 | } |
mehatfie | 5:e36e0538a903 | 283 | |
mehatfie | 5:e36e0538a903 | 284 | // Since the k + 1 value is included in the xAND or xOR exclusively, skip checking that value, and add the appropriate AND / OR as the |
mehatfie | 5:e36e0538a903 | 285 | // operator of this exclusive xAND / xOR set |
mehatfie | 5:e36e0538a903 | 286 | if ((paramCondition[k + 1].op == AND || paramCondition[k + 1].op == OR) && (prevCondition == xAND || prevCondition == xOR)){ |
mehatfie | 5:e36e0538a903 | 287 | combinedCondition.back().op = paramCondition[k + 1].op; |
mehatfie | 5:e36e0538a903 | 288 | k++; |
mehatfie | 5:e36e0538a903 | 289 | } |
mehatfie | 5:e36e0538a903 | 290 | |
mehatfie | 5:e36e0538a903 | 291 | } |
mehatfie | 5:e36e0538a903 | 292 | } |
mehatfie | 5:e36e0538a903 | 293 | |
mehatfie | 6:d1594fd2ec5a | 294 | //else if (last && (prevCondition != AND || prevCondition != OR)){ |
mehatfie | 6:d1594fd2ec5a | 295 | |
mehatfie | 6:d1594fd2ec5a | 296 | |
mehatfie | 5:e36e0538a903 | 297 | //the last value was not included in any combination, since directly before the last value was an xAND / xOR set that |
mehatfie | 5:e36e0538a903 | 298 | // included the very last AND / OR as the set's operator |
mehatfie | 6:d1594fd2ec5a | 299 | else if (last && (prevCondition != AND || prevCondition != OR)){ |
mehatfie | 5:e36e0538a903 | 300 | tempCombinedCondition.value = paramCondition[k].value; |
mehatfie | 5:e36e0538a903 | 301 | tempCombinedCondition.op = NONE; |
mehatfie | 5:e36e0538a903 | 302 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 5:e36e0538a903 | 303 | } |
mehatfie | 5:e36e0538a903 | 304 | |
mehatfie | 5:e36e0538a903 | 305 | //reset the tempCombinedCondition variable |
mehatfie | 5:e36e0538a903 | 306 | tempCombinedCondition = ConditionOp(); |
mehatfie | 5:e36e0538a903 | 307 | } |
mehatfie | 5:e36e0538a903 | 308 | |
mehatfie | 6:d1594fd2ec5a | 309 | |
mehatfie | 6:d1594fd2ec5a | 310 | lcd.setAddress(0,3); |
mehatfie | 6:d1594fd2ec5a | 311 | lcd.printf("cCsize: %d ", combinedCondition.size()); |
mehatfie | 6:d1594fd2ec5a | 312 | wait(1); |
mehatfie | 6:d1594fd2ec5a | 313 | |
mehatfie | 5:e36e0538a903 | 314 | // run through all values in the combined Condition vector, AND'ing / OR'ing as appropriate |
mehatfie | 5:e36e0538a903 | 315 | // in the end, the last value in the array should be the final condition of the Condition statement... whether it was successful or failed |
mehatfie | 5:e36e0538a903 | 316 | for (i = 0; i < (combinedCondition.size() - 1); i++){ |
mehatfie | 5:e36e0538a903 | 317 | if (combinedCondition[i].op == AND) |
mehatfie | 5:e36e0538a903 | 318 | combinedCondition[i + 1].value = combinedCondition[i].value && combinedCondition[i + 1].value; |
mehatfie | 5:e36e0538a903 | 319 | else if (combinedCondition[i].op == OR) |
mehatfie | 5:e36e0538a903 | 320 | combinedCondition[i + 1].value = combinedCondition[i].value || combinedCondition[i + 1].value; |
mehatfie | 5:e36e0538a903 | 321 | } |
mehatfie | 5:e36e0538a903 | 322 | |
mehatfie | 5:e36e0538a903 | 323 | int conditionSuccess = combinedCondition.back().value; //value is the success(1) or failure(0) of the condition statement |
mehatfie | 5:e36e0538a903 | 324 | |
mehatfie | 5:e36e0538a903 | 325 | int checkEnd = 0; |
mehatfie | 5:e36e0538a903 | 326 | if (!conditionSuccess){ |
mehatfie | 6:d1594fd2ec5a | 327 | |
mehatfie | 5:e36e0538a903 | 328 | while (checkEnd != 4){ |
mehatfie | 5:e36e0538a903 | 329 | |
mehatfie | 6:d1594fd2ec5a | 330 | getNextLine(selectedFile, lineData); |
mehatfie | 6:d1594fd2ec5a | 331 | |
mehatfie | 6:d1594fd2ec5a | 332 | // check if the first word is an end command (avoids interpreting functions that perform actions) |
mehatfie | 5:e36e0538a903 | 333 | if (lineData.word[0].compare("end") == 0) |
mehatfie | 5:e36e0538a903 | 334 | checkEnd = interpretCommand(selectedFile, lineData); |
mehatfie | 5:e36e0538a903 | 335 | |
mehatfie | 5:e36e0538a903 | 336 | if (checkEnd == 4) // custom return value for this function |
mehatfie | 5:e36e0538a903 | 337 | return 0; |
mehatfie | 5:e36e0538a903 | 338 | } |
mehatfie | 5:e36e0538a903 | 339 | } |
mehatfie | 5:e36e0538a903 | 340 | |
mehatfie | 5:e36e0538a903 | 341 | // Return success as the function either met the condition and will continue from the next line, or |
mehatfie | 5:e36e0538a903 | 342 | // failed to meet the condition and ran through the lines inside the condition until "end condition" was found, therefore |
mehatfie | 5:e36e0538a903 | 343 | // the program will proceed from the line after the "end condition" line |
mehatfie | 6:d1594fd2ec5a | 344 | return 1; |
mehatfie | 5:e36e0538a903 | 345 | } |
mehatfie | 6:d1594fd2ec5a | 346 | |
mehatfie | 0:22618cf06f45 | 347 | |
mehatfie | 1:5731f31f96be | 348 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 349 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 350 | /************************** <FUNCTION: loopCommand> *****************************/ |
mehatfie | 1:5731f31f96be | 351 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 352 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 353 | |
mehatfie | 5:e36e0538a903 | 354 | int loopCommand(FILE *selectedFile, LineData &lineData){ |
mehatfie | 1:5731f31f96be | 355 | |
mehatfie | 2:3e7baa3e3fec | 356 | //Get the Condition value for number of times to loop |
mehatfie | 2:3e7baa3e3fec | 357 | string loopCondition = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 358 | int loopConditionValue = 0; |
mehatfie | 2:3e7baa3e3fec | 359 | sscanf(loopCondition.c_str(), "%d", &loopConditionValue); |
mehatfie | 1:5731f31f96be | 360 | |
mehatfie | 2:3e7baa3e3fec | 361 | int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1; |
mehatfie | 6:d1594fd2ec5a | 362 | |
mehatfie | 6:d1594fd2ec5a | 363 | lcd.setAddress(0,0); |
mehatfie | 6:d1594fd2ec5a | 364 | lcd.printf("Cycle 1 of %d", loopConditionValue); |
mehatfie | 1:5731f31f96be | 365 | |
mehatfie | 6:d1594fd2ec5a | 366 | Timer cycleTimer; |
mehatfie | 6:d1594fd2ec5a | 367 | float totalLoopTime = 0; |
mehatfie | 6:d1594fd2ec5a | 368 | cycleTimer.reset(); |
mehatfie | 6:d1594fd2ec5a | 369 | cycleTimer.start(); |
mehatfie | 6:d1594fd2ec5a | 370 | |
mehatfie | 6:d1594fd2ec5a | 371 | int counter = 1, checkEnd = 0; |
mehatfie | 6:d1594fd2ec5a | 372 | while (counter <= loopConditionValue){ |
mehatfie | 5:e36e0538a903 | 373 | |
mehatfie | 5:e36e0538a903 | 374 | getNextLine(selectedFile, lineData); |
mehatfie | 5:e36e0538a903 | 375 | |
mehatfie | 5:e36e0538a903 | 376 | //Must get the address before entering the interpret command |
mehatfie | 5:e36e0538a903 | 377 | // if a Condition command is immediately after, and the condition fails, then |
mehatfie | 5:e36e0538a903 | 378 | // the interpret command will return the line at the "end condition" line, and therefore |
mehatfie | 5:e36e0538a903 | 379 | // set the loop's first line to be the "end condition" line, if interpretCommand is called BEFORE setting the first loop line address |
mehatfie | 1:5731f31f96be | 380 | if (firstLineOfLoop){ |
mehatfie | 1:5731f31f96be | 381 | loopStartAddress = lineData.lineAddress; //Save the Line Address |
mehatfie | 1:5731f31f96be | 382 | loopLineNumber = lineData.lineNumber; //Save the Line Number |
mehatfie | 1:5731f31f96be | 383 | firstLineOfLoop = 0; |
mehatfie | 1:5731f31f96be | 384 | } |
mehatfie | 5:e36e0538a903 | 385 | |
mehatfie | 5:e36e0538a903 | 386 | checkEnd = interpretCommand(selectedFile, lineData); |
mehatfie | 6:d1594fd2ec5a | 387 | |
mehatfie | 1:5731f31f96be | 388 | //Increase the loop counter and go back to the beginning of the loop |
mehatfie | 5:e36e0538a903 | 389 | if (checkEnd == 3){ |
mehatfie | 6:d1594fd2ec5a | 390 | |
mehatfie | 6:d1594fd2ec5a | 391 | //Output the Avg Cycle Time |
mehatfie | 6:d1594fd2ec5a | 392 | cycleTimer.stop(); |
mehatfie | 6:d1594fd2ec5a | 393 | totalLoopTime += cycleTimer.read(); |
mehatfie | 2:3e7baa3e3fec | 394 | |
mehatfie | 6:d1594fd2ec5a | 395 | lcd.setAddress(0,1); |
mehatfie | 6:d1594fd2ec5a | 396 | lcd.printf("Avg t(sec): %1.3f", (totalLoopTime / counter)); |
mehatfie | 6:d1594fd2ec5a | 397 | |
mehatfie | 6:d1594fd2ec5a | 398 | //Output Cycle Number |
mehatfie | 6:d1594fd2ec5a | 399 | counter++; |
mehatfie | 6:d1594fd2ec5a | 400 | lcd.setAddress(0,0); |
mehatfie | 6:d1594fd2ec5a | 401 | lcd.printf("Cycle %d of %d", counter, loopConditionValue); |
mehatfie | 2:3e7baa3e3fec | 402 | |
mehatfie | 1:5731f31f96be | 403 | fseek(selectedFile, loopStartAddress, SEEK_SET); |
mehatfie | 2:3e7baa3e3fec | 404 | lineData.lineNumber = loopLineNumber - 2; |
mehatfie | 5:e36e0538a903 | 405 | checkEnd = 0; |
mehatfie | 6:d1594fd2ec5a | 406 | |
mehatfie | 6:d1594fd2ec5a | 407 | //Restart the timer for the next loop |
mehatfie | 6:d1594fd2ec5a | 408 | cycleTimer.reset(); |
mehatfie | 6:d1594fd2ec5a | 409 | cycleTimer.start(); |
mehatfie | 1:5731f31f96be | 410 | } |
mehatfie | 1:5731f31f96be | 411 | } |
mehatfie | 2:3e7baa3e3fec | 412 | |
mehatfie | 1:5731f31f96be | 413 | return 1; |
mehatfie | 1:5731f31f96be | 414 | } |
mehatfie | 1:5731f31f96be | 415 | |
mehatfie | 1:5731f31f96be | 416 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 417 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 418 | /************************* <FUNCTION: interpretCommand> *************************/ |
mehatfie | 1:5731f31f96be | 419 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 420 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 421 | |
mehatfie | 5:e36e0538a903 | 422 | int interpretCommand(FILE *selectedFile, LineData &lineData){ |
mehatfie | 2:3e7baa3e3fec | 423 | |
mehatfie | 2:3e7baa3e3fec | 424 | if (lineData.word[0].compare("device") == 0){ |
mehatfie | 2:3e7baa3e3fec | 425 | |
mehatfie | 1:5731f31f96be | 426 | int i = 0, deviceFound = -1; |
mehatfie | 2:3e7baa3e3fec | 427 | for (i = 0; i < numDevices; i++){ |
mehatfie | 4:86d0d04cc055 | 428 | if (lineData.word[2].compare(DeviceNames[i]) == 0){ |
mehatfie | 1:5731f31f96be | 429 | deviceFound = i; |
mehatfie | 4:86d0d04cc055 | 430 | } |
mehatfie | 2:3e7baa3e3fec | 431 | } |
mehatfie | 1:5731f31f96be | 432 | |
mehatfie | 1:5731f31f96be | 433 | //if the device type does not match any known type, error out |
mehatfie | 1:5731f31f96be | 434 | if (deviceFound == -1){ |
mehatfie | 1:5731f31f96be | 435 | //Error Out since the device Name was not matched with anything ************************* |
mehatfie | 1:5731f31f96be | 436 | } |
mehatfie | 1:5731f31f96be | 437 | |
mehatfie | 1:5731f31f96be | 438 | //Add device to the array of devices and initialize it |
mehatfie | 2:3e7baa3e3fec | 439 | else{ |
mehatfie | 2:3e7baa3e3fec | 440 | devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData)); |
mehatfie | 2:3e7baa3e3fec | 441 | devices.back()->name = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 442 | } |
mehatfie | 1:5731f31f96be | 443 | } |
mehatfie | 1:5731f31f96be | 444 | |
mehatfie | 2:3e7baa3e3fec | 445 | else if (lineData.word[0].compare("delay") == 0){ |
mehatfie | 2:3e7baa3e3fec | 446 | string duration = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 447 | int durationValue = 0; |
mehatfie | 2:3e7baa3e3fec | 448 | sscanf(duration.c_str(), "%d", &durationValue); |
mehatfie | 1:5731f31f96be | 449 | |
mehatfie | 1:5731f31f96be | 450 | if (durationValue){ |
mehatfie | 1:5731f31f96be | 451 | timer.reset(); |
mehatfie | 1:5731f31f96be | 452 | timer.start(); |
mehatfie | 1:5731f31f96be | 453 | while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration |
mehatfie | 1:5731f31f96be | 454 | timer.stop(); //Stop the Timer |
mehatfie | 1:5731f31f96be | 455 | } |
mehatfie | 1:5731f31f96be | 456 | else{ |
mehatfie | 1:5731f31f96be | 457 | //Error Out |
mehatfie | 1:5731f31f96be | 458 | return -1; |
mehatfie | 1:5731f31f96be | 459 | } |
mehatfie | 1:5731f31f96be | 460 | } |
mehatfie | 1:5731f31f96be | 461 | |
mehatfie | 2:3e7baa3e3fec | 462 | else if (lineData.word[0].compare("loop") == 0){ |
mehatfie | 2:3e7baa3e3fec | 463 | int checkLoopEnd = loopCommand(selectedFile, lineData); |
mehatfie | 1:5731f31f96be | 464 | if (checkLoopEnd == 1) |
mehatfie | 1:5731f31f96be | 465 | return 1; |
mehatfie | 1:5731f31f96be | 466 | } |
mehatfie | 5:e36e0538a903 | 467 | |
mehatfie | 5:e36e0538a903 | 468 | else if (lineData.word[0].compare("condition") == 0){ |
mehatfie | 5:e36e0538a903 | 469 | int checkLoopEnd = conditionCommand(selectedFile, lineData); |
mehatfie | 5:e36e0538a903 | 470 | if (checkLoopEnd == 1) |
mehatfie | 5:e36e0538a903 | 471 | return 2; |
mehatfie | 5:e36e0538a903 | 472 | } |
mehatfie | 5:e36e0538a903 | 473 | // end with custom return value for specific function |
mehatfie | 2:3e7baa3e3fec | 474 | else if (lineData.word[0].compare("end") == 0){ |
mehatfie | 2:3e7baa3e3fec | 475 | if (lineData.word[1].compare("program") == 0){ |
mehatfie | 5:e36e0538a903 | 476 | return 2; |
mehatfie | 2:3e7baa3e3fec | 477 | } |
mehatfie | 2:3e7baa3e3fec | 478 | else if (lineData.word[1].compare("loop") == 0){ |
mehatfie | 5:e36e0538a903 | 479 | return 3; |
mehatfie | 5:e36e0538a903 | 480 | } |
mehatfie | 5:e36e0538a903 | 481 | else if (lineData.word[1].compare("condition") == 0){ |
mehatfie | 5:e36e0538a903 | 482 | return 4; |
mehatfie | 2:3e7baa3e3fec | 483 | } |
mehatfie | 1:5731f31f96be | 484 | } |
mehatfie | 1:5731f31f96be | 485 | |
mehatfie | 1:5731f31f96be | 486 | //not a keyword so check if it's a localName for a device |
mehatfie | 1:5731f31f96be | 487 | else{ |
mehatfie | 1:5731f31f96be | 488 | |
mehatfie | 1:5731f31f96be | 489 | int i = 0, deviceFound = -1; |
mehatfie | 2:3e7baa3e3fec | 490 | for (i = 0; i < devices.size(); i++){ |
mehatfie | 2:3e7baa3e3fec | 491 | if (lineData.word[0].compare(devices[i]->name) == 0) |
mehatfie | 1:5731f31f96be | 492 | deviceFound = i; |
mehatfie | 1:5731f31f96be | 493 | } |
mehatfie | 1:5731f31f96be | 494 | |
mehatfie | 1:5731f31f96be | 495 | //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 | 496 | if (deviceFound == -1){ |
mehatfie | 1:5731f31f96be | 497 | lcd.setAddress(0,3); |
mehatfie | 1:5731f31f96be | 498 | lcd.printf("Final ERROR!"); |
mehatfie | 1:5731f31f96be | 499 | wait(10); |
mehatfie | 1:5731f31f96be | 500 | } |
mehatfie | 1:5731f31f96be | 501 | |
mehatfie | 1:5731f31f96be | 502 | //Local Name matches a device, send line to that device in order to process the functionality |
mehatfie | 2:3e7baa3e3fec | 503 | else{ |
mehatfie | 2:3e7baa3e3fec | 504 | //addDevice(deviceFound); |
mehatfie | 5:e36e0538a903 | 505 | return devices[deviceFound]->interpret(lineData); |
mehatfie | 2:3e7baa3e3fec | 506 | } |
mehatfie | 1:5731f31f96be | 507 | } |
mehatfie | 1:5731f31f96be | 508 | |
mehatfie | 1:5731f31f96be | 509 | return -1; |
mehatfie | 1:5731f31f96be | 510 | } |
mehatfie | 1:5731f31f96be | 511 | |
mehatfie | 1:5731f31f96be | 512 | |
mehatfie | 1:5731f31f96be | 513 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 514 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 515 | /****************************** <FUNCTION: main> ********************************/ |
mehatfie | 1:5731f31f96be | 516 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 517 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 518 | |
mehatfie | 0:22618cf06f45 | 519 | int main() { |
mehatfie | 0:22618cf06f45 | 520 | |
mehatfie | 0:22618cf06f45 | 521 | fullInit(); //Initialize anything that's required to run the code (LCD) |
mehatfie | 0:22618cf06f45 | 522 | |
mehatfie | 6:d1594fd2ec5a | 523 | LineData lineData; |
mehatfie | 2:3e7baa3e3fec | 524 | resetLineData(lineData); |
mehatfie | 2:3e7baa3e3fec | 525 | |
mehatfie | 0:22618cf06f45 | 526 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 527 | /*** <Get all the Potential Programs> ***/ |
mehatfie | 0:22618cf06f45 | 528 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 529 | int numTextFiles = 0; |
mehatfie | 2:3e7baa3e3fec | 530 | vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card |
mehatfie | 2:3e7baa3e3fec | 531 | vector<string> filenames = readFileNames("/sd"); |
mehatfie | 2:3e7baa3e3fec | 532 | numTextFiles = getFileNamesWithoutExt(textFiles, filenames); |
mehatfie | 0:22618cf06f45 | 533 | |
mehatfie | 0:22618cf06f45 | 534 | |
mehatfie | 0:22618cf06f45 | 535 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 536 | /*** <Select the Program txt File> ***/ |
mehatfie | 0:22618cf06f45 | 537 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 538 | int fileSelected = 0, selectedFileIndex = 0; |
mehatfie | 0:22618cf06f45 | 539 | |
mehatfie | 0:22618cf06f45 | 540 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 541 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 542 | lcd.printf("Select Your Program"); |
mehatfie | 0:22618cf06f45 | 543 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 544 | lcd.printf("Num Programs = %d", numTextFiles); |
mehatfie | 0:22618cf06f45 | 545 | |
mehatfie | 6:d1594fd2ec5a | 546 | uint8_t lastButState = 0; |
mehatfie | 0:22618cf06f45 | 547 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
mehatfie | 2:3e7baa3e3fec | 548 | |
mehatfie | 0:22618cf06f45 | 549 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen |
mehatfie | 0:22618cf06f45 | 550 | while(!fileSelected) { |
mehatfie | 0:22618cf06f45 | 551 | |
mehatfie | 0:22618cf06f45 | 552 | uint8_t curButState = buttons.readBus(); |
mehatfie | 0:22618cf06f45 | 553 | if(curButState != lastButState){ |
mehatfie | 0:22618cf06f45 | 554 | lastButState = curButState; |
mehatfie | 0:22618cf06f45 | 555 | if(buttons.readRight()) |
mehatfie | 0:22618cf06f45 | 556 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 1); |
mehatfie | 0:22618cf06f45 | 557 | else if(buttons.readLeft()) |
mehatfie | 0:22618cf06f45 | 558 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 0); |
mehatfie | 0:22618cf06f45 | 559 | else if(buttons.readSel()) |
mehatfie | 0:22618cf06f45 | 560 | fileSelected = 1; |
mehatfie | 0:22618cf06f45 | 561 | } |
mehatfie | 0:22618cf06f45 | 562 | } |
mehatfie | 0:22618cf06f45 | 563 | |
mehatfie | 0:22618cf06f45 | 564 | char selectedFileName[50]; |
mehatfie | 0:22618cf06f45 | 565 | strcpy(selectedFileName, textFiles[selectedFileIndex].c_str()); |
mehatfie | 0:22618cf06f45 | 566 | |
mehatfie | 0:22618cf06f45 | 567 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 568 | /*** <Open the Program txt File> ***/ |
mehatfie | 0:22618cf06f45 | 569 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 570 | |
mehatfie | 0:22618cf06f45 | 571 | //Create the string of the full directory and path to the program txt file |
mehatfie | 0:22618cf06f45 | 572 | char selectedFileFullName[100] = "/sd/"; //Assuming that no directory and file name will be longer than 100 characters |
mehatfie | 0:22618cf06f45 | 573 | strcat(selectedFileFullName, selectedFileName); |
mehatfie | 0:22618cf06f45 | 574 | strcat(selectedFileFullName, ".txt"); |
mehatfie | 0:22618cf06f45 | 575 | |
mehatfie | 0:22618cf06f45 | 576 | FILE *selectedFile = fopen(selectedFileFullName, "r"); |
mehatfie | 2:3e7baa3e3fec | 577 | |
mehatfie | 0:22618cf06f45 | 578 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 579 | |
mehatfie | 0:22618cf06f45 | 580 | if(selectedFile == NULL) { |
mehatfie | 0:22618cf06f45 | 581 | lcd.setAddress(0,0); |
mehatfie | 0:22618cf06f45 | 582 | lcd.printf("Invalid"); |
mehatfie | 0:22618cf06f45 | 583 | wait(10); |
mehatfie | 0:22618cf06f45 | 584 | } |
mehatfie | 0:22618cf06f45 | 585 | |
mehatfie | 0:22618cf06f45 | 586 | |
mehatfie | 0:22618cf06f45 | 587 | while(1){ |
mehatfie | 0:22618cf06f45 | 588 | |
mehatfie | 2:3e7baa3e3fec | 589 | resetLineData(lineData); |
mehatfie | 0:22618cf06f45 | 590 | |
mehatfie | 0:22618cf06f45 | 591 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 592 | lcd.setAddress(0,0); |
mehatfie | 6:d1594fd2ec5a | 593 | //lcd.printf("Program: %s", selectedFileName); |
mehatfie | 0:22618cf06f45 | 594 | |
mehatfie | 0:22618cf06f45 | 595 | |
mehatfie | 0:22618cf06f45 | 596 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 597 | /*** <Start Running through the Program txt File Lines> ***/ |
mehatfie | 0:22618cf06f45 | 598 | /******************************************************************************/ |
mehatfie | 0:22618cf06f45 | 599 | |
mehatfie | 0:22618cf06f45 | 600 | int endOfFile = 0; |
mehatfie | 0:22618cf06f45 | 601 | |
mehatfie | 0:22618cf06f45 | 602 | while (!endOfFile){ |
mehatfie | 0:22618cf06f45 | 603 | |
mehatfie | 2:3e7baa3e3fec | 604 | getNextLine(selectedFile, lineData); |
mehatfie | 2:3e7baa3e3fec | 605 | int checkEnd = interpretCommand(selectedFile, lineData); |
mehatfie | 2:3e7baa3e3fec | 606 | |
mehatfie | 1:5731f31f96be | 607 | if (checkEnd == 0) |
mehatfie | 1:5731f31f96be | 608 | endOfFile = 1; |
mehatfie | 0:22618cf06f45 | 609 | } |
mehatfie | 0:22618cf06f45 | 610 | |
mehatfie | 0:22618cf06f45 | 611 | |
mehatfie | 0:22618cf06f45 | 612 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 613 | lcd.setAddress(0,0); |
mehatfie | 0:22618cf06f45 | 614 | lcd.printf("END OF PROGRAM"); |
mehatfie | 0:22618cf06f45 | 615 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 616 | lcd.printf("To Restart..."); |
mehatfie | 0:22618cf06f45 | 617 | lcd.setAddress(0,3); |
mehatfie | 0:22618cf06f45 | 618 | lcd.printf("Press BACK"); |
mehatfie | 0:22618cf06f45 | 619 | |
mehatfie | 0:22618cf06f45 | 620 | while(!buttons.readBack()); |
mehatfie | 0:22618cf06f45 | 621 | |
mehatfie | 0:22618cf06f45 | 622 | lcd.setAddress(0,1); |
mehatfie | 2:3e7baa3e3fec | 623 | //rewind(selectedFile); |
mehatfie | 0:22618cf06f45 | 624 | } |
mehatfie | 0:22618cf06f45 | 625 | |
mehatfie | 2:3e7baa3e3fec | 626 | } |
mehatfie | 2:3e7baa3e3fec | 627 | |
mehatfie | 2:3e7baa3e3fec | 628 | |
mehatfie | 2:3e7baa3e3fec | 629 | |
mehatfie | 2:3e7baa3e3fec | 630 | |
mehatfie | 2:3e7baa3e3fec | 631 | |
mehatfie | 2:3e7baa3e3fec | 632 | |
mehatfie | 2:3e7baa3e3fec | 633 | |
mehatfie | 2:3e7baa3e3fec | 634 | |
mehatfie | 2:3e7baa3e3fec | 635 | |
mehatfie | 2:3e7baa3e3fec | 636 | |
mehatfie | 2:3e7baa3e3fec | 637 | |
mehatfie | 2:3e7baa3e3fec | 638 | |
mehatfie | 2:3e7baa3e3fec | 639 | |
mehatfie | 2:3e7baa3e3fec | 640 | |
mehatfie | 2:3e7baa3e3fec | 641 | |
mehatfie | 2:3e7baa3e3fec | 642 | |
mehatfie | 2:3e7baa3e3fec | 643 | |
mehatfie | 2:3e7baa3e3fec | 644 | |
mehatfie | 2:3e7baa3e3fec | 645 | |
mehatfie | 2:3e7baa3e3fec | 646 | |
mehatfie | 0:22618cf06f45 | 647 | /***** Cycle through txt lines and remember last lines ******/ |
mehatfie | 0:22618cf06f45 | 648 | /* |
mehatfie | 0:22618cf06f45 | 649 | int running = 0, selectedFile = 0; |
mehatfie | 0:22618cf06f45 | 650 | int locCount = 0, tempSpot = 0; |
mehatfie | 0:22618cf06f45 | 651 | int loc[4]; |
mehatfie | 0:22618cf06f45 | 652 | char line[32]; |
mehatfie | 0:22618cf06f45 | 653 | uint8_t lastButState; |
mehatfie | 0:22618cf06f45 | 654 | |
mehatfie | 0:22618cf06f45 | 655 | |
mehatfie | 0:22618cf06f45 | 656 | while(1){ |
mehatfie | 0:22618cf06f45 | 657 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
mehatfie | 0:22618cf06f45 | 658 | while(!running) { |
mehatfie | 0:22618cf06f45 | 659 | uint8_t curButState = buttons.readBus(); |
mehatfie | 0:22618cf06f45 | 660 | |
mehatfie | 0:22618cf06f45 | 661 | if(curButState != lastButState){ |
mehatfie | 0:22618cf06f45 | 662 | switch(lastButState = curButState){ |
mehatfie | 0:22618cf06f45 | 663 | case 0x1F: //right |
mehatfie | 0:22618cf06f45 | 664 | |
mehatfie | 0:22618cf06f45 | 665 | loc[locCount] = ftell(fp); |
mehatfie | 0:22618cf06f45 | 666 | |
mehatfie | 0:22618cf06f45 | 667 | if (locCount >= 3) |
mehatfie | 0:22618cf06f45 | 668 | locCount = 0; |
mehatfie | 0:22618cf06f45 | 669 | else |
mehatfie | 0:22618cf06f45 | 670 | locCount++; |
mehatfie | 0:22618cf06f45 | 671 | |
mehatfie | 0:22618cf06f45 | 672 | //tempSpot = ftell(fp); |
mehatfie | 0:22618cf06f45 | 673 | fgets(line, 32, fp); |
mehatfie | 0:22618cf06f45 | 674 | |
mehatfie | 0:22618cf06f45 | 675 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 676 | lcd.printf("L: %s", line); |
mehatfie | 0:22618cf06f45 | 677 | wait(0.2); |
mehatfie | 0:22618cf06f45 | 678 | break; |
mehatfie | 0:22618cf06f45 | 679 | case 0x2F: //left |
mehatfie | 0:22618cf06f45 | 680 | |
mehatfie | 0:22618cf06f45 | 681 | if (locCount == 0) { |
mehatfie | 0:22618cf06f45 | 682 | locCount = 3; |
mehatfie | 0:22618cf06f45 | 683 | fseek(fp, loc[locCount], SEEK_SET); |
mehatfie | 0:22618cf06f45 | 684 | } |
mehatfie | 0:22618cf06f45 | 685 | else { |
mehatfie | 0:22618cf06f45 | 686 | fseek(fp, loc[locCount - 1], SEEK_SET); |
mehatfie | 0:22618cf06f45 | 687 | locCount--; |
mehatfie | 0:22618cf06f45 | 688 | } |
mehatfie | 0:22618cf06f45 | 689 | fgets(line, 32, fp); |
mehatfie | 0:22618cf06f45 | 690 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 691 | lcd.printf("L: %s", line); |
mehatfie | 0:22618cf06f45 | 692 | wait(0.2); |
mehatfie | 0:22618cf06f45 | 693 | break; |
mehatfie | 0:22618cf06f45 | 694 | } |
mehatfie | 0:22618cf06f45 | 695 | } |
mehatfie | 0:22618cf06f45 | 696 | } |
mehatfie | 0:22618cf06f45 | 697 | } |
mehatfie | 0:22618cf06f45 | 698 | */ |
mehatfie | 2:3e7baa3e3fec | 699 | |
mehatfie | 0:22618cf06f45 | 700 | |
mehatfie | 0:22618cf06f45 | 701 | |
mehatfie | 0:22618cf06f45 | 702 | /******* Select the Program txt File ***********/ |
mehatfie | 0:22618cf06f45 | 703 | /* |
mehatfie | 0:22618cf06f45 | 704 | int numTextFiles = 0; |
mehatfie | 0:22618cf06f45 | 705 | string tempTextFiles[25]; //Assuming Maximum of 25 txt files will be on the SD Card |
mehatfie | 0:22618cf06f45 | 706 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 707 | readFileNames("/sd"); |
mehatfie | 0:22618cf06f45 | 708 | numTextFiles = getFileNamesWithoutExt(tempTextFiles); |
mehatfie | 0:22618cf06f45 | 709 | |
mehatfie | 0:22618cf06f45 | 710 | string *textFiles = resize_StringArr(tempTextFiles, numTextFiles); //Resize Array |
mehatfie | 0:22618cf06f45 | 711 | //delete [] tempTextFiles; //free previous array |
mehatfie | 0:22618cf06f45 | 712 | |
mehatfie | 0:22618cf06f45 | 713 | |
mehatfie | 0:22618cf06f45 | 714 | int running = 0, selectedFile = 0; |
mehatfie | 0:22618cf06f45 | 715 | |
mehatfie | 0:22618cf06f45 | 716 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 717 | lcd.setAddress(0,1); |
mehatfie | 0:22618cf06f45 | 718 | lcd.printf("Select Your Program"); |
mehatfie | 0:22618cf06f45 | 719 | lcd.setAddress(0,2); |
mehatfie | 0:22618cf06f45 | 720 | lcd.printf("Num Programs = %d", numTextFiles); |
mehatfie | 0:22618cf06f45 | 721 | |
mehatfie | 0:22618cf06f45 | 722 | uint8_t lastButState; |
mehatfie | 0:22618cf06f45 | 723 | while(1){ |
mehatfie | 0:22618cf06f45 | 724 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
mehatfie | 0:22618cf06f45 | 725 | while(!running) { |
mehatfie | 0:22618cf06f45 | 726 | uint8_t curButState = buttons.readBus(); |
mehatfie | 0:22618cf06f45 | 727 | |
mehatfie | 0:22618cf06f45 | 728 | if(curButState != lastButState){ |
mehatfie | 0:22618cf06f45 | 729 | switch(lastButState = curButState){ |
mehatfie | 0:22618cf06f45 | 730 | case 0x1F: //right |
mehatfie | 0:22618cf06f45 | 731 | selectedFile = cyclePrograms(textFiles, numTextFiles, selectedFile, 1); |
mehatfie | 0:22618cf06f45 | 732 | break; |
mehatfie | 0:22618cf06f45 | 733 | case 0x2F: //left |
mehatfie | 0:22618cf06f45 | 734 | selectedFile = cyclePrograms(textFiles, numTextFiles, selectedFile, 0); |
mehatfie | 0:22618cf06f45 | 735 | break; |
mehatfie | 0:22618cf06f45 | 736 | } |
mehatfie | 0:22618cf06f45 | 737 | } |
mehatfie | 0:22618cf06f45 | 738 | } |
mehatfie | 0:22618cf06f45 | 739 | } |
mehatfie | 0:22618cf06f45 | 740 | |
mehatfie | 0:22618cf06f45 | 741 | */ |
mehatfie | 0:22618cf06f45 | 742 | |
mehatfie | 0:22618cf06f45 | 743 | /*float speed = 0.5; |
mehatfie | 0:22618cf06f45 | 744 | while(1){ |
mehatfie | 0:22618cf06f45 | 745 | bridges.drive(1, -1*speed); |
mehatfie | 0:22618cf06f45 | 746 | wait(2); |
mehatfie | 0:22618cf06f45 | 747 | bridges.drive(1, speed); |
mehatfie | 0:22618cf06f45 | 748 | wait(2); |
mehatfie | 0:22618cf06f45 | 749 | }*/ |
mehatfie | 0:22618cf06f45 | 750 | |
mehatfie | 0:22618cf06f45 | 751 | //BridgeDriver::MOTOR_A |