Component Test's Software to work with "Universal Controller Box" - Software is an interpreter or "compiler" for programs to be done with a .txt file and read off of the SD Card

Dependencies:   BridgeDriver FrontPanelButtons MCP23017 SDFileSystem TextLCD mbed

Committer:
mehatfie
Date:
Wed Oct 01 19:31:02 2014 +0000
Revision:
12:2e3e86714243
Parent:
11:bc9cd2869f95
Child:
13:899db9d635e5
- Working Copy; - Cycle Functionality working, able to update desired number of cycles on program; -Error Watch working, except doesn't seem to work for the first cycle... at the moment this is fine, considering technician will be there for first cycle

Who changed what in which revision?

UserRevisionLine numberNew 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 12:2e3e86714243 333
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 12:2e3e86714243 340 Timer cycleTimer;
mehatfie 12:2e3e86714243 341 CycleWatch cycleWatch;
mehatfie 12:2e3e86714243 342 int cycleCommand(LineData &lineData, int cycleState){
mehatfie 12:2e3e86714243 343
mehatfie 12:2e3e86714243 344 // if cycleState is 1, then initialize the cycle
mehatfie 12:2e3e86714243 345 if (cycleState == 1){
mehatfie 12:2e3e86714243 346
mehatfie 12:2e3e86714243 347 //Get the Condition value for number of times to loop
mehatfie 12:2e3e86714243 348 string numCycles = lineData.word[1];
mehatfie 12:2e3e86714243 349 int numValuesFound = sscanf(numCycles.c_str(), "%d", &cycleWatch.numCycles);
mehatfie 12:2e3e86714243 350 if (numValuesFound < 1){
mehatfie 12:2e3e86714243 351 ErrorOut("Parameter Unknown, loopCondition Value can't be converted", lineData.lineNumber);
mehatfie 12:2e3e86714243 352 return -1;
mehatfie 12:2e3e86714243 353 }
mehatfie 12:2e3e86714243 354
mehatfie 12:2e3e86714243 355
mehatfie 12:2e3e86714243 356 //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to loop again
mehatfie 12:2e3e86714243 357 if (DummyMode)
mehatfie 12:2e3e86714243 358 return 0; //Function operated successfully but doesn't return a value
mehatfie 12:2e3e86714243 359
mehatfie 12:2e3e86714243 360
mehatfie 12:2e3e86714243 361 //****************//
mehatfie 12:2e3e86714243 362 //Get the person to dynamically select what number of cycles they'd like to do, starting with the read value as the base value
mehatfie 12:2e3e86714243 363 //****************//
mehatfie 12:2e3e86714243 364 lcd.cls(); //clear the display
mehatfie 12:2e3e86714243 365 lcd.setAddress ( 0, 2 );
mehatfie 12:2e3e86714243 366 lcd.printf( "<<ACTION REQUIRED>>" );
mehatfie 12:2e3e86714243 367 lcd.setAddress ( 0, 3 );
mehatfie 12:2e3e86714243 368 lcd.printf( "<Press Sel to start>" );
mehatfie 12:2e3e86714243 369 while (!buttons.readSel()){
mehatfie 12:2e3e86714243 370 if(buttons.readUp() && cycleWatch.numCycles < 999999 ){
mehatfie 12:2e3e86714243 371 cycleWatch.numCycles++;
mehatfie 12:2e3e86714243 372 wait(0.05); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second
mehatfie 12:2e3e86714243 373 }
mehatfie 12:2e3e86714243 374 else if (buttons.readDown() && cycleWatch.numCycles > 0 ){
mehatfie 12:2e3e86714243 375 cycleWatch.numCycles--;
mehatfie 12:2e3e86714243 376 wait(0.05); //so that the speed of changing the numbers is more controllable, should mean you can move 20 digits per second
mehatfie 12:2e3e86714243 377 }
mehatfie 12:2e3e86714243 378 lcd.setAddress ( 0, 0 );
mehatfie 12:2e3e86714243 379 lcd.printf( "<Num Cycles: %d >" , cycleWatch.numCycles );
mehatfie 12:2e3e86714243 380 }
mehatfie 12:2e3e86714243 381
mehatfie 12:2e3e86714243 382 //Initialize the counter variable of the struct, and start the cycle timer
mehatfie 12:2e3e86714243 383 cycleWatch.counter = 0;
mehatfie 12:2e3e86714243 384 cycleTimer.start();
mehatfie 12:2e3e86714243 385
mehatfie 12:2e3e86714243 386 //Update the LCD to display the desired data
mehatfie 12:2e3e86714243 387 lcd.cls(); //clear the display
mehatfie 12:2e3e86714243 388 //Output the Avg Cycle Time
mehatfie 12:2e3e86714243 389 cycleTimer.stop();
mehatfie 12:2e3e86714243 390 cycleWatch.totalCycleTime += cycleTimer.read();
mehatfie 12:2e3e86714243 391 lcd.setAddress(0,1);
mehatfie 12:2e3e86714243 392 lcd.printf("Avg t(sec): 0.000");
mehatfie 12:2e3e86714243 393
mehatfie 12:2e3e86714243 394 //Output Cycle Number
mehatfie 12:2e3e86714243 395 cycleWatch.counter++;
mehatfie 12:2e3e86714243 396 lcd.setAddress(0,0);
mehatfie 12:2e3e86714243 397 lcd.printf("Cycle %d of %d", cycleWatch.counter, cycleWatch.numCycles);
mehatfie 12:2e3e86714243 398
mehatfie 12:2e3e86714243 399
mehatfie 12:2e3e86714243 400 //get the next line in order to get the line address to return to
mehatfie 12:2e3e86714243 401 int returnValue = getNextLine(selectedFile, lineData);
mehatfie 12:2e3e86714243 402 //if getNextLine returned an error, then error out
mehatfie 12:2e3e86714243 403 if (returnValue == -1)
mehatfie 12:2e3e86714243 404 return -1;
mehatfie 12:2e3e86714243 405
mehatfie 12:2e3e86714243 406 //save the staring location of this cycle loop
mehatfie 12:2e3e86714243 407 cycleWatch.startAddress = lineData.lineAddress;
mehatfie 12:2e3e86714243 408 cycleWatch.startLineNumber = lineData.lineNumber;
mehatfie 12:2e3e86714243 409
mehatfie 12:2e3e86714243 410 //seek back a line, so once this function returns, the next line reteived is that of the next line that should be processed.... basically... so we don't skip a line
mehatfie 12:2e3e86714243 411 lineData.lineNumber = cycleWatch.startLineNumber - 1;
mehatfie 12:2e3e86714243 412 int seekFailure = fseek(selectedFile, cycleWatch.startAddress, SEEK_SET); //fseek returns 0 on success
mehatfie 12:2e3e86714243 413 if (seekFailure){
mehatfie 12:2e3e86714243 414 ErrorOut("Init Cycle Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD
mehatfie 12:2e3e86714243 415 return -1;
mehatfie 12:2e3e86714243 416 }
mehatfie 12:2e3e86714243 417 }
mehatfie 11:bc9cd2869f95 418
mehatfie 12:2e3e86714243 419
mehatfie 12:2e3e86714243 420
mehatfie 12:2e3e86714243 421
mehatfie 12:2e3e86714243 422 // if cycleState is 1, then check for ending conditions
mehatfie 12:2e3e86714243 423 else if (cycleState == 0){
mehatfie 12:2e3e86714243 424
mehatfie 12:2e3e86714243 425 //All syntax checking done by this point, if Dummy then return success in order to check the code, no need to loop again
mehatfie 12:2e3e86714243 426 if (DummyMode)
mehatfie 12:2e3e86714243 427 return 0; //Function operated successfully but doesn't return a value
mehatfie 12:2e3e86714243 428
mehatfie 12:2e3e86714243 429 //Output the Avg Cycle Time
mehatfie 12:2e3e86714243 430 cycleTimer.stop();
mehatfie 12:2e3e86714243 431 cycleWatch.totalCycleTime += cycleTimer.read();
mehatfie 12:2e3e86714243 432 lcd.setAddress(0,1);
mehatfie 12:2e3e86714243 433 lcd.printf("Avg t(sec): %1.3f", (cycleWatch.totalCycleTime / cycleWatch.counter));
mehatfie 12:2e3e86714243 434
mehatfie 12:2e3e86714243 435 //Output Cycle Number
mehatfie 12:2e3e86714243 436 cycleWatch.counter++;
mehatfie 12:2e3e86714243 437 lcd.setAddress(0,0);
mehatfie 12:2e3e86714243 438 lcd.printf("Cycle %d of %d", cycleWatch.counter, cycleWatch.numCycles);
mehatfie 12:2e3e86714243 439
mehatfie 12:2e3e86714243 440
mehatfie 12:2e3e86714243 441 if (cycleWatch.counter <= cycleWatch.numCycles){
mehatfie 12:2e3e86714243 442
mehatfie 12:2e3e86714243 443 //seek back to the start of the cycle loop
mehatfie 12:2e3e86714243 444 lineData.lineNumber = cycleWatch.startLineNumber - 1;
mehatfie 12:2e3e86714243 445 int seekFailure = fseek(selectedFile, cycleWatch.startAddress, SEEK_SET); //fseek returns 0 on success
mehatfie 12:2e3e86714243 446 if (seekFailure){
mehatfie 12:2e3e86714243 447 ErrorOut("In Cycle Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD
mehatfie 12:2e3e86714243 448 return -1;
mehatfie 12:2e3e86714243 449 }
mehatfie 12:2e3e86714243 450
mehatfie 12:2e3e86714243 451 //Restart the timer for the next cycle
mehatfie 12:2e3e86714243 452 cycleTimer.reset();
mehatfie 12:2e3e86714243 453 cycleTimer.start();
mehatfie 12:2e3e86714243 454 }
mehatfie 11:bc9cd2869f95 455 }
mehatfie 11:bc9cd2869f95 456
mehatfie 12:2e3e86714243 457 return 0; //Return Success, no value is being sent so don't return 1
mehatfie 12:2e3e86714243 458 }
mehatfie 12:2e3e86714243 459
mehatfie 12:2e3e86714243 460
mehatfie 12:2e3e86714243 461
mehatfie 12:2e3e86714243 462 /**********************************************************************************************************************************/
mehatfie 12:2e3e86714243 463 /**********************************************************************************************************************************/
mehatfie 12:2e3e86714243 464 /************************** <FUNCTION: loopCommand> *****************************/
mehatfie 12:2e3e86714243 465 /**********************************************************************************************************************************/
mehatfie 12:2e3e86714243 466 /**********************************************************************************************************************************/
mehatfie 12:2e3e86714243 467
mehatfie 12:2e3e86714243 468 int loopCommand(LineData &lineData){
mehatfie 12:2e3e86714243 469
mehatfie 2:3e7baa3e3fec 470 //Get the Condition value for number of times to loop
mehatfie 2:3e7baa3e3fec 471 string loopCondition = lineData.word[1];
mehatfie 2:3e7baa3e3fec 472 int loopConditionValue = 0;
mehatfie 11:bc9cd2869f95 473 int loopConditionState = 0; //State 1 = device condtition, State 2 = numerical condition
mehatfie 9:5a0c4c6e39c7 474
mehatfie 11:bc9cd2869f95 475 LineData conditionLine;
mehatfie 11:bc9cd2869f95 476 //if the loop is supposed to happen under specific device conditions
mehatfie 11:bc9cd2869f95 477 if (loopCondition.compare("condition") == 0){
mehatfie 11:bc9cd2869f95 478
mehatfie 11:bc9cd2869f95 479 loopConditionState = 1;
mehatfie 11:bc9cd2869f95 480
mehatfie 11:bc9cd2869f95 481 //extract the command condition to be checked each loop
mehatfie 11:bc9cd2869f95 482 int i = 2, funcNumWords = 0;
mehatfie 11:bc9cd2869f95 483 for(i = 2; i < lineData.numWords; i++){
mehatfie 11:bc9cd2869f95 484 conditionLine.word[funcNumWords] = lineData.word[i];
mehatfie 11:bc9cd2869f95 485 funcNumWords++;
mehatfie 11:bc9cd2869f95 486 }
mehatfie 11:bc9cd2869f95 487
mehatfie 11:bc9cd2869f95 488 conditionLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 489 conditionLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 490 conditionLine.lineNumber = lineData.lineNumber;
mehatfie 9:5a0c4c6e39c7 491 }
mehatfie 1:5731f31f96be 492
mehatfie 11:bc9cd2869f95 493 //if the second word isn't condition, it means it's a number
mehatfie 11:bc9cd2869f95 494 else{
mehatfie 11:bc9cd2869f95 495 loopConditionState = 2;
mehatfie 11:bc9cd2869f95 496
mehatfie 11:bc9cd2869f95 497 int numValuesFound = sscanf(loopCondition.c_str(), "%d", &loopConditionValue);
mehatfie 11:bc9cd2869f95 498 if (numValuesFound < 1){
mehatfie 11:bc9cd2869f95 499 ErrorOut("Parameter Unknown, loopCondition Value can't be converted", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 500 return -1;
mehatfie 11:bc9cd2869f95 501 }
mehatfie 11:bc9cd2869f95 502
mehatfie 11:bc9cd2869f95 503 //loop condition must be greater than 0
mehatfie 11:bc9cd2869f95 504 if (loopConditionValue <= 0){
mehatfie 11:bc9cd2869f95 505 ErrorOut("Loop Condition must be greater than 0", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 506 return -1;
mehatfie 11:bc9cd2869f95 507 }
mehatfie 11:bc9cd2869f95 508 }
mehatfie 11:bc9cd2869f95 509
mehatfie 12:2e3e86714243 510
mehatfie 2:3e7baa3e3fec 511 int loopStartAddress = 0, loopLineNumber = 0, firstLineOfLoop = 1;
mehatfie 11:bc9cd2869f95 512 int counter = 1, checkEnd = 0, returnValue, conditionMet = 0;
mehatfie 11:bc9cd2869f95 513
mehatfie 11:bc9cd2869f95 514
mehatfie 11:bc9cd2869f95 515 //Before starting the loop, get the state of the device conditions
mehatfie 11:bc9cd2869f95 516 if (loopConditionState == 1){
mehatfie 11:bc9cd2869f95 517 conditionMet = interpretCommand(conditionLine);
mehatfie 11:bc9cd2869f95 518 if (conditionMet == -1)
mehatfie 11:bc9cd2869f95 519 return -1; //if the interpretCommand returned an error, then error out
mehatfie 11:bc9cd2869f95 520
mehatfie 11:bc9cd2869f95 521 //condition met, so skip to end of loop
mehatfie 11:bc9cd2869f95 522 if (conditionMet == 1){
mehatfie 11:bc9cd2869f95 523 int checkEnd = 0, returnValue = 0;
mehatfie 11:bc9cd2869f95 524 while (checkEnd != 3){
mehatfie 11:bc9cd2869f95 525
mehatfie 11:bc9cd2869f95 526 returnValue = getNextLine(selectedFile, lineData);
mehatfie 11:bc9cd2869f95 527
mehatfie 11:bc9cd2869f95 528 //if getNextLine returned an error, then error out
mehatfie 11:bc9cd2869f95 529 if (returnValue == -1)
mehatfie 11:bc9cd2869f95 530 return -1;
mehatfie 11:bc9cd2869f95 531
mehatfie 11:bc9cd2869f95 532 // check if the first word is an end command (avoids interpreting functions that perform actions)
mehatfie 11:bc9cd2869f95 533 if (lineData.word[0].compare("end") == 0)
mehatfie 11:bc9cd2869f95 534 checkEnd = interpretCommand(lineData);
mehatfie 11:bc9cd2869f95 535
mehatfie 11:bc9cd2869f95 536 if (checkEnd == 4) // custom return value for this function
mehatfie 11:bc9cd2869f95 537 return 0; //Function operated successfully but doesn't return a value
mehatfie 11:bc9cd2869f95 538 else if (checkEnd == -1) //if interpretCommand returned an error, then error out
mehatfie 11:bc9cd2869f95 539 return -1;
mehatfie 11:bc9cd2869f95 540 }
mehatfie 11:bc9cd2869f95 541 }
mehatfie 11:bc9cd2869f95 542 }
mehatfie 11:bc9cd2869f95 543
mehatfie 11:bc9cd2869f95 544
mehatfie 11:bc9cd2869f95 545 while (!conditionMet){
mehatfie 5:e36e0538a903 546
mehatfie 9:5a0c4c6e39c7 547 returnValue = getNextLine(selectedFile, lineData);
mehatfie 11:bc9cd2869f95 548
mehatfie 9:5a0c4c6e39c7 549 //if getNextLine returned an error, then return error out
mehatfie 9:5a0c4c6e39c7 550 if (returnValue == -1)
mehatfie 9:5a0c4c6e39c7 551 return -1;
mehatfie 11:bc9cd2869f95 552
mehatfie 5:e36e0538a903 553 //Must get the address before entering the interpret command
mehatfie 5:e36e0538a903 554 // if a Condition command is immediately after, and the condition fails, then
mehatfie 5:e36e0538a903 555 // the interpret command will return the line at the "end condition" line, and therefore
mehatfie 5:e36e0538a903 556 // 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 557 if (firstLineOfLoop){
mehatfie 1:5731f31f96be 558 loopStartAddress = lineData.lineAddress; //Save the Line Address
mehatfie 1:5731f31f96be 559 loopLineNumber = lineData.lineNumber; //Save the Line Number
mehatfie 1:5731f31f96be 560 firstLineOfLoop = 0;
mehatfie 1:5731f31f96be 561 }
mehatfie 11:bc9cd2869f95 562
mehatfie 11:bc9cd2869f95 563 checkEnd = interpretCommand(lineData);
mehatfie 6:d1594fd2ec5a 564
mehatfie 1:5731f31f96be 565 //Increase the loop counter and go back to the beginning of the loop
mehatfie 5:e36e0538a903 566 if (checkEnd == 3){
mehatfie 11:bc9cd2869f95 567
mehatfie 10:e8db892fbc52 568 //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 569 if (DummyMode)
mehatfie 10:e8db892fbc52 570 return 0; //Function operated successfully but doesn't return a value
mehatfie 10:e8db892fbc52 571
mehatfie 11:bc9cd2869f95 572 //Check whether the we should stop looping based on the state that the loop is based on (device conditional / numerical conditional)
mehatfie 11:bc9cd2869f95 573 if (loopConditionState == 1){
mehatfie 11:bc9cd2869f95 574 conditionMet = interpretCommand(conditionLine);
mehatfie 11:bc9cd2869f95 575 if (conditionMet == -1)
mehatfie 11:bc9cd2869f95 576 return -1; //if the interpretCommand returned an error, then error out
mehatfie 11:bc9cd2869f95 577 }
mehatfie 11:bc9cd2869f95 578 else if (loopConditionState == 2){
mehatfie 11:bc9cd2869f95 579 if (counter >= loopConditionValue)
mehatfie 11:bc9cd2869f95 580 conditionMet = 1;
mehatfie 9:5a0c4c6e39c7 581 }
mehatfie 9:5a0c4c6e39c7 582
mehatfie 11:bc9cd2869f95 583 //if the condition has not been met, then seek back to the beginning of the loop
mehatfie 11:bc9cd2869f95 584 if (!conditionMet){
mehatfie 11:bc9cd2869f95 585 int seekFailure = fseek(selectedFile, loopStartAddress, SEEK_SET); //fseek returns 0 on success
mehatfie 11:bc9cd2869f95 586 if (seekFailure){
mehatfie 11:bc9cd2869f95 587 ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD
mehatfie 11:bc9cd2869f95 588 return -1;
mehatfie 11:bc9cd2869f95 589 }
mehatfie 11:bc9cd2869f95 590 }
mehatfie 11:bc9cd2869f95 591
mehatfie 9:5a0c4c6e39c7 592 lineData.lineNumber = loopLineNumber - 1;
mehatfie 12:2e3e86714243 593 checkEnd = 0;
mehatfie 11:bc9cd2869f95 594 }
mehatfie 11:bc9cd2869f95 595
mehatfie 12:2e3e86714243 596 else if (checkEnd == -1) //if interpretCommand returned an error, then return error out
mehatfie 11:bc9cd2869f95 597 return -1;
mehatfie 11:bc9cd2869f95 598 }
mehatfie 11:bc9cd2869f95 599
mehatfie 9:5a0c4c6e39c7 600 return 0; //Return Success, no value is being sent so don't return 1
mehatfie 1:5731f31f96be 601 }
mehatfie 1:5731f31f96be 602
mehatfie 1:5731f31f96be 603 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 604 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 605 /************************* <FUNCTION: interpretCommand> *************************/
mehatfie 1:5731f31f96be 606 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 607 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 608
mehatfie 11:bc9cd2869f95 609 int interprettingErrorFlag = 0;
mehatfie 11:bc9cd2869f95 610
mehatfie 11:bc9cd2869f95 611 class ErrorCondition{
mehatfie 11:bc9cd2869f95 612
mehatfie 11:bc9cd2869f95 613 public:
mehatfie 11:bc9cd2869f95 614 LineData errorToWatch;
mehatfie 11:bc9cd2869f95 615 LineData errorFix;
mehatfie 11:bc9cd2869f95 616 int hasFix;
mehatfie 11:bc9cd2869f95 617 };
mehatfie 11:bc9cd2869f95 618
mehatfie 11:bc9cd2869f95 619 //vector<ErrorCondition> errorMonitors; //Initialize vector of errors to monitor
mehatfie 11:bc9cd2869f95 620 ErrorCondition errorMonitors[5];
mehatfie 11:bc9cd2869f95 621 int numErrorsConditions = 0;
mehatfie 11:bc9cd2869f95 622
mehatfie 11:bc9cd2869f95 623
mehatfie 11:bc9cd2869f95 624 int interpretCommand(LineData &lineData){
mehatfie 11:bc9cd2869f95 625
mehatfie 11:bc9cd2869f95 626 //Monitors the conditions to watch for erroring, and pauses system if any of the conditions turn out to be true
mehatfie 11:bc9cd2869f95 627 if (!interprettingErrorFlag){
mehatfie 11:bc9cd2869f95 628 int j = 0, error = -1, numError = 0;
mehatfie 11:bc9cd2869f95 629 for(j = 0; j < numErrorsConditions; j++){
mehatfie 11:bc9cd2869f95 630 int checkCondition = 0;
mehatfie 11:bc9cd2869f95 631
mehatfie 11:bc9cd2869f95 632 interprettingErrorFlag = 1;
mehatfie 11:bc9cd2869f95 633 checkCondition = interpretCommand(errorMonitors[j].errorToWatch);
mehatfie 11:bc9cd2869f95 634 interprettingErrorFlag = 0;
mehatfie 11:bc9cd2869f95 635
mehatfie 11:bc9cd2869f95 636 //if Condition is true, that means the error occurred
mehatfie 11:bc9cd2869f95 637 if (checkCondition == 1){
mehatfie 11:bc9cd2869f95 638 numError++;
mehatfie 11:bc9cd2869f95 639
mehatfie 11:bc9cd2869f95 640 //if the error has a Fix / Reset command, do it
mehatfie 11:bc9cd2869f95 641 if (errorMonitors[j].hasFix){
mehatfie 11:bc9cd2869f95 642 int returnValue;
mehatfie 11:bc9cd2869f95 643 interprettingErrorFlag = 1;
mehatfie 11:bc9cd2869f95 644 returnValue = interpretCommand(errorMonitors[j].errorFix); //Fix / Reset the error based on how the user justified to do so
mehatfie 11:bc9cd2869f95 645 interprettingErrorFlag = 0;
mehatfie 11:bc9cd2869f95 646 if (returnValue == -1)
mehatfie 11:bc9cd2869f95 647 return -1;
mehatfie 11:bc9cd2869f95 648 }
mehatfie 11:bc9cd2869f95 649
mehatfie 11:bc9cd2869f95 650 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 651 }
mehatfie 11:bc9cd2869f95 652
mehatfie 11:bc9cd2869f95 653 else if (checkCondition == -1)
mehatfie 11:bc9cd2869f95 654 return -1;
mehatfie 11:bc9cd2869f95 655 }
mehatfie 11:bc9cd2869f95 656
mehatfie 11:bc9cd2869f95 657 if (numError){
mehatfie 12:2e3e86714243 658 char errorMsg[100] = "errorWatch! Item: ";
mehatfie 11:bc9cd2869f95 659 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 660 ErrorOut(errorMsg, numError);
mehatfie 11:bc9cd2869f95 661 //errorFLAG = 1; //set error flag equal to 1 if error occurred
mehatfie 11:bc9cd2869f95 662
mehatfie 11:bc9cd2869f95 663 //place all devices into the pause functionality
mehatfie 11:bc9cd2869f95 664 int i = 0;
mehatfie 11:bc9cd2869f95 665 for(i = 0; i < devices.size(); i++)
mehatfie 11:bc9cd2869f95 666 devices[i]->pause();
mehatfie 11:bc9cd2869f95 667
mehatfie 12:2e3e86714243 668 cycleTimer.stop(); //pause the cycle timer
mehatfie 12:2e3e86714243 669
mehatfie 11:bc9cd2869f95 670 //LCD has already been adjusted with the ErrorMonitor function
mehatfie 11:bc9cd2869f95 671 //Simply wait for the user to press select in order to acknowledge the issue and try to fix it
mehatfie 11:bc9cd2869f95 672 while(!buttons.readSel());
mehatfie 12:2e3e86714243 673
mehatfie 11:bc9cd2869f95 674 //place all devices into the resume functionality
mehatfie 11:bc9cd2869f95 675 for(i = 0; i < devices.size(); i++)
mehatfie 11:bc9cd2869f95 676 devices[i]->resume();
mehatfie 11:bc9cd2869f95 677
mehatfie 12:2e3e86714243 678 cycleTimer.start(); //start the cycle timer
mehatfie 12:2e3e86714243 679
mehatfie 11:bc9cd2869f95 680 lcd.cls(); //clear the display
mehatfie 11:bc9cd2869f95 681 }
mehatfie 11:bc9cd2869f95 682 }
mehatfie 11:bc9cd2869f95 683
mehatfie 2:3e7baa3e3fec 684
mehatfie 11:bc9cd2869f95 685
mehatfie 11:bc9cd2869f95 686 /******************************************************************************/
mehatfie 11:bc9cd2869f95 687 /*** <Functionality: device> ***/
mehatfie 11:bc9cd2869f95 688 /******************************************************************************/
mehatfie 11:bc9cd2869f95 689
mehatfie 2:3e7baa3e3fec 690 if (lineData.word[0].compare("device") == 0){
mehatfie 2:3e7baa3e3fec 691
mehatfie 1:5731f31f96be 692 int i = 0, deviceFound = -1;
mehatfie 2:3e7baa3e3fec 693 for (i = 0; i < numDevices; i++){
mehatfie 4:86d0d04cc055 694 if (lineData.word[2].compare(DeviceNames[i]) == 0){
mehatfie 1:5731f31f96be 695 deviceFound = i;
mehatfie 4:86d0d04cc055 696 }
mehatfie 2:3e7baa3e3fec 697 }
mehatfie 9:5a0c4c6e39c7 698
mehatfie 1:5731f31f96be 699 //if the device type does not match any known type, error out
mehatfie 1:5731f31f96be 700 if (deviceFound == -1){
mehatfie 9:5a0c4c6e39c7 701 ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 702 return -1;
mehatfie 1:5731f31f96be 703 }
mehatfie 1:5731f31f96be 704
mehatfie 1:5731f31f96be 705 //Add device to the array of devices and initialize it
mehatfie 2:3e7baa3e3fec 706 else{
mehatfie 2:3e7baa3e3fec 707 devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData));
mehatfie 9:5a0c4c6e39c7 708 devices.back()->name = lineData.word[1];
mehatfie 9:5a0c4c6e39c7 709
mehatfie 9:5a0c4c6e39c7 710 //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 711 if (devices.back()->errorFlag == 1){
mehatfie 9:5a0c4c6e39c7 712 ErrorOut("Error initializing device", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 713 return -1;
mehatfie 9:5a0c4c6e39c7 714 }
mehatfie 2:3e7baa3e3fec 715 }
mehatfie 1:5731f31f96be 716 }
mehatfie 1:5731f31f96be 717
mehatfie 11:bc9cd2869f95 718
mehatfie 11:bc9cd2869f95 719
mehatfie 11:bc9cd2869f95 720 /******************************************************************************/
mehatfie 11:bc9cd2869f95 721 /*** <Functionality: errorWatch> ***/
mehatfie 11:bc9cd2869f95 722 /******************************************************************************/
mehatfie 11:bc9cd2869f95 723
mehatfie 11:bc9cd2869f95 724 else if (lineData.word[0].compare("errorWatch") == 0){
mehatfie 11:bc9cd2869f95 725 //line looks like: error, _Condition_, FIX(?), _Command_
mehatfie 11:bc9cd2869f95 726 //create a temp LineData variable to store the correct information of the error Condition (discluding the first "error" keyword
mehatfie 11:bc9cd2869f95 727 ErrorCondition tempError;
mehatfie 11:bc9cd2869f95 728 LineData tempLine;
mehatfie 11:bc9cd2869f95 729
mehatfie 11:bc9cd2869f95 730 int i = 1, funcNumWords = 0;
mehatfie 11:bc9cd2869f95 731 for (i = 1; i < lineData.numWords; i++){
mehatfie 11:bc9cd2869f95 732
mehatfie 11:bc9cd2869f95 733 //if the keyword FIX is not this word, than add it to the LineData
mehatfie 11:bc9cd2869f95 734 if (lineData.word[i].compare("FIX") != 0){
mehatfie 11:bc9cd2869f95 735 tempLine.word[funcNumWords] = lineData.word[i];
mehatfie 11:bc9cd2869f95 736 funcNumWords++;
mehatfie 11:bc9cd2869f95 737 }
mehatfie 11:bc9cd2869f95 738
mehatfie 11:bc9cd2869f95 739 //if the keyword FIX was found, this means there's a FIX / Reset command if the specified error being monitored occurs
mehatfie 11:bc9cd2869f95 740 else if (lineData.word[i].compare("FIX") == 0){
mehatfie 11:bc9cd2869f95 741 tempError.hasFix = 1;
mehatfie 11:bc9cd2869f95 742
mehatfie 11:bc9cd2869f95 743 //Save the line as the "Error to Watch" value
mehatfie 11:bc9cd2869f95 744 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 745 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 746 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 747 tempError.errorToWatch = tempLine;
mehatfie 11:bc9cd2869f95 748
mehatfie 11:bc9cd2869f95 749 //reset lineData and num words for the command / function in order to process the FIX command into the error condition
mehatfie 11:bc9cd2869f95 750 funcNumWords = 0;
mehatfie 11:bc9cd2869f95 751 tempLine = LineData(); //reset tempLine
mehatfie 11:bc9cd2869f95 752 }
mehatfie 11:bc9cd2869f95 753 }
mehatfie 11:bc9cd2869f95 754
mehatfie 11:bc9cd2869f95 755 //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 756 if (tempError.hasFix){
mehatfie 11:bc9cd2869f95 757 //Save the line as the "Error Fix" value
mehatfie 11:bc9cd2869f95 758 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 759 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 760 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 761 tempError.errorFix = tempLine;
mehatfie 11:bc9cd2869f95 762 }
mehatfie 11:bc9cd2869f95 763
mehatfie 11:bc9cd2869f95 764 //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 765 else{
mehatfie 11:bc9cd2869f95 766 //Save the line as the "Error to Watch" value
mehatfie 11:bc9cd2869f95 767 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 768 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 769 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 770 tempError.errorToWatch = tempLine;
mehatfie 11:bc9cd2869f95 771 }
mehatfie 11:bc9cd2869f95 772
mehatfie 11:bc9cd2869f95 773 //if DummyMode, send error Condition to the interpretCommand so that we can check the syntax
mehatfie 11:bc9cd2869f95 774 if (DummyMode){
mehatfie 11:bc9cd2869f95 775 int returnValue1 = interpretCommand(tempError.errorToWatch);
mehatfie 11:bc9cd2869f95 776 int returnValue2 = interpretCommand(tempError.errorFix);
mehatfie 11:bc9cd2869f95 777 if (returnValue1 == -1 || returnValue2 == -1)
mehatfie 11:bc9cd2869f95 778 return -1;
mehatfie 11:bc9cd2869f95 779 }
mehatfie 11:bc9cd2869f95 780
mehatfie 11:bc9cd2869f95 781 errorMonitors[numErrorsConditions] = tempError;
mehatfie 11:bc9cd2869f95 782 numErrorsConditions++;
mehatfie 11:bc9cd2869f95 783 }
mehatfie 11:bc9cd2869f95 784
mehatfie 11:bc9cd2869f95 785
mehatfie 11:bc9cd2869f95 786 /******************************************************************************/
mehatfie 11:bc9cd2869f95 787 /*** <Functionality: delay> ***/
mehatfie 11:bc9cd2869f95 788 /******************************************************************************/
mehatfie 11:bc9cd2869f95 789
mehatfie 2:3e7baa3e3fec 790 else if (lineData.word[0].compare("delay") == 0){
mehatfie 10:e8db892fbc52 791
mehatfie 11:bc9cd2869f95 792 if (lineData.numWords != 2){
mehatfie 11:bc9cd2869f95 793 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 794 return -1;
mehatfie 11:bc9cd2869f95 795 }
mehatfie 11:bc9cd2869f95 796
mehatfie 2:3e7baa3e3fec 797 string duration = lineData.word[1];
mehatfie 2:3e7baa3e3fec 798 int durationValue = 0;
mehatfie 9:5a0c4c6e39c7 799 int numValuesFound = sscanf(duration.c_str(), "%d", &durationValue);
mehatfie 1:5731f31f96be 800
mehatfie 9:5a0c4c6e39c7 801 if (numValuesFound < 1){
mehatfie 9:5a0c4c6e39c7 802 ErrorOut("Parameter Unknown, Duration Value can't be converted", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 803 return -1;
mehatfie 9:5a0c4c6e39c7 804 }
mehatfie 9:5a0c4c6e39c7 805
mehatfie 9:5a0c4c6e39c7 806 if (durationValue > 0){
mehatfie 11:bc9cd2869f95 807
mehatfie 11:bc9cd2869f95 808 //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 809 if (DummyMode)
mehatfie 11:bc9cd2869f95 810 return 0; //Function operated successfully but doesn't return a value
mehatfie 11:bc9cd2869f95 811
mehatfie 1:5731f31f96be 812 timer.reset();
mehatfie 1:5731f31f96be 813 timer.start();
mehatfie 1:5731f31f96be 814 while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration
mehatfie 1:5731f31f96be 815 timer.stop(); //Stop the Timer
mehatfie 1:5731f31f96be 816 }
mehatfie 1:5731f31f96be 817 else{
mehatfie 11:bc9cd2869f95 818 ErrorOut("Duration value is less than or equal to 0", lineData.lineNumber);
mehatfie 1:5731f31f96be 819 return -1;
mehatfie 1:5731f31f96be 820 }
mehatfie 1:5731f31f96be 821 }
mehatfie 1:5731f31f96be 822
mehatfie 11:bc9cd2869f95 823 /******************************************************************************/
mehatfie 11:bc9cd2869f95 824 /*** <Functionality: pause> ***/
mehatfie 11:bc9cd2869f95 825 /******************************************************************************/
mehatfie 11:bc9cd2869f95 826
mehatfie 11:bc9cd2869f95 827 else if (lineData.word[0].compare("pause") == 0){
mehatfie 11:bc9cd2869f95 828
mehatfie 11:bc9cd2869f95 829 if (lineData.numWords != 1){
mehatfie 11:bc9cd2869f95 830 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 831 return -1;
mehatfie 11:bc9cd2869f95 832 }
mehatfie 11:bc9cd2869f95 833
mehatfie 11:bc9cd2869f95 834 lcd.cls(); //clear the display
mehatfie 11:bc9cd2869f95 835 lcd.setAddress(0,0);
mehatfie 11:bc9cd2869f95 836 lcd.printf("System Pause");
mehatfie 11:bc9cd2869f95 837 lcd.setAddress(0,2);
mehatfie 11:bc9cd2869f95 838 lcd.printf("Line Number: %d", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 839 lcd.setAddress(0,3);
mehatfie 11:bc9cd2869f95 840 lcd.printf("Press Sel to Resume");
mehatfie 11:bc9cd2869f95 841 while(!buttons.readSel());
mehatfie 11:bc9cd2869f95 842 }
mehatfie 11:bc9cd2869f95 843
mehatfie 11:bc9cd2869f95 844 /******************************************************************************/
mehatfie 11:bc9cd2869f95 845 /*** <Functionality: GoTo> ***/
mehatfie 11:bc9cd2869f95 846 /******************************************************************************/
mehatfie 11:bc9cd2869f95 847
mehatfie 11:bc9cd2869f95 848 else if (lineData.word[0].compare("GoTo") == 0){
mehatfie 11:bc9cd2869f95 849
mehatfie 11:bc9cd2869f95 850 if (lineData.word[1].compare("label") == 0){
mehatfie 11:bc9cd2869f95 851
mehatfie 11:bc9cd2869f95 852 if (lineData.numWords != 3){
mehatfie 11:bc9cd2869f95 853 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 854 return -1;
mehatfie 11:bc9cd2869f95 855 }
mehatfie 11:bc9cd2869f95 856
mehatfie 11:bc9cd2869f95 857 int labelExists = 0;
mehatfie 11:bc9cd2869f95 858 for(vector<GoToLabel>::iterator it=GoToLabels.begin(); it < GoToLabels.end(); it++){
mehatfie 11:bc9cd2869f95 859 if (lineData.word[1].compare((*it).name) == 0)
mehatfie 11:bc9cd2869f95 860 labelExists = 1;
mehatfie 11:bc9cd2869f95 861 }
mehatfie 11:bc9cd2869f95 862
mehatfie 11:bc9cd2869f95 863 //if the label does not exist then add it to the vector
mehatfie 11:bc9cd2869f95 864 //if it turned out to exist then do nothing
mehatfie 11:bc9cd2869f95 865 if (!labelExists){
mehatfie 11:bc9cd2869f95 866 GoToLabel tempLabel;
mehatfie 11:bc9cd2869f95 867 tempLabel.name = lineData.word[2]; //Save the Label Name
mehatfie 11:bc9cd2869f95 868 tempLabel.lineAddress = lineData.lineAddress; //Save the Line Address
mehatfie 11:bc9cd2869f95 869 tempLabel.lineNumber = lineData.lineNumber; //Save the Line Number
mehatfie 11:bc9cd2869f95 870 GoToLabels.push_back(tempLabel);
mehatfie 11:bc9cd2869f95 871 }
mehatfie 11:bc9cd2869f95 872 }
mehatfie 11:bc9cd2869f95 873
mehatfie 11:bc9cd2869f95 874 //if the second word was not "label," then it means the word should correspond to an already created GoTo Label
mehatfie 11:bc9cd2869f95 875 else{
mehatfie 11:bc9cd2869f95 876
mehatfie 11:bc9cd2869f95 877 if (lineData.numWords != 2){
mehatfie 11:bc9cd2869f95 878 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 879 return -1;
mehatfie 11:bc9cd2869f95 880 }
mehatfie 11:bc9cd2869f95 881
mehatfie 11:bc9cd2869f95 882 //All syntax checking done by this point, if Dummy then return success in order to check the code
mehatfie 11:bc9cd2869f95 883 //Unable to continue further checking since an aspect of DummyMode run through is to collect the GoTo Label Locations
mehatfie 11:bc9cd2869f95 884 //so that we have them in address
mehatfie 11:bc9cd2869f95 885 if (DummyMode)
mehatfie 11:bc9cd2869f95 886 return 0; //Function operated successfully but doesn't return a value
mehatfie 11:bc9cd2869f95 887
mehatfie 11:bc9cd2869f95 888 int i = 0, labelFound = -1;
mehatfie 11:bc9cd2869f95 889 for(i = 0; i < GoToLabels.size(); i++){
mehatfie 11:bc9cd2869f95 890 if (lineData.word[1].compare(GoToLabels[i].name) == 0){
mehatfie 11:bc9cd2869f95 891 labelFound = i;
mehatfie 11:bc9cd2869f95 892 }
mehatfie 11:bc9cd2869f95 893 }
mehatfie 11:bc9cd2869f95 894
mehatfie 11:bc9cd2869f95 895 //if the label was not found then error out
mehatfie 11:bc9cd2869f95 896 if (labelFound == -1){
mehatfie 11:bc9cd2869f95 897 ErrorOut("No label match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 11:bc9cd2869f95 898 return -1;
mehatfie 11:bc9cd2869f95 899 }
mehatfie 11:bc9cd2869f95 900
mehatfie 11:bc9cd2869f95 901 //if the label was found, then seek to that GoTo position
mehatfie 11:bc9cd2869f95 902 else{
mehatfie 11:bc9cd2869f95 903
mehatfie 11:bc9cd2869f95 904 int seekFailure = fseek(selectedFile, GoToLabels[labelFound].lineAddress, SEEK_SET); //fseek returns 0 on success
mehatfie 11:bc9cd2869f95 905 if (seekFailure){
mehatfie 11:bc9cd2869f95 906 ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD
mehatfie 11:bc9cd2869f95 907 return -1;
mehatfie 11:bc9cd2869f95 908 }
mehatfie 11:bc9cd2869f95 909
mehatfie 11:bc9cd2869f95 910 lineData.lineNumber = GoToLabels[labelFound].lineNumber - 1; //set the lineNumber value
mehatfie 11:bc9cd2869f95 911 }
mehatfie 11:bc9cd2869f95 912 }
mehatfie 11:bc9cd2869f95 913 }
mehatfie 11:bc9cd2869f95 914
mehatfie 11:bc9cd2869f95 915 /******************************************************************************/
mehatfie 11:bc9cd2869f95 916 /*** <Functionality: loop> ***/
mehatfie 11:bc9cd2869f95 917 /******************************************************************************/
mehatfie 11:bc9cd2869f95 918
mehatfie 9:5a0c4c6e39c7 919 else if (lineData.word[0].compare("loop") == 0)
mehatfie 11:bc9cd2869f95 920 return loopCommand(lineData); //Process the loop command and return the value that it returns
mehatfie 11:bc9cd2869f95 921
mehatfie 5:e36e0538a903 922
mehatfie 11:bc9cd2869f95 923 /******************************************************************************/
mehatfie 11:bc9cd2869f95 924 /*** <Functionality: condition> ***/
mehatfie 11:bc9cd2869f95 925 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 926 else if (lineData.word[0].compare("condition") == 0)
mehatfie 9:5a0c4c6e39c7 927 return conditionCommand(selectedFile, lineData); //Process the condition command and return the value that it returns
mehatfie 9:5a0c4c6e39c7 928
mehatfie 11:bc9cd2869f95 929
mehatfie 11:bc9cd2869f95 930 /******************************************************************************/
mehatfie 11:bc9cd2869f95 931 /*** <Functionality: cycle> ***/
mehatfie 11:bc9cd2869f95 932 /******************************************************************************/
mehatfie 11:bc9cd2869f95 933 else if (lineData.word[0].compare("cycle") == 0)
mehatfie 12:2e3e86714243 934 return cycleCommand(lineData, 1); //Sending 1 means it's initializing
mehatfie 11:bc9cd2869f95 935
mehatfie 11:bc9cd2869f95 936
mehatfie 11:bc9cd2869f95 937 /******************************************************************************/
mehatfie 11:bc9cd2869f95 938 /*** <Functionality: end> ***/
mehatfie 11:bc9cd2869f95 939 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 940 //end has custom return value for specific functions, since "end" is a common keyword amongst functions
mehatfie 9:5a0c4c6e39c7 941 else if (lineData.word[0].compare("end") == 0){
mehatfie 9:5a0c4c6e39c7 942 if (lineData.word[1].compare("program") == 0)
mehatfie 5:e36e0538a903 943 return 2;
mehatfie 9:5a0c4c6e39c7 944 else if (lineData.word[1].compare("loop") == 0)
mehatfie 5:e36e0538a903 945 return 3;
mehatfie 9:5a0c4c6e39c7 946 else if (lineData.word[1].compare("condition") == 0)
mehatfie 5:e36e0538a903 947 return 4;
mehatfie 11:bc9cd2869f95 948 else if (lineData.word[1].compare("cycle") == 0){
mehatfie 12:2e3e86714243 949 cycleCommand(lineData, 0); //Sending 0 means it's checking for the ending
mehatfie 11:bc9cd2869f95 950 return 5;
mehatfie 11:bc9cd2869f95 951 }
mehatfie 9:5a0c4c6e39c7 952
mehatfie 9:5a0c4c6e39c7 953 else{
mehatfie 9:5a0c4c6e39c7 954 ErrorOut("Unknown function ending", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 955 return -1;
mehatfie 2:3e7baa3e3fec 956 }
mehatfie 1:5731f31f96be 957 }
mehatfie 1:5731f31f96be 958
mehatfie 11:bc9cd2869f95 959 /******************************************************************************/
mehatfie 11:bc9cd2869f95 960 /*** <Functionality: local_name Checking> ***/
mehatfie 11:bc9cd2869f95 961 /******************************************************************************/
mehatfie 11:bc9cd2869f95 962
mehatfie 1:5731f31f96be 963 //not a keyword so check if it's a localName for a device
mehatfie 1:5731f31f96be 964 else{
mehatfie 11:bc9cd2869f95 965
mehatfie 1:5731f31f96be 966
mehatfie 11:bc9cd2869f95 967 // lcd.setAddress(0,1);
mehatfie 11:bc9cd2869f95 968 // lcd.printf("wrd1: %s", lineData.word[0]);
mehatfie 11:bc9cd2869f95 969 // wait(1);
mehatfie 11:bc9cd2869f95 970
mehatfie 1:5731f31f96be 971 int i = 0, deviceFound = -1;
mehatfie 2:3e7baa3e3fec 972 for (i = 0; i < devices.size(); i++){
mehatfie 2:3e7baa3e3fec 973 if (lineData.word[0].compare(devices[i]->name) == 0)
mehatfie 1:5731f31f96be 974 deviceFound = i;
mehatfie 1:5731f31f96be 975 }
mehatfie 1:5731f31f96be 976
mehatfie 1:5731f31f96be 977 //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 978 if (deviceFound == -1){
mehatfie 9:5a0c4c6e39c7 979 ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 980 return -1;
mehatfie 1:5731f31f96be 981 }
mehatfie 1:5731f31f96be 982
mehatfie 11:bc9cd2869f95 983
mehatfie 11:bc9cd2869f95 984
mehatfie 1:5731f31f96be 985 //Local Name matches a device, send line to that device in order to process the functionality
mehatfie 9:5a0c4c6e39c7 986 else
mehatfie 9:5a0c4c6e39c7 987 return devices[deviceFound]->interpret(lineData); //call the device specific interpret command, and return the value it returns
mehatfie 1:5731f31f96be 988 }
mehatfie 1:5731f31f96be 989
mehatfie 9:5a0c4c6e39c7 990 return 0; //Return Success, no value is being sent so don't return 1
mehatfie 1:5731f31f96be 991 }
mehatfie 1:5731f31f96be 992
mehatfie 1:5731f31f96be 993
mehatfie 11:bc9cd2869f95 994
mehatfie 11:bc9cd2869f95 995
mehatfie 1:5731f31f96be 996 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 997 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 998 /****************************** <FUNCTION: main> ********************************/
mehatfie 1:5731f31f96be 999 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 1000 /**********************************************************************************************************************************/
mehatfie 1:5731f31f96be 1001
mehatfie 0:22618cf06f45 1002 int main() {
mehatfie 0:22618cf06f45 1003
mehatfie 0:22618cf06f45 1004 fullInit(); //Initialize anything that's required to run the code (LCD)
mehatfie 0:22618cf06f45 1005
mehatfie 6:d1594fd2ec5a 1006 LineData lineData;
mehatfie 2:3e7baa3e3fec 1007 resetLineData(lineData);
mehatfie 2:3e7baa3e3fec 1008
mehatfie 9:5a0c4c6e39c7 1009 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1010 /*** <Get all the Potential Programs> ***/
mehatfie 9:5a0c4c6e39c7 1011 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1012 int numTextFiles = 0;
mehatfie 9:5a0c4c6e39c7 1013 vector<string> textFiles; //Assuming Maximum of 25 txt files will be on the SD Card
mehatfie 9:5a0c4c6e39c7 1014 vector<string> filenames = readFileNames("/sd");
mehatfie 9:5a0c4c6e39c7 1015
mehatfie 9:5a0c4c6e39c7 1016 //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 1017 if (filenames.size() == 0){
mehatfie 9:5a0c4c6e39c7 1018 ErrorOut("No Files Found, or Directory can't be accessed", 0); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 1019 return -1; //End program by returning in the main()
mehatfie 9:5a0c4c6e39c7 1020 }
mehatfie 9:5a0c4c6e39c7 1021
mehatfie 9:5a0c4c6e39c7 1022 numTextFiles = getFileNamesWithoutExt(textFiles, filenames);
mehatfie 0:22618cf06f45 1023
mehatfie 9:5a0c4c6e39c7 1024 //Error check whether the SD Card has any txt files in it's first directory
mehatfie 9:5a0c4c6e39c7 1025 if (numTextFiles == 0){
mehatfie 9:5a0c4c6e39c7 1026 ErrorOut("No Program (.txt) Files Found in first Directory", 0); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 1027 return -1; //End program by returning in the main()
mehatfie 9:5a0c4c6e39c7 1028 }
mehatfie 0:22618cf06f45 1029
mehatfie 9:5a0c4c6e39c7 1030 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1031 /*** <Select the Program txt File> ***/
mehatfie 9:5a0c4c6e39c7 1032 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1033 int fileSelected = 0, selectedFileIndex = 0;
mehatfie 9:5a0c4c6e39c7 1034
mehatfie 9:5a0c4c6e39c7 1035 lcd.cls(); //clear the display
mehatfie 9:5a0c4c6e39c7 1036 lcd.setAddress(0,1);
mehatfie 9:5a0c4c6e39c7 1037 lcd.printf("Select Your Program");
mehatfie 9:5a0c4c6e39c7 1038 lcd.setAddress(0,2);
mehatfie 9:5a0c4c6e39c7 1039 lcd.printf("Num Programs = %d", numTextFiles);
mehatfie 2:3e7baa3e3fec 1040
mehatfie 9:5a0c4c6e39c7 1041 uint8_t lastButState = 0;
mehatfie 9:5a0c4c6e39c7 1042 lcd.setCursor(TextLCD::CurOn_BlkOn); //turn blinking cursor on
mehatfie 9:5a0c4c6e39c7 1043
mehatfie 9:5a0c4c6e39c7 1044 selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, -1); //Initialize the first file to the screen
mehatfie 9:5a0c4c6e39c7 1045 while(!fileSelected) {
mehatfie 0:22618cf06f45 1046
mehatfie 9:5a0c4c6e39c7 1047 uint8_t curButState = buttons.readBus();
mehatfie 9:5a0c4c6e39c7 1048 if(curButState != lastButState){
mehatfie 9:5a0c4c6e39c7 1049 lastButState = curButState;
mehatfie 9:5a0c4c6e39c7 1050 if(buttons.readRight())
mehatfie 9:5a0c4c6e39c7 1051 selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 1);
mehatfie 9:5a0c4c6e39c7 1052 else if(buttons.readLeft())
mehatfie 9:5a0c4c6e39c7 1053 selectedFileIndex = cyclePrograms(textFiles, numTextFiles, selectedFileIndex, 0);
mehatfie 9:5a0c4c6e39c7 1054 else if(buttons.readSel())
mehatfie 9:5a0c4c6e39c7 1055 fileSelected = 1;
mehatfie 0:22618cf06f45 1056 }
mehatfie 9:5a0c4c6e39c7 1057 }
mehatfie 9:5a0c4c6e39c7 1058
mehatfie 9:5a0c4c6e39c7 1059 lcd.setCursor(TextLCD::CurOn_BlkOff); //turn blinking cursor off
mehatfie 9:5a0c4c6e39c7 1060
mehatfie 9:5a0c4c6e39c7 1061 char selectedFileName[50];
mehatfie 9:5a0c4c6e39c7 1062 strcpy(selectedFileName, textFiles[selectedFileIndex].c_str());
mehatfie 9:5a0c4c6e39c7 1063
mehatfie 9:5a0c4c6e39c7 1064 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1065 /*** <Open the Program txt File> ***/
mehatfie 9:5a0c4c6e39c7 1066 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1067
mehatfie 9:5a0c4c6e39c7 1068 //Create the string of the full directory and path to the program txt file
mehatfie 9:5a0c4c6e39c7 1069 char selectedFileFullName[100] = "/sd/"; //Assuming that no directory and file name will be longer than 100 characters
mehatfie 9:5a0c4c6e39c7 1070 strcat(selectedFileFullName, selectedFileName);
mehatfie 9:5a0c4c6e39c7 1071 strcat(selectedFileFullName, ".txt");
mehatfie 9:5a0c4c6e39c7 1072
mehatfie 11:bc9cd2869f95 1073 selectedFile = fopen(selectedFileFullName, "r");
mehatfie 9:5a0c4c6e39c7 1074
mehatfie 9:5a0c4c6e39c7 1075 //Error out of attempt to open the selected file was unsuccessful
mehatfie 9:5a0c4c6e39c7 1076 if(selectedFile == NULL) {
mehatfie 9:5a0c4c6e39c7 1077 ErrorOut("Unable to Open Selected File", 0); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 1078 return -1; //End program by returning in the main()
mehatfie 9:5a0c4c6e39c7 1079 }
mehatfie 0:22618cf06f45 1080
mehatfie 0:22618cf06f45 1081
mehatfie 9:5a0c4c6e39c7 1082 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 1083 /*** <Start Running through the Program txt File Lines> ***/
mehatfie 9:5a0c4c6e39c7 1084 /******************************************************************************/
mehatfie 10:e8db892fbc52 1085
mehatfie 10:e8db892fbc52 1086 resetLineData(lineData); //Reset the values in the struct that holds the File / Line Data
mehatfie 10:e8db892fbc52 1087 lcd.cls(); //clear the display
mehatfie 0:22618cf06f45 1088
mehatfie 10:e8db892fbc52 1089 int endOfFile = 0, error = 0, returnValue, checkEnd;
mehatfie 10:e8db892fbc52 1090 DummyMode = 1; //set Dummy Mode equal to 1 to simulate the first run through of the code
mehatfie 10:e8db892fbc52 1091 while (!endOfFile){
mehatfie 0:22618cf06f45 1092
mehatfie 10:e8db892fbc52 1093 //Re-initialize variables
mehatfie 10:e8db892fbc52 1094 returnValue = 0;
mehatfie 10:e8db892fbc52 1095 checkEnd = 0;
mehatfie 10:e8db892fbc52 1096
mehatfie 10:e8db892fbc52 1097 returnValue = getNextLine(selectedFile, lineData); //get the next line of data
mehatfie 10:e8db892fbc52 1098
mehatfie 10:e8db892fbc52 1099 //if getNextLine returned an error, then return error in main
mehatfie 10:e8db892fbc52 1100 if (returnValue == -1)
mehatfie 10:e8db892fbc52 1101 error = 1;
mehatfie 9:5a0c4c6e39c7 1102
mehatfie 11:bc9cd2869f95 1103 checkEnd = interpretCommand(lineData); //interpret the line data
mehatfie 9:5a0c4c6e39c7 1104
mehatfie 11:bc9cd2869f95 1105 if (checkEnd == 2){
mehatfie 11:bc9cd2869f95 1106 //Dummy Mode will be turned on for the first run through, set it to 0 after the first run through,
mehatfie 11:bc9cd2869f95 1107 //as the syntax will be checked without erroring, and therefore it is possible to try and run the .txt file
mehatfie 11:bc9cd2869f95 1108 //Seek back to beginning of file
mehatfie 11:bc9cd2869f95 1109 if (DummyMode){
mehatfie 11:bc9cd2869f95 1110 DummyMode = 0;
mehatfie 11:bc9cd2869f95 1111 rewind(selectedFile); //seek to the beginning of the file
mehatfie 11:bc9cd2869f95 1112 resetLineData(lineData); //Reset the values in the struct that holds the File / Line Data
mehatfie 11:bc9cd2869f95 1113 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 1114 int k = 0;
mehatfie 11:bc9cd2869f95 1115 for (k = 0; k < numErrorsConditions; k++)
mehatfie 11:bc9cd2869f95 1116 errorMonitors[k] = ErrorCondition();
mehatfie 11:bc9cd2869f95 1117 numErrorsConditions = 0;
mehatfie 11:bc9cd2869f95 1118 }
mehatfie 11:bc9cd2869f95 1119 else
mehatfie 11:bc9cd2869f95 1120 endOfFile = 1;
mehatfie 11:bc9cd2869f95 1121 }
mehatfie 10:e8db892fbc52 1122 else if (checkEnd == -1) //if interpretCommand returned an error, then return error in main
mehatfie 10:e8db892fbc52 1123 error = 1;
mehatfie 10:e8db892fbc52 1124
mehatfie 10:e8db892fbc52 1125 //Before erroring out, turn all devices off so that they power down
mehatfie 10:e8db892fbc52 1126 if (error){
mehatfie 10:e8db892fbc52 1127 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 1128 for(vector<Device*>::iterator it=devices.begin(); it < devices.end(); it++)
mehatfie 9:5a0c4c6e39c7 1129 (*it)->off();
mehatfie 9:5a0c4c6e39c7 1130 }
mehatfie 10:e8db892fbc52 1131
mehatfie 10:e8db892fbc52 1132 return -1;
mehatfie 11:bc9cd2869f95 1133 }
mehatfie 11:bc9cd2869f95 1134 }
mehatfie 10:e8db892fbc52 1135
mehatfie 11:bc9cd2869f95 1136 //Exit the program and remove needed items from memory
mehatfie 11:bc9cd2869f95 1137 GoToLabels.erase(GoToLabels.begin(), GoToLabels.end()); //remove the GoToLabels vector from memory
mehatfie 11:bc9cd2869f95 1138 devices.erase(devices.begin(), devices.end()); //remove the devices vector from memory
mehatfie 11:bc9cd2869f95 1139
mehatfie 10:e8db892fbc52 1140 lcd.cls(); //clear the display
mehatfie 10:e8db892fbc52 1141 lcd.setAddress(0,0);
mehatfie 10:e8db892fbc52 1142 lcd.printf("END OF PROGRAM");
mehatfie 10:e8db892fbc52 1143 lcd.setAddress(0,2);
mehatfie 10:e8db892fbc52 1144 lcd.printf("To Restart...");
mehatfie 10:e8db892fbc52 1145 lcd.setAddress(0,3);
mehatfie 10:e8db892fbc52 1146 lcd.printf("Press BACK");
mehatfie 10:e8db892fbc52 1147
mehatfie 10:e8db892fbc52 1148 while(!buttons.readBack());
mehatfie 10:e8db892fbc52 1149
mehatfie 10:e8db892fbc52 1150 lcd.setAddress(0,1);
mehatfie 10:e8db892fbc52 1151 //rewind(selectedFile);
mehatfie 0:22618cf06f45 1152
mehatfie 2:3e7baa3e3fec 1153 }
mehatfie 2:3e7baa3e3fec 1154
mehatfie 9:5a0c4c6e39c7 1155