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:
Tue Sep 23 23:42:32 2014 +0000
Revision:
6:d1594fd2ec5a
Parent:
5:e36e0538a903
Child:
7:cca801103b86
- System Functional for Door_Latch_Test; - Fixed issue with Motors, needed to initialize the Motors for PWM; - Confirmed code works to read from SD Card that has spaces and tabs

Who changed what in which revision?

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