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