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@11:bc9cd2869f95, 2014-10-01 (annotated)
- Committer:
- mehatfie
- Date:
- Wed Oct 01 18:11:38 2014 +0000
- Revision:
- 11:bc9cd2869f95
- Parent:
- 10:e8db892fbc52
- Child:
- 12:2e3e86714243
- Latest revision is not a working copy, simply publishing all previous working copies to the server
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mehatfie | 0:22618cf06f45 | 1 | #include "mbed.h" |
mehatfie | 0:22618cf06f45 | 2 | #include "LocalPinNames.h" |
mehatfie | 0:22618cf06f45 | 3 | #include "BridgeDriver.h" |
mehatfie | 0:22618cf06f45 | 4 | #include "FrontPanelButtons.h" |
mehatfie | 0:22618cf06f45 | 5 | #include "TextLCD.h" |
mehatfie | 0:22618cf06f45 | 6 | #include "SDFileSystem.h" |
mehatfie | 0:22618cf06f45 | 7 | #include "Initialization.hpp" |
mehatfie | 9:5a0c4c6e39c7 | 8 | //#include "mainFunctions.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 | 9:5a0c4c6e39c7 | 22 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 23 | /*** <Function: resetLineData> ***/ |
mehatfie | 9:5a0c4c6e39c7 | 24 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 25 | |
mehatfie | 5:e36e0538a903 | 26 | void resetLineData(LineData &lineData){ |
mehatfie | 0:22618cf06f45 | 27 | |
mehatfie | 0:22618cf06f45 | 28 | lineData.lineNumber = 0; |
mehatfie | 0:22618cf06f45 | 29 | lineData.numWords = 0; |
mehatfie | 2:3e7baa3e3fec | 30 | lineData.lineAddress = 0; |
mehatfie | 0:22618cf06f45 | 31 | } |
mehatfie | 0:22618cf06f45 | 32 | |
mehatfie | 1:5731f31f96be | 33 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 34 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 35 | /************************ <FUNCTION: cyclePrograms> *****************************/ |
mehatfie | 1:5731f31f96be | 36 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 37 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 38 | |
mehatfie | 2:3e7baa3e3fec | 39 | int cyclePrograms(vector<string> files, int SIZE, int currIndex, int direction){ |
mehatfie | 0:22618cf06f45 | 40 | |
mehatfie | 0:22618cf06f45 | 41 | int nextIndex = 0; |
mehatfie | 0:22618cf06f45 | 42 | switch(direction){ |
mehatfie | 0:22618cf06f45 | 43 | case 0: //Cycle Back one File |
mehatfie | 0:22618cf06f45 | 44 | if ((currIndex - 1) < 0) |
mehatfie | 0:22618cf06f45 | 45 | nextIndex = SIZE - 1; |
mehatfie | 0:22618cf06f45 | 46 | else |
mehatfie | 0:22618cf06f45 | 47 | nextIndex = currIndex - 1; |
mehatfie | 0:22618cf06f45 | 48 | break; |
mehatfie | 0:22618cf06f45 | 49 | case 1: //Cycle Forward one File |
mehatfie | 0:22618cf06f45 | 50 | if ((currIndex + 1) >= SIZE) |
mehatfie | 0:22618cf06f45 | 51 | nextIndex = 0; |
mehatfie | 0:22618cf06f45 | 52 | else |
mehatfie | 0:22618cf06f45 | 53 | nextIndex = currIndex + 1; |
mehatfie | 0:22618cf06f45 | 54 | break; |
mehatfie | 0:22618cf06f45 | 55 | case -1: //set the selectedFile to the currIndex (used for initialization) |
mehatfie | 0:22618cf06f45 | 56 | nextIndex = currIndex; |
mehatfie | 0:22618cf06f45 | 57 | break; |
mehatfie | 0:22618cf06f45 | 58 | } |
mehatfie | 0:22618cf06f45 | 59 | |
mehatfie | 0:22618cf06f45 | 60 | //Output file on Display |
mehatfie | 11:bc9cd2869f95 | 61 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 62 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
mehatfie | 0:22618cf06f45 | 63 | lcd.setAddress(0,3); |
mehatfie | 11:bc9cd2869f95 | 64 | lcd.printf(" "); // Clear the Line using Spaces (Emptyness) - Note one line is 20 Characters |
mehatfie | 0:22618cf06f45 | 65 | wait(.2); |
mehatfie | 0:22618cf06f45 | 66 | lcd.setAddress(0,3); |
mehatfie | 4:86d0d04cc055 | 67 | lcd.printf("%s", files[nextIndex]); |
mehatfie | 0:22618cf06f45 | 68 | |
mehatfie | 0:22618cf06f45 | 69 | return nextIndex; // Return the file index in the Array |
mehatfie | 0:22618cf06f45 | 70 | } |
mehatfie | 0:22618cf06f45 | 71 | |
mehatfie | 0:22618cf06f45 | 72 | |
mehatfie | 5:e36e0538a903 | 73 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 74 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 75 | /************************** <FUNCTION: conditionCommand> *****************************/ |
mehatfie | 5:e36e0538a903 | 76 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 77 | /**********************************************************************************************************************************/ |
mehatfie | 5:e36e0538a903 | 78 | |
mehatfie | 5:e36e0538a903 | 79 | //Create an enum map of the positble conditions |
mehatfie | 9:5a0c4c6e39c7 | 80 | enum ConditionType{xAND, AND, xOR, OR, NONE}; |
mehatfie | 5:e36e0538a903 | 81 | |
mehatfie | 5:e36e0538a903 | 82 | struct ConditionOp{ |
mehatfie | 5:e36e0538a903 | 83 | |
mehatfie | 5:e36e0538a903 | 84 | int value; //returned value of the interpret function: 1 = meets criteria, 0 = criteria not met, -1 = failed to interpret |
mehatfie | 9:5a0c4c6e39c7 | 85 | 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 | 86 | }; |
mehatfie | 5:e36e0538a903 | 87 | |
mehatfie | 5:e36e0538a903 | 88 | int conditionCommand(FILE *selectedFile, LineData &lineData){ |
mehatfie | 10:e8db892fbc52 | 89 | |
mehatfie | 9:5a0c4c6e39c7 | 90 | //Initialize variables |
mehatfie | 11:bc9cd2869f95 | 91 | LineData param[15]; // assume no more than 15 conditions will be needed |
mehatfie | 5:e36e0538a903 | 92 | vector<ConditionOp> paramCondition; |
mehatfie | 5:e36e0538a903 | 93 | |
mehatfie | 5:e36e0538a903 | 94 | //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 | 9:5a0c4c6e39c7 | 95 | //this line reads: Condition, data_for_param1, CONDTITION_OP1, data_for_param2, CONDTITION_OP2, data_for_param3...... |
mehatfie | 9:5a0c4c6e39c7 | 96 | //Staring index of first data parameter is the 2nd word, therefore 1 |
mehatfie | 9:5a0c4c6e39c7 | 97 | int i = 1, numParam = 0, paramNumWords = 0; |
mehatfie | 9:5a0c4c6e39c7 | 98 | for (i = 1; i < lineData.numWords; i++){ |
mehatfie | 6:d1594fd2ec5a | 99 | |
mehatfie | 5:e36e0538a903 | 100 | // if the word is not an AND or an OR, it must mean it's for the specific function |
mehatfie | 5:e36e0538a903 | 101 | // set the current parameter's next word to be equal to the current word we're checking |
mehatfie | 5:e36e0538a903 | 102 | // increase number of words that the parameter has |
mehatfie | 5:e36e0538a903 | 103 | if (lineData.word[i] != "AND" && lineData.word[i] != "xAND" && lineData.word[i] != "OR" && lineData.word[i] != "xOR"){ |
mehatfie | 6:d1594fd2ec5a | 104 | |
mehatfie | 5:e36e0538a903 | 105 | param[numParam].word[paramNumWords] = lineData.word[i]; |
mehatfie | 5:e36e0538a903 | 106 | paramNumWords++; |
mehatfie | 5:e36e0538a903 | 107 | |
mehatfie | 5:e36e0538a903 | 108 | //if this is the last word in the line.... |
mehatfie | 5:e36e0538a903 | 109 | if(i == (lineData.numWords - 1)){ |
mehatfie | 11:bc9cd2869f95 | 110 | |
mehatfie | 5:e36e0538a903 | 111 | param[numParam].numWords = paramNumWords; |
mehatfie | 11:bc9cd2869f95 | 112 | |
mehatfie | 11:bc9cd2869f95 | 113 | //if numParam = 0 at this point, it means there's only one condition to check... need to add a member to the vector since none have been added yet |
mehatfie | 11:bc9cd2869f95 | 114 | if (numParam == 0) |
mehatfie | 11:bc9cd2869f95 | 115 | paramCondition.push_back(ConditionOp()); |
mehatfie | 11:bc9cd2869f95 | 116 | |
mehatfie | 9:5a0c4c6e39c7 | 117 | paramCondition[numParam].op = NONE; |
mehatfie | 11:bc9cd2869f95 | 118 | |
mehatfie | 5:e36e0538a903 | 119 | numParam++; |
mehatfie | 11:bc9cd2869f95 | 120 | } |
mehatfie | 5:e36e0538a903 | 121 | } |
mehatfie | 5:e36e0538a903 | 122 | |
mehatfie | 5:e36e0538a903 | 123 | // if the word is an AND or an OR, it must mean the last function has been completely identified |
mehatfie | 5:e36e0538a903 | 124 | // set the parameters number of Words value to the calculated value |
mehatfie | 5:e36e0538a903 | 125 | // increase the number of Parameters (the current parameter function we're filling) |
mehatfie | 5:e36e0538a903 | 126 | 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 | 127 | |
mehatfie | 5:e36e0538a903 | 128 | param[numParam].numWords = paramNumWords; |
mehatfie | 5:e36e0538a903 | 129 | |
mehatfie | 5:e36e0538a903 | 130 | paramCondition.push_back(ConditionOp()); |
mehatfie | 5:e36e0538a903 | 131 | if (lineData.word[i].compare("AND") == 0) |
mehatfie | 5:e36e0538a903 | 132 | paramCondition[numParam].op = AND; |
mehatfie | 5:e36e0538a903 | 133 | else if (lineData.word[i].compare("xAND") == 0) |
mehatfie | 5:e36e0538a903 | 134 | paramCondition[numParam].op = xAND; |
mehatfie | 5:e36e0538a903 | 135 | else if (lineData.word[i].compare("OR") == 0) |
mehatfie | 5:e36e0538a903 | 136 | paramCondition[numParam].op = OR; |
mehatfie | 5:e36e0538a903 | 137 | else if (lineData.word[i].compare("xOR") == 0) |
mehatfie | 5:e36e0538a903 | 138 | paramCondition[numParam].op = xOR; |
mehatfie | 5:e36e0538a903 | 139 | |
mehatfie | 5:e36e0538a903 | 140 | numParam++; // increase the index of param |
mehatfie | 5:e36e0538a903 | 141 | paramNumWords = 0; // reset the number of words |
mehatfie | 5:e36e0538a903 | 142 | } |
mehatfie | 5:e36e0538a903 | 143 | } |
mehatfie | 5:e36e0538a903 | 144 | |
mehatfie | 11:bc9cd2869f95 | 145 | |
mehatfie | 9:5a0c4c6e39c7 | 146 | //send the data parameters in order to get them interpreted by the appropriate device |
mehatfie | 9:5a0c4c6e39c7 | 147 | //if the value it's checking for meets the criteria you want, the device should return 1, if it doesn't meet the criteria the device should return 0 |
mehatfie | 6:d1594fd2ec5a | 148 | int j = 0, k = 0; |
mehatfie | 5:e36e0538a903 | 149 | for (j = 0; j < numParam; j++){ |
mehatfie | 11:bc9cd2869f95 | 150 | paramCondition[j].value = interpretCommand(param[j]); |
mehatfie | 9:5a0c4c6e39c7 | 151 | |
mehatfie | 9:5a0c4c6e39c7 | 152 | //error out if the interpretted command returned an error |
mehatfie | 9:5a0c4c6e39c7 | 153 | if (paramCondition[j].value == -1) |
mehatfie | 9:5a0c4c6e39c7 | 154 | return -1; |
mehatfie | 5:e36e0538a903 | 155 | } |
mehatfie | 5:e36e0538a903 | 156 | |
mehatfie | 9:5a0c4c6e39c7 | 157 | |
mehatfie | 5:e36e0538a903 | 158 | //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 | 159 | //this should make the xAND's / xOR's into a single member of the combinedCondition vector |
mehatfie | 5:e36e0538a903 | 160 | enum ConditionType prevCondition = NONE; |
mehatfie | 9:5a0c4c6e39c7 | 161 | vector<ConditionOp> combinedCondition; |
mehatfie | 9:5a0c4c6e39c7 | 162 | ConditionOp tempCombinedCondition; |
mehatfie | 5:e36e0538a903 | 163 | int first = 1, last = 0; |
mehatfie | 5:e36e0538a903 | 164 | for (k = 0; k < numParam; k++){ |
mehatfie | 6:d1594fd2ec5a | 165 | |
mehatfie | 5:e36e0538a903 | 166 | if (k == numParam - 1) |
mehatfie | 5:e36e0538a903 | 167 | last = 1; |
mehatfie | 5:e36e0538a903 | 168 | |
mehatfie | 11:bc9cd2869f95 | 169 | //Only one condition to check |
mehatfie | 11:bc9cd2869f95 | 170 | if (numParam == 1){ |
mehatfie | 11:bc9cd2869f95 | 171 | tempCombinedCondition.value = paramCondition[k].value; |
mehatfie | 11:bc9cd2869f95 | 172 | tempCombinedCondition.op = NONE; |
mehatfie | 11:bc9cd2869f95 | 173 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 174 | } |
mehatfie | 11:bc9cd2869f95 | 175 | |
mehatfie | 11:bc9cd2869f95 | 176 | else{ |
mehatfie | 11:bc9cd2869f95 | 177 | if (!last){ |
mehatfie | 11:bc9cd2869f95 | 178 | if (paramCondition[k].op != xAND && paramCondition[k].op != xOR && paramCondition[k + 1].op != xAND && paramCondition[k + 1].op != xOR){ |
mehatfie | 11:bc9cd2869f95 | 179 | //AND |
mehatfie | 11:bc9cd2869f95 | 180 | if (paramCondition[k].op == AND){ |
mehatfie | 11:bc9cd2869f95 | 181 | if (!first && prevCondition != xAND && prevCondition != xOR) |
mehatfie | 11:bc9cd2869f95 | 182 | combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 183 | else if (first || prevCondition == xAND || prevCondition == xOR){ |
mehatfie | 11:bc9cd2869f95 | 184 | tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 185 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 186 | first = 0; |
mehatfie | 11:bc9cd2869f95 | 187 | } |
mehatfie | 11:bc9cd2869f95 | 188 | prevCondition = AND; |
mehatfie | 11:bc9cd2869f95 | 189 | } |
mehatfie | 11:bc9cd2869f95 | 190 | |
mehatfie | 11:bc9cd2869f95 | 191 | //OR |
mehatfie | 11:bc9cd2869f95 | 192 | else if (paramCondition[k].op == OR){ |
mehatfie | 11:bc9cd2869f95 | 193 | if (!first && prevCondition != xAND && prevCondition != xOR) |
mehatfie | 11:bc9cd2869f95 | 194 | combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 195 | else if (first || prevCondition == xAND || prevCondition == xOR){ |
mehatfie | 11:bc9cd2869f95 | 196 | tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 197 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 198 | first = 0; |
mehatfie | 11:bc9cd2869f95 | 199 | } |
mehatfie | 11:bc9cd2869f95 | 200 | prevCondition = OR; |
mehatfie | 11:bc9cd2869f95 | 201 | } |
mehatfie | 5:e36e0538a903 | 202 | } |
mehatfie | 5:e36e0538a903 | 203 | |
mehatfie | 11:bc9cd2869f95 | 204 | // first value is something, not exclusive, but next values are exclusive |
mehatfie | 11:bc9cd2869f95 | 205 | else if (first && (paramCondition[k].op == AND || paramCondition[k].op == OR) && (paramCondition[k + 1].op == xAND || paramCondition[k + 1].op == xOR)){ |
mehatfie | 11:bc9cd2869f95 | 206 | tempCombinedCondition.value = paramCondition[k].value; |
mehatfie | 11:bc9cd2869f95 | 207 | tempCombinedCondition.op = paramCondition[k].op; |
mehatfie | 11:bc9cd2869f95 | 208 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 209 | prevCondition = paramCondition[k].op; |
mehatfie | 11:bc9cd2869f95 | 210 | first = 0; |
mehatfie | 5:e36e0538a903 | 211 | } |
mehatfie | 11:bc9cd2869f95 | 212 | |
mehatfie | 11:bc9cd2869f95 | 213 | else{ |
mehatfie | 11:bc9cd2869f95 | 214 | //xAND |
mehatfie | 11:bc9cd2869f95 | 215 | if (paramCondition[k].op == xAND){ |
mehatfie | 11:bc9cd2869f95 | 216 | if (combinedCondition.size() == 0){ // No values so start a new combinedCondition |
mehatfie | 11:bc9cd2869f95 | 217 | tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; |
mehatfie | 8:e9f836163229 | 218 | tempCombinedCondition.op = xAND; |
mehatfie | 8:e9f836163229 | 219 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 8:e9f836163229 | 220 | prevCondition = xAND; |
mehatfie | 8:e9f836163229 | 221 | } |
mehatfie | 11:bc9cd2869f95 | 222 | else{ |
mehatfie | 11:bc9cd2869f95 | 223 | if (combinedCondition.back().op == xAND){ // AND the value to the back most combinedCondition |
mehatfie | 11:bc9cd2869f95 | 224 | combinedCondition.back().value = combinedCondition.back().value && paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 225 | prevCondition = xAND; |
mehatfie | 11:bc9cd2869f95 | 226 | } |
mehatfie | 11:bc9cd2869f95 | 227 | else if (combinedCondition.back().op != xAND){ // Start a new combinedCondition |
mehatfie | 11:bc9cd2869f95 | 228 | tempCombinedCondition.value = paramCondition[k].value && paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 229 | tempCombinedCondition.op = xAND; |
mehatfie | 11:bc9cd2869f95 | 230 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 231 | prevCondition = xAND; |
mehatfie | 11:bc9cd2869f95 | 232 | } |
mehatfie | 11:bc9cd2869f95 | 233 | } |
mehatfie | 11:bc9cd2869f95 | 234 | |
mehatfie | 8:e9f836163229 | 235 | } |
mehatfie | 11:bc9cd2869f95 | 236 | |
mehatfie | 11:bc9cd2869f95 | 237 | //xOR |
mehatfie | 11:bc9cd2869f95 | 238 | else if (paramCondition[k].op == xOR){ |
mehatfie | 11:bc9cd2869f95 | 239 | if (combinedCondition.size() == 0){ // No values so start a new combinedCondition |
mehatfie | 8:e9f836163229 | 240 | tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; |
mehatfie | 8:e9f836163229 | 241 | tempCombinedCondition.op = xOR; |
mehatfie | 8:e9f836163229 | 242 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 8:e9f836163229 | 243 | prevCondition = xOR; |
mehatfie | 8:e9f836163229 | 244 | } |
mehatfie | 11:bc9cd2869f95 | 245 | else{ |
mehatfie | 11:bc9cd2869f95 | 246 | if (combinedCondition.back().op == xOR){ // OR the value to the back most combinedCondition |
mehatfie | 11:bc9cd2869f95 | 247 | combinedCondition.back().value = combinedCondition.back().value || paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 248 | prevCondition = xOR; |
mehatfie | 11:bc9cd2869f95 | 249 | } |
mehatfie | 11:bc9cd2869f95 | 250 | else if (combinedCondition.back().op != xOR){ // Start a new combinedCondition |
mehatfie | 11:bc9cd2869f95 | 251 | tempCombinedCondition.value = paramCondition[k].value || paramCondition[k + 1].value; |
mehatfie | 11:bc9cd2869f95 | 252 | tempCombinedCondition.op = xOR; |
mehatfie | 11:bc9cd2869f95 | 253 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 254 | prevCondition = xOR; |
mehatfie | 11:bc9cd2869f95 | 255 | } |
mehatfie | 11:bc9cd2869f95 | 256 | } |
mehatfie | 11:bc9cd2869f95 | 257 | |
mehatfie | 8:e9f836163229 | 258 | } |
mehatfie | 11:bc9cd2869f95 | 259 | |
mehatfie | 11:bc9cd2869f95 | 260 | // 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 | 11:bc9cd2869f95 | 261 | // operator of this exclusive xAND / xOR set |
mehatfie | 11:bc9cd2869f95 | 262 | if ((paramCondition[k + 1].op == AND || paramCondition[k + 1].op == OR) && (prevCondition == xAND || prevCondition == xOR)){ |
mehatfie | 11:bc9cd2869f95 | 263 | combinedCondition.back().op = paramCondition[k + 1].op; |
mehatfie | 11:bc9cd2869f95 | 264 | k++; |
mehatfie | 11:bc9cd2869f95 | 265 | } |
mehatfie | 11:bc9cd2869f95 | 266 | |
mehatfie | 5:e36e0538a903 | 267 | } |
mehatfie | 11:bc9cd2869f95 | 268 | } |
mehatfie | 6:d1594fd2ec5a | 269 | |
mehatfie | 11:bc9cd2869f95 | 270 | // the last value was not included in any combination, since directly before the last value was an xAND / xOR set that |
mehatfie | 11:bc9cd2869f95 | 271 | // included the very last AND / OR as the set's operator, yet there is still another value that has not been combined, as it is supposed |
mehatfie | 11:bc9cd2869f95 | 272 | // to be AND /OR to the exclusive xAND / xOR set |
mehatfie | 11:bc9cd2869f95 | 273 | else if (last && (prevCondition == xAND || prevCondition == xOR) && (combinedCondition.back().op == AND || combinedCondition.back().op == OR)){ |
mehatfie | 11:bc9cd2869f95 | 274 | tempCombinedCondition.value = paramCondition[k].value; |
mehatfie | 11:bc9cd2869f95 | 275 | tempCombinedCondition.op = NONE; |
mehatfie | 11:bc9cd2869f95 | 276 | combinedCondition.push_back(tempCombinedCondition); |
mehatfie | 11:bc9cd2869f95 | 277 | } |
mehatfie | 5:e36e0538a903 | 278 | } |
mehatfie | 5:e36e0538a903 | 279 | |
mehatfie | 5:e36e0538a903 | 280 | //reset the tempCombinedCondition variable |
mehatfie | 5:e36e0538a903 | 281 | tempCombinedCondition = ConditionOp(); |
mehatfie | 5:e36e0538a903 | 282 | } |
mehatfie | 5:e36e0538a903 | 283 | |
mehatfie | 11:bc9cd2869f95 | 284 | |
mehatfie | 6:d1594fd2ec5a | 285 | |
mehatfie | 5:e36e0538a903 | 286 | // run through all values in the combined Condition vector, AND'ing / OR'ing as appropriate |
mehatfie | 5:e36e0538a903 | 287 | // 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 | 11:bc9cd2869f95 | 288 | if (numParam > 1){ |
mehatfie | 11:bc9cd2869f95 | 289 | for (i = 0; i < (combinedCondition.size() - 1); i++){ |
mehatfie | 11:bc9cd2869f95 | 290 | if (combinedCondition[i].op == AND) |
mehatfie | 11:bc9cd2869f95 | 291 | combinedCondition[i + 1].value = combinedCondition[i].value && combinedCondition[i + 1].value; |
mehatfie | 11:bc9cd2869f95 | 292 | else if (combinedCondition[i].op == OR) |
mehatfie | 11:bc9cd2869f95 | 293 | combinedCondition[i + 1].value = combinedCondition[i].value || combinedCondition[i + 1].value; |
mehatfie | 11:bc9cd2869f95 | 294 | } |
mehatfie | 5:e36e0538a903 | 295 | } |
mehatfie | 10:e8db892fbc52 | 296 | |
mehatfie | 10:e8db892fbc52 | 297 | //All syntax checking done by this point, if Dummy then return success in order to check the code within the Condition |
mehatfie | 10:e8db892fbc52 | 298 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 299 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 300 | |
mehatfie | 10:e8db892fbc52 | 301 | |
mehatfie | 5:e36e0538a903 | 302 | int conditionSuccess = combinedCondition.back().value; //value is the success(1) or failure(0) of the condition statement |
mehatfie | 5:e36e0538a903 | 303 | |
mehatfie | 9:5a0c4c6e39c7 | 304 | int checkEnd = 0, returnValue; |
mehatfie | 5:e36e0538a903 | 305 | if (!conditionSuccess){ |
mehatfie | 6:d1594fd2ec5a | 306 | |
mehatfie | 5:e36e0538a903 | 307 | while (checkEnd != 4){ |
mehatfie | 5:e36e0538a903 | 308 | |
mehatfie | 9:5a0c4c6e39c7 | 309 | returnValue = getNextLine(selectedFile, lineData); |
mehatfie | 9:5a0c4c6e39c7 | 310 | |
mehatfie | 9:5a0c4c6e39c7 | 311 | //if getNextLine returned an error, then error out |
mehatfie | 9:5a0c4c6e39c7 | 312 | if (returnValue == -1) |
mehatfie | 9:5a0c4c6e39c7 | 313 | return -1; |
mehatfie | 6:d1594fd2ec5a | 314 | |
mehatfie | 6:d1594fd2ec5a | 315 | // check if the first word is an end command (avoids interpreting functions that perform actions) |
mehatfie | 5:e36e0538a903 | 316 | if (lineData.word[0].compare("end") == 0) |
mehatfie | 11:bc9cd2869f95 | 317 | checkEnd = interpretCommand(lineData); |
mehatfie | 5:e36e0538a903 | 318 | |
mehatfie | 5:e36e0538a903 | 319 | if (checkEnd == 4) // custom return value for this function |
mehatfie | 9:5a0c4c6e39c7 | 320 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 9:5a0c4c6e39c7 | 321 | else if (checkEnd == -1) //if interpretCommand returned an error, then error out |
mehatfie | 9:5a0c4c6e39c7 | 322 | return -1; |
mehatfie | 5:e36e0538a903 | 323 | } |
mehatfie | 5:e36e0538a903 | 324 | } |
mehatfie | 5:e36e0538a903 | 325 | |
mehatfie | 5:e36e0538a903 | 326 | // Return success as the function either met the condition and will continue from the next line, or |
mehatfie | 5:e36e0538a903 | 327 | // failed to meet the condition and ran through the lines inside the condition until "end condition" was found, therefore |
mehatfie | 5:e36e0538a903 | 328 | // the program will proceed from the line after the "end condition" line |
mehatfie | 9:5a0c4c6e39c7 | 329 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 5:e36e0538a903 | 330 | } |
mehatfie | 11:bc9cd2869f95 | 331 | |
mehatfie | 0:22618cf06f45 | 332 | |
mehatfie | 11:bc9cd2869f95 | 333 | cycleWatch |
mehatfie | 1:5731f31f96be | 334 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 335 | /**********************************************************************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 336 | /************************** <FUNCTION: cycleCommand> *****************************/ |
mehatfie | 1:5731f31f96be | 337 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 338 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 339 | |
mehatfie | 11:bc9cd2869f95 | 340 | int loopCommand(LineData &lineData){ |
mehatfie | 11:bc9cd2869f95 | 341 | |
mehatfie | 11:bc9cd2869f95 | 342 | int thisLoopMain = 0; |
mehatfie | 11:bc9cd2869f95 | 343 | if (mainLoopFlag == 0){ |
mehatfie | 11:bc9cd2869f95 | 344 | thisLoopMain = 1; |
mehatfie | 11:bc9cd2869f95 | 345 | mainLoopFlag = 1; |
mehatfie | 11:bc9cd2869f95 | 346 | } |
mehatfie | 11:bc9cd2869f95 | 347 | |
mehatfie | 2:3e7baa3e3fec | 348 | //Get the Condition value for number of times to loop |
mehatfie | 2:3e7baa3e3fec | 349 | string loopCondition = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 350 | int loopConditionValue = 0; |
mehatfie | 11:bc9cd2869f95 | 351 | int loopConditionState = 0; //State 1 = device condtition, State 2 = numerical condition |
mehatfie | 9:5a0c4c6e39c7 | 352 | |
mehatfie | 11:bc9cd2869f95 | 353 | LineData conditionLine; |
mehatfie | 11:bc9cd2869f95 | 354 | //if the loop is supposed to happen under specific device conditions |
mehatfie | 11:bc9cd2869f95 | 355 | if (loopCondition.compare("condition") == 0){ |
mehatfie | 11:bc9cd2869f95 | 356 | |
mehatfie | 11:bc9cd2869f95 | 357 | loopConditionState = 1; |
mehatfie | 11:bc9cd2869f95 | 358 | |
mehatfie | 11:bc9cd2869f95 | 359 | //extract the command condition to be checked each loop |
mehatfie | 11:bc9cd2869f95 | 360 | int i = 2, funcNumWords = 0; |
mehatfie | 11:bc9cd2869f95 | 361 | for(i = 2; i < lineData.numWords; i++){ |
mehatfie | 11:bc9cd2869f95 | 362 | conditionLine.word[funcNumWords] = lineData.word[i]; |
mehatfie | 11:bc9cd2869f95 | 363 | funcNumWords++; |
mehatfie | 11:bc9cd2869f95 | 364 | } |
mehatfie | 11:bc9cd2869f95 | 365 | |
mehatfie | 11:bc9cd2869f95 | 366 | conditionLine.numWords = funcNumWords; |
mehatfie | 11:bc9cd2869f95 | 367 | conditionLine.lineAddress = lineData.lineAddress; |
mehatfie | 11:bc9cd2869f95 | 368 | conditionLine.lineNumber = lineData.lineNumber; |
mehatfie | 9:5a0c4c6e39c7 | 369 | } |
mehatfie | 1:5731f31f96be | 370 | |
mehatfie | 11:bc9cd2869f95 | 371 | //if the second word isn't condition, it means it's a number |
mehatfie | 11:bc9cd2869f95 | 372 | else{ |
mehatfie | 11:bc9cd2869f95 | 373 | loopConditionState = 2; |
mehatfie | 11:bc9cd2869f95 | 374 | |
mehatfie | 11:bc9cd2869f95 | 375 | int numValuesFound = sscanf(loopCondition.c_str(), "%d", &loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 376 | if (numValuesFound < 1){ |
mehatfie | 11:bc9cd2869f95 | 377 | ErrorOut("Parameter Unknown, loopCondition Value can't be converted", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 378 | return -1; |
mehatfie | 11:bc9cd2869f95 | 379 | } |
mehatfie | 11:bc9cd2869f95 | 380 | |
mehatfie | 11:bc9cd2869f95 | 381 | //loop condition must be greater than 0 |
mehatfie | 11:bc9cd2869f95 | 382 | if (loopConditionValue <= 0){ |
mehatfie | 11:bc9cd2869f95 | 383 | ErrorOut("Loop Condition must be greater than 0", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 384 | return -1; |
mehatfie | 11:bc9cd2869f95 | 385 | } |
mehatfie | 11:bc9cd2869f95 | 386 | } |
mehatfie | 11:bc9cd2869f95 | 387 | |
mehatfie | 11:bc9cd2869f95 | 388 | |
mehatfie | 2:3e7baa3e3fec | 389 | int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1; |
mehatfie | 6:d1594fd2ec5a | 390 | |
mehatfie | 11:bc9cd2869f95 | 391 | if (thisLoopMain){ |
mehatfie | 11:bc9cd2869f95 | 392 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 393 | lcd.printf("Cycle 1 of %d", loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 394 | } |
mehatfie | 11:bc9cd2869f95 | 395 | |
mehatfie | 9:5a0c4c6e39c7 | 396 | float totalLoopTime = 0; |
mehatfie | 6:d1594fd2ec5a | 397 | Timer cycleTimer; |
mehatfie | 6:d1594fd2ec5a | 398 | cycleTimer.reset(); |
mehatfie | 6:d1594fd2ec5a | 399 | cycleTimer.start(); |
mehatfie | 6:d1594fd2ec5a | 400 | |
mehatfie | 11:bc9cd2869f95 | 401 | int counter = 1, checkEnd = 0, returnValue, conditionMet = 0; |
mehatfie | 11:bc9cd2869f95 | 402 | |
mehatfie | 11:bc9cd2869f95 | 403 | |
mehatfie | 11:bc9cd2869f95 | 404 | //Before starting the loop, get the state of the device conditions |
mehatfie | 11:bc9cd2869f95 | 405 | if (loopConditionState == 1){ |
mehatfie | 11:bc9cd2869f95 | 406 | conditionMet = interpretCommand(conditionLine); |
mehatfie | 11:bc9cd2869f95 | 407 | if (conditionMet == -1) |
mehatfie | 11:bc9cd2869f95 | 408 | return -1; //if the interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 409 | |
mehatfie | 11:bc9cd2869f95 | 410 | //condition met, so skip to end of loop |
mehatfie | 11:bc9cd2869f95 | 411 | if (conditionMet == 1){ |
mehatfie | 11:bc9cd2869f95 | 412 | int checkEnd = 0, returnValue = 0; |
mehatfie | 11:bc9cd2869f95 | 413 | while (checkEnd != 3){ |
mehatfie | 11:bc9cd2869f95 | 414 | |
mehatfie | 11:bc9cd2869f95 | 415 | returnValue = getNextLine(selectedFile, lineData); |
mehatfie | 11:bc9cd2869f95 | 416 | |
mehatfie | 11:bc9cd2869f95 | 417 | //if getNextLine returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 418 | if (returnValue == -1) |
mehatfie | 11:bc9cd2869f95 | 419 | return -1; |
mehatfie | 11:bc9cd2869f95 | 420 | |
mehatfie | 11:bc9cd2869f95 | 421 | // check if the first word is an end command (avoids interpreting functions that perform actions) |
mehatfie | 11:bc9cd2869f95 | 422 | if (lineData.word[0].compare("end") == 0) |
mehatfie | 11:bc9cd2869f95 | 423 | checkEnd = interpretCommand(lineData); |
mehatfie | 11:bc9cd2869f95 | 424 | |
mehatfie | 11:bc9cd2869f95 | 425 | if (checkEnd == 4) // custom return value for this function |
mehatfie | 11:bc9cd2869f95 | 426 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 11:bc9cd2869f95 | 427 | else if (checkEnd == -1) //if interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 428 | return -1; |
mehatfie | 11:bc9cd2869f95 | 429 | } |
mehatfie | 11:bc9cd2869f95 | 430 | } |
mehatfie | 11:bc9cd2869f95 | 431 | } |
mehatfie | 11:bc9cd2869f95 | 432 | |
mehatfie | 11:bc9cd2869f95 | 433 | |
mehatfie | 11:bc9cd2869f95 | 434 | while (!conditionMet){ |
mehatfie | 5:e36e0538a903 | 435 | |
mehatfie | 9:5a0c4c6e39c7 | 436 | returnValue = getNextLine(selectedFile, lineData); |
mehatfie | 11:bc9cd2869f95 | 437 | |
mehatfie | 9:5a0c4c6e39c7 | 438 | //if getNextLine returned an error, then return error out |
mehatfie | 9:5a0c4c6e39c7 | 439 | if (returnValue == -1) |
mehatfie | 9:5a0c4c6e39c7 | 440 | return -1; |
mehatfie | 11:bc9cd2869f95 | 441 | |
mehatfie | 5:e36e0538a903 | 442 | //Must get the address before entering the interpret command |
mehatfie | 5:e36e0538a903 | 443 | // if a Condition command is immediately after, and the condition fails, then |
mehatfie | 5:e36e0538a903 | 444 | // the interpret command will return the line at the "end condition" line, and therefore |
mehatfie | 5:e36e0538a903 | 445 | // set the loop's first line to be the "end condition" line, if interpretCommand is called BEFORE setting the first loop line address |
mehatfie | 11:bc9cd2869f95 | 446 | if (firstLineOfLoop){ |
mehatfie | 1:5731f31f96be | 447 | loopStartAddress = lineData.lineAddress; //Save the Line Address |
mehatfie | 1:5731f31f96be | 448 | loopLineNumber = lineData.lineNumber; //Save the Line Number |
mehatfie | 1:5731f31f96be | 449 | firstLineOfLoop = 0; |
mehatfie | 1:5731f31f96be | 450 | } |
mehatfie | 11:bc9cd2869f95 | 451 | |
mehatfie | 11:bc9cd2869f95 | 452 | checkEnd = interpretCommand(lineData); |
mehatfie | 6:d1594fd2ec5a | 453 | |
mehatfie | 1:5731f31f96be | 454 | //Increase the loop counter and go back to the beginning of the loop |
mehatfie | 5:e36e0538a903 | 455 | if (checkEnd == 3){ |
mehatfie | 11:bc9cd2869f95 | 456 | |
mehatfie | 10:e8db892fbc52 | 457 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to loop again |
mehatfie | 10:e8db892fbc52 | 458 | if (DummyMode) |
mehatfie | 10:e8db892fbc52 | 459 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 10:e8db892fbc52 | 460 | |
mehatfie | 6:d1594fd2ec5a | 461 | //Output the Avg Cycle Time |
mehatfie | 6:d1594fd2ec5a | 462 | cycleTimer.stop(); |
mehatfie | 6:d1594fd2ec5a | 463 | totalLoopTime += cycleTimer.read(); |
mehatfie | 11:bc9cd2869f95 | 464 | |
mehatfie | 11:bc9cd2869f95 | 465 | /* lcd.setAddress(0,2); |
mehatfie | 11:bc9cd2869f95 | 466 | lcd.printf("TEST: %d", thisLoopMain); |
mehatfie | 11:bc9cd2869f95 | 467 | wait(2);*/ |
mehatfie | 11:bc9cd2869f95 | 468 | |
mehatfie | 11:bc9cd2869f95 | 469 | //if (thisLoopMain == 1){ |
mehatfie | 11:bc9cd2869f95 | 470 | |
mehatfie | 11:bc9cd2869f95 | 471 | lcd.setAddress(0,1); |
mehatfie | 11:bc9cd2869f95 | 472 | lcd.printf("Avg t(sec): %1.3f", (totalLoopTime / counter)); |
mehatfie | 11:bc9cd2869f95 | 473 | |
mehatfie | 11:bc9cd2869f95 | 474 | //Output Cycle Number |
mehatfie | 11:bc9cd2869f95 | 475 | counter++; |
mehatfie | 11:bc9cd2869f95 | 476 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 477 | lcd.printf("Cycle %d of %d", counter, loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 478 | wait(2); |
mehatfie | 11:bc9cd2869f95 | 479 | //} |
mehatfie | 11:bc9cd2869f95 | 480 | |
mehatfie | 11:bc9cd2869f95 | 481 | |
mehatfie | 11:bc9cd2869f95 | 482 | //Check whether the we should stop looping based on the state that the loop is based on (device conditional / numerical conditional) |
mehatfie | 11:bc9cd2869f95 | 483 | if (loopConditionState == 1){ |
mehatfie | 11:bc9cd2869f95 | 484 | conditionMet = interpretCommand(conditionLine); |
mehatfie | 11:bc9cd2869f95 | 485 | if (conditionMet == -1) |
mehatfie | 11:bc9cd2869f95 | 486 | return -1; //if the interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 487 | } |
mehatfie | 11:bc9cd2869f95 | 488 | else if (loopConditionState == 2){ |
mehatfie | 11:bc9cd2869f95 | 489 | if (counter >= loopConditionValue) |
mehatfie | 11:bc9cd2869f95 | 490 | conditionMet = 1; |
mehatfie | 9:5a0c4c6e39c7 | 491 | } |
mehatfie | 9:5a0c4c6e39c7 | 492 | |
mehatfie | 11:bc9cd2869f95 | 493 | //if the condition has not been met, then seek back to the beginning of the loop |
mehatfie | 11:bc9cd2869f95 | 494 | if (!conditionMet){ |
mehatfie | 11:bc9cd2869f95 | 495 | int seekFailure = fseek(selectedFile, loopStartAddress, SEEK_SET); //fseek returns 0 on success |
mehatfie | 11:bc9cd2869f95 | 496 | if (seekFailure){ |
mehatfie | 11:bc9cd2869f95 | 497 | ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD |
mehatfie | 11:bc9cd2869f95 | 498 | return -1; |
mehatfie | 11:bc9cd2869f95 | 499 | } |
mehatfie | 11:bc9cd2869f95 | 500 | } |
mehatfie | 11:bc9cd2869f95 | 501 | |
mehatfie | 9:5a0c4c6e39c7 | 502 | lineData.lineNumber = loopLineNumber - 1; |
mehatfie | 5:e36e0538a903 | 503 | checkEnd = 0; |
mehatfie | 11:bc9cd2869f95 | 504 | |
mehatfie | 6:d1594fd2ec5a | 505 | //Restart the timer for the next loop |
mehatfie | 6:d1594fd2ec5a | 506 | cycleTimer.reset(); |
mehatfie | 6:d1594fd2ec5a | 507 | cycleTimer.start(); |
mehatfie | 11:bc9cd2869f95 | 508 | |
mehatfie | 1:5731f31f96be | 509 | } |
mehatfie | 11:bc9cd2869f95 | 510 | else if (checkEnd == -1){ //if interpretCommand returned an error, then return error out |
mehatfie | 9:5a0c4c6e39c7 | 511 | return -1; |
mehatfie | 11:bc9cd2869f95 | 512 | } |
mehatfie | 1:5731f31f96be | 513 | } |
mehatfie | 11:bc9cd2869f95 | 514 | |
mehatfie | 11:bc9cd2869f95 | 515 | |
mehatfie | 11:bc9cd2869f95 | 516 | //give the "main loop" classification up to the next loop that wants it |
mehatfie | 11:bc9cd2869f95 | 517 | if (thisLoopMain == 1){ |
mehatfie | 11:bc9cd2869f95 | 518 | thisLoopMain = 0; |
mehatfie | 11:bc9cd2869f95 | 519 | mainLoopFlag = 0; |
mehatfie | 11:bc9cd2869f95 | 520 | } |
mehatfie | 11:bc9cd2869f95 | 521 | |
mehatfie | 11:bc9cd2869f95 | 522 | return 0; //Return Success, no value is being sent so don't return 1 |
mehatfie | 11:bc9cd2869f95 | 523 | } |
mehatfie | 11:bc9cd2869f95 | 524 | |
mehatfie | 11:bc9cd2869f95 | 525 | |
mehatfie | 11:bc9cd2869f95 | 526 | |
mehatfie | 11:bc9cd2869f95 | 527 | /**********************************************************************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 528 | /**********************************************************************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 529 | /************************** <FUNCTION: loopCommand> *****************************/ |
mehatfie | 11:bc9cd2869f95 | 530 | /**********************************************************************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 531 | /**********************************************************************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 532 | |
mehatfie | 11:bc9cd2869f95 | 533 | int mainLoopFlag = 0; //so that we only show cycle count for the main loop |
mehatfie | 11:bc9cd2869f95 | 534 | |
mehatfie | 11:bc9cd2869f95 | 535 | int loopCommand(LineData &lineData){ |
mehatfie | 11:bc9cd2869f95 | 536 | |
mehatfie | 11:bc9cd2869f95 | 537 | int thisLoopMain = 0; |
mehatfie | 11:bc9cd2869f95 | 538 | if (mainLoopFlag == 0){ |
mehatfie | 11:bc9cd2869f95 | 539 | thisLoopMain = 1; |
mehatfie | 11:bc9cd2869f95 | 540 | mainLoopFlag = 1; |
mehatfie | 11:bc9cd2869f95 | 541 | } |
mehatfie | 11:bc9cd2869f95 | 542 | |
mehatfie | 11:bc9cd2869f95 | 543 | //Get the Condition value for number of times to loop |
mehatfie | 11:bc9cd2869f95 | 544 | string loopCondition = lineData.word[1]; |
mehatfie | 11:bc9cd2869f95 | 545 | int loopConditionValue = 0; |
mehatfie | 11:bc9cd2869f95 | 546 | int loopConditionState = 0; //State 1 = device condtition, State 2 = numerical condition |
mehatfie | 11:bc9cd2869f95 | 547 | |
mehatfie | 11:bc9cd2869f95 | 548 | LineData conditionLine; |
mehatfie | 11:bc9cd2869f95 | 549 | //if the loop is supposed to happen under specific device conditions |
mehatfie | 11:bc9cd2869f95 | 550 | if (loopCondition.compare("condition") == 0){ |
mehatfie | 2:3e7baa3e3fec | 551 | |
mehatfie | 11:bc9cd2869f95 | 552 | loopConditionState = 1; |
mehatfie | 11:bc9cd2869f95 | 553 | |
mehatfie | 11:bc9cd2869f95 | 554 | //extract the command condition to be checked each loop |
mehatfie | 11:bc9cd2869f95 | 555 | int i = 2, funcNumWords = 0; |
mehatfie | 11:bc9cd2869f95 | 556 | for(i = 2; i < lineData.numWords; i++){ |
mehatfie | 11:bc9cd2869f95 | 557 | conditionLine.word[funcNumWords] = lineData.word[i]; |
mehatfie | 11:bc9cd2869f95 | 558 | funcNumWords++; |
mehatfie | 11:bc9cd2869f95 | 559 | } |
mehatfie | 11:bc9cd2869f95 | 560 | |
mehatfie | 11:bc9cd2869f95 | 561 | conditionLine.numWords = funcNumWords; |
mehatfie | 11:bc9cd2869f95 | 562 | conditionLine.lineAddress = lineData.lineAddress; |
mehatfie | 11:bc9cd2869f95 | 563 | conditionLine.lineNumber = lineData.lineNumber; |
mehatfie | 11:bc9cd2869f95 | 564 | } |
mehatfie | 11:bc9cd2869f95 | 565 | |
mehatfie | 11:bc9cd2869f95 | 566 | //if the second word isn't condition, it means it's a number |
mehatfie | 11:bc9cd2869f95 | 567 | else{ |
mehatfie | 11:bc9cd2869f95 | 568 | loopConditionState = 2; |
mehatfie | 11:bc9cd2869f95 | 569 | |
mehatfie | 11:bc9cd2869f95 | 570 | int numValuesFound = sscanf(loopCondition.c_str(), "%d", &loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 571 | if (numValuesFound < 1){ |
mehatfie | 11:bc9cd2869f95 | 572 | ErrorOut("Parameter Unknown, loopCondition Value can't be converted", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 573 | return -1; |
mehatfie | 11:bc9cd2869f95 | 574 | } |
mehatfie | 11:bc9cd2869f95 | 575 | |
mehatfie | 11:bc9cd2869f95 | 576 | //loop condition must be greater than 0 |
mehatfie | 11:bc9cd2869f95 | 577 | if (loopConditionValue <= 0){ |
mehatfie | 11:bc9cd2869f95 | 578 | ErrorOut("Loop Condition must be greater than 0", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 579 | return -1; |
mehatfie | 11:bc9cd2869f95 | 580 | } |
mehatfie | 11:bc9cd2869f95 | 581 | } |
mehatfie | 11:bc9cd2869f95 | 582 | |
mehatfie | 11:bc9cd2869f95 | 583 | |
mehatfie | 11:bc9cd2869f95 | 584 | int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1; |
mehatfie | 11:bc9cd2869f95 | 585 | |
mehatfie | 11:bc9cd2869f95 | 586 | if (thisLoopMain){ |
mehatfie | 11:bc9cd2869f95 | 587 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 588 | lcd.printf("Cycle 1 of %d", loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 589 | } |
mehatfie | 11:bc9cd2869f95 | 590 | |
mehatfie | 11:bc9cd2869f95 | 591 | float totalLoopTime = 0; |
mehatfie | 11:bc9cd2869f95 | 592 | Timer cycleTimer; |
mehatfie | 11:bc9cd2869f95 | 593 | cycleTimer.reset(); |
mehatfie | 11:bc9cd2869f95 | 594 | cycleTimer.start(); |
mehatfie | 11:bc9cd2869f95 | 595 | |
mehatfie | 11:bc9cd2869f95 | 596 | int counter = 1, checkEnd = 0, returnValue, conditionMet = 0; |
mehatfie | 11:bc9cd2869f95 | 597 | |
mehatfie | 11:bc9cd2869f95 | 598 | |
mehatfie | 11:bc9cd2869f95 | 599 | //Before starting the loop, get the state of the device conditions |
mehatfie | 11:bc9cd2869f95 | 600 | if (loopConditionState == 1){ |
mehatfie | 11:bc9cd2869f95 | 601 | conditionMet = interpretCommand(conditionLine); |
mehatfie | 11:bc9cd2869f95 | 602 | if (conditionMet == -1) |
mehatfie | 11:bc9cd2869f95 | 603 | return -1; //if the interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 604 | |
mehatfie | 11:bc9cd2869f95 | 605 | //condition met, so skip to end of loop |
mehatfie | 11:bc9cd2869f95 | 606 | if (conditionMet == 1){ |
mehatfie | 11:bc9cd2869f95 | 607 | int checkEnd = 0, returnValue = 0; |
mehatfie | 11:bc9cd2869f95 | 608 | while (checkEnd != 3){ |
mehatfie | 11:bc9cd2869f95 | 609 | |
mehatfie | 11:bc9cd2869f95 | 610 | returnValue = getNextLine(selectedFile, lineData); |
mehatfie | 11:bc9cd2869f95 | 611 | |
mehatfie | 11:bc9cd2869f95 | 612 | //if getNextLine returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 613 | if (returnValue == -1) |
mehatfie | 11:bc9cd2869f95 | 614 | return -1; |
mehatfie | 11:bc9cd2869f95 | 615 | |
mehatfie | 11:bc9cd2869f95 | 616 | // check if the first word is an end command (avoids interpreting functions that perform actions) |
mehatfie | 11:bc9cd2869f95 | 617 | if (lineData.word[0].compare("end") == 0) |
mehatfie | 11:bc9cd2869f95 | 618 | checkEnd = interpretCommand(lineData); |
mehatfie | 11:bc9cd2869f95 | 619 | |
mehatfie | 11:bc9cd2869f95 | 620 | if (checkEnd == 4) // custom return value for this function |
mehatfie | 11:bc9cd2869f95 | 621 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 11:bc9cd2869f95 | 622 | else if (checkEnd == -1) //if interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 623 | return -1; |
mehatfie | 11:bc9cd2869f95 | 624 | } |
mehatfie | 11:bc9cd2869f95 | 625 | } |
mehatfie | 11:bc9cd2869f95 | 626 | } |
mehatfie | 11:bc9cd2869f95 | 627 | |
mehatfie | 11:bc9cd2869f95 | 628 | |
mehatfie | 11:bc9cd2869f95 | 629 | while (!conditionMet){ |
mehatfie | 11:bc9cd2869f95 | 630 | |
mehatfie | 11:bc9cd2869f95 | 631 | returnValue = getNextLine(selectedFile, lineData); |
mehatfie | 11:bc9cd2869f95 | 632 | |
mehatfie | 11:bc9cd2869f95 | 633 | //if getNextLine returned an error, then return error out |
mehatfie | 11:bc9cd2869f95 | 634 | if (returnValue == -1) |
mehatfie | 11:bc9cd2869f95 | 635 | return -1; |
mehatfie | 11:bc9cd2869f95 | 636 | |
mehatfie | 11:bc9cd2869f95 | 637 | //Must get the address before entering the interpret command |
mehatfie | 11:bc9cd2869f95 | 638 | // if a Condition command is immediately after, and the condition fails, then |
mehatfie | 11:bc9cd2869f95 | 639 | // the interpret command will return the line at the "end condition" line, and therefore |
mehatfie | 11:bc9cd2869f95 | 640 | // set the loop's first line to be the "end condition" line, if interpretCommand is called BEFORE setting the first loop line address |
mehatfie | 11:bc9cd2869f95 | 641 | if (firstLineOfLoop){ |
mehatfie | 11:bc9cd2869f95 | 642 | loopStartAddress = lineData.lineAddress; //Save the Line Address |
mehatfie | 11:bc9cd2869f95 | 643 | loopLineNumber = lineData.lineNumber; //Save the Line Number |
mehatfie | 11:bc9cd2869f95 | 644 | firstLineOfLoop = 0; |
mehatfie | 11:bc9cd2869f95 | 645 | } |
mehatfie | 11:bc9cd2869f95 | 646 | |
mehatfie | 11:bc9cd2869f95 | 647 | checkEnd = interpretCommand(lineData); |
mehatfie | 11:bc9cd2869f95 | 648 | |
mehatfie | 11:bc9cd2869f95 | 649 | //Increase the loop counter and go back to the beginning of the loop |
mehatfie | 11:bc9cd2869f95 | 650 | if (checkEnd == 3){ |
mehatfie | 11:bc9cd2869f95 | 651 | |
mehatfie | 11:bc9cd2869f95 | 652 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to loop again |
mehatfie | 11:bc9cd2869f95 | 653 | if (DummyMode) |
mehatfie | 11:bc9cd2869f95 | 654 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 11:bc9cd2869f95 | 655 | |
mehatfie | 11:bc9cd2869f95 | 656 | //Output the Avg Cycle Time |
mehatfie | 11:bc9cd2869f95 | 657 | cycleTimer.stop(); |
mehatfie | 11:bc9cd2869f95 | 658 | totalLoopTime += cycleTimer.read(); |
mehatfie | 11:bc9cd2869f95 | 659 | |
mehatfie | 11:bc9cd2869f95 | 660 | /* lcd.setAddress(0,2); |
mehatfie | 11:bc9cd2869f95 | 661 | lcd.printf("TEST: %d", thisLoopMain); |
mehatfie | 11:bc9cd2869f95 | 662 | wait(2);*/ |
mehatfie | 11:bc9cd2869f95 | 663 | |
mehatfie | 11:bc9cd2869f95 | 664 | //if (thisLoopMain == 1){ |
mehatfie | 11:bc9cd2869f95 | 665 | |
mehatfie | 11:bc9cd2869f95 | 666 | lcd.setAddress(0,1); |
mehatfie | 11:bc9cd2869f95 | 667 | lcd.printf("Avg t(sec): %1.3f", (totalLoopTime / counter)); |
mehatfie | 11:bc9cd2869f95 | 668 | |
mehatfie | 11:bc9cd2869f95 | 669 | //Output Cycle Number |
mehatfie | 11:bc9cd2869f95 | 670 | counter++; |
mehatfie | 11:bc9cd2869f95 | 671 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 672 | lcd.printf("Cycle %d of %d", counter, loopConditionValue); |
mehatfie | 11:bc9cd2869f95 | 673 | wait(2); |
mehatfie | 11:bc9cd2869f95 | 674 | //} |
mehatfie | 11:bc9cd2869f95 | 675 | |
mehatfie | 11:bc9cd2869f95 | 676 | |
mehatfie | 11:bc9cd2869f95 | 677 | //Check whether the we should stop looping based on the state that the loop is based on (device conditional / numerical conditional) |
mehatfie | 11:bc9cd2869f95 | 678 | if (loopConditionState == 1){ |
mehatfie | 11:bc9cd2869f95 | 679 | conditionMet = interpretCommand(conditionLine); |
mehatfie | 11:bc9cd2869f95 | 680 | if (conditionMet == -1) |
mehatfie | 11:bc9cd2869f95 | 681 | return -1; //if the interpretCommand returned an error, then error out |
mehatfie | 11:bc9cd2869f95 | 682 | } |
mehatfie | 11:bc9cd2869f95 | 683 | else if (loopConditionState == 2){ |
mehatfie | 11:bc9cd2869f95 | 684 | if (counter >= loopConditionValue) |
mehatfie | 11:bc9cd2869f95 | 685 | conditionMet = 1; |
mehatfie | 11:bc9cd2869f95 | 686 | } |
mehatfie | 11:bc9cd2869f95 | 687 | |
mehatfie | 11:bc9cd2869f95 | 688 | //if the condition has not been met, then seek back to the beginning of the loop |
mehatfie | 11:bc9cd2869f95 | 689 | if (!conditionMet){ |
mehatfie | 11:bc9cd2869f95 | 690 | int seekFailure = fseek(selectedFile, loopStartAddress, SEEK_SET); //fseek returns 0 on success |
mehatfie | 11:bc9cd2869f95 | 691 | if (seekFailure){ |
mehatfie | 11:bc9cd2869f95 | 692 | ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD |
mehatfie | 11:bc9cd2869f95 | 693 | return -1; |
mehatfie | 11:bc9cd2869f95 | 694 | } |
mehatfie | 11:bc9cd2869f95 | 695 | } |
mehatfie | 11:bc9cd2869f95 | 696 | |
mehatfie | 11:bc9cd2869f95 | 697 | lineData.lineNumber = loopLineNumber - 1; |
mehatfie | 11:bc9cd2869f95 | 698 | checkEnd = 0; |
mehatfie | 11:bc9cd2869f95 | 699 | |
mehatfie | 11:bc9cd2869f95 | 700 | //Restart the timer for the next loop |
mehatfie | 11:bc9cd2869f95 | 701 | cycleTimer.reset(); |
mehatfie | 11:bc9cd2869f95 | 702 | cycleTimer.start(); |
mehatfie | 11:bc9cd2869f95 | 703 | |
mehatfie | 11:bc9cd2869f95 | 704 | } |
mehatfie | 11:bc9cd2869f95 | 705 | else if (checkEnd == -1){ //if interpretCommand returned an error, then return error out |
mehatfie | 11:bc9cd2869f95 | 706 | return -1; |
mehatfie | 11:bc9cd2869f95 | 707 | } |
mehatfie | 11:bc9cd2869f95 | 708 | } |
mehatfie | 11:bc9cd2869f95 | 709 | |
mehatfie | 11:bc9cd2869f95 | 710 | |
mehatfie | 11:bc9cd2869f95 | 711 | //give the "main loop" classification up to the next loop that wants it |
mehatfie | 11:bc9cd2869f95 | 712 | if (thisLoopMain == 1){ |
mehatfie | 11:bc9cd2869f95 | 713 | thisLoopMain = 0; |
mehatfie | 11:bc9cd2869f95 | 714 | mainLoopFlag = 0; |
mehatfie | 11:bc9cd2869f95 | 715 | } |
mehatfie | 11:bc9cd2869f95 | 716 | |
mehatfie | 9:5a0c4c6e39c7 | 717 | return 0; //Return Success, no value is being sent so don't return 1 |
mehatfie | 1:5731f31f96be | 718 | } |
mehatfie | 1:5731f31f96be | 719 | |
mehatfie | 1:5731f31f96be | 720 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 721 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 722 | /************************* <FUNCTION: interpretCommand> *************************/ |
mehatfie | 1:5731f31f96be | 723 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 724 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 725 | |
mehatfie | 11:bc9cd2869f95 | 726 | int interprettingErrorFlag = 0; |
mehatfie | 11:bc9cd2869f95 | 727 | |
mehatfie | 11:bc9cd2869f95 | 728 | class ErrorCondition{ |
mehatfie | 11:bc9cd2869f95 | 729 | |
mehatfie | 11:bc9cd2869f95 | 730 | public: |
mehatfie | 11:bc9cd2869f95 | 731 | LineData errorToWatch; |
mehatfie | 11:bc9cd2869f95 | 732 | LineData errorFix; |
mehatfie | 11:bc9cd2869f95 | 733 | int hasFix; |
mehatfie | 11:bc9cd2869f95 | 734 | }; |
mehatfie | 11:bc9cd2869f95 | 735 | |
mehatfie | 11:bc9cd2869f95 | 736 | //vector<ErrorCondition> errorMonitors; //Initialize vector of errors to monitor |
mehatfie | 11:bc9cd2869f95 | 737 | ErrorCondition errorMonitors[5]; |
mehatfie | 11:bc9cd2869f95 | 738 | int numErrorsConditions = 0; |
mehatfie | 11:bc9cd2869f95 | 739 | |
mehatfie | 11:bc9cd2869f95 | 740 | |
mehatfie | 11:bc9cd2869f95 | 741 | int interpretCommand(LineData &lineData){ |
mehatfie | 11:bc9cd2869f95 | 742 | |
mehatfie | 11:bc9cd2869f95 | 743 | //Monitors the conditions to watch for erroring, and pauses system if any of the conditions turn out to be true |
mehatfie | 11:bc9cd2869f95 | 744 | if (!interprettingErrorFlag){ |
mehatfie | 11:bc9cd2869f95 | 745 | int j = 0, error = -1, numError = 0; |
mehatfie | 11:bc9cd2869f95 | 746 | for(j = 0; j < numErrorsConditions; j++){ |
mehatfie | 11:bc9cd2869f95 | 747 | int checkCondition = 0; |
mehatfie | 11:bc9cd2869f95 | 748 | |
mehatfie | 11:bc9cd2869f95 | 749 | interprettingErrorFlag = 1; |
mehatfie | 11:bc9cd2869f95 | 750 | checkCondition = interpretCommand(errorMonitors[j].errorToWatch); |
mehatfie | 11:bc9cd2869f95 | 751 | interprettingErrorFlag = 0; |
mehatfie | 11:bc9cd2869f95 | 752 | |
mehatfie | 11:bc9cd2869f95 | 753 | //if Condition is true, that means the error occurred |
mehatfie | 11:bc9cd2869f95 | 754 | if (checkCondition == 1){ |
mehatfie | 11:bc9cd2869f95 | 755 | numError++; |
mehatfie | 11:bc9cd2869f95 | 756 | |
mehatfie | 11:bc9cd2869f95 | 757 | //if the error has a Fix / Reset command, do it |
mehatfie | 11:bc9cd2869f95 | 758 | if (errorMonitors[j].hasFix){ |
mehatfie | 11:bc9cd2869f95 | 759 | int returnValue; |
mehatfie | 11:bc9cd2869f95 | 760 | interprettingErrorFlag = 1; |
mehatfie | 11:bc9cd2869f95 | 761 | returnValue = interpretCommand(errorMonitors[j].errorFix); //Fix / Reset the error based on how the user justified to do so |
mehatfie | 11:bc9cd2869f95 | 762 | interprettingErrorFlag = 0; |
mehatfie | 11:bc9cd2869f95 | 763 | if (returnValue == -1) |
mehatfie | 11:bc9cd2869f95 | 764 | return -1; |
mehatfie | 11:bc9cd2869f95 | 765 | } |
mehatfie | 11:bc9cd2869f95 | 766 | |
mehatfie | 11:bc9cd2869f95 | 767 | error = j; //Record index of error, will display only one error, but error will be last error that was true in the list |
mehatfie | 11:bc9cd2869f95 | 768 | } |
mehatfie | 11:bc9cd2869f95 | 769 | |
mehatfie | 11:bc9cd2869f95 | 770 | else if (checkCondition == -1) |
mehatfie | 11:bc9cd2869f95 | 771 | return -1; |
mehatfie | 11:bc9cd2869f95 | 772 | } |
mehatfie | 11:bc9cd2869f95 | 773 | |
mehatfie | 11:bc9cd2869f95 | 774 | if (numError){ |
mehatfie | 11:bc9cd2869f95 | 775 | char errorMsg[100] = "ERROR!!! Item: "; |
mehatfie | 11:bc9cd2869f95 | 776 | strcat(errorMsg, errorMonitors[error].errorToWatch.word[0].c_str()); //Send the first word of the error condition to help find out what the error was |
mehatfie | 11:bc9cd2869f95 | 777 | ErrorOut(errorMsg, numError); |
mehatfie | 11:bc9cd2869f95 | 778 | //errorFLAG = 1; //set error flag equal to 1 if error occurred |
mehatfie | 11:bc9cd2869f95 | 779 | |
mehatfie | 11:bc9cd2869f95 | 780 | //place all devices into the pause functionality |
mehatfie | 11:bc9cd2869f95 | 781 | int i = 0; |
mehatfie | 11:bc9cd2869f95 | 782 | for(i = 0; i < devices.size(); i++) |
mehatfie | 11:bc9cd2869f95 | 783 | devices[i]->pause(); |
mehatfie | 11:bc9cd2869f95 | 784 | |
mehatfie | 11:bc9cd2869f95 | 785 | //LCD has already been adjusted with the ErrorMonitor function |
mehatfie | 11:bc9cd2869f95 | 786 | //Simply wait for the user to press select in order to acknowledge the issue and try to fix it |
mehatfie | 11:bc9cd2869f95 | 787 | while(!buttons.readSel()); |
mehatfie | 11:bc9cd2869f95 | 788 | |
mehatfie | 11:bc9cd2869f95 | 789 | //place all devices into the resume functionality |
mehatfie | 11:bc9cd2869f95 | 790 | for(i = 0; i < devices.size(); i++) |
mehatfie | 11:bc9cd2869f95 | 791 | devices[i]->resume(); |
mehatfie | 11:bc9cd2869f95 | 792 | |
mehatfie | 11:bc9cd2869f95 | 793 | lcd.cls(); //clear the display |
mehatfie | 11:bc9cd2869f95 | 794 | } |
mehatfie | 11:bc9cd2869f95 | 795 | } |
mehatfie | 11:bc9cd2869f95 | 796 | |
mehatfie | 2:3e7baa3e3fec | 797 | |
mehatfie | 11:bc9cd2869f95 | 798 | |
mehatfie | 11:bc9cd2869f95 | 799 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 800 | /*** <Functionality: device> ***/ |
mehatfie | 11:bc9cd2869f95 | 801 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 802 | |
mehatfie | 2:3e7baa3e3fec | 803 | if (lineData.word[0].compare("device") == 0){ |
mehatfie | 2:3e7baa3e3fec | 804 | |
mehatfie | 1:5731f31f96be | 805 | int i = 0, deviceFound = -1; |
mehatfie | 2:3e7baa3e3fec | 806 | for (i = 0; i < numDevices; i++){ |
mehatfie | 4:86d0d04cc055 | 807 | if (lineData.word[2].compare(DeviceNames[i]) == 0){ |
mehatfie | 1:5731f31f96be | 808 | deviceFound = i; |
mehatfie | 4:86d0d04cc055 | 809 | } |
mehatfie | 2:3e7baa3e3fec | 810 | } |
mehatfie | 9:5a0c4c6e39c7 | 811 | |
mehatfie | 1:5731f31f96be | 812 | //if the device type does not match any known type, error out |
mehatfie | 1:5731f31f96be | 813 | if (deviceFound == -1){ |
mehatfie | 9:5a0c4c6e39c7 | 814 | ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD |
mehatfie | 9:5a0c4c6e39c7 | 815 | return -1; |
mehatfie | 1:5731f31f96be | 816 | } |
mehatfie | 1:5731f31f96be | 817 | |
mehatfie | 1:5731f31f96be | 818 | //Add device to the array of devices and initialize it |
mehatfie | 2:3e7baa3e3fec | 819 | else{ |
mehatfie | 2:3e7baa3e3fec | 820 | devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData)); |
mehatfie | 9:5a0c4c6e39c7 | 821 | devices.back()->name = lineData.word[1]; |
mehatfie | 9:5a0c4c6e39c7 | 822 | |
mehatfie | 9:5a0c4c6e39c7 | 823 | //since the constructor cannot return a value, it will trip the error Flag if something is wrong, check that flag, and return error if it has been tripped |
mehatfie | 9:5a0c4c6e39c7 | 824 | if (devices.back()->errorFlag == 1){ |
mehatfie | 9:5a0c4c6e39c7 | 825 | ErrorOut("Error initializing device", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 826 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 827 | } |
mehatfie | 2:3e7baa3e3fec | 828 | } |
mehatfie | 1:5731f31f96be | 829 | } |
mehatfie | 1:5731f31f96be | 830 | |
mehatfie | 11:bc9cd2869f95 | 831 | |
mehatfie | 11:bc9cd2869f95 | 832 | |
mehatfie | 11:bc9cd2869f95 | 833 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 834 | /*** <Functionality: errorWatch> ***/ |
mehatfie | 11:bc9cd2869f95 | 835 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 836 | |
mehatfie | 11:bc9cd2869f95 | 837 | else if (lineData.word[0].compare("errorWatch") == 0){ |
mehatfie | 11:bc9cd2869f95 | 838 | //line looks like: error, _Condition_, FIX(?), _Command_ |
mehatfie | 11:bc9cd2869f95 | 839 | //create a temp LineData variable to store the correct information of the error Condition (discluding the first "error" keyword |
mehatfie | 11:bc9cd2869f95 | 840 | ErrorCondition tempError; |
mehatfie | 11:bc9cd2869f95 | 841 | LineData tempLine; |
mehatfie | 11:bc9cd2869f95 | 842 | |
mehatfie | 11:bc9cd2869f95 | 843 | int i = 1, funcNumWords = 0; |
mehatfie | 11:bc9cd2869f95 | 844 | for (i = 1; i < lineData.numWords; i++){ |
mehatfie | 11:bc9cd2869f95 | 845 | |
mehatfie | 11:bc9cd2869f95 | 846 | //if the keyword FIX is not this word, than add it to the LineData |
mehatfie | 11:bc9cd2869f95 | 847 | if (lineData.word[i].compare("FIX") != 0){ |
mehatfie | 11:bc9cd2869f95 | 848 | tempLine.word[funcNumWords] = lineData.word[i]; |
mehatfie | 11:bc9cd2869f95 | 849 | funcNumWords++; |
mehatfie | 11:bc9cd2869f95 | 850 | } |
mehatfie | 11:bc9cd2869f95 | 851 | |
mehatfie | 11:bc9cd2869f95 | 852 | //if the keyword FIX was found, this means there's a FIX / Reset command if the specified error being monitored occurs |
mehatfie | 11:bc9cd2869f95 | 853 | else if (lineData.word[i].compare("FIX") == 0){ |
mehatfie | 11:bc9cd2869f95 | 854 | tempError.hasFix = 1; |
mehatfie | 11:bc9cd2869f95 | 855 | |
mehatfie | 11:bc9cd2869f95 | 856 | //Save the line as the "Error to Watch" value |
mehatfie | 11:bc9cd2869f95 | 857 | tempLine.numWords = funcNumWords; |
mehatfie | 11:bc9cd2869f95 | 858 | tempLine.lineAddress = lineData.lineAddress; |
mehatfie | 11:bc9cd2869f95 | 859 | tempLine.lineNumber = lineData.lineNumber; |
mehatfie | 11:bc9cd2869f95 | 860 | tempError.errorToWatch = tempLine; |
mehatfie | 11:bc9cd2869f95 | 861 | |
mehatfie | 11:bc9cd2869f95 | 862 | //reset lineData and num words for the command / function in order to process the FIX command into the error condition |
mehatfie | 11:bc9cd2869f95 | 863 | funcNumWords = 0; |
mehatfie | 11:bc9cd2869f95 | 864 | tempLine = LineData(); //reset tempLine |
mehatfie | 11:bc9cd2869f95 | 865 | } |
mehatfie | 11:bc9cd2869f95 | 866 | } |
mehatfie | 11:bc9cd2869f95 | 867 | |
mehatfie | 11:bc9cd2869f95 | 868 | //if the error has a fix, it means that it already process the "Error to Watch" but needs to process the "Error Fix" into the error condition |
mehatfie | 11:bc9cd2869f95 | 869 | if (tempError.hasFix){ |
mehatfie | 11:bc9cd2869f95 | 870 | //Save the line as the "Error Fix" value |
mehatfie | 11:bc9cd2869f95 | 871 | tempLine.numWords = funcNumWords; |
mehatfie | 11:bc9cd2869f95 | 872 | tempLine.lineAddress = lineData.lineAddress; |
mehatfie | 11:bc9cd2869f95 | 873 | tempLine.lineNumber = lineData.lineNumber; |
mehatfie | 11:bc9cd2869f95 | 874 | tempError.errorFix = tempLine; |
mehatfie | 11:bc9cd2869f95 | 875 | } |
mehatfie | 11:bc9cd2869f95 | 876 | |
mehatfie | 11:bc9cd2869f95 | 877 | //if the error does not have a fix, that means it still has to process the "Error to Watch" into the error condition |
mehatfie | 11:bc9cd2869f95 | 878 | else{ |
mehatfie | 11:bc9cd2869f95 | 879 | //Save the line as the "Error to Watch" value |
mehatfie | 11:bc9cd2869f95 | 880 | tempLine.numWords = funcNumWords; |
mehatfie | 11:bc9cd2869f95 | 881 | tempLine.lineAddress = lineData.lineAddress; |
mehatfie | 11:bc9cd2869f95 | 882 | tempLine.lineNumber = lineData.lineNumber; |
mehatfie | 11:bc9cd2869f95 | 883 | tempError.errorToWatch = tempLine; |
mehatfie | 11:bc9cd2869f95 | 884 | } |
mehatfie | 11:bc9cd2869f95 | 885 | |
mehatfie | 11:bc9cd2869f95 | 886 | //if DummyMode, send error Condition to the interpretCommand so that we can check the syntax |
mehatfie | 11:bc9cd2869f95 | 887 | if (DummyMode){ |
mehatfie | 11:bc9cd2869f95 | 888 | int returnValue1 = interpretCommand(tempError.errorToWatch); |
mehatfie | 11:bc9cd2869f95 | 889 | int returnValue2 = interpretCommand(tempError.errorFix); |
mehatfie | 11:bc9cd2869f95 | 890 | if (returnValue1 == -1 || returnValue2 == -1) |
mehatfie | 11:bc9cd2869f95 | 891 | return -1; |
mehatfie | 11:bc9cd2869f95 | 892 | } |
mehatfie | 11:bc9cd2869f95 | 893 | |
mehatfie | 11:bc9cd2869f95 | 894 | errorMonitors[numErrorsConditions] = tempError; |
mehatfie | 11:bc9cd2869f95 | 895 | numErrorsConditions++; |
mehatfie | 11:bc9cd2869f95 | 896 | } |
mehatfie | 11:bc9cd2869f95 | 897 | |
mehatfie | 11:bc9cd2869f95 | 898 | |
mehatfie | 11:bc9cd2869f95 | 899 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 900 | /*** <Functionality: delay> ***/ |
mehatfie | 11:bc9cd2869f95 | 901 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 902 | |
mehatfie | 2:3e7baa3e3fec | 903 | else if (lineData.word[0].compare("delay") == 0){ |
mehatfie | 10:e8db892fbc52 | 904 | |
mehatfie | 11:bc9cd2869f95 | 905 | if (lineData.numWords != 2){ |
mehatfie | 11:bc9cd2869f95 | 906 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 907 | return -1; |
mehatfie | 11:bc9cd2869f95 | 908 | } |
mehatfie | 11:bc9cd2869f95 | 909 | |
mehatfie | 2:3e7baa3e3fec | 910 | string duration = lineData.word[1]; |
mehatfie | 2:3e7baa3e3fec | 911 | int durationValue = 0; |
mehatfie | 9:5a0c4c6e39c7 | 912 | int numValuesFound = sscanf(duration.c_str(), "%d", &durationValue); |
mehatfie | 1:5731f31f96be | 913 | |
mehatfie | 9:5a0c4c6e39c7 | 914 | if (numValuesFound < 1){ |
mehatfie | 9:5a0c4c6e39c7 | 915 | ErrorOut("Parameter Unknown, Duration Value can't be converted", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 916 | return -1; |
mehatfie | 9:5a0c4c6e39c7 | 917 | } |
mehatfie | 9:5a0c4c6e39c7 | 918 | |
mehatfie | 9:5a0c4c6e39c7 | 919 | if (durationValue > 0){ |
mehatfie | 11:bc9cd2869f95 | 920 | |
mehatfie | 11:bc9cd2869f95 | 921 | //All syntax checking done by this point, if Dummy then return success in order to check the code, no need wait for a delay |
mehatfie | 11:bc9cd2869f95 | 922 | if (DummyMode) |
mehatfie | 11:bc9cd2869f95 | 923 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 11:bc9cd2869f95 | 924 | |
mehatfie | 1:5731f31f96be | 925 | timer.reset(); |
mehatfie | 1:5731f31f96be | 926 | timer.start(); |
mehatfie | 1:5731f31f96be | 927 | while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration |
mehatfie | 1:5731f31f96be | 928 | timer.stop(); //Stop the Timer |
mehatfie | 1:5731f31f96be | 929 | } |
mehatfie | 1:5731f31f96be | 930 | else{ |
mehatfie | 11:bc9cd2869f95 | 931 | ErrorOut("Duration value is less than or equal to 0", lineData.lineNumber); |
mehatfie | 1:5731f31f96be | 932 | return -1; |
mehatfie | 1:5731f31f96be | 933 | } |
mehatfie | 1:5731f31f96be | 934 | } |
mehatfie | 1:5731f31f96be | 935 | |
mehatfie | 11:bc9cd2869f95 | 936 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 937 | /*** <Functionality: pause> ***/ |
mehatfie | 11:bc9cd2869f95 | 938 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 939 | |
mehatfie | 11:bc9cd2869f95 | 940 | else if (lineData.word[0].compare("pause") == 0){ |
mehatfie | 11:bc9cd2869f95 | 941 | |
mehatfie | 11:bc9cd2869f95 | 942 | if (lineData.numWords != 1){ |
mehatfie | 11:bc9cd2869f95 | 943 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 944 | return -1; |
mehatfie | 11:bc9cd2869f95 | 945 | } |
mehatfie | 11:bc9cd2869f95 | 946 | |
mehatfie | 11:bc9cd2869f95 | 947 | lcd.cls(); //clear the display |
mehatfie | 11:bc9cd2869f95 | 948 | lcd.setAddress(0,0); |
mehatfie | 11:bc9cd2869f95 | 949 | lcd.printf("System Pause"); |
mehatfie | 11:bc9cd2869f95 | 950 | lcd.setAddress(0,2); |
mehatfie | 11:bc9cd2869f95 | 951 | lcd.printf("Line Number: %d", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 952 | lcd.setAddress(0,3); |
mehatfie | 11:bc9cd2869f95 | 953 | lcd.printf("Press Sel to Resume"); |
mehatfie | 11:bc9cd2869f95 | 954 | while(!buttons.readSel()); |
mehatfie | 11:bc9cd2869f95 | 955 | } |
mehatfie | 11:bc9cd2869f95 | 956 | |
mehatfie | 11:bc9cd2869f95 | 957 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 958 | /*** <Functionality: GoTo> ***/ |
mehatfie | 11:bc9cd2869f95 | 959 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 960 | |
mehatfie | 11:bc9cd2869f95 | 961 | else if (lineData.word[0].compare("GoTo") == 0){ |
mehatfie | 11:bc9cd2869f95 | 962 | |
mehatfie | 11:bc9cd2869f95 | 963 | if (lineData.word[1].compare("label") == 0){ |
mehatfie | 11:bc9cd2869f95 | 964 | |
mehatfie | 11:bc9cd2869f95 | 965 | if (lineData.numWords != 3){ |
mehatfie | 11:bc9cd2869f95 | 966 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 967 | return -1; |
mehatfie | 11:bc9cd2869f95 | 968 | } |
mehatfie | 11:bc9cd2869f95 | 969 | |
mehatfie | 11:bc9cd2869f95 | 970 | int labelExists = 0; |
mehatfie | 11:bc9cd2869f95 | 971 | for(vector<GoToLabel>::iterator it=GoToLabels.begin(); it < GoToLabels.end(); it++){ |
mehatfie | 11:bc9cd2869f95 | 972 | if (lineData.word[1].compare((*it).name) == 0) |
mehatfie | 11:bc9cd2869f95 | 973 | labelExists = 1; |
mehatfie | 11:bc9cd2869f95 | 974 | } |
mehatfie | 11:bc9cd2869f95 | 975 | |
mehatfie | 11:bc9cd2869f95 | 976 | //if the label does not exist then add it to the vector |
mehatfie | 11:bc9cd2869f95 | 977 | //if it turned out to exist then do nothing |
mehatfie | 11:bc9cd2869f95 | 978 | if (!labelExists){ |
mehatfie | 11:bc9cd2869f95 | 979 | GoToLabel tempLabel; |
mehatfie | 11:bc9cd2869f95 | 980 | tempLabel.name = lineData.word[2]; //Save the Label Name |
mehatfie | 11:bc9cd2869f95 | 981 | tempLabel.lineAddress = lineData.lineAddress; //Save the Line Address |
mehatfie | 11:bc9cd2869f95 | 982 | tempLabel.lineNumber = lineData.lineNumber; //Save the Line Number |
mehatfie | 11:bc9cd2869f95 | 983 | GoToLabels.push_back(tempLabel); |
mehatfie | 11:bc9cd2869f95 | 984 | } |
mehatfie | 11:bc9cd2869f95 | 985 | } |
mehatfie | 11:bc9cd2869f95 | 986 | |
mehatfie | 11:bc9cd2869f95 | 987 | //if the second word was not "label," then it means the word should correspond to an already created GoTo Label |
mehatfie | 11:bc9cd2869f95 | 988 | else{ |
mehatfie | 11:bc9cd2869f95 | 989 | |
mehatfie | 11:bc9cd2869f95 | 990 | if (lineData.numWords != 2){ |
mehatfie | 11:bc9cd2869f95 | 991 | ErrorOut("Incorrect number of parameters", lineData.lineNumber); |
mehatfie | 11:bc9cd2869f95 | 992 | return -1; |
mehatfie | 11:bc9cd2869f95 | 993 | } |
mehatfie | 11:bc9cd2869f95 | 994 | |
mehatfie | 11:bc9cd2869f95 | 995 | //All syntax checking done by this point, if Dummy then return success in order to check the code |
mehatfie | 11:bc9cd2869f95 | 996 | //Unable to continue further checking since an aspect of DummyMode run through is to collect the GoTo Label Locations |
mehatfie | 11:bc9cd2869f95 | 997 | //so that we have them in address |
mehatfie | 11:bc9cd2869f95 | 998 | if (DummyMode) |
mehatfie | 11:bc9cd2869f95 | 999 | return 0; //Function operated successfully but doesn't return a value |
mehatfie | 11:bc9cd2869f95 | 1000 | |
mehatfie | 11:bc9cd2869f95 | 1001 | int i = 0, labelFound = -1; |
mehatfie | 11:bc9cd2869f95 | 1002 | for(i = 0; i < GoToLabels.size(); i++){ |
mehatfie | 11:bc9cd2869f95 | 1003 | if (lineData.word[1].compare(GoToLabels[i].name) == 0){ |
mehatfie | 11:bc9cd2869f95 | 1004 | labelFound = i; |
mehatfie | 11:bc9cd2869f95 | 1005 | } |
mehatfie | 11:bc9cd2869f95 | 1006 | } |
mehatfie | 11:bc9cd2869f95 | 1007 | |
mehatfie | 11:bc9cd2869f95 | 1008 | //if the label was not found then error out |
mehatfie | 11:bc9cd2869f95 | 1009 | if (labelFound == -1){ |
mehatfie | 11:bc9cd2869f95 | 1010 | ErrorOut("No label match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD |
mehatfie | 11:bc9cd2869f95 | 1011 | return -1; |
mehatfie | 11:bc9cd2869f95 | 1012 | } |
mehatfie | 11:bc9cd2869f95 | 1013 | |
mehatfie | 11:bc9cd2869f95 | 1014 | //if the label was found, then seek to that GoTo position |
mehatfie | 11:bc9cd2869f95 | 1015 | else{ |
mehatfie | 11:bc9cd2869f95 | 1016 | |
mehatfie | 11:bc9cd2869f95 | 1017 | int seekFailure = fseek(selectedFile, GoToLabels[labelFound].lineAddress, SEEK_SET); //fseek returns 0 on success |
mehatfie | 11:bc9cd2869f95 | 1018 | if (seekFailure){ |
mehatfie | 11:bc9cd2869f95 | 1019 | ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD |
mehatfie | 11:bc9cd2869f95 | 1020 | return -1; |
mehatfie | 11:bc9cd2869f95 | 1021 | } |
mehatfie | 11:bc9cd2869f95 | 1022 | |
mehatfie | 11:bc9cd2869f95 | 1023 | lineData.lineNumber = GoToLabels[labelFound].lineNumber - 1; //set the lineNumber value |
mehatfie | 11:bc9cd2869f95 | 1024 | } |
mehatfie | 11:bc9cd2869f95 | 1025 | } |
mehatfie | 11:bc9cd2869f95 | 1026 | } |
mehatfie | 11:bc9cd2869f95 | 1027 | |
mehatfie | 11:bc9cd2869f95 | 1028 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1029 | /*** <Functionality: loop> ***/ |
mehatfie | 11:bc9cd2869f95 | 1030 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1031 | |
mehatfie | 9:5a0c4c6e39c7 | 1032 | else if (lineData.word[0].compare("loop") == 0) |
mehatfie | 11:bc9cd2869f95 | 1033 | return loopCommand(lineData); //Process the loop command and return the value that it returns |
mehatfie | 11:bc9cd2869f95 | 1034 | |
mehatfie | 5:e36e0538a903 | 1035 | |
mehatfie | 11:bc9cd2869f95 | 1036 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1037 | /*** <Functionality: condition> ***/ |
mehatfie | 11:bc9cd2869f95 | 1038 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1039 | else if (lineData.word[0].compare("condition") == 0) |
mehatfie | 9:5a0c4c6e39c7 | 1040 | return conditionCommand(selectedFile, lineData); //Process the condition command and return the value that it returns |
mehatfie | 9:5a0c4c6e39c7 | 1041 | |
mehatfie | 11:bc9cd2869f95 | 1042 | |
mehatfie | 11:bc9cd2869f95 | 1043 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1044 | /*** <Functionality: cycle> ***/ |
mehatfie | 11:bc9cd2869f95 | 1045 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1046 | else if (lineData.word[0].compare("cycle") == 0) |
mehatfie | 11:bc9cd2869f95 | 1047 | return cycleCommand(selectedFile, lineData, 1); //Sending 1 means it's initializing |
mehatfie | 11:bc9cd2869f95 | 1048 | |
mehatfie | 11:bc9cd2869f95 | 1049 | |
mehatfie | 11:bc9cd2869f95 | 1050 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1051 | /*** <Functionality: end> ***/ |
mehatfie | 11:bc9cd2869f95 | 1052 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1053 | //end has custom return value for specific functions, since "end" is a common keyword amongst functions |
mehatfie | 9:5a0c4c6e39c7 | 1054 | else if (lineData.word[0].compare("end") == 0){ |
mehatfie | 9:5a0c4c6e39c7 | 1055 | if (lineData.word[1].compare("program") == 0) |
mehatfie | 5:e36e0538a903 | 1056 | return 2; |
mehatfie | 9:5a0c4c6e39c7 | 1057 | else if (lineData.word[1].compare("loop") == 0) |
mehatfie | 5:e36e0538a903 | 1058 | return 3; |
mehatfie | 9:5a0c4c6e39c7 | 1059 | else if (lineData.word[1].compare("condition") == 0) |
mehatfie | 5:e36e0538a903 | 1060 | return 4; |
mehatfie | 11:bc9cd2869f95 | 1061 | else if (lineData.word[1].compare("cycle") == 0){ |
mehatfie | 11:bc9cd2869f95 | 1062 | checkCycle(selectedFile, lineData, 0) //Sending 0 means it's checking for the ending |
mehatfie | 11:bc9cd2869f95 | 1063 | return 5; |
mehatfie | 11:bc9cd2869f95 | 1064 | } |
mehatfie | 9:5a0c4c6e39c7 | 1065 | |
mehatfie | 9:5a0c4c6e39c7 | 1066 | else{ |
mehatfie | 9:5a0c4c6e39c7 | 1067 | ErrorOut("Unknown function ending", lineData.lineNumber); |
mehatfie | 9:5a0c4c6e39c7 | 1068 | return -1; |
mehatfie | 2:3e7baa3e3fec | 1069 | } |
mehatfie | 1:5731f31f96be | 1070 | } |
mehatfie | 1:5731f31f96be | 1071 | |
mehatfie | 11:bc9cd2869f95 | 1072 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1073 | /*** <Functionality: local_name Checking> ***/ |
mehatfie | 11:bc9cd2869f95 | 1074 | /******************************************************************************/ |
mehatfie | 11:bc9cd2869f95 | 1075 | |
mehatfie | 1:5731f31f96be | 1076 | //not a keyword so check if it's a localName for a device |
mehatfie | 1:5731f31f96be | 1077 | else{ |
mehatfie | 11:bc9cd2869f95 | 1078 | |
mehatfie | 1:5731f31f96be | 1079 | |
mehatfie | 11:bc9cd2869f95 | 1080 | // lcd.setAddress(0,1); |
mehatfie | 11:bc9cd2869f95 | 1081 | // lcd.printf("wrd1: %s", lineData.word[0]); |
mehatfie | 11:bc9cd2869f95 | 1082 | // wait(1); |
mehatfie | 11:bc9cd2869f95 | 1083 | |
mehatfie | 1:5731f31f96be | 1084 | int i = 0, deviceFound = -1; |
mehatfie | 2:3e7baa3e3fec | 1085 | for (i = 0; i < devices.size(); i++){ |
mehatfie | 2:3e7baa3e3fec | 1086 | if (lineData.word[0].compare(devices[i]->name) == 0) |
mehatfie | 1:5731f31f96be | 1087 | deviceFound = i; |
mehatfie | 1:5731f31f96be | 1088 | } |
mehatfie | 1:5731f31f96be | 1089 | |
mehatfie | 1:5731f31f96be | 1090 | //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 | 1091 | if (deviceFound == -1){ |
mehatfie | 9:5a0c4c6e39c7 | 1092 | ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD |
mehatfie | 9:5a0c4c6e39c7 | 1093 | return -1; |
mehatfie | 1:5731f31f96be | 1094 | } |
mehatfie | 1:5731f31f96be | 1095 | |
mehatfie | 11:bc9cd2869f95 | 1096 | |
mehatfie | 11:bc9cd2869f95 | 1097 | |
mehatfie | 1:5731f31f96be | 1098 | //Local Name matches a device, send line to that device in order to process the functionality |
mehatfie | 9:5a0c4c6e39c7 | 1099 | else |
mehatfie | 9:5a0c4c6e39c7 | 1100 | return devices[deviceFound]->interpret(lineData); //call the device specific interpret command, and return the value it returns |
mehatfie | 1:5731f31f96be | 1101 | } |
mehatfie | 1:5731f31f96be | 1102 | |
mehatfie | 9:5a0c4c6e39c7 | 1103 | return 0; //Return Success, no value is being sent so don't return 1 |
mehatfie | 1:5731f31f96be | 1104 | } |
mehatfie | 1:5731f31f96be | 1105 | |
mehatfie | 1:5731f31f96be | 1106 | |
mehatfie | 11:bc9cd2869f95 | 1107 | |
mehatfie | 11:bc9cd2869f95 | 1108 | |
mehatfie | 1:5731f31f96be | 1109 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 1110 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 1111 | /****************************** <FUNCTION: main> ********************************/ |
mehatfie | 1:5731f31f96be | 1112 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 1113 | /**********************************************************************************************************************************/ |
mehatfie | 1:5731f31f96be | 1114 | |
mehatfie | 0:22618cf06f45 | 1115 | int main() { |
mehatfie | 0:22618cf06f45 | 1116 | |
mehatfie | 0:22618cf06f45 | 1117 | fullInit(); //Initialize anything that's required to run the code (LCD) |
mehatfie | 0:22618cf06f45 | 1118 | |
mehatfie | 6:d1594fd2ec5a | 1119 | LineData lineData; |
mehatfie | 2:3e7baa3e3fec | 1120 | resetLineData(lineData); |
mehatfie | 2:3e7baa3e3fec | 1121 | |
mehatfie | 9:5a0c4c6e39c7 | 1122 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1123 | /*** <Get all the Potential Programs> ***/ |
mehatfie | 9:5a0c4c6e39c7 | 1124 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1125 | int numTextFiles = 0; |
mehatfie | 9:5a0c4c6e39c7 | 1126 | vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card |
mehatfie | 9:5a0c4c6e39c7 | 1127 | vector<string> filenames = readFileNames("/sd"); |
mehatfie | 9:5a0c4c6e39c7 | 1128 | |
mehatfie | 9:5a0c4c6e39c7 | 1129 | //Error check whether the SD Card exists and was able to be accessed.... or if there's no files on the SD Card |
mehatfie | 9:5a0c4c6e39c7 | 1130 | if (filenames.size() == 0){ |
mehatfie | 9:5a0c4c6e39c7 | 1131 | ErrorOut("No Files Found, or Directory can't be accessed", 0); //Spaces make it look nice on LCD |
mehatfie | 9:5a0c4c6e39c7 | 1132 | return -1; //End program by returning in the main() |
mehatfie | 9:5a0c4c6e39c7 | 1133 | } |
mehatfie | 9:5a0c4c6e39c7 | 1134 | |
mehatfie | 9:5a0c4c6e39c7 | 1135 | numTextFiles = getFileNamesWithoutExt(textFiles, filenames); |
mehatfie | 0:22618cf06f45 | 1136 | |
mehatfie | 9:5a0c4c6e39c7 | 1137 | //Error check whether the SD Card has any txt files in it's first directory |
mehatfie | 9:5a0c4c6e39c7 | 1138 | if (numTextFiles == 0){ |
mehatfie | 9:5a0c4c6e39c7 | 1139 | ErrorOut("No Program (.txt) Files Found in first Directory", 0); //Spaces make it look nice on LCD |
mehatfie | 9:5a0c4c6e39c7 | 1140 | return -1; //End program by returning in the main() |
mehatfie | 9:5a0c4c6e39c7 | 1141 | } |
mehatfie | 0:22618cf06f45 | 1142 | |
mehatfie | 9:5a0c4c6e39c7 | 1143 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1144 | /*** <Select the Program txt File> ***/ |
mehatfie | 9:5a0c4c6e39c7 | 1145 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1146 | int fileSelected = 0, selectedFileIndex = 0; |
mehatfie | 9:5a0c4c6e39c7 | 1147 | |
mehatfie | 9:5a0c4c6e39c7 | 1148 | lcd.cls(); //clear the display |
mehatfie | 9:5a0c4c6e39c7 | 1149 | lcd.setAddress(0,1); |
mehatfie | 9:5a0c4c6e39c7 | 1150 | lcd.printf("Select Your Program"); |
mehatfie | 9:5a0c4c6e39c7 | 1151 | lcd.setAddress(0,2); |
mehatfie | 9:5a0c4c6e39c7 | 1152 | lcd.printf("Num Programs = %d", numTextFiles); |
mehatfie | 2:3e7baa3e3fec | 1153 | |
mehatfie | 9:5a0c4c6e39c7 | 1154 | uint8_t lastButState = 0; |
mehatfie | 9:5a0c4c6e39c7 | 1155 | lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on |
mehatfie | 9:5a0c4c6e39c7 | 1156 | |
mehatfie | 9:5a0c4c6e39c7 | 1157 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen |
mehatfie | 9:5a0c4c6e39c7 | 1158 | while(!fileSelected) { |
mehatfie | 0:22618cf06f45 | 1159 | |
mehatfie | 9:5a0c4c6e39c7 | 1160 | uint8_t curButState = buttons.readBus(); |
mehatfie | 9:5a0c4c6e39c7 | 1161 | if(curButState != lastButState){ |
mehatfie | 9:5a0c4c6e39c7 | 1162 | lastButState = curButState; |
mehatfie | 9:5a0c4c6e39c7 | 1163 | if(buttons.readRight()) |
mehatfie | 9:5a0c4c6e39c7 | 1164 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 1); |
mehatfie | 9:5a0c4c6e39c7 | 1165 | else if(buttons.readLeft()) |
mehatfie | 9:5a0c4c6e39c7 | 1166 | selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 0); |
mehatfie | 9:5a0c4c6e39c7 | 1167 | else if(buttons.readSel()) |
mehatfie | 9:5a0c4c6e39c7 | 1168 | fileSelected = 1; |
mehatfie | 0:22618cf06f45 | 1169 | } |
mehatfie | 9:5a0c4c6e39c7 | 1170 | } |
mehatfie | 9:5a0c4c6e39c7 | 1171 | |
mehatfie | 9:5a0c4c6e39c7 | 1172 | lcd.setCursor(TextLCD::CurOn_BlkOff); //turn blinking cursor off |
mehatfie | 9:5a0c4c6e39c7 | 1173 | |
mehatfie | 9:5a0c4c6e39c7 | 1174 | char selectedFileName[50]; |
mehatfie | 9:5a0c4c6e39c7 | 1175 | strcpy(selectedFileName, textFiles[selectedFileIndex].c_str()); |
mehatfie | 9:5a0c4c6e39c7 | 1176 | |
mehatfie | 9:5a0c4c6e39c7 | 1177 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1178 | /*** <Open the Program txt File> ***/ |
mehatfie | 9:5a0c4c6e39c7 | 1179 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1180 | |
mehatfie | 9:5a0c4c6e39c7 | 1181 | //Create the string of the full directory and path to the program txt file |
mehatfie | 9:5a0c4c6e39c7 | 1182 | char selectedFileFullName[100] = "/sd/"; //Assuming that no directory and file name will be longer than 100 characters |
mehatfie | 9:5a0c4c6e39c7 | 1183 | strcat(selectedFileFullName, selectedFileName); |
mehatfie | 9:5a0c4c6e39c7 | 1184 | strcat(selectedFileFullName, ".txt"); |
mehatfie | 9:5a0c4c6e39c7 | 1185 | |
mehatfie | 11:bc9cd2869f95 | 1186 | selectedFile = fopen(selectedFileFullName, "r"); |
mehatfie | 9:5a0c4c6e39c7 | 1187 | |
mehatfie | 9:5a0c4c6e39c7 | 1188 | //Error out of attempt to open the selected file was unsuccessful |
mehatfie | 9:5a0c4c6e39c7 | 1189 | if(selectedFile == NULL) { |
mehatfie | 9:5a0c4c6e39c7 | 1190 | ErrorOut("Unable to Open Selected File", 0); //Spaces make it look nice on LCD |
mehatfie | 9:5a0c4c6e39c7 | 1191 | return -1; //End program by returning in the main() |
mehatfie | 9:5a0c4c6e39c7 | 1192 | } |
mehatfie | 0:22618cf06f45 | 1193 | |
mehatfie | 0:22618cf06f45 | 1194 | |
mehatfie | 9:5a0c4c6e39c7 | 1195 | /******************************************************************************/ |
mehatfie | 9:5a0c4c6e39c7 | 1196 | /*** <Start Running through the Program txt File Lines> ***/ |
mehatfie | 9:5a0c4c6e39c7 | 1197 | /******************************************************************************/ |
mehatfie | 10:e8db892fbc52 | 1198 | |
mehatfie | 10:e8db892fbc52 | 1199 | resetLineData(lineData); //Reset the values in the struct that holds the File / Line Data |
mehatfie | 10:e8db892fbc52 | 1200 | lcd.cls(); //clear the display |
mehatfie | 0:22618cf06f45 | 1201 | |
mehatfie | 10:e8db892fbc52 | 1202 | int endOfFile = 0, error = 0, returnValue, checkEnd; |
mehatfie | 10:e8db892fbc52 | 1203 | DummyMode = 1; //set Dummy Mode equal to 1 to simulate the first run through of the code |
mehatfie | 10:e8db892fbc52 | 1204 | while (!endOfFile){ |
mehatfie | 0:22618cf06f45 | 1205 | |
mehatfie | 10:e8db892fbc52 | 1206 | //Re-initialize variables |
mehatfie | 10:e8db892fbc52 | 1207 | returnValue = 0; |
mehatfie | 10:e8db892fbc52 | 1208 | checkEnd = 0; |
mehatfie | 10:e8db892fbc52 | 1209 | |
mehatfie | 10:e8db892fbc52 | 1210 | returnValue = getNextLine(selectedFile, lineData); //get the next line of data |
mehatfie | 10:e8db892fbc52 | 1211 | |
mehatfie | 10:e8db892fbc52 | 1212 | //if getNextLine returned an error, then return error in main |
mehatfie | 10:e8db892fbc52 | 1213 | if (returnValue == -1) |
mehatfie | 10:e8db892fbc52 | 1214 | error = 1; |
mehatfie | 9:5a0c4c6e39c7 | 1215 | |
mehatfie | 11:bc9cd2869f95 | 1216 | checkEnd = interpretCommand(lineData); //interpret the line data |
mehatfie | 9:5a0c4c6e39c7 | 1217 | |
mehatfie | 11:bc9cd2869f95 | 1218 | if (checkEnd == 2){ |
mehatfie | 11:bc9cd2869f95 | 1219 | //Dummy Mode will be turned on for the first run through, set it to 0 after the first run through, |
mehatfie | 11:bc9cd2869f95 | 1220 | //as the syntax will be checked without erroring, and therefore it is possible to try and run the .txt file |
mehatfie | 11:bc9cd2869f95 | 1221 | //Seek back to beginning of file |
mehatfie | 11:bc9cd2869f95 | 1222 | if (DummyMode){ |
mehatfie | 11:bc9cd2869f95 | 1223 | DummyMode = 0; |
mehatfie | 11:bc9cd2869f95 | 1224 | rewind(selectedFile); //seek to the beginning of the file |
mehatfie | 11:bc9cd2869f95 | 1225 | resetLineData(lineData); //Reset the values in the struct that holds the File / Line Data |
mehatfie | 11:bc9cd2869f95 | 1226 | devices.erase(devices.begin(), devices.end()); //remove the devices vector from memory so that we don't duplicate items on the real run through |
mehatfie | 11:bc9cd2869f95 | 1227 | int k = 0; |
mehatfie | 11:bc9cd2869f95 | 1228 | for (k = 0; k < numErrorsConditions; k++) |
mehatfie | 11:bc9cd2869f95 | 1229 | errorMonitors[k] = ErrorCondition(); |
mehatfie | 11:bc9cd2869f95 | 1230 | numErrorsConditions = 0; |
mehatfie | 11:bc9cd2869f95 | 1231 | } |
mehatfie | 11:bc9cd2869f95 | 1232 | else |
mehatfie | 11:bc9cd2869f95 | 1233 | endOfFile = 1; |
mehatfie | 11:bc9cd2869f95 | 1234 | } |
mehatfie | 10:e8db892fbc52 | 1235 | else if (checkEnd == -1) //if interpretCommand returned an error, then return error in main |
mehatfie | 10:e8db892fbc52 | 1236 | error = 1; |
mehatfie | 10:e8db892fbc52 | 1237 | |
mehatfie | 10:e8db892fbc52 | 1238 | //Before erroring out, turn all devices off so that they power down |
mehatfie | 10:e8db892fbc52 | 1239 | if (error){ |
mehatfie | 10:e8db892fbc52 | 1240 | if (!DummyMode){ //if it is Dummy Mode, then no functionality has actually been turned on, so no need to shut anything off |
mehatfie | 9:5a0c4c6e39c7 | 1241 | for(vector<Device*>::iterator it=devices.begin(); it < devices.end(); it++) |
mehatfie | 9:5a0c4c6e39c7 | 1242 | (*it)->off(); |
mehatfie | 9:5a0c4c6e39c7 | 1243 | } |
mehatfie | 10:e8db892fbc52 | 1244 | |
mehatfie | 10:e8db892fbc52 | 1245 | return -1; |
mehatfie | 11:bc9cd2869f95 | 1246 | } |
mehatfie | 11:bc9cd2869f95 | 1247 | } |
mehatfie | 10:e8db892fbc52 | 1248 | |
mehatfie | 11:bc9cd2869f95 | 1249 | //Exit the program and remove needed items from memory |
mehatfie | 11:bc9cd2869f95 | 1250 | GoToLabels.erase(GoToLabels.begin(), GoToLabels.end()); //remove the GoToLabels vector from memory |
mehatfie | 11:bc9cd2869f95 | 1251 | devices.erase(devices.begin(), devices.end()); //remove the devices vector from memory |
mehatfie | 11:bc9cd2869f95 | 1252 | |
mehatfie | 10:e8db892fbc52 | 1253 | lcd.cls(); //clear the display |
mehatfie | 10:e8db892fbc52 | 1254 | lcd.setAddress(0,0); |
mehatfie | 10:e8db892fbc52 | 1255 | lcd.printf("END OF PROGRAM"); |
mehatfie | 10:e8db892fbc52 | 1256 | lcd.setAddress(0,2); |
mehatfie | 10:e8db892fbc52 | 1257 | lcd.printf("To Restart..."); |
mehatfie | 10:e8db892fbc52 | 1258 | lcd.setAddress(0,3); |
mehatfie | 10:e8db892fbc52 | 1259 | lcd.printf("Press BACK"); |
mehatfie | 10:e8db892fbc52 | 1260 | |
mehatfie | 10:e8db892fbc52 | 1261 | while(!buttons.readBack()); |
mehatfie | 10:e8db892fbc52 | 1262 | |
mehatfie | 10:e8db892fbc52 | 1263 | lcd.setAddress(0,1); |
mehatfie | 10:e8db892fbc52 | 1264 | //rewind(selectedFile); |
mehatfie | 0:22618cf06f45 | 1265 | |
mehatfie | 2:3e7baa3e3fec | 1266 | } |
mehatfie | 2:3e7baa3e3fec | 1267 | |
mehatfie | 9:5a0c4c6e39c7 | 1268 |