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 23:08:39 2014 +0000
Revision:
13:899db9d635e5
Parent:
12:2e3e86714243
Child:
14:953820302fb7
- cycle function working; - errorWatch does in fact work on the first cycle around; - error corrected in code where the tempError is not initialized, and then falsely sets the second errorWatch to have a fix, when it doesn't

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 13:899db9d635e5 610 int enableErrors = 0;
mehatfie 11:bc9cd2869f95 611
mehatfie 11:bc9cd2869f95 612 class ErrorCondition{
mehatfie 11:bc9cd2869f95 613
mehatfie 11:bc9cd2869f95 614 public:
mehatfie 11:bc9cd2869f95 615 LineData errorToWatch;
mehatfie 11:bc9cd2869f95 616 LineData errorFix;
mehatfie 11:bc9cd2869f95 617 int hasFix;
mehatfie 11:bc9cd2869f95 618 };
mehatfie 11:bc9cd2869f95 619
mehatfie 11:bc9cd2869f95 620 //vector<ErrorCondition> errorMonitors; //Initialize vector of errors to monitor
mehatfie 11:bc9cd2869f95 621 ErrorCondition errorMonitors[5];
mehatfie 11:bc9cd2869f95 622 int numErrorsConditions = 0;
mehatfie 11:bc9cd2869f95 623
mehatfie 11:bc9cd2869f95 624
mehatfie 11:bc9cd2869f95 625 int interpretCommand(LineData &lineData){
mehatfie 13:899db9d635e5 626
mehatfie 11:bc9cd2869f95 627 //Monitors the conditions to watch for erroring, and pauses system if any of the conditions turn out to be true
mehatfie 13:899db9d635e5 628 if (!interprettingErrorFlag && enableErrors){
mehatfie 11:bc9cd2869f95 629 int j = 0, error = -1, numError = 0;
mehatfie 11:bc9cd2869f95 630 for(j = 0; j < numErrorsConditions; j++){
mehatfie 11:bc9cd2869f95 631 int checkCondition = 0;
mehatfie 11:bc9cd2869f95 632
mehatfie 11:bc9cd2869f95 633 interprettingErrorFlag = 1;
mehatfie 11:bc9cd2869f95 634 checkCondition = interpretCommand(errorMonitors[j].errorToWatch);
mehatfie 11:bc9cd2869f95 635 interprettingErrorFlag = 0;
mehatfie 11:bc9cd2869f95 636
mehatfie 11:bc9cd2869f95 637 //if Condition is true, that means the error occurred
mehatfie 11:bc9cd2869f95 638 if (checkCondition == 1){
mehatfie 11:bc9cd2869f95 639 numError++;
mehatfie 11:bc9cd2869f95 640
mehatfie 11:bc9cd2869f95 641 //if the error has a Fix / Reset command, do it
mehatfie 11:bc9cd2869f95 642 if (errorMonitors[j].hasFix){
mehatfie 11:bc9cd2869f95 643 interprettingErrorFlag = 1;
mehatfie 13:899db9d635e5 644 int 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
mehatfie 11:bc9cd2869f95 662 //place all devices into the pause functionality
mehatfie 11:bc9cd2869f95 663 int i = 0;
mehatfie 11:bc9cd2869f95 664 for(i = 0; i < devices.size(); i++)
mehatfie 11:bc9cd2869f95 665 devices[i]->pause();
mehatfie 11:bc9cd2869f95 666
mehatfie 12:2e3e86714243 667 cycleTimer.stop(); //pause the cycle timer
mehatfie 12:2e3e86714243 668
mehatfie 11:bc9cd2869f95 669 //LCD has already been adjusted with the ErrorMonitor function
mehatfie 11:bc9cd2869f95 670 //Simply wait for the user to press select in order to acknowledge the issue and try to fix it
mehatfie 11:bc9cd2869f95 671 while(!buttons.readSel());
mehatfie 12:2e3e86714243 672
mehatfie 11:bc9cd2869f95 673 //place all devices into the resume functionality
mehatfie 11:bc9cd2869f95 674 for(i = 0; i < devices.size(); i++)
mehatfie 11:bc9cd2869f95 675 devices[i]->resume();
mehatfie 11:bc9cd2869f95 676
mehatfie 12:2e3e86714243 677 cycleTimer.start(); //start the cycle timer
mehatfie 12:2e3e86714243 678
mehatfie 11:bc9cd2869f95 679 lcd.cls(); //clear the display
mehatfie 11:bc9cd2869f95 680 }
mehatfie 11:bc9cd2869f95 681 }
mehatfie 11:bc9cd2869f95 682
mehatfie 2:3e7baa3e3fec 683
mehatfie 11:bc9cd2869f95 684
mehatfie 11:bc9cd2869f95 685 /******************************************************************************/
mehatfie 11:bc9cd2869f95 686 /*** <Functionality: device> ***/
mehatfie 11:bc9cd2869f95 687 /******************************************************************************/
mehatfie 11:bc9cd2869f95 688
mehatfie 2:3e7baa3e3fec 689 if (lineData.word[0].compare("device") == 0){
mehatfie 2:3e7baa3e3fec 690
mehatfie 1:5731f31f96be 691 int i = 0, deviceFound = -1;
mehatfie 2:3e7baa3e3fec 692 for (i = 0; i < numDevices; i++){
mehatfie 4:86d0d04cc055 693 if (lineData.word[2].compare(DeviceNames[i]) == 0){
mehatfie 1:5731f31f96be 694 deviceFound = i;
mehatfie 4:86d0d04cc055 695 }
mehatfie 2:3e7baa3e3fec 696 }
mehatfie 9:5a0c4c6e39c7 697
mehatfie 1:5731f31f96be 698 //if the device type does not match any known type, error out
mehatfie 1:5731f31f96be 699 if (deviceFound == -1){
mehatfie 9:5a0c4c6e39c7 700 ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 701 return -1;
mehatfie 1:5731f31f96be 702 }
mehatfie 1:5731f31f96be 703
mehatfie 1:5731f31f96be 704 //Add device to the array of devices and initialize it
mehatfie 2:3e7baa3e3fec 705 else{
mehatfie 2:3e7baa3e3fec 706 devices.push_back(Device::newDevice(deviceFound, lineData.word[1], lineData));
mehatfie 9:5a0c4c6e39c7 707 devices.back()->name = lineData.word[1];
mehatfie 9:5a0c4c6e39c7 708
mehatfie 9:5a0c4c6e39c7 709 //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 710 if (devices.back()->errorFlag == 1){
mehatfie 9:5a0c4c6e39c7 711 ErrorOut("Error initializing device", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 712 return -1;
mehatfie 9:5a0c4c6e39c7 713 }
mehatfie 2:3e7baa3e3fec 714 }
mehatfie 1:5731f31f96be 715 }
mehatfie 1:5731f31f96be 716
mehatfie 11:bc9cd2869f95 717
mehatfie 11:bc9cd2869f95 718
mehatfie 11:bc9cd2869f95 719 /******************************************************************************/
mehatfie 11:bc9cd2869f95 720 /*** <Functionality: errorWatch> ***/
mehatfie 11:bc9cd2869f95 721 /******************************************************************************/
mehatfie 11:bc9cd2869f95 722
mehatfie 11:bc9cd2869f95 723 else if (lineData.word[0].compare("errorWatch") == 0){
mehatfie 11:bc9cd2869f95 724 //line looks like: error, _Condition_, FIX(?), _Command_
mehatfie 11:bc9cd2869f95 725 //create a temp LineData variable to store the correct information of the error Condition (discluding the first "error" keyword
mehatfie 13:899db9d635e5 726 ErrorCondition tempError = ErrorCondition();
mehatfie 11:bc9cd2869f95 727 LineData tempLine;
mehatfie 11:bc9cd2869f95 728
mehatfie 11:bc9cd2869f95 729 int i = 1, funcNumWords = 0;
mehatfie 11:bc9cd2869f95 730 for (i = 1; i < lineData.numWords; i++){
mehatfie 11:bc9cd2869f95 731
mehatfie 11:bc9cd2869f95 732 //if the keyword FIX is not this word, than add it to the LineData
mehatfie 11:bc9cd2869f95 733 if (lineData.word[i].compare("FIX") != 0){
mehatfie 11:bc9cd2869f95 734 tempLine.word[funcNumWords] = lineData.word[i];
mehatfie 11:bc9cd2869f95 735 funcNumWords++;
mehatfie 11:bc9cd2869f95 736 }
mehatfie 11:bc9cd2869f95 737
mehatfie 11:bc9cd2869f95 738 //if the keyword FIX was found, this means there's a FIX / Reset command if the specified error being monitored occurs
mehatfie 11:bc9cd2869f95 739 else if (lineData.word[i].compare("FIX") == 0){
mehatfie 11:bc9cd2869f95 740 tempError.hasFix = 1;
mehatfie 11:bc9cd2869f95 741
mehatfie 11:bc9cd2869f95 742 //Save the line as the "Error to Watch" value
mehatfie 11:bc9cd2869f95 743 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 744 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 745 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 746 tempError.errorToWatch = tempLine;
mehatfie 11:bc9cd2869f95 747
mehatfie 11:bc9cd2869f95 748 //reset lineData and num words for the command / function in order to process the FIX command into the error condition
mehatfie 11:bc9cd2869f95 749 funcNumWords = 0;
mehatfie 11:bc9cd2869f95 750 tempLine = LineData(); //reset tempLine
mehatfie 11:bc9cd2869f95 751 }
mehatfie 11:bc9cd2869f95 752 }
mehatfie 11:bc9cd2869f95 753
mehatfie 11:bc9cd2869f95 754 //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 13:899db9d635e5 755 if (tempError.hasFix == 1){
mehatfie 11:bc9cd2869f95 756 //Save the line as the "Error Fix" value
mehatfie 11:bc9cd2869f95 757 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 758 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 759 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 760 tempError.errorFix = tempLine;
mehatfie 11:bc9cd2869f95 761 }
mehatfie 11:bc9cd2869f95 762
mehatfie 11:bc9cd2869f95 763 //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 764 else{
mehatfie 11:bc9cd2869f95 765 //Save the line as the "Error to Watch" value
mehatfie 11:bc9cd2869f95 766 tempLine.numWords = funcNumWords;
mehatfie 11:bc9cd2869f95 767 tempLine.lineAddress = lineData.lineAddress;
mehatfie 11:bc9cd2869f95 768 tempLine.lineNumber = lineData.lineNumber;
mehatfie 11:bc9cd2869f95 769 tempError.errorToWatch = tempLine;
mehatfie 11:bc9cd2869f95 770 }
mehatfie 11:bc9cd2869f95 771
mehatfie 11:bc9cd2869f95 772 //if DummyMode, send error Condition to the interpretCommand so that we can check the syntax
mehatfie 11:bc9cd2869f95 773 if (DummyMode){
mehatfie 11:bc9cd2869f95 774 int returnValue1 = interpretCommand(tempError.errorToWatch);
mehatfie 13:899db9d635e5 775
mehatfie 13:899db9d635e5 776 //if this error has a fix then check the syntax
mehatfie 13:899db9d635e5 777 int returnValue2 = 0;
mehatfie 13:899db9d635e5 778 if (tempError.hasFix)
mehatfie 13:899db9d635e5 779 returnValue2 = interpretCommand(tempError.errorFix);
mehatfie 13:899db9d635e5 780
mehatfie 11:bc9cd2869f95 781 if (returnValue1 == -1 || returnValue2 == -1)
mehatfie 11:bc9cd2869f95 782 return -1;
mehatfie 11:bc9cd2869f95 783 }
mehatfie 11:bc9cd2869f95 784
mehatfie 11:bc9cd2869f95 785 errorMonitors[numErrorsConditions] = tempError;
mehatfie 11:bc9cd2869f95 786 numErrorsConditions++;
mehatfie 11:bc9cd2869f95 787 }
mehatfie 11:bc9cd2869f95 788
mehatfie 11:bc9cd2869f95 789
mehatfie 11:bc9cd2869f95 790 /******************************************************************************/
mehatfie 11:bc9cd2869f95 791 /*** <Functionality: delay> ***/
mehatfie 11:bc9cd2869f95 792 /******************************************************************************/
mehatfie 11:bc9cd2869f95 793
mehatfie 2:3e7baa3e3fec 794 else if (lineData.word[0].compare("delay") == 0){
mehatfie 10:e8db892fbc52 795
mehatfie 11:bc9cd2869f95 796 if (lineData.numWords != 2){
mehatfie 11:bc9cd2869f95 797 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 798 return -1;
mehatfie 11:bc9cd2869f95 799 }
mehatfie 11:bc9cd2869f95 800
mehatfie 2:3e7baa3e3fec 801 string duration = lineData.word[1];
mehatfie 2:3e7baa3e3fec 802 int durationValue = 0;
mehatfie 9:5a0c4c6e39c7 803 int numValuesFound = sscanf(duration.c_str(), "%d", &durationValue);
mehatfie 1:5731f31f96be 804
mehatfie 9:5a0c4c6e39c7 805 if (numValuesFound < 1){
mehatfie 9:5a0c4c6e39c7 806 ErrorOut("Parameter Unknown, Duration Value can't be converted", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 807 return -1;
mehatfie 9:5a0c4c6e39c7 808 }
mehatfie 9:5a0c4c6e39c7 809
mehatfie 9:5a0c4c6e39c7 810 if (durationValue > 0){
mehatfie 11:bc9cd2869f95 811
mehatfie 11:bc9cd2869f95 812 //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 813 if (DummyMode)
mehatfie 11:bc9cd2869f95 814 return 0; //Function operated successfully but doesn't return a value
mehatfie 11:bc9cd2869f95 815
mehatfie 1:5731f31f96be 816 timer.reset();
mehatfie 1:5731f31f96be 817 timer.start();
mehatfie 1:5731f31f96be 818 while (timer.read_ms() < durationValue); //Do Nothing while the timer has not reached the duration
mehatfie 1:5731f31f96be 819 timer.stop(); //Stop the Timer
mehatfie 1:5731f31f96be 820 }
mehatfie 1:5731f31f96be 821 else{
mehatfie 11:bc9cd2869f95 822 ErrorOut("Duration value is less than or equal to 0", lineData.lineNumber);
mehatfie 1:5731f31f96be 823 return -1;
mehatfie 1:5731f31f96be 824 }
mehatfie 1:5731f31f96be 825 }
mehatfie 1:5731f31f96be 826
mehatfie 11:bc9cd2869f95 827 /******************************************************************************/
mehatfie 11:bc9cd2869f95 828 /*** <Functionality: pause> ***/
mehatfie 11:bc9cd2869f95 829 /******************************************************************************/
mehatfie 11:bc9cd2869f95 830
mehatfie 11:bc9cd2869f95 831 else if (lineData.word[0].compare("pause") == 0){
mehatfie 11:bc9cd2869f95 832
mehatfie 11:bc9cd2869f95 833 if (lineData.numWords != 1){
mehatfie 11:bc9cd2869f95 834 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 835 return -1;
mehatfie 11:bc9cd2869f95 836 }
mehatfie 11:bc9cd2869f95 837
mehatfie 11:bc9cd2869f95 838 lcd.cls(); //clear the display
mehatfie 11:bc9cd2869f95 839 lcd.setAddress(0,0);
mehatfie 11:bc9cd2869f95 840 lcd.printf("System Pause");
mehatfie 11:bc9cd2869f95 841 lcd.setAddress(0,2);
mehatfie 11:bc9cd2869f95 842 lcd.printf("Line Number: %d", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 843 lcd.setAddress(0,3);
mehatfie 11:bc9cd2869f95 844 lcd.printf("Press Sel to Resume");
mehatfie 11:bc9cd2869f95 845 while(!buttons.readSel());
mehatfie 11:bc9cd2869f95 846 }
mehatfie 11:bc9cd2869f95 847
mehatfie 11:bc9cd2869f95 848 /******************************************************************************/
mehatfie 11:bc9cd2869f95 849 /*** <Functionality: GoTo> ***/
mehatfie 11:bc9cd2869f95 850 /******************************************************************************/
mehatfie 11:bc9cd2869f95 851
mehatfie 11:bc9cd2869f95 852 else if (lineData.word[0].compare("GoTo") == 0){
mehatfie 11:bc9cd2869f95 853
mehatfie 11:bc9cd2869f95 854 if (lineData.word[1].compare("label") == 0){
mehatfie 11:bc9cd2869f95 855
mehatfie 11:bc9cd2869f95 856 if (lineData.numWords != 3){
mehatfie 11:bc9cd2869f95 857 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 858 return -1;
mehatfie 11:bc9cd2869f95 859 }
mehatfie 11:bc9cd2869f95 860
mehatfie 11:bc9cd2869f95 861 int labelExists = 0;
mehatfie 11:bc9cd2869f95 862 for(vector<GoToLabel>::iterator it=GoToLabels.begin(); it < GoToLabels.end(); it++){
mehatfie 11:bc9cd2869f95 863 if (lineData.word[1].compare((*it).name) == 0)
mehatfie 11:bc9cd2869f95 864 labelExists = 1;
mehatfie 11:bc9cd2869f95 865 }
mehatfie 11:bc9cd2869f95 866
mehatfie 11:bc9cd2869f95 867 //if the label does not exist then add it to the vector
mehatfie 11:bc9cd2869f95 868 //if it turned out to exist then do nothing
mehatfie 11:bc9cd2869f95 869 if (!labelExists){
mehatfie 11:bc9cd2869f95 870 GoToLabel tempLabel;
mehatfie 11:bc9cd2869f95 871 tempLabel.name = lineData.word[2]; //Save the Label Name
mehatfie 11:bc9cd2869f95 872 tempLabel.lineAddress = lineData.lineAddress; //Save the Line Address
mehatfie 11:bc9cd2869f95 873 tempLabel.lineNumber = lineData.lineNumber; //Save the Line Number
mehatfie 11:bc9cd2869f95 874 GoToLabels.push_back(tempLabel);
mehatfie 11:bc9cd2869f95 875 }
mehatfie 11:bc9cd2869f95 876 }
mehatfie 11:bc9cd2869f95 877
mehatfie 11:bc9cd2869f95 878 //if the second word was not "label," then it means the word should correspond to an already created GoTo Label
mehatfie 11:bc9cd2869f95 879 else{
mehatfie 11:bc9cd2869f95 880
mehatfie 11:bc9cd2869f95 881 if (lineData.numWords != 2){
mehatfie 11:bc9cd2869f95 882 ErrorOut("Incorrect number of parameters", lineData.lineNumber);
mehatfie 11:bc9cd2869f95 883 return -1;
mehatfie 11:bc9cd2869f95 884 }
mehatfie 11:bc9cd2869f95 885
mehatfie 11:bc9cd2869f95 886 //All syntax checking done by this point, if Dummy then return success in order to check the code
mehatfie 11:bc9cd2869f95 887 //Unable to continue further checking since an aspect of DummyMode run through is to collect the GoTo Label Locations
mehatfie 11:bc9cd2869f95 888 //so that we have them in address
mehatfie 11:bc9cd2869f95 889 if (DummyMode)
mehatfie 11:bc9cd2869f95 890 return 0; //Function operated successfully but doesn't return a value
mehatfie 11:bc9cd2869f95 891
mehatfie 11:bc9cd2869f95 892 int i = 0, labelFound = -1;
mehatfie 11:bc9cd2869f95 893 for(i = 0; i < GoToLabels.size(); i++){
mehatfie 11:bc9cd2869f95 894 if (lineData.word[1].compare(GoToLabels[i].name) == 0){
mehatfie 11:bc9cd2869f95 895 labelFound = i;
mehatfie 11:bc9cd2869f95 896 }
mehatfie 11:bc9cd2869f95 897 }
mehatfie 11:bc9cd2869f95 898
mehatfie 11:bc9cd2869f95 899 //if the label was not found then error out
mehatfie 11:bc9cd2869f95 900 if (labelFound == -1){
mehatfie 11:bc9cd2869f95 901 ErrorOut("No label match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 11:bc9cd2869f95 902 return -1;
mehatfie 11:bc9cd2869f95 903 }
mehatfie 11:bc9cd2869f95 904
mehatfie 11:bc9cd2869f95 905 //if the label was found, then seek to that GoTo position
mehatfie 11:bc9cd2869f95 906 else{
mehatfie 11:bc9cd2869f95 907
mehatfie 11:bc9cd2869f95 908 int seekFailure = fseek(selectedFile, GoToLabels[labelFound].lineAddress, SEEK_SET); //fseek returns 0 on success
mehatfie 11:bc9cd2869f95 909 if (seekFailure){
mehatfie 11:bc9cd2869f95 910 ErrorOut("In Loop Failed to seek line", lineData.lineNumber); //Spaces make it look nice on the LCD
mehatfie 11:bc9cd2869f95 911 return -1;
mehatfie 11:bc9cd2869f95 912 }
mehatfie 11:bc9cd2869f95 913
mehatfie 11:bc9cd2869f95 914 lineData.lineNumber = GoToLabels[labelFound].lineNumber - 1; //set the lineNumber value
mehatfie 11:bc9cd2869f95 915 }
mehatfie 11:bc9cd2869f95 916 }
mehatfie 11:bc9cd2869f95 917 }
mehatfie 11:bc9cd2869f95 918
mehatfie 11:bc9cd2869f95 919 /******************************************************************************/
mehatfie 11:bc9cd2869f95 920 /*** <Functionality: loop> ***/
mehatfie 11:bc9cd2869f95 921 /******************************************************************************/
mehatfie 11:bc9cd2869f95 922
mehatfie 13:899db9d635e5 923 else if (lineData.word[0].compare("loop") == 0){
mehatfie 13:899db9d635e5 924 enableErrors = 1;
mehatfie 11:bc9cd2869f95 925 return loopCommand(lineData); //Process the loop command and return the value that it returns
mehatfie 13:899db9d635e5 926 }
mehatfie 5:e36e0538a903 927
mehatfie 11:bc9cd2869f95 928 /******************************************************************************/
mehatfie 11:bc9cd2869f95 929 /*** <Functionality: condition> ***/
mehatfie 11:bc9cd2869f95 930 /******************************************************************************/
mehatfie 13:899db9d635e5 931 else if (lineData.word[0].compare("condition") == 0){
mehatfie 13:899db9d635e5 932 enableErrors = 1;
mehatfie 9:5a0c4c6e39c7 933 return conditionCommand(selectedFile, lineData); //Process the condition command and return the value that it returns
mehatfie 13:899db9d635e5 934 }
mehatfie 11:bc9cd2869f95 935
mehatfie 11:bc9cd2869f95 936 /******************************************************************************/
mehatfie 11:bc9cd2869f95 937 /*** <Functionality: cycle> ***/
mehatfie 11:bc9cd2869f95 938 /******************************************************************************/
mehatfie 13:899db9d635e5 939 else if (lineData.word[0].compare("cycle") == 0){
mehatfie 13:899db9d635e5 940 enableErrors = 1;
mehatfie 12:2e3e86714243 941 return cycleCommand(lineData, 1); //Sending 1 means it's initializing
mehatfie 13:899db9d635e5 942 }
mehatfie 11:bc9cd2869f95 943
mehatfie 11:bc9cd2869f95 944 /******************************************************************************/
mehatfie 11:bc9cd2869f95 945 /*** <Functionality: end> ***/
mehatfie 11:bc9cd2869f95 946 /******************************************************************************/
mehatfie 9:5a0c4c6e39c7 947 //end has custom return value for specific functions, since "end" is a common keyword amongst functions
mehatfie 9:5a0c4c6e39c7 948 else if (lineData.word[0].compare("end") == 0){
mehatfie 9:5a0c4c6e39c7 949 if (lineData.word[1].compare("program") == 0)
mehatfie 5:e36e0538a903 950 return 2;
mehatfie 9:5a0c4c6e39c7 951 else if (lineData.word[1].compare("loop") == 0)
mehatfie 5:e36e0538a903 952 return 3;
mehatfie 9:5a0c4c6e39c7 953 else if (lineData.word[1].compare("condition") == 0)
mehatfie 5:e36e0538a903 954 return 4;
mehatfie 11:bc9cd2869f95 955 else if (lineData.word[1].compare("cycle") == 0){
mehatfie 12:2e3e86714243 956 cycleCommand(lineData, 0); //Sending 0 means it's checking for the ending
mehatfie 11:bc9cd2869f95 957 return 5;
mehatfie 11:bc9cd2869f95 958 }
mehatfie 9:5a0c4c6e39c7 959
mehatfie 9:5a0c4c6e39c7 960 else{
mehatfie 9:5a0c4c6e39c7 961 ErrorOut("Unknown function ending", lineData.lineNumber);
mehatfie 9:5a0c4c6e39c7 962 return -1;
mehatfie 2:3e7baa3e3fec 963 }
mehatfie 1:5731f31f96be 964 }
mehatfie 1:5731f31f96be 965
mehatfie 11:bc9cd2869f95 966 /******************************************************************************/
mehatfie 11:bc9cd2869f95 967 /*** <Functionality: local_name Checking> ***/
mehatfie 11:bc9cd2869f95 968 /******************************************************************************/
mehatfie 11:bc9cd2869f95 969
mehatfie 1:5731f31f96be 970 //not a keyword so check if it's a localName for a device
mehatfie 1:5731f31f96be 971 else{
mehatfie 11:bc9cd2869f95 972
mehatfie 1:5731f31f96be 973 int i = 0, deviceFound = -1;
mehatfie 13:899db9d635e5 974 for (i = 0; i < devices.size(); i++){
mehatfie 2:3e7baa3e3fec 975 if (lineData.word[0].compare(devices[i]->name) == 0)
mehatfie 1:5731f31f96be 976 deviceFound = i;
mehatfie 1:5731f31f96be 977 }
mehatfie 1:5731f31f96be 978
mehatfie 1:5731f31f96be 979 //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 980 if (deviceFound == -1){
mehatfie 9:5a0c4c6e39c7 981 ErrorOut("No device match found in the system", lineData.lineNumber); //Spaces make it look nice on LCD
mehatfie 9:5a0c4c6e39c7 982 return -1;
mehatfie 13:899db9d635e5 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