A scripting environment used to define precise output/input temporal relationships.
Dependencies: SMARTWAV mbed HelloWorld
Dependents: perturbRoom_legacy
Fork of HelloWorld by
behave.cpp@4:34aca2142df9, 2014-09-25 (annotated)
- Committer:
- mkarlsso
- Date:
- Thu Sep 25 23:42:30 2014 +0000
- Revision:
- 4:34aca2142df9
- Parent:
- 3:ae33b7f5a7c1
Longer script loading without hanging due to memory limitations
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mkarlsso | 2:298679fff37c | 1 | #include "behave.h" |
mkarlsso | 2:298679fff37c | 2 | #include <ctype.h> |
mkarlsso | 2:298679fff37c | 3 | #include <sstream> |
mkarlsso | 2:298679fff37c | 4 | |
mkarlsso | 4:34aca2142df9 | 5 | int16_t randomSeedCounter; //used for seeding random numbers |
mkarlsso | 4:34aca2142df9 | 6 | |
mkarlsso | 2:298679fff37c | 7 | // These external symbols are maintained by the linker to indicate the |
mkarlsso | 2:298679fff37c | 8 | // location of various regions in the device's memory. They will be used by |
mkarlsso | 2:298679fff37c | 9 | // DisplayRAMBanks() to dump the size of each RAM bank to stdout. |
mkarlsso | 2:298679fff37c | 10 | extern unsigned int Image$$RW_IRAM1$$Base; |
mkarlsso | 2:298679fff37c | 11 | extern unsigned int Image$$RW_IRAM1$$ZI$$Limit; |
mkarlsso | 2:298679fff37c | 12 | extern unsigned int Image$$RW_IRAM2$$Base; |
mkarlsso | 2:298679fff37c | 13 | extern unsigned int Image$$RW_IRAM2$$ZI$$Limit; |
mkarlsso | 2:298679fff37c | 14 | extern unsigned int Image$$RW_IRAM3$$Base; |
mkarlsso | 2:298679fff37c | 15 | extern unsigned int Image$$RW_IRAM3$$ZI$$Limit; |
mkarlsso | 2:298679fff37c | 16 | |
mkarlsso | 2:298679fff37c | 17 | uint32_t* globalTimeKeeperPtr; //this is a pointer to the slave timer (do not change this directly) |
mkarlsso | 2:298679fff37c | 18 | extern eventQueue mainQueue; |
mkarlsso | 2:298679fff37c | 19 | extern digitalPort* portVector[]; |
mkarlsso | 2:298679fff37c | 20 | |
mkarlsso | 2:298679fff37c | 21 | extern Serial pc; |
mkarlsso | 2:298679fff37c | 22 | extern bool resetTimer; |
mkarlsso | 2:298679fff37c | 23 | extern bool clockSlave; |
mkarlsso | 2:298679fff37c | 24 | extern bool changeToSlave; |
mkarlsso | 2:298679fff37c | 25 | extern bool changeToStandAlone; |
mkarlsso | 2:298679fff37c | 26 | extern outputStream textDisplay; |
mkarlsso | 2:298679fff37c | 27 | extern int currentDIOstate[2]; |
mkarlsso | 2:298679fff37c | 28 | extern bool broadCastStateChanges; |
mkarlsso | 2:298679fff37c | 29 | extern bool textStreaming; |
mkarlsso | 2:298679fff37c | 30 | |
mkarlsso | 2:298679fff37c | 31 | |
mkarlsso | 2:298679fff37c | 32 | //static pools of memory for each object type |
mkarlsso | 2:298679fff37c | 33 | extern event eventBlock[NUMEVENTS]; |
mkarlsso | 2:298679fff37c | 34 | extern condition conditionBlock[NUMCONDITIONS]; |
mkarlsso | 2:298679fff37c | 35 | extern intCompare intCompareBlock[NUMINTCOMPARE]; |
mkarlsso | 2:298679fff37c | 36 | extern action actionBlock[NUMACTIONS]; |
mkarlsso | 2:298679fff37c | 37 | extern portMessage portMessageBlock[NUMPORTMESSAGES]; |
mkarlsso | 2:298679fff37c | 38 | extern intOperation intOperationBlock[NUMINTOPERATIONS]; |
mkarlsso | 2:298679fff37c | 39 | extern displayAction displayActionBlock[NUMDISPLAYACTIONS]; |
mkarlsso | 2:298679fff37c | 40 | |
mkarlsso | 2:298679fff37c | 41 | //used to find the first available object in the staticly defined pools of memory |
mkarlsso | 2:298679fff37c | 42 | template<class T> T* findFirstUnUsed(T a[], const int arrayLength) { |
mkarlsso | 2:298679fff37c | 43 | |
mkarlsso | 2:298679fff37c | 44 | T* returnPtr = NULL; |
mkarlsso | 2:298679fff37c | 45 | for (int i = 0; i < arrayLength; i++) { |
mkarlsso | 2:298679fff37c | 46 | if (!a[i].isUsed) { |
mkarlsso | 2:298679fff37c | 47 | returnPtr = &a[i]; |
mkarlsso | 2:298679fff37c | 48 | break; |
mkarlsso | 2:298679fff37c | 49 | } |
mkarlsso | 2:298679fff37c | 50 | } |
mkarlsso | 2:298679fff37c | 51 | return returnPtr; |
mkarlsso | 2:298679fff37c | 52 | } |
mkarlsso | 2:298679fff37c | 53 | |
mkarlsso | 2:298679fff37c | 54 | //used to count the number of available objects in the memory pool |
mkarlsso | 2:298679fff37c | 55 | template<class T> int countUnUsed(T a[], const int arrayLength) { |
mkarlsso | 2:298679fff37c | 56 | |
mkarlsso | 2:298679fff37c | 57 | int count = 0; |
mkarlsso | 2:298679fff37c | 58 | for (int i = 0; i < arrayLength; i++) { |
mkarlsso | 2:298679fff37c | 59 | if (!a[i].isUsed) { |
mkarlsso | 2:298679fff37c | 60 | count++; |
mkarlsso | 2:298679fff37c | 61 | } |
mkarlsso | 2:298679fff37c | 62 | } |
mkarlsso | 2:298679fff37c | 63 | return count; |
mkarlsso | 2:298679fff37c | 64 | } |
mkarlsso | 2:298679fff37c | 65 | |
mkarlsso | 2:298679fff37c | 66 | |
mkarlsso | 2:298679fff37c | 67 | void displayMemoryLeft(void) { |
mkarlsso | 2:298679fff37c | 68 | pc.printf("Available slots left in memory\r\n"); |
mkarlsso | 2:298679fff37c | 69 | pc.printf(" Blocks: %d\r\n", countUnUsed(eventBlock, NUMEVENTS)); |
mkarlsso | 2:298679fff37c | 70 | pc.printf(" Condition containers: %d\r\n", countUnUsed(conditionBlock, NUMCONDITIONS)); |
mkarlsso | 2:298679fff37c | 71 | pc.printf(" Int compare conditions: %d\r\n", countUnUsed(intCompareBlock, NUMINTCOMPARE)); |
mkarlsso | 2:298679fff37c | 72 | pc.printf(" Command containers: %d\r\n", countUnUsed(actionBlock, NUMACTIONS)); |
mkarlsso | 2:298679fff37c | 73 | pc.printf(" Port commands: %d\r\n", countUnUsed(portMessageBlock, NUMPORTMESSAGES)); |
mkarlsso | 2:298679fff37c | 74 | pc.printf(" Integer operater commands: %d\r\n", countUnUsed(intOperationBlock, NUMINTOPERATIONS)); |
mkarlsso | 2:298679fff37c | 75 | pc.printf(" Text display commands: %d\r\n", countUnUsed(displayActionBlock, NUMDISPLAYACTIONS)); |
mkarlsso | 2:298679fff37c | 76 | } |
mkarlsso | 2:298679fff37c | 77 | |
mkarlsso | 2:298679fff37c | 78 | // Displays the size of static allocations for each RAM bank as indicated by |
mkarlsso | 2:298679fff37c | 79 | // ARM linker to stdout. |
mkarlsso | 2:298679fff37c | 80 | static void DisplayRAMBanks(void) { |
mkarlsso | 2:298679fff37c | 81 | |
mkarlsso | 2:298679fff37c | 82 | pc.printf("Static RAM bank allocations\r\n"); |
mkarlsso | 2:298679fff37c | 83 | pc.printf(" Main RAM = %u\r\n", (unsigned int)&Image$$RW_IRAM1$$ZI$$Limit - |
mkarlsso | 2:298679fff37c | 84 | (unsigned int)&Image$$RW_IRAM1$$Base); |
mkarlsso | 2:298679fff37c | 85 | pc.printf(" RAM0 = %u\r\n", (unsigned int)&Image$$RW_IRAM2$$ZI$$Limit - |
mkarlsso | 2:298679fff37c | 86 | (unsigned int)&Image$$RW_IRAM2$$Base); |
mkarlsso | 2:298679fff37c | 87 | pc.printf(" RAM1 = %u\r\n", (unsigned int)&Image$$RW_IRAM3$$ZI$$Limit - |
mkarlsso | 2:298679fff37c | 88 | (unsigned int)&Image$$RW_IRAM3$$Base); |
mkarlsso | 2:298679fff37c | 89 | } |
mkarlsso | 2:298679fff37c | 90 | |
mkarlsso | 2:298679fff37c | 91 | |
mkarlsso | 2:298679fff37c | 92 | bool isNumber(const std::string& s) { |
mkarlsso | 2:298679fff37c | 93 | std::string::const_iterator it = s.begin(); |
mkarlsso | 2:298679fff37c | 94 | while (it != s.end() && isdigit(*it)) ++it; |
mkarlsso | 2:298679fff37c | 95 | return !s.empty() && it == s.end(); |
mkarlsso | 2:298679fff37c | 96 | } |
mkarlsso | 2:298679fff37c | 97 | |
mkarlsso | 2:298679fff37c | 98 | void tokenize(const string& str, |
mkarlsso | 2:298679fff37c | 99 | vector<string>& tokens, |
mkarlsso | 2:298679fff37c | 100 | const string& delimiters = " ") |
mkarlsso | 2:298679fff37c | 101 | { |
mkarlsso | 2:298679fff37c | 102 | // Skip delimiters at beginning. |
mkarlsso | 2:298679fff37c | 103 | string::size_type lastPos = str.find_first_not_of(delimiters, 0); |
mkarlsso | 2:298679fff37c | 104 | // Find first "non-delimiter". |
mkarlsso | 2:298679fff37c | 105 | string::size_type pos = str.find_first_of(delimiters, lastPos); |
mkarlsso | 2:298679fff37c | 106 | |
mkarlsso | 2:298679fff37c | 107 | while (string::npos != pos || string::npos != lastPos) |
mkarlsso | 2:298679fff37c | 108 | { |
mkarlsso | 2:298679fff37c | 109 | // Found a token, add it to the vector. |
mkarlsso | 2:298679fff37c | 110 | tokens.push_back(str.substr(lastPos, pos - lastPos)); |
mkarlsso | 2:298679fff37c | 111 | // Skip delimiters. Note the "not_of" |
mkarlsso | 2:298679fff37c | 112 | lastPos = str.find_first_not_of(delimiters, pos); |
mkarlsso | 2:298679fff37c | 113 | // Find next "non-delimiter" |
mkarlsso | 2:298679fff37c | 114 | pos = str.find_first_of(delimiters, lastPos); |
mkarlsso | 2:298679fff37c | 115 | } |
mkarlsso | 2:298679fff37c | 116 | } |
mkarlsso | 2:298679fff37c | 117 | |
mkarlsso | 2:298679fff37c | 118 | |
mkarlsso | 2:298679fff37c | 119 | digitalPort::digitalPort(DigitalOut* DOP, DigitalIn* DIP): |
mkarlsso | 2:298679fff37c | 120 | outPin(DOP), |
mkarlsso | 2:298679fff37c | 121 | inPin(DIP), |
mkarlsso | 2:298679fff37c | 122 | outState(0){ |
mkarlsso | 2:298679fff37c | 123 | |
mkarlsso | 2:298679fff37c | 124 | lastInState = getDigitalIn(); |
mkarlsso | 2:298679fff37c | 125 | inState = getDigitalIn(); |
mkarlsso | 2:298679fff37c | 126 | lastChangeTime = 0; |
mkarlsso | 2:298679fff37c | 127 | lastOutChangeTime = 0; |
mkarlsso | 2:298679fff37c | 128 | lastChangeInterval = 0; |
mkarlsso | 2:298679fff37c | 129 | lastDownEvent.triggered = false; |
mkarlsso | 2:298679fff37c | 130 | lastUpEvent.triggered = false; |
mkarlsso | 2:298679fff37c | 131 | triggerUpEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 132 | triggerDownEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 133 | outStateChanged = false; |
mkarlsso | 2:298679fff37c | 134 | } |
mkarlsso | 2:298679fff37c | 135 | |
mkarlsso | 2:298679fff37c | 136 | void digitalPort::setTriggerUpEvent(event* eventInput) { |
mkarlsso | 2:298679fff37c | 137 | if (triggerUpEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 138 | //delete triggerUpEventPtr; |
mkarlsso | 2:298679fff37c | 139 | triggerUpEventPtr->release(); |
mkarlsso | 2:298679fff37c | 140 | } |
mkarlsso | 2:298679fff37c | 141 | triggerUpEventPtr = eventInput; |
mkarlsso | 2:298679fff37c | 142 | } |
mkarlsso | 2:298679fff37c | 143 | |
mkarlsso | 2:298679fff37c | 144 | void digitalPort::setTriggerDownEvent(event* eventInput) { |
mkarlsso | 2:298679fff37c | 145 | if (triggerDownEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 146 | //delete triggerDownEventPtr; |
mkarlsso | 2:298679fff37c | 147 | triggerDownEventPtr->release(); |
mkarlsso | 2:298679fff37c | 148 | |
mkarlsso | 2:298679fff37c | 149 | } |
mkarlsso | 2:298679fff37c | 150 | triggerDownEventPtr = eventInput; |
mkarlsso | 2:298679fff37c | 151 | } |
mkarlsso | 2:298679fff37c | 152 | |
mkarlsso | 2:298679fff37c | 153 | |
mkarlsso | 2:298679fff37c | 154 | int digitalPort::getDigitalIn() { |
mkarlsso | 2:298679fff37c | 155 | return inPin->read(); |
mkarlsso | 2:298679fff37c | 156 | } |
mkarlsso | 2:298679fff37c | 157 | |
mkarlsso | 2:298679fff37c | 158 | |
mkarlsso | 2:298679fff37c | 159 | void digitalPort::setDigitalOut(int outVal) { |
mkarlsso | 2:298679fff37c | 160 | if (outVal == -1) { |
mkarlsso | 2:298679fff37c | 161 | outVal = 1-outState; |
mkarlsso | 2:298679fff37c | 162 | } |
mkarlsso | 2:298679fff37c | 163 | outPin->write(outVal); |
mkarlsso | 2:298679fff37c | 164 | if (outState != outVal) { |
mkarlsso | 2:298679fff37c | 165 | outStateChanged = true; |
mkarlsso | 2:298679fff37c | 166 | lastOutChangeTime = *globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 167 | } |
mkarlsso | 2:298679fff37c | 168 | outState = outVal; |
mkarlsso | 2:298679fff37c | 169 | } |
mkarlsso | 2:298679fff37c | 170 | |
mkarlsso | 2:298679fff37c | 171 | //called when a digital in gets triggered |
mkarlsso | 2:298679fff37c | 172 | void digitalPort::addStateChange(int newState, uint32_t timeStamp) { |
mkarlsso | 2:298679fff37c | 173 | |
mkarlsso | 2:298679fff37c | 174 | if ((newState == 0) && (!lastDownEvent.triggered)){ |
mkarlsso | 2:298679fff37c | 175 | lastDownEvent.timeStamp = timeStamp; |
mkarlsso | 2:298679fff37c | 176 | lastDownEvent.triggered = true; |
mkarlsso | 2:298679fff37c | 177 | } else if ((newState == 1) && (!lastUpEvent.triggered)) { |
mkarlsso | 2:298679fff37c | 178 | lastUpEvent.timeStamp = timeStamp; |
mkarlsso | 2:298679fff37c | 179 | lastUpEvent.triggered = true; |
mkarlsso | 2:298679fff37c | 180 | } |
mkarlsso | 2:298679fff37c | 181 | } |
mkarlsso | 2:298679fff37c | 182 | |
mkarlsso | 2:298679fff37c | 183 | bool digitalPort::update() { |
mkarlsso | 2:298679fff37c | 184 | |
mkarlsso | 2:298679fff37c | 185 | bool changed = false; |
mkarlsso | 2:298679fff37c | 186 | if ((*globalTimeKeeperPtr - lastChangeTime) > 1) { //prevents flutter triggers when button is pressed |
mkarlsso | 2:298679fff37c | 187 | inState = getDigitalIn(); |
mkarlsso | 2:298679fff37c | 188 | changed = (lastInState != inState); |
mkarlsso | 2:298679fff37c | 189 | if (changed) { |
mkarlsso | 2:298679fff37c | 190 | if (inState == 1) { |
mkarlsso | 2:298679fff37c | 191 | if (lastUpEvent.triggered) { |
mkarlsso | 2:298679fff37c | 192 | //there were hardware triggered since the last main loop. We use that time |
mkarlsso | 2:298679fff37c | 193 | lastChangeInterval = lastUpEvent.timeStamp - lastChangeTime; |
mkarlsso | 2:298679fff37c | 194 | lastChangeTime = lastUpEvent.timeStamp; |
mkarlsso | 2:298679fff37c | 195 | } else { |
mkarlsso | 2:298679fff37c | 196 | //otherwise we use the current time |
mkarlsso | 2:298679fff37c | 197 | lastChangeInterval = *globalTimeKeeperPtr - lastChangeTime; |
mkarlsso | 2:298679fff37c | 198 | lastChangeTime = *globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 199 | } |
mkarlsso | 2:298679fff37c | 200 | if (triggerUpEventPtr != NULL) {triggerUpEventPtr->execute();} |
mkarlsso | 2:298679fff37c | 201 | } else if (inState == 0) { |
mkarlsso | 2:298679fff37c | 202 | if (lastDownEvent.triggered) { |
mkarlsso | 2:298679fff37c | 203 | lastChangeInterval = lastDownEvent.timeStamp - lastChangeTime; |
mkarlsso | 2:298679fff37c | 204 | lastChangeTime = lastDownEvent.timeStamp; |
mkarlsso | 2:298679fff37c | 205 | } else { |
mkarlsso | 2:298679fff37c | 206 | lastChangeInterval = *globalTimeKeeperPtr - lastChangeTime; |
mkarlsso | 2:298679fff37c | 207 | lastChangeTime = *globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 208 | } |
mkarlsso | 2:298679fff37c | 209 | if (triggerDownEventPtr != NULL) {triggerDownEventPtr->execute();} |
mkarlsso | 2:298679fff37c | 210 | } |
mkarlsso | 2:298679fff37c | 211 | |
mkarlsso | 2:298679fff37c | 212 | } |
mkarlsso | 2:298679fff37c | 213 | |
mkarlsso | 2:298679fff37c | 214 | lastInState = inState; |
mkarlsso | 2:298679fff37c | 215 | |
mkarlsso | 2:298679fff37c | 216 | } |
mkarlsso | 2:298679fff37c | 217 | lastUpEvent.triggered = false; |
mkarlsso | 2:298679fff37c | 218 | lastDownEvent.triggered = false; |
mkarlsso | 2:298679fff37c | 219 | return changed; |
mkarlsso | 2:298679fff37c | 220 | } |
mkarlsso | 2:298679fff37c | 221 | |
mkarlsso | 2:298679fff37c | 222 | int digitalPort::getLastChangeState() { |
mkarlsso | 2:298679fff37c | 223 | return lastInState; |
mkarlsso | 2:298679fff37c | 224 | } |
mkarlsso | 2:298679fff37c | 225 | uint32_t digitalPort::getTimeSinceLastChange() { |
mkarlsso | 2:298679fff37c | 226 | return lastChangeInterval; |
mkarlsso | 2:298679fff37c | 227 | |
mkarlsso | 2:298679fff37c | 228 | } |
mkarlsso | 2:298679fff37c | 229 | |
mkarlsso | 2:298679fff37c | 230 | intVariable::intVariable(): |
mkarlsso | 2:298679fff37c | 231 | value(0), |
mkarlsso | 2:298679fff37c | 232 | tag(string("--------------------")) { |
mkarlsso | 2:298679fff37c | 233 | isUsed = false; |
mkarlsso | 2:298679fff37c | 234 | } |
mkarlsso | 2:298679fff37c | 235 | |
mkarlsso | 2:298679fff37c | 236 | intVariable::intVariable(string tagInput, int initialValue): |
mkarlsso | 2:298679fff37c | 237 | value(initialValue), |
mkarlsso | 2:298679fff37c | 238 | tag(string(tagInput)) { |
mkarlsso | 2:298679fff37c | 239 | isUsed = true; |
mkarlsso | 2:298679fff37c | 240 | } |
mkarlsso | 2:298679fff37c | 241 | |
mkarlsso | 2:298679fff37c | 242 | void intVariable::set(string tagInput, int initialValue) { |
mkarlsso | 2:298679fff37c | 243 | value = initialValue; |
mkarlsso | 2:298679fff37c | 244 | tag = string(tagInput); |
mkarlsso | 2:298679fff37c | 245 | isUsed = true; |
mkarlsso | 2:298679fff37c | 246 | } |
mkarlsso | 2:298679fff37c | 247 | |
mkarlsso | 2:298679fff37c | 248 | displayAction::displayAction(): |
mkarlsso | 2:298679fff37c | 249 | dText(string("--------------------------------------------------")), |
mkarlsso | 2:298679fff37c | 250 | pcPtr(NULL) { |
mkarlsso | 2:298679fff37c | 251 | dVariable = NULL; |
mkarlsso | 2:298679fff37c | 252 | isUsed = false; |
mkarlsso | 2:298679fff37c | 253 | } |
mkarlsso | 2:298679fff37c | 254 | |
mkarlsso | 2:298679fff37c | 255 | void displayAction::release() { |
mkarlsso | 2:298679fff37c | 256 | dText = "--------------------------------------------------"; |
mkarlsso | 2:298679fff37c | 257 | pcPtr = NULL; |
mkarlsso | 2:298679fff37c | 258 | dVariable = NULL; |
mkarlsso | 2:298679fff37c | 259 | isUsed = false; |
mkarlsso | 2:298679fff37c | 260 | } |
mkarlsso | 2:298679fff37c | 261 | |
mkarlsso | 2:298679fff37c | 262 | displayAction::displayAction(int* variable, string varNameInput, Serial* pcPtrInput): |
mkarlsso | 2:298679fff37c | 263 | dVariable(variable), |
mkarlsso | 2:298679fff37c | 264 | dText(varNameInput), |
mkarlsso | 2:298679fff37c | 265 | pcPtr(pcPtrInput) { |
mkarlsso | 2:298679fff37c | 266 | isUsed = true; |
mkarlsso | 2:298679fff37c | 267 | |
mkarlsso | 2:298679fff37c | 268 | } |
mkarlsso | 2:298679fff37c | 269 | |
mkarlsso | 2:298679fff37c | 270 | displayAction::displayAction(string text, Serial* pcPtrInput): |
mkarlsso | 2:298679fff37c | 271 | dText(text), |
mkarlsso | 2:298679fff37c | 272 | pcPtr(pcPtrInput) { |
mkarlsso | 2:298679fff37c | 273 | dVariable = NULL; |
mkarlsso | 2:298679fff37c | 274 | isUsed = true; |
mkarlsso | 2:298679fff37c | 275 | } |
mkarlsso | 2:298679fff37c | 276 | |
mkarlsso | 2:298679fff37c | 277 | void displayAction::set(int* variable, string varNameInput, Serial* pcPtrInput) { |
mkarlsso | 2:298679fff37c | 278 | dVariable = variable; |
mkarlsso | 2:298679fff37c | 279 | dText = varNameInput; |
mkarlsso | 2:298679fff37c | 280 | pcPtr = pcPtrInput; |
mkarlsso | 2:298679fff37c | 281 | isUsed = true; |
mkarlsso | 2:298679fff37c | 282 | |
mkarlsso | 2:298679fff37c | 283 | } |
mkarlsso | 2:298679fff37c | 284 | |
mkarlsso | 2:298679fff37c | 285 | void displayAction::set(string text, Serial* pcPtrInput) { |
mkarlsso | 2:298679fff37c | 286 | dText = text; |
mkarlsso | 2:298679fff37c | 287 | pcPtr = pcPtrInput; |
mkarlsso | 2:298679fff37c | 288 | dVariable = NULL; |
mkarlsso | 2:298679fff37c | 289 | isUsed = true; |
mkarlsso | 2:298679fff37c | 290 | } |
mkarlsso | 2:298679fff37c | 291 | |
mkarlsso | 2:298679fff37c | 292 | void displayAction::execute() { |
mkarlsso | 2:298679fff37c | 293 | |
mkarlsso | 2:298679fff37c | 294 | ostringstream convert; // stream used for the conversion |
mkarlsso | 2:298679fff37c | 295 | convert << *globalTimeKeeperPtr; // insert the textual representation of 'Number' in the characters in the stream |
mkarlsso | 2:298679fff37c | 296 | |
mkarlsso | 2:298679fff37c | 297 | if (dVariable != NULL) { |
mkarlsso | 2:298679fff37c | 298 | ostringstream varConvert; |
mkarlsso | 2:298679fff37c | 299 | varConvert << *dVariable; |
mkarlsso | 2:298679fff37c | 300 | //pcPtr->printf("%d %s = %d\r\n",*globalTimeKeeperPtr, dText.data(),*dVariable); |
mkarlsso | 2:298679fff37c | 301 | textDisplay.send(convert.str() + " " + dText + " = " + varConvert.str() + "\r\n"); |
mkarlsso | 2:298679fff37c | 302 | } else { |
mkarlsso | 2:298679fff37c | 303 | //pcPtr->printf("%d %s\r\n",*globalTimeKeeperPtr, dText.data()); |
mkarlsso | 2:298679fff37c | 304 | textDisplay.send(convert.str() + " " + dText + "\r\n"); |
mkarlsso | 2:298679fff37c | 305 | |
mkarlsso | 2:298679fff37c | 306 | } |
mkarlsso | 2:298679fff37c | 307 | } |
mkarlsso | 2:298679fff37c | 308 | |
mkarlsso | 2:298679fff37c | 309 | intCompare::intCompare(): |
mkarlsso | 2:298679fff37c | 310 | port(NULL) { |
mkarlsso | 2:298679fff37c | 311 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 312 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 313 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 314 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 315 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 316 | isUsed = false; |
mkarlsso | 2:298679fff37c | 317 | |
mkarlsso | 2:298679fff37c | 318 | } |
mkarlsso | 2:298679fff37c | 319 | intCompare::intCompare(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse): |
mkarlsso | 2:298679fff37c | 320 | port(portInput) { |
mkarlsso | 2:298679fff37c | 321 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 322 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 323 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 324 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 325 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 326 | isUsed = true; |
mkarlsso | 2:298679fff37c | 327 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 328 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 329 | } else { |
mkarlsso | 2:298679fff37c | 330 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 331 | } |
mkarlsso | 2:298679fff37c | 332 | } |
mkarlsso | 2:298679fff37c | 333 | |
mkarlsso | 2:298679fff37c | 334 | intCompare::intCompare(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse): |
mkarlsso | 2:298679fff37c | 335 | port(portInput), |
mkarlsso | 2:298679fff37c | 336 | cmpVal(cmpIntVarInput) { |
mkarlsso | 2:298679fff37c | 337 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 338 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 339 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 340 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 341 | isUsed = true; |
mkarlsso | 2:298679fff37c | 342 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 343 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 344 | } else { |
mkarlsso | 2:298679fff37c | 345 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 346 | } |
mkarlsso | 2:298679fff37c | 347 | } |
mkarlsso | 2:298679fff37c | 348 | |
mkarlsso | 2:298679fff37c | 349 | intCompare::intCompare(int* intVarInput, const char* cmpString, int* cmpIntVarInput): |
mkarlsso | 2:298679fff37c | 350 | cmpVal(cmpIntVarInput), |
mkarlsso | 2:298679fff37c | 351 | intVal(intVarInput) { |
mkarlsso | 2:298679fff37c | 352 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 353 | port = NULL; |
mkarlsso | 2:298679fff37c | 354 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 355 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 356 | isUsed = true; |
mkarlsso | 2:298679fff37c | 357 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 358 | } |
mkarlsso | 2:298679fff37c | 359 | |
mkarlsso | 2:298679fff37c | 360 | intCompare::intCompare(int* intVarInput, const char* cmpString, int cmpValInput): |
mkarlsso | 2:298679fff37c | 361 | intVal(intVarInput){ |
mkarlsso | 2:298679fff37c | 362 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 363 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 364 | port = NULL; |
mkarlsso | 2:298679fff37c | 365 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 366 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 367 | isUsed = true; |
mkarlsso | 2:298679fff37c | 368 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 369 | } |
mkarlsso | 2:298679fff37c | 370 | |
mkarlsso | 2:298679fff37c | 371 | intCompare::intCompare(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput): |
mkarlsso | 2:298679fff37c | 372 | intVal(intVarInput) { |
mkarlsso | 2:298679fff37c | 373 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 374 | port = NULL; |
mkarlsso | 2:298679fff37c | 375 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 376 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 377 | intOp = cmpIntOpInput; |
mkarlsso | 2:298679fff37c | 378 | isUsed = true; |
mkarlsso | 2:298679fff37c | 379 | setPointer_operation(cmpString); |
mkarlsso | 2:298679fff37c | 380 | } |
mkarlsso | 2:298679fff37c | 381 | |
mkarlsso | 2:298679fff37c | 382 | intCompare::intCompare(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse): |
mkarlsso | 2:298679fff37c | 383 | port(portInput) { |
mkarlsso | 2:298679fff37c | 384 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 385 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 386 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 387 | intOp = cmpIntOpInput; |
mkarlsso | 2:298679fff37c | 388 | setPointer_operation(cmpString); |
mkarlsso | 2:298679fff37c | 389 | isUsed = true; |
mkarlsso | 2:298679fff37c | 390 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 391 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 392 | } else { |
mkarlsso | 2:298679fff37c | 393 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 394 | } |
mkarlsso | 2:298679fff37c | 395 | } |
mkarlsso | 2:298679fff37c | 396 | |
mkarlsso | 2:298679fff37c | 397 | |
mkarlsso | 2:298679fff37c | 398 | |
mkarlsso | 2:298679fff37c | 399 | void intCompare::set(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse) { |
mkarlsso | 2:298679fff37c | 400 | port = portInput; |
mkarlsso | 2:298679fff37c | 401 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 402 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 403 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 404 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 405 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 406 | isUsed = true; |
mkarlsso | 2:298679fff37c | 407 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 408 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 409 | } else { |
mkarlsso | 2:298679fff37c | 410 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 411 | } |
mkarlsso | 2:298679fff37c | 412 | } |
mkarlsso | 2:298679fff37c | 413 | |
mkarlsso | 2:298679fff37c | 414 | void intCompare::set(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse) { |
mkarlsso | 2:298679fff37c | 415 | port = portInput; |
mkarlsso | 2:298679fff37c | 416 | cmpVal = cmpIntVarInput; |
mkarlsso | 2:298679fff37c | 417 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 418 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 419 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 420 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 421 | isUsed = true; |
mkarlsso | 2:298679fff37c | 422 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 423 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 424 | } else { |
mkarlsso | 2:298679fff37c | 425 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 426 | } |
mkarlsso | 2:298679fff37c | 427 | } |
mkarlsso | 2:298679fff37c | 428 | |
mkarlsso | 2:298679fff37c | 429 | void intCompare::set(int* intVarInput, const char* cmpString, int* cmpIntVarInput) { |
mkarlsso | 2:298679fff37c | 430 | cmpVal = cmpIntVarInput; |
mkarlsso | 2:298679fff37c | 431 | intVal = intVarInput; |
mkarlsso | 2:298679fff37c | 432 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 433 | port = NULL; |
mkarlsso | 2:298679fff37c | 434 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 435 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 436 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 437 | isUsed = true; |
mkarlsso | 2:298679fff37c | 438 | } |
mkarlsso | 2:298679fff37c | 439 | |
mkarlsso | 2:298679fff37c | 440 | void intCompare::set(int* intVarInput, const char* cmpString, int cmpValInput) { |
mkarlsso | 2:298679fff37c | 441 | intVal = intVarInput; |
mkarlsso | 2:298679fff37c | 442 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 443 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 444 | port = NULL; |
mkarlsso | 2:298679fff37c | 445 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 446 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 447 | setPointer(cmpString); |
mkarlsso | 2:298679fff37c | 448 | isUsed = true; |
mkarlsso | 2:298679fff37c | 449 | } |
mkarlsso | 2:298679fff37c | 450 | |
mkarlsso | 2:298679fff37c | 451 | void intCompare::set(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput) { |
mkarlsso | 2:298679fff37c | 452 | intVal = intVarInput; |
mkarlsso | 2:298679fff37c | 453 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 454 | port = NULL; |
mkarlsso | 2:298679fff37c | 455 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 456 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 457 | intOp = cmpIntOpInput; |
mkarlsso | 2:298679fff37c | 458 | setPointer_operation(cmpString); |
mkarlsso | 2:298679fff37c | 459 | isUsed = true; |
mkarlsso | 2:298679fff37c | 460 | } |
mkarlsso | 2:298679fff37c | 461 | |
mkarlsso | 2:298679fff37c | 462 | void intCompare::set(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse) { |
mkarlsso | 2:298679fff37c | 463 | port = portInput; |
mkarlsso | 2:298679fff37c | 464 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 465 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 466 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 467 | intOp = cmpIntOpInput; |
mkarlsso | 2:298679fff37c | 468 | setPointer_operation(cmpString); |
mkarlsso | 2:298679fff37c | 469 | isUsed = true; |
mkarlsso | 2:298679fff37c | 470 | if (whichToUse == 1) { |
mkarlsso | 2:298679fff37c | 471 | portValPtr = &port->inState; |
mkarlsso | 2:298679fff37c | 472 | } else { |
mkarlsso | 2:298679fff37c | 473 | portValPtr = &port->outState; |
mkarlsso | 2:298679fff37c | 474 | } |
mkarlsso | 2:298679fff37c | 475 | } |
mkarlsso | 2:298679fff37c | 476 | |
mkarlsso | 2:298679fff37c | 477 | |
mkarlsso | 2:298679fff37c | 478 | |
mkarlsso | 2:298679fff37c | 479 | intCompare::~intCompare() { |
mkarlsso | 2:298679fff37c | 480 | if (!cmpValGlobal) delete cmpVal; //we only delete the intCompare object if it was created locally |
mkarlsso | 2:298679fff37c | 481 | delete intOp; |
mkarlsso | 2:298679fff37c | 482 | } |
mkarlsso | 2:298679fff37c | 483 | |
mkarlsso | 2:298679fff37c | 484 | void intCompare::release() { |
mkarlsso | 2:298679fff37c | 485 | if (!cmpValGlobal) delete cmpVal; //we only delete the intCompare object if it was created locally |
mkarlsso | 2:298679fff37c | 486 | if (intOp != NULL) { |
mkarlsso | 2:298679fff37c | 487 | intOp->release(); |
mkarlsso | 2:298679fff37c | 488 | } |
mkarlsso | 2:298679fff37c | 489 | port = NULL; |
mkarlsso | 2:298679fff37c | 490 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 491 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 492 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 493 | intOp = NULL; |
mkarlsso | 2:298679fff37c | 494 | portValPtr = NULL; |
mkarlsso | 2:298679fff37c | 495 | isUsed = false; |
mkarlsso | 2:298679fff37c | 496 | } |
mkarlsso | 2:298679fff37c | 497 | |
mkarlsso | 2:298679fff37c | 498 | |
mkarlsso | 2:298679fff37c | 499 | void intCompare::setPointer(const char* cmpString) { |
mkarlsso | 2:298679fff37c | 500 | if (strcmp(cmpString, ">") == 0) { |
mkarlsso | 2:298679fff37c | 501 | isTruePtr = &intCompare::greaterThan; |
mkarlsso | 2:298679fff37c | 502 | }else if (strcmp(cmpString, ">=") == 0) { |
mkarlsso | 2:298679fff37c | 503 | isTruePtr = &intCompare::greaterOrEqual; |
mkarlsso | 2:298679fff37c | 504 | }else if (strcmp(cmpString, "<") == 0) { |
mkarlsso | 2:298679fff37c | 505 | isTruePtr = &intCompare::lessThan; |
mkarlsso | 2:298679fff37c | 506 | }else if (strcmp(cmpString, "<=") == 0) { |
mkarlsso | 2:298679fff37c | 507 | isTruePtr = &intCompare::lessOrEqual; |
mkarlsso | 2:298679fff37c | 508 | }else if (strcmp(cmpString, "==") == 0) { |
mkarlsso | 2:298679fff37c | 509 | isTruePtr = &intCompare::equal; |
mkarlsso | 2:298679fff37c | 510 | }else if (strcmp(cmpString, "!=") == 0) { |
mkarlsso | 2:298679fff37c | 511 | isTruePtr = &intCompare::notEqual; |
mkarlsso | 2:298679fff37c | 512 | } |
mkarlsso | 2:298679fff37c | 513 | } |
mkarlsso | 2:298679fff37c | 514 | |
mkarlsso | 2:298679fff37c | 515 | void intCompare::setPointer_operation(const char* cmpString) { |
mkarlsso | 2:298679fff37c | 516 | if (strcmp(cmpString, ">") == 0) { |
mkarlsso | 2:298679fff37c | 517 | isTruePtr = &intCompare::greaterThan_op; |
mkarlsso | 2:298679fff37c | 518 | }else if (strcmp(cmpString, ">=") == 0) { |
mkarlsso | 2:298679fff37c | 519 | isTruePtr = &intCompare::greaterOrEqual_op; |
mkarlsso | 2:298679fff37c | 520 | }else if (strcmp(cmpString, "<") == 0) { |
mkarlsso | 2:298679fff37c | 521 | isTruePtr = &intCompare::lessThan_op; |
mkarlsso | 2:298679fff37c | 522 | }else if (strcmp(cmpString, "<=") == 0) { |
mkarlsso | 2:298679fff37c | 523 | isTruePtr = &intCompare::lessOrEqual_op; |
mkarlsso | 2:298679fff37c | 524 | }else if (strcmp(cmpString, "==") == 0) { |
mkarlsso | 2:298679fff37c | 525 | isTruePtr = &intCompare::equal_op; |
mkarlsso | 2:298679fff37c | 526 | }else if (strcmp(cmpString, "!=") == 0) { |
mkarlsso | 2:298679fff37c | 527 | isTruePtr = &intCompare::notEqual_op; |
mkarlsso | 2:298679fff37c | 528 | } |
mkarlsso | 2:298679fff37c | 529 | } |
mkarlsso | 2:298679fff37c | 530 | |
mkarlsso | 2:298679fff37c | 531 | bool intCompare::isTrue() { |
mkarlsso | 2:298679fff37c | 532 | return (this->*isTruePtr)(); |
mkarlsso | 2:298679fff37c | 533 | |
mkarlsso | 2:298679fff37c | 534 | } |
mkarlsso | 2:298679fff37c | 535 | |
mkarlsso | 2:298679fff37c | 536 | bool intCompare::notEqual() { |
mkarlsso | 2:298679fff37c | 537 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 538 | return (*intVal != *cmpVal); |
mkarlsso | 2:298679fff37c | 539 | } else { |
mkarlsso | 2:298679fff37c | 540 | return (*portValPtr != *cmpVal); |
mkarlsso | 2:298679fff37c | 541 | } |
mkarlsso | 2:298679fff37c | 542 | } |
mkarlsso | 2:298679fff37c | 543 | |
mkarlsso | 2:298679fff37c | 544 | bool intCompare::greaterThan() { |
mkarlsso | 2:298679fff37c | 545 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 546 | return (*intVal > *cmpVal); |
mkarlsso | 2:298679fff37c | 547 | } else { |
mkarlsso | 2:298679fff37c | 548 | return (*portValPtr > *cmpVal); |
mkarlsso | 2:298679fff37c | 549 | } |
mkarlsso | 2:298679fff37c | 550 | } |
mkarlsso | 2:298679fff37c | 551 | |
mkarlsso | 2:298679fff37c | 552 | bool intCompare::greaterOrEqual() { |
mkarlsso | 2:298679fff37c | 553 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 554 | return (*intVal >= *cmpVal); |
mkarlsso | 2:298679fff37c | 555 | } else { |
mkarlsso | 2:298679fff37c | 556 | return (*portValPtr >= *cmpVal); |
mkarlsso | 2:298679fff37c | 557 | } |
mkarlsso | 2:298679fff37c | 558 | } |
mkarlsso | 2:298679fff37c | 559 | |
mkarlsso | 2:298679fff37c | 560 | bool intCompare::lessThan() { |
mkarlsso | 2:298679fff37c | 561 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 562 | return (*intVal < *cmpVal); |
mkarlsso | 2:298679fff37c | 563 | } else { |
mkarlsso | 2:298679fff37c | 564 | return (*portValPtr < *cmpVal); |
mkarlsso | 2:298679fff37c | 565 | } |
mkarlsso | 2:298679fff37c | 566 | } |
mkarlsso | 2:298679fff37c | 567 | |
mkarlsso | 2:298679fff37c | 568 | bool intCompare::lessOrEqual() { |
mkarlsso | 2:298679fff37c | 569 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 570 | return (*intVal <= *cmpVal); |
mkarlsso | 2:298679fff37c | 571 | } else { |
mkarlsso | 2:298679fff37c | 572 | return (*portValPtr <= *cmpVal); |
mkarlsso | 2:298679fff37c | 573 | } |
mkarlsso | 2:298679fff37c | 574 | } |
mkarlsso | 2:298679fff37c | 575 | |
mkarlsso | 2:298679fff37c | 576 | bool intCompare::equal() { |
mkarlsso | 2:298679fff37c | 577 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 578 | return (*intVal == *cmpVal); |
mkarlsso | 2:298679fff37c | 579 | } else { |
mkarlsso | 2:298679fff37c | 580 | return (*portValPtr == *cmpVal); |
mkarlsso | 2:298679fff37c | 581 | } |
mkarlsso | 2:298679fff37c | 582 | } |
mkarlsso | 2:298679fff37c | 583 | |
mkarlsso | 2:298679fff37c | 584 | bool intCompare::notEqual_op() { |
mkarlsso | 2:298679fff37c | 585 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 586 | return (*intVal != intOp->execute()); |
mkarlsso | 2:298679fff37c | 587 | } else { |
mkarlsso | 2:298679fff37c | 588 | return (*portValPtr != intOp->execute()); |
mkarlsso | 2:298679fff37c | 589 | } |
mkarlsso | 2:298679fff37c | 590 | } |
mkarlsso | 2:298679fff37c | 591 | |
mkarlsso | 2:298679fff37c | 592 | bool intCompare::greaterThan_op() { |
mkarlsso | 2:298679fff37c | 593 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 594 | return (*intVal > intOp->execute()); |
mkarlsso | 2:298679fff37c | 595 | } else { |
mkarlsso | 2:298679fff37c | 596 | return (*portValPtr > intOp->execute()); |
mkarlsso | 2:298679fff37c | 597 | } |
mkarlsso | 2:298679fff37c | 598 | } |
mkarlsso | 2:298679fff37c | 599 | |
mkarlsso | 2:298679fff37c | 600 | bool intCompare::greaterOrEqual_op() { |
mkarlsso | 2:298679fff37c | 601 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 602 | return (*intVal >= intOp->execute()); |
mkarlsso | 2:298679fff37c | 603 | } else { |
mkarlsso | 2:298679fff37c | 604 | return (*portValPtr >= intOp->execute()); |
mkarlsso | 2:298679fff37c | 605 | } |
mkarlsso | 2:298679fff37c | 606 | } |
mkarlsso | 2:298679fff37c | 607 | |
mkarlsso | 2:298679fff37c | 608 | bool intCompare::lessThan_op() { |
mkarlsso | 2:298679fff37c | 609 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 610 | return (*intVal < intOp->execute()); |
mkarlsso | 2:298679fff37c | 611 | } else { |
mkarlsso | 2:298679fff37c | 612 | return (*portValPtr < intOp->execute()); |
mkarlsso | 2:298679fff37c | 613 | } |
mkarlsso | 2:298679fff37c | 614 | } |
mkarlsso | 2:298679fff37c | 615 | |
mkarlsso | 2:298679fff37c | 616 | bool intCompare::lessOrEqual_op() { |
mkarlsso | 2:298679fff37c | 617 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 618 | return (*intVal <= intOp->execute()); |
mkarlsso | 2:298679fff37c | 619 | } else { |
mkarlsso | 2:298679fff37c | 620 | return (*portValPtr <= intOp->execute()); |
mkarlsso | 2:298679fff37c | 621 | } |
mkarlsso | 2:298679fff37c | 622 | } |
mkarlsso | 2:298679fff37c | 623 | |
mkarlsso | 2:298679fff37c | 624 | bool intCompare::equal_op() { |
mkarlsso | 2:298679fff37c | 625 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 626 | return (*intVal == intOp->execute()); |
mkarlsso | 2:298679fff37c | 627 | } else { |
mkarlsso | 2:298679fff37c | 628 | return (*portValPtr == intOp->execute()); |
mkarlsso | 2:298679fff37c | 629 | } |
mkarlsso | 2:298679fff37c | 630 | } |
mkarlsso | 2:298679fff37c | 631 | |
mkarlsso | 2:298679fff37c | 632 | intOperation::intOperation(): |
mkarlsso | 2:298679fff37c | 633 | randHigh(-1) { |
mkarlsso | 2:298679fff37c | 634 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 635 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 636 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 637 | executePtr = NULL; |
mkarlsso | 2:298679fff37c | 638 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 639 | isUsed = false; |
mkarlsso | 2:298679fff37c | 640 | |
mkarlsso | 2:298679fff37c | 641 | } |
mkarlsso | 2:298679fff37c | 642 | |
mkarlsso | 2:298679fff37c | 643 | intOperation::intOperation(int randParam, const char* cmpString, int cmpValInput): |
mkarlsso | 2:298679fff37c | 644 | randHigh(randParam) { |
mkarlsso | 2:298679fff37c | 645 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 646 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 647 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 648 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 649 | isUsed = true; |
mkarlsso | 2:298679fff37c | 650 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 651 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 652 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 653 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 654 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 655 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 656 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 657 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 658 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 659 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 660 | } |
mkarlsso | 2:298679fff37c | 661 | |
mkarlsso | 2:298679fff37c | 662 | } |
mkarlsso | 2:298679fff37c | 663 | |
mkarlsso | 2:298679fff37c | 664 | intOperation::intOperation(int randParam, const char* cmpString, int* cmpIntVarInput): |
mkarlsso | 2:298679fff37c | 665 | randHigh(randParam), |
mkarlsso | 2:298679fff37c | 666 | cmpVal(cmpIntVarInput) { |
mkarlsso | 2:298679fff37c | 667 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 668 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 669 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 670 | isUsed = true; |
mkarlsso | 2:298679fff37c | 671 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 672 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 673 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 674 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 675 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 676 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 677 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 678 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 679 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 680 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 681 | } |
mkarlsso | 2:298679fff37c | 682 | } |
mkarlsso | 2:298679fff37c | 683 | |
mkarlsso | 2:298679fff37c | 684 | intOperation::intOperation(int* intVarInput, const char* cmpString, int cmpValInput): |
mkarlsso | 2:298679fff37c | 685 | intVal(intVarInput) { |
mkarlsso | 2:298679fff37c | 686 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 687 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 688 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 689 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 690 | isUsed = true; |
mkarlsso | 2:298679fff37c | 691 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 692 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 693 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 694 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 695 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 696 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 697 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 698 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 699 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 700 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 701 | } |
mkarlsso | 2:298679fff37c | 702 | } |
mkarlsso | 2:298679fff37c | 703 | |
mkarlsso | 2:298679fff37c | 704 | intOperation::intOperation(int* intVarInput, const char* cmpString, int* cmpIntVarInput): |
mkarlsso | 2:298679fff37c | 705 | cmpVal(cmpIntVarInput), |
mkarlsso | 2:298679fff37c | 706 | intVal(intVarInput) { |
mkarlsso | 2:298679fff37c | 707 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 708 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 709 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 710 | isUsed = true; |
mkarlsso | 2:298679fff37c | 711 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 712 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 713 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 714 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 715 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 716 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 717 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 718 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 719 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 720 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 721 | } |
mkarlsso | 2:298679fff37c | 722 | } |
mkarlsso | 2:298679fff37c | 723 | |
mkarlsso | 2:298679fff37c | 724 | intOperation::intOperation(int* intVarInput, intOperation* operationInput): |
mkarlsso | 2:298679fff37c | 725 | intVal(intVarInput) { |
mkarlsso | 2:298679fff37c | 726 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 727 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 728 | opPtr = operationInput; |
mkarlsso | 2:298679fff37c | 729 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 730 | isUsed = true; |
mkarlsso | 2:298679fff37c | 731 | } |
mkarlsso | 2:298679fff37c | 732 | |
mkarlsso | 2:298679fff37c | 733 | void intOperation::set(int randParam, const char* cmpString, int cmpValInput) { |
mkarlsso | 2:298679fff37c | 734 | randHigh = randParam; |
mkarlsso | 2:298679fff37c | 735 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 736 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 737 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 738 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 739 | isUsed = true; |
mkarlsso | 2:298679fff37c | 740 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 741 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 742 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 743 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 744 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 745 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 746 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 747 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 748 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 749 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 750 | } |
mkarlsso | 2:298679fff37c | 751 | |
mkarlsso | 2:298679fff37c | 752 | } |
mkarlsso | 2:298679fff37c | 753 | |
mkarlsso | 2:298679fff37c | 754 | void intOperation::set(int randParam, const char* cmpString, int* cmpIntVarInput) { |
mkarlsso | 2:298679fff37c | 755 | randHigh = randParam; |
mkarlsso | 2:298679fff37c | 756 | cmpVal = cmpIntVarInput; |
mkarlsso | 2:298679fff37c | 757 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 758 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 759 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 760 | isUsed = true; |
mkarlsso | 2:298679fff37c | 761 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 762 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 763 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 764 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 765 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 766 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 767 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 768 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 769 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 770 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 771 | } |
mkarlsso | 2:298679fff37c | 772 | } |
mkarlsso | 2:298679fff37c | 773 | |
mkarlsso | 2:298679fff37c | 774 | void intOperation::set(int* intVarInput, const char* cmpString, int cmpValInput) { |
mkarlsso | 3:ae33b7f5a7c1 | 775 | |
mkarlsso | 2:298679fff37c | 776 | intVal = intVarInput; |
mkarlsso | 2:298679fff37c | 777 | cmpVal = new int(cmpValInput); |
mkarlsso | 2:298679fff37c | 778 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 779 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 780 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 781 | isUsed = true; |
mkarlsso | 2:298679fff37c | 782 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 783 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 784 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 785 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 786 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 787 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 788 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 789 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 790 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 791 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 792 | } |
mkarlsso | 2:298679fff37c | 793 | } |
mkarlsso | 2:298679fff37c | 794 | |
mkarlsso | 2:298679fff37c | 795 | void intOperation::set(int* intVarInput, const char* cmpString, int* cmpIntVarInput) { |
mkarlsso | 2:298679fff37c | 796 | cmpVal = cmpIntVarInput; |
mkarlsso | 2:298679fff37c | 797 | intVal =intVarInput; |
mkarlsso | 2:298679fff37c | 798 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 799 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 800 | cmpValGlobal = true; |
mkarlsso | 2:298679fff37c | 801 | isUsed = true; |
mkarlsso | 2:298679fff37c | 802 | if (strcmp(cmpString, "+") == 0) { |
mkarlsso | 2:298679fff37c | 803 | executePtr = &intOperation::add; |
mkarlsso | 2:298679fff37c | 804 | }else if (strcmp(cmpString, "-") == 0) { |
mkarlsso | 2:298679fff37c | 805 | executePtr = &intOperation::subtract; |
mkarlsso | 2:298679fff37c | 806 | }else if (strcmp(cmpString, "+=") == 0) { |
mkarlsso | 2:298679fff37c | 807 | executePtr = &intOperation::addAndStore; |
mkarlsso | 2:298679fff37c | 808 | }else if (strcmp(cmpString, "-=") == 0) { |
mkarlsso | 2:298679fff37c | 809 | executePtr = &intOperation::subtractAndStore; |
mkarlsso | 2:298679fff37c | 810 | }else if (strcmp(cmpString, "=") == 0) { |
mkarlsso | 2:298679fff37c | 811 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 812 | } |
mkarlsso | 2:298679fff37c | 813 | } |
mkarlsso | 2:298679fff37c | 814 | |
mkarlsso | 2:298679fff37c | 815 | void intOperation::set(int* intVarInput, intOperation* operationInput) { |
mkarlsso | 3:ae33b7f5a7c1 | 816 | |
mkarlsso | 2:298679fff37c | 817 | intVal = intVarInput; |
mkarlsso | 2:298679fff37c | 818 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 819 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 820 | opPtr = operationInput; |
mkarlsso | 2:298679fff37c | 821 | executePtr = &intOperation::equals; |
mkarlsso | 2:298679fff37c | 822 | isUsed = true; |
mkarlsso | 2:298679fff37c | 823 | |
mkarlsso | 2:298679fff37c | 824 | } |
mkarlsso | 2:298679fff37c | 825 | |
mkarlsso | 2:298679fff37c | 826 | |
mkarlsso | 2:298679fff37c | 827 | intOperation::~intOperation() { |
mkarlsso | 2:298679fff37c | 828 | if (!cmpValGlobal) delete cmpVal; |
mkarlsso | 2:298679fff37c | 829 | delete opPtr; |
mkarlsso | 2:298679fff37c | 830 | } |
mkarlsso | 2:298679fff37c | 831 | |
mkarlsso | 2:298679fff37c | 832 | void intOperation::release() { |
mkarlsso | 2:298679fff37c | 833 | if (!cmpValGlobal) delete cmpVal; |
mkarlsso | 2:298679fff37c | 834 | if (opPtr != NULL) { |
mkarlsso | 2:298679fff37c | 835 | opPtr->release(); |
mkarlsso | 2:298679fff37c | 836 | } |
mkarlsso | 2:298679fff37c | 837 | randHigh = -1; |
mkarlsso | 2:298679fff37c | 838 | cmpVal = NULL; |
mkarlsso | 2:298679fff37c | 839 | intVal = NULL; |
mkarlsso | 2:298679fff37c | 840 | opPtr = NULL; |
mkarlsso | 2:298679fff37c | 841 | executePtr = NULL; |
mkarlsso | 2:298679fff37c | 842 | cmpValGlobal = false; |
mkarlsso | 2:298679fff37c | 843 | isUsed = false; |
mkarlsso | 2:298679fff37c | 844 | } |
mkarlsso | 2:298679fff37c | 845 | |
mkarlsso | 2:298679fff37c | 846 | |
mkarlsso | 2:298679fff37c | 847 | int intOperation::execute() { |
mkarlsso | 3:ae33b7f5a7c1 | 848 | |
mkarlsso | 2:298679fff37c | 849 | return (this->*executePtr)(); |
mkarlsso | 2:298679fff37c | 850 | |
mkarlsso | 3:ae33b7f5a7c1 | 851 | |
mkarlsso | 2:298679fff37c | 852 | } |
mkarlsso | 2:298679fff37c | 853 | |
mkarlsso | 2:298679fff37c | 854 | int intOperation::add() { |
mkarlsso | 3:ae33b7f5a7c1 | 855 | |
mkarlsso | 2:298679fff37c | 856 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 857 | return (*intVal + *cmpVal); |
mkarlsso | 2:298679fff37c | 858 | } |
mkarlsso | 2:298679fff37c | 859 | else { |
mkarlsso | 2:298679fff37c | 860 | //srand(time(NULL)); |
mkarlsso | 2:298679fff37c | 861 | srand(*globalTimeKeeperPtr); |
mkarlsso | 2:298679fff37c | 862 | return (rand() % (randHigh+1)) + *cmpVal; |
mkarlsso | 2:298679fff37c | 863 | //return (port->getState() + *cmpVal); |
mkarlsso | 2:298679fff37c | 864 | } |
mkarlsso | 2:298679fff37c | 865 | } |
mkarlsso | 2:298679fff37c | 866 | |
mkarlsso | 2:298679fff37c | 867 | int intOperation::subtract() { |
mkarlsso | 2:298679fff37c | 868 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 869 | return (*intVal - *cmpVal); |
mkarlsso | 2:298679fff37c | 870 | } |
mkarlsso | 2:298679fff37c | 871 | else { |
mkarlsso | 2:298679fff37c | 872 | srand(*globalTimeKeeperPtr); |
mkarlsso | 2:298679fff37c | 873 | return (rand() % (randHigh+1)) - *cmpVal; |
mkarlsso | 2:298679fff37c | 874 | //return (port->getState() - *cmpVal); |
mkarlsso | 2:298679fff37c | 875 | } |
mkarlsso | 2:298679fff37c | 876 | } |
mkarlsso | 2:298679fff37c | 877 | |
mkarlsso | 2:298679fff37c | 878 | int intOperation::addAndStore() { |
mkarlsso | 2:298679fff37c | 879 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 880 | *intVal = *intVal + *cmpVal; |
mkarlsso | 2:298679fff37c | 881 | return *intVal; |
mkarlsso | 2:298679fff37c | 882 | } |
mkarlsso | 2:298679fff37c | 883 | else { |
mkarlsso | 2:298679fff37c | 884 | |
mkarlsso | 2:298679fff37c | 885 | //Doesn't happen |
mkarlsso | 2:298679fff37c | 886 | return 0; |
mkarlsso | 2:298679fff37c | 887 | //port->setState(port->getState() + *cmpVal); |
mkarlsso | 2:298679fff37c | 888 | //return port->getState(); |
mkarlsso | 2:298679fff37c | 889 | } |
mkarlsso | 2:298679fff37c | 890 | |
mkarlsso | 2:298679fff37c | 891 | } |
mkarlsso | 2:298679fff37c | 892 | |
mkarlsso | 2:298679fff37c | 893 | int intOperation::subtractAndStore() { |
mkarlsso | 2:298679fff37c | 894 | if (intVal != NULL) { |
mkarlsso | 2:298679fff37c | 895 | *intVal = *intVal - *cmpVal; |
mkarlsso | 2:298679fff37c | 896 | return *intVal; |
mkarlsso | 2:298679fff37c | 897 | } else { |
mkarlsso | 2:298679fff37c | 898 | //doesn't happen |
mkarlsso | 2:298679fff37c | 899 | return 0; |
mkarlsso | 2:298679fff37c | 900 | //port->setState(port->getState() - *cmpVal); |
mkarlsso | 2:298679fff37c | 901 | //return port->getState(); |
mkarlsso | 2:298679fff37c | 902 | } |
mkarlsso | 2:298679fff37c | 903 | } |
mkarlsso | 2:298679fff37c | 904 | |
mkarlsso | 2:298679fff37c | 905 | int intOperation::equals() { |
mkarlsso | 2:298679fff37c | 906 | if ((intVal != NULL) && (opPtr == NULL)) { |
mkarlsso | 2:298679fff37c | 907 | *intVal = *cmpVal; |
mkarlsso | 2:298679fff37c | 908 | return *intVal; |
mkarlsso | 2:298679fff37c | 909 | } else if ((intVal != NULL) && (opPtr != NULL)) { |
mkarlsso | 3:ae33b7f5a7c1 | 910 | |
mkarlsso | 2:298679fff37c | 911 | *intVal = opPtr->execute(); |
mkarlsso | 2:298679fff37c | 912 | return *intVal; |
mkarlsso | 2:298679fff37c | 913 | } else if (cmpVal != NULL){ |
mkarlsso | 2:298679fff37c | 914 | |
mkarlsso | 4:34aca2142df9 | 915 | srand(*globalTimeKeeperPtr+randomSeedCounter); |
mkarlsso | 4:34aca2142df9 | 916 | randomSeedCounter++; //for seeding the next rand call, just in case it happens before the clock advances |
mkarlsso | 2:298679fff37c | 917 | *cmpVal = (rand() % (randHigh+1)); //this is how we assign a random number to variable |
mkarlsso | 2:298679fff37c | 918 | return *cmpVal; |
mkarlsso | 2:298679fff37c | 919 | |
mkarlsso | 2:298679fff37c | 920 | } |
mkarlsso | 2:298679fff37c | 921 | return -1; |
mkarlsso | 2:298679fff37c | 922 | } |
mkarlsso | 2:298679fff37c | 923 | |
mkarlsso | 2:298679fff37c | 924 | condition::condition() { |
mkarlsso | 2:298679fff37c | 925 | intCmp = NULL; |
mkarlsso | 2:298679fff37c | 926 | conditionPtrs[0] = NULL; |
mkarlsso | 2:298679fff37c | 927 | conditionPtrs[1] = NULL; |
mkarlsso | 2:298679fff37c | 928 | isUsed = false; |
mkarlsso | 2:298679fff37c | 929 | conditionType = ARITHMATIC_CONDITION; |
mkarlsso | 2:298679fff37c | 930 | } |
mkarlsso | 2:298679fff37c | 931 | |
mkarlsso | 2:298679fff37c | 932 | condition::condition(intCompare* compareInput) { |
mkarlsso | 2:298679fff37c | 933 | |
mkarlsso | 2:298679fff37c | 934 | intCmp = compareInput; |
mkarlsso | 2:298679fff37c | 935 | conditionPtrs[0] = NULL; |
mkarlsso | 2:298679fff37c | 936 | conditionPtrs[1] = NULL; |
mkarlsso | 2:298679fff37c | 937 | |
mkarlsso | 2:298679fff37c | 938 | isUsed = true; |
mkarlsso | 2:298679fff37c | 939 | conditionType = ARITHMATIC_CONDITION; |
mkarlsso | 2:298679fff37c | 940 | |
mkarlsso | 2:298679fff37c | 941 | } |
mkarlsso | 2:298679fff37c | 942 | |
mkarlsso | 2:298679fff37c | 943 | condition::condition(condition* condition1, char condType, condition* condition2) { |
mkarlsso | 2:298679fff37c | 944 | intCmp = NULL; |
mkarlsso | 2:298679fff37c | 945 | conditionPtrs[0] = condition1; |
mkarlsso | 2:298679fff37c | 946 | conditionPtrs[1] = condition2; |
mkarlsso | 2:298679fff37c | 947 | isUsed = true; |
mkarlsso | 2:298679fff37c | 948 | conditionType = condType; |
mkarlsso | 2:298679fff37c | 949 | } |
mkarlsso | 2:298679fff37c | 950 | |
mkarlsso | 2:298679fff37c | 951 | condition::~condition() { |
mkarlsso | 2:298679fff37c | 952 | if (intCmp != NULL) { |
mkarlsso | 2:298679fff37c | 953 | delete intCmp; |
mkarlsso | 2:298679fff37c | 954 | } |
mkarlsso | 2:298679fff37c | 955 | } |
mkarlsso | 2:298679fff37c | 956 | |
mkarlsso | 2:298679fff37c | 957 | void condition::release() { |
mkarlsso | 2:298679fff37c | 958 | if (intCmp != NULL) { |
mkarlsso | 2:298679fff37c | 959 | intCmp->release(); |
mkarlsso | 2:298679fff37c | 960 | intCmp=NULL; |
mkarlsso | 2:298679fff37c | 961 | } |
mkarlsso | 2:298679fff37c | 962 | if (conditionPtrs[0] != NULL) { |
mkarlsso | 2:298679fff37c | 963 | conditionPtrs[0]->release(); |
mkarlsso | 2:298679fff37c | 964 | conditionPtrs[1]->release(); |
mkarlsso | 2:298679fff37c | 965 | conditionPtrs[0]=NULL; |
mkarlsso | 2:298679fff37c | 966 | conditionPtrs[1]=NULL; |
mkarlsso | 2:298679fff37c | 967 | } |
mkarlsso | 2:298679fff37c | 968 | isUsed = false; |
mkarlsso | 2:298679fff37c | 969 | } |
mkarlsso | 2:298679fff37c | 970 | |
mkarlsso | 2:298679fff37c | 971 | void condition::set(intCompare* compareInput) { |
mkarlsso | 2:298679fff37c | 972 | release(); |
mkarlsso | 2:298679fff37c | 973 | intCmp = compareInput; |
mkarlsso | 2:298679fff37c | 974 | conditionPtrs[0] = NULL; |
mkarlsso | 2:298679fff37c | 975 | conditionPtrs[1] = NULL; |
mkarlsso | 2:298679fff37c | 976 | isUsed = true; |
mkarlsso | 2:298679fff37c | 977 | conditionType = ARITHMATIC_CONDITION; |
mkarlsso | 2:298679fff37c | 978 | } |
mkarlsso | 2:298679fff37c | 979 | |
mkarlsso | 2:298679fff37c | 980 | void condition::set(condition* condition1, char condType, condition* condition2) { |
mkarlsso | 2:298679fff37c | 981 | release(); |
mkarlsso | 2:298679fff37c | 982 | intCmp = NULL; |
mkarlsso | 2:298679fff37c | 983 | conditionPtrs[0] = condition1; |
mkarlsso | 2:298679fff37c | 984 | conditionPtrs[1] = condition2; |
mkarlsso | 2:298679fff37c | 985 | isUsed = true; |
mkarlsso | 2:298679fff37c | 986 | conditionType = condType; |
mkarlsso | 2:298679fff37c | 987 | } |
mkarlsso | 2:298679fff37c | 988 | |
mkarlsso | 2:298679fff37c | 989 | bool condition::isTrue() { |
mkarlsso | 2:298679fff37c | 990 | |
mkarlsso | 2:298679fff37c | 991 | |
mkarlsso | 2:298679fff37c | 992 | bool result = true; |
mkarlsso | 2:298679fff37c | 993 | if (conditionType == ARITHMATIC_CONDITION) { |
mkarlsso | 2:298679fff37c | 994 | //pc.printf("Evauating arithmatic condition \r\n"); |
mkarlsso | 2:298679fff37c | 995 | result = (intCmp->isTrue)(); |
mkarlsso | 2:298679fff37c | 996 | } else if (conditionType == AND_CONDITION) { |
mkarlsso | 2:298679fff37c | 997 | //pc.printf("Evauating AND condition \r\n"); |
mkarlsso | 2:298679fff37c | 998 | result = conditionPtrs[0]->isTrue() && conditionPtrs[1]->isTrue(); |
mkarlsso | 2:298679fff37c | 999 | } else if (conditionType == OR_CONDITION) { |
mkarlsso | 2:298679fff37c | 1000 | //pc.printf("Evauating OR condition \r\n"); |
mkarlsso | 2:298679fff37c | 1001 | result = conditionPtrs[0]->isTrue() || conditionPtrs[1]->isTrue(); |
mkarlsso | 2:298679fff37c | 1002 | } |
mkarlsso | 2:298679fff37c | 1003 | return result; |
mkarlsso | 2:298679fff37c | 1004 | |
mkarlsso | 2:298679fff37c | 1005 | } |
mkarlsso | 2:298679fff37c | 1006 | |
mkarlsso | 2:298679fff37c | 1007 | portMessage::portMessage(): |
mkarlsso | 2:298679fff37c | 1008 | whichToSet(0), |
mkarlsso | 2:298679fff37c | 1009 | value(0), |
mkarlsso | 2:298679fff37c | 1010 | port(NULL) { |
mkarlsso | 2:298679fff37c | 1011 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1012 | } |
mkarlsso | 2:298679fff37c | 1013 | |
mkarlsso | 2:298679fff37c | 1014 | void portMessage::release() { |
mkarlsso | 2:298679fff37c | 1015 | |
mkarlsso | 2:298679fff37c | 1016 | whichToSet = 0; |
mkarlsso | 2:298679fff37c | 1017 | value = 0; |
mkarlsso | 2:298679fff37c | 1018 | port = NULL; |
mkarlsso | 2:298679fff37c | 1019 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1020 | } |
mkarlsso | 2:298679fff37c | 1021 | |
mkarlsso | 2:298679fff37c | 1022 | /* |
mkarlsso | 2:298679fff37c | 1023 | portMessage::portMessage(digitalPort* portIn, int whichToSetIn, int valueIn): |
mkarlsso | 2:298679fff37c | 1024 | whichToSet(whichToSetIn), |
mkarlsso | 2:298679fff37c | 1025 | value(valueIn), |
mkarlsso | 2:298679fff37c | 1026 | port(portIn) { |
mkarlsso | 2:298679fff37c | 1027 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1028 | } |
mkarlsso | 2:298679fff37c | 1029 | |
mkarlsso | 2:298679fff37c | 1030 | void portMessage::setMessage(digitalPort* portIn, int whichToSetIn, int valueIn) { |
mkarlsso | 2:298679fff37c | 1031 | whichToSet = whichToSetIn; |
mkarlsso | 2:298679fff37c | 1032 | value = valueIn; |
mkarlsso | 2:298679fff37c | 1033 | port = portIn; |
mkarlsso | 2:298679fff37c | 1034 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1035 | }*/ |
mkarlsso | 2:298679fff37c | 1036 | |
mkarlsso | 2:298679fff37c | 1037 | portMessage::portMessage(int* portIn, int whichToSetIn, int valueIn): |
mkarlsso | 2:298679fff37c | 1038 | whichToSet(whichToSetIn), |
mkarlsso | 2:298679fff37c | 1039 | value(valueIn), |
mkarlsso | 2:298679fff37c | 1040 | port(portIn) { |
mkarlsso | 2:298679fff37c | 1041 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1042 | } |
mkarlsso | 2:298679fff37c | 1043 | |
mkarlsso | 2:298679fff37c | 1044 | void portMessage::setMessage(int* portIn, int whichToSetIn, int valueIn) { |
mkarlsso | 2:298679fff37c | 1045 | whichToSet = whichToSetIn; |
mkarlsso | 2:298679fff37c | 1046 | value = valueIn; |
mkarlsso | 2:298679fff37c | 1047 | port = portIn; |
mkarlsso | 2:298679fff37c | 1048 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1049 | } |
mkarlsso | 2:298679fff37c | 1050 | |
mkarlsso | 2:298679fff37c | 1051 | void portMessage::execute() { |
mkarlsso | 2:298679fff37c | 1052 | |
mkarlsso | 2:298679fff37c | 1053 | if (port != NULL) { |
mkarlsso | 2:298679fff37c | 1054 | if ((*port > 0) && (*port <= NUMPORTS)) { |
mkarlsso | 2:298679fff37c | 1055 | portVector[*port]->setDigitalOut(value); |
mkarlsso | 2:298679fff37c | 1056 | } else { |
mkarlsso | 2:298679fff37c | 1057 | pc.printf("Error: port index assigned by variable does not exist."); |
mkarlsso | 2:298679fff37c | 1058 | } |
mkarlsso | 2:298679fff37c | 1059 | } else { |
mkarlsso | 2:298679fff37c | 1060 | portVector[whichToSet]->setDigitalOut(value); |
mkarlsso | 2:298679fff37c | 1061 | } |
mkarlsso | 2:298679fff37c | 1062 | |
mkarlsso | 2:298679fff37c | 1063 | /* |
mkarlsso | 2:298679fff37c | 1064 | if (whichToSet == 1) { |
mkarlsso | 2:298679fff37c | 1065 | port->setDigitalOut(value); |
mkarlsso | 2:298679fff37c | 1066 | } else if (whichToSet == 2) { |
mkarlsso | 2:298679fff37c | 1067 | //port->setState(value); |
mkarlsso | 2:298679fff37c | 1068 | }*/ |
mkarlsso | 2:298679fff37c | 1069 | } |
mkarlsso | 2:298679fff37c | 1070 | |
mkarlsso | 2:298679fff37c | 1071 | action::action(): |
mkarlsso | 2:298679fff37c | 1072 | actionType(0) { |
mkarlsso | 2:298679fff37c | 1073 | op = NULL; |
mkarlsso | 2:298679fff37c | 1074 | message = NULL; |
mkarlsso | 2:298679fff37c | 1075 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1076 | displayActionPtr = NULL; |
mkarlsso | 2:298679fff37c | 1077 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1078 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1079 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1080 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1081 | |
mkarlsso | 2:298679fff37c | 1082 | } |
mkarlsso | 2:298679fff37c | 1083 | |
mkarlsso | 2:298679fff37c | 1084 | action::~action() { |
mkarlsso | 2:298679fff37c | 1085 | if (eventToCreate != NULL) delete eventToCreate; |
mkarlsso | 2:298679fff37c | 1086 | if (op != NULL) delete op; |
mkarlsso | 2:298679fff37c | 1087 | if (message != NULL) delete message; |
mkarlsso | 2:298679fff37c | 1088 | delete displayActionPtr; |
mkarlsso | 2:298679fff37c | 1089 | delete sound; |
mkarlsso | 2:298679fff37c | 1090 | } |
mkarlsso | 2:298679fff37c | 1091 | |
mkarlsso | 2:298679fff37c | 1092 | void action::release() { |
mkarlsso | 2:298679fff37c | 1093 | if (eventToCreate != NULL) eventToCreate->release(); |
mkarlsso | 2:298679fff37c | 1094 | if (op != NULL) op->release(); |
mkarlsso | 2:298679fff37c | 1095 | if (message != NULL) message->release(); |
mkarlsso | 2:298679fff37c | 1096 | if (displayActionPtr != NULL) displayActionPtr->release(); |
mkarlsso | 2:298679fff37c | 1097 | delete sound; //still need to make a static soundControl array |
mkarlsso | 2:298679fff37c | 1098 | |
mkarlsso | 2:298679fff37c | 1099 | actionType = 0; |
mkarlsso | 2:298679fff37c | 1100 | op = NULL; |
mkarlsso | 2:298679fff37c | 1101 | message = NULL; |
mkarlsso | 2:298679fff37c | 1102 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1103 | displayActionPtr = NULL; |
mkarlsso | 2:298679fff37c | 1104 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1105 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1106 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1107 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1108 | } |
mkarlsso | 2:298679fff37c | 1109 | |
mkarlsso | 2:298679fff37c | 1110 | action::action(intOperation* opInput): |
mkarlsso | 2:298679fff37c | 1111 | actionType(1) { |
mkarlsso | 2:298679fff37c | 1112 | op = opInput; |
mkarlsso | 2:298679fff37c | 1113 | message = NULL; |
mkarlsso | 2:298679fff37c | 1114 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1115 | displayActionPtr= NULL; |
mkarlsso | 2:298679fff37c | 1116 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1117 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1118 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1119 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1120 | } |
mkarlsso | 2:298679fff37c | 1121 | |
mkarlsso | 2:298679fff37c | 1122 | action::action(portMessage* messageInput): |
mkarlsso | 2:298679fff37c | 1123 | actionType(2) { |
mkarlsso | 2:298679fff37c | 1124 | op = NULL; |
mkarlsso | 2:298679fff37c | 1125 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1126 | message = messageInput; |
mkarlsso | 2:298679fff37c | 1127 | displayActionPtr= NULL; |
mkarlsso | 2:298679fff37c | 1128 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1129 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1130 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1131 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1132 | |
mkarlsso | 2:298679fff37c | 1133 | } |
mkarlsso | 2:298679fff37c | 1134 | |
mkarlsso | 2:298679fff37c | 1135 | action::action(event* eventInput): |
mkarlsso | 2:298679fff37c | 1136 | actionType(3) { |
mkarlsso | 2:298679fff37c | 1137 | op = NULL; |
mkarlsso | 2:298679fff37c | 1138 | message = NULL; |
mkarlsso | 2:298679fff37c | 1139 | eventToCreate = eventInput; |
mkarlsso | 2:298679fff37c | 1140 | displayActionPtr= NULL; |
mkarlsso | 2:298679fff37c | 1141 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1142 | |
mkarlsso | 2:298679fff37c | 1143 | //eventDelay = eventInput->timeLag; |
mkarlsso | 2:298679fff37c | 1144 | |
mkarlsso | 2:298679fff37c | 1145 | |
mkarlsso | 2:298679fff37c | 1146 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1147 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1148 | } |
mkarlsso | 2:298679fff37c | 1149 | |
mkarlsso | 2:298679fff37c | 1150 | /* |
mkarlsso | 2:298679fff37c | 1151 | action::action(event* eventInput, uint32_t delay): |
mkarlsso | 2:298679fff37c | 1152 | actionType(3) { |
mkarlsso | 2:298679fff37c | 1153 | op = NULL; |
mkarlsso | 2:298679fff37c | 1154 | message = NULL; |
mkarlsso | 2:298679fff37c | 1155 | eventToCreate = eventInput; |
mkarlsso | 2:298679fff37c | 1156 | displayActionPtr= NULL; |
mkarlsso | 2:298679fff37c | 1157 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1158 | eventDelay = delay; |
mkarlsso | 2:298679fff37c | 1159 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1160 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1161 | |
mkarlsso | 2:298679fff37c | 1162 | }*/ |
mkarlsso | 2:298679fff37c | 1163 | |
mkarlsso | 2:298679fff37c | 1164 | |
mkarlsso | 2:298679fff37c | 1165 | |
mkarlsso | 2:298679fff37c | 1166 | action::action(displayAction* displayInput): |
mkarlsso | 2:298679fff37c | 1167 | actionType(4) { |
mkarlsso | 2:298679fff37c | 1168 | op = NULL; |
mkarlsso | 2:298679fff37c | 1169 | message = NULL; |
mkarlsso | 2:298679fff37c | 1170 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1171 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1172 | displayActionPtr = displayInput; |
mkarlsso | 2:298679fff37c | 1173 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1174 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1175 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1176 | } |
mkarlsso | 2:298679fff37c | 1177 | |
mkarlsso | 2:298679fff37c | 1178 | action::action(soundControl* soundInput): |
mkarlsso | 2:298679fff37c | 1179 | actionType(5) { |
mkarlsso | 2:298679fff37c | 1180 | op = NULL; |
mkarlsso | 2:298679fff37c | 1181 | message = NULL; |
mkarlsso | 2:298679fff37c | 1182 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1183 | sound = soundInput; |
mkarlsso | 2:298679fff37c | 1184 | displayActionPtr = NULL; |
mkarlsso | 2:298679fff37c | 1185 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1186 | sysCommand = -1; |
mkarlsso | 2:298679fff37c | 1187 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1188 | } |
mkarlsso | 2:298679fff37c | 1189 | |
mkarlsso | 2:298679fff37c | 1190 | action::action(int8_t sysCommandInput): |
mkarlsso | 2:298679fff37c | 1191 | actionType(6) { |
mkarlsso | 2:298679fff37c | 1192 | op = NULL; |
mkarlsso | 2:298679fff37c | 1193 | message = NULL; |
mkarlsso | 2:298679fff37c | 1194 | eventToCreate = NULL; |
mkarlsso | 2:298679fff37c | 1195 | sound = NULL; |
mkarlsso | 2:298679fff37c | 1196 | displayActionPtr = NULL; |
mkarlsso | 2:298679fff37c | 1197 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1198 | sysCommand = sysCommandInput; |
mkarlsso | 2:298679fff37c | 1199 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1200 | } |
mkarlsso | 2:298679fff37c | 1201 | |
mkarlsso | 2:298679fff37c | 1202 | void action::set(intOperation* opInput) { |
mkarlsso | 2:298679fff37c | 1203 | actionType = 1; |
mkarlsso | 2:298679fff37c | 1204 | op = opInput; |
mkarlsso | 2:298679fff37c | 1205 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1206 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1207 | |
mkarlsso | 2:298679fff37c | 1208 | } |
mkarlsso | 2:298679fff37c | 1209 | |
mkarlsso | 2:298679fff37c | 1210 | void action::set(portMessage* messageInput) { |
mkarlsso | 2:298679fff37c | 1211 | actionType = 2; |
mkarlsso | 2:298679fff37c | 1212 | message = messageInput; |
mkarlsso | 2:298679fff37c | 1213 | //eventDelay = 0; |
mkarlsso | 2:298679fff37c | 1214 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1215 | |
mkarlsso | 2:298679fff37c | 1216 | } |
mkarlsso | 2:298679fff37c | 1217 | |
mkarlsso | 2:298679fff37c | 1218 | void action::set(event* eventInput) { |
mkarlsso | 2:298679fff37c | 1219 | actionType = 3; |
mkarlsso | 2:298679fff37c | 1220 | eventToCreate = eventInput; |
mkarlsso | 2:298679fff37c | 1221 | //eventDelay = eventInput->timeLag; |
mkarlsso | 2:298679fff37c | 1222 | |
mkarlsso | 2:298679fff37c | 1223 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1224 | |
mkarlsso | 2:298679fff37c | 1225 | } |
mkarlsso | 2:298679fff37c | 1226 | |
mkarlsso | 2:298679fff37c | 1227 | /* |
mkarlsso | 2:298679fff37c | 1228 | void action::set(event* eventInput, uint32_t delay) { |
mkarlsso | 2:298679fff37c | 1229 | actionType = 3; |
mkarlsso | 2:298679fff37c | 1230 | eventToCreate = eventInput; |
mkarlsso | 2:298679fff37c | 1231 | eventDelay = delay; |
mkarlsso | 2:298679fff37c | 1232 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1233 | |
mkarlsso | 2:298679fff37c | 1234 | }*/ |
mkarlsso | 2:298679fff37c | 1235 | |
mkarlsso | 2:298679fff37c | 1236 | |
mkarlsso | 2:298679fff37c | 1237 | |
mkarlsso | 2:298679fff37c | 1238 | void action::set(displayAction* displayInput) { |
mkarlsso | 2:298679fff37c | 1239 | actionType = 4; |
mkarlsso | 2:298679fff37c | 1240 | displayActionPtr = displayInput; |
mkarlsso | 2:298679fff37c | 1241 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1242 | } |
mkarlsso | 2:298679fff37c | 1243 | |
mkarlsso | 2:298679fff37c | 1244 | void action::set(soundControl* soundInput) { |
mkarlsso | 2:298679fff37c | 1245 | actionType = 5; |
mkarlsso | 2:298679fff37c | 1246 | sound = soundInput; |
mkarlsso | 2:298679fff37c | 1247 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1248 | } |
mkarlsso | 2:298679fff37c | 1249 | |
mkarlsso | 2:298679fff37c | 1250 | void action::set(int8_t sysCommandInput) { |
mkarlsso | 2:298679fff37c | 1251 | actionType = 6; |
mkarlsso | 2:298679fff37c | 1252 | sysCommand = sysCommandInput; |
mkarlsso | 2:298679fff37c | 1253 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1254 | } |
mkarlsso | 2:298679fff37c | 1255 | |
mkarlsso | 2:298679fff37c | 1256 | void action::execute() { |
mkarlsso | 2:298679fff37c | 1257 | |
mkarlsso | 2:298679fff37c | 1258 | if (actionType == 1) { |
mkarlsso | 2:298679fff37c | 1259 | op->execute(); |
mkarlsso | 2:298679fff37c | 1260 | } else if (actionType == 2) { |
mkarlsso | 2:298679fff37c | 1261 | message->execute(); |
mkarlsso | 2:298679fff37c | 1262 | } else if (actionType == 3) { |
mkarlsso | 2:298679fff37c | 1263 | this->execute(*globalTimeKeeperPtr); //route to the other overloaded method |
mkarlsso | 2:298679fff37c | 1264 | } else if (actionType == 4) { |
mkarlsso | 2:298679fff37c | 1265 | displayActionPtr->execute(); //send text via serial |
mkarlsso | 2:298679fff37c | 1266 | } else if (actionType == 5) { |
mkarlsso | 2:298679fff37c | 1267 | sound->execute(); //operate sound device |
mkarlsso | 2:298679fff37c | 1268 | } else if (actionType == 6) { |
mkarlsso | 2:298679fff37c | 1269 | switch(sysCommand) { |
mkarlsso | 2:298679fff37c | 1270 | case 0: |
mkarlsso | 2:298679fff37c | 1271 | mainQueue.eraseQueue(); |
mkarlsso | 2:298679fff37c | 1272 | break; |
mkarlsso | 2:298679fff37c | 1273 | case 1: |
mkarlsso | 2:298679fff37c | 1274 | textStreaming = true; |
mkarlsso | 2:298679fff37c | 1275 | break; |
mkarlsso | 2:298679fff37c | 1276 | case 2: |
mkarlsso | 2:298679fff37c | 1277 | textStreaming = false; |
mkarlsso | 2:298679fff37c | 1278 | break; |
mkarlsso | 2:298679fff37c | 1279 | case 3: |
mkarlsso | 2:298679fff37c | 1280 | broadCastStateChanges = true; |
mkarlsso | 2:298679fff37c | 1281 | break; |
mkarlsso | 2:298679fff37c | 1282 | case 4: |
mkarlsso | 2:298679fff37c | 1283 | broadCastStateChanges = false; |
mkarlsso | 2:298679fff37c | 1284 | break; |
mkarlsso | 2:298679fff37c | 1285 | } |
mkarlsso | 2:298679fff37c | 1286 | |
mkarlsso | 2:298679fff37c | 1287 | } |
mkarlsso | 2:298679fff37c | 1288 | } |
mkarlsso | 2:298679fff37c | 1289 | |
mkarlsso | 2:298679fff37c | 1290 | void action::execute(uint32_t blockExecTime) { |
mkarlsso | 2:298679fff37c | 1291 | |
mkarlsso | 2:298679fff37c | 1292 | if (actionType == 1) { |
mkarlsso | 2:298679fff37c | 1293 | op->execute(); |
mkarlsso | 2:298679fff37c | 1294 | } else if (actionType == 2) { |
mkarlsso | 2:298679fff37c | 1295 | message->execute(); |
mkarlsso | 2:298679fff37c | 1296 | } else if (actionType == 3) { //an event block |
mkarlsso | 2:298679fff37c | 1297 | //Because time will pass from the begining of the block, any defined delays should be updated |
mkarlsso | 2:298679fff37c | 1298 | |
mkarlsso | 2:298679fff37c | 1299 | //int newDelay = eventDelay-(*globalTimeKeeperPtr-blockExecTime); |
mkarlsso | 2:298679fff37c | 1300 | int newDelay; |
mkarlsso | 2:298679fff37c | 1301 | if (eventToCreate->timeLagIsConstant) { |
mkarlsso | 2:298679fff37c | 1302 | newDelay = eventToCreate->timeLag - (*globalTimeKeeperPtr-blockExecTime); |
mkarlsso | 2:298679fff37c | 1303 | } else { |
mkarlsso | 2:298679fff37c | 1304 | newDelay = *eventToCreate->timeLagVar - (*globalTimeKeeperPtr-blockExecTime); |
mkarlsso | 2:298679fff37c | 1305 | } |
mkarlsso | 2:298679fff37c | 1306 | if (newDelay < 0) {newDelay = 0;} |
mkarlsso | 2:298679fff37c | 1307 | eventToCreate->addToQueue(newDelay); //add the event to the queue to be executed later |
mkarlsso | 2:298679fff37c | 1308 | |
mkarlsso | 2:298679fff37c | 1309 | if (eventToCreate->hasWhileLoop) { //this is a while loop |
mkarlsso | 2:298679fff37c | 1310 | if (eventToCreate->isConditionTrue()) { |
mkarlsso | 2:298679fff37c | 1311 | //newDelay = (blockExecTime + eventToCreate->whileLoopPeriod) - *globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1312 | int tmpPeriod; |
mkarlsso | 2:298679fff37c | 1313 | if (eventToCreate->whileLoopPeriodIsConstant) { //constant while loop period |
mkarlsso | 2:298679fff37c | 1314 | newDelay = (blockExecTime + eventToCreate->whileLoopPeriod); |
mkarlsso | 2:298679fff37c | 1315 | tmpPeriod = eventToCreate->whileLoopPeriod; |
mkarlsso | 2:298679fff37c | 1316 | } else { |
mkarlsso | 2:298679fff37c | 1317 | tmpPeriod = *eventToCreate->whileLoopPeriodVar; |
mkarlsso | 2:298679fff37c | 1318 | if (tmpPeriod < 0) { |
mkarlsso | 2:298679fff37c | 1319 | tmpPeriod = 0; |
mkarlsso | 2:298679fff37c | 1320 | } |
mkarlsso | 2:298679fff37c | 1321 | newDelay = (blockExecTime + tmpPeriod); |
mkarlsso | 2:298679fff37c | 1322 | } |
mkarlsso | 2:298679fff37c | 1323 | while ( (newDelay-*globalTimeKeeperPtr < 0) && (eventToCreate->isConditionTrue()) ) { |
mkarlsso | 2:298679fff37c | 1324 | eventToCreate->execute(); |
mkarlsso | 2:298679fff37c | 1325 | newDelay = newDelay + tmpPeriod; |
mkarlsso | 2:298679fff37c | 1326 | |
mkarlsso | 2:298679fff37c | 1327 | } |
mkarlsso | 2:298679fff37c | 1328 | newDelay = newDelay-*globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1329 | if (newDelay > 0) { |
mkarlsso | 2:298679fff37c | 1330 | eventToCreate->addToQueue(newDelay); |
mkarlsso | 2:298679fff37c | 1331 | } else { |
mkarlsso | 2:298679fff37c | 1332 | eventToCreate->addToQueue(1); |
mkarlsso | 2:298679fff37c | 1333 | } |
mkarlsso | 2:298679fff37c | 1334 | } else if (eventToCreate->nextElseEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 1335 | eventToCreate->nextElseEventPtr->addToQueue(); |
mkarlsso | 2:298679fff37c | 1336 | |
mkarlsso | 2:298679fff37c | 1337 | } |
mkarlsso | 2:298679fff37c | 1338 | } |
mkarlsso | 2:298679fff37c | 1339 | } else if (actionType == 4) { |
mkarlsso | 2:298679fff37c | 1340 | displayActionPtr->execute(); //send text via serial |
mkarlsso | 2:298679fff37c | 1341 | } else if (actionType == 5) { |
mkarlsso | 2:298679fff37c | 1342 | sound->execute(); //operate sound device |
mkarlsso | 2:298679fff37c | 1343 | } else if (actionType == 6) { |
mkarlsso | 2:298679fff37c | 1344 | switch(sysCommand) { |
mkarlsso | 2:298679fff37c | 1345 | case 0: |
mkarlsso | 2:298679fff37c | 1346 | mainQueue.eraseQueue(); |
mkarlsso | 2:298679fff37c | 1347 | break; |
mkarlsso | 2:298679fff37c | 1348 | case 1: |
mkarlsso | 2:298679fff37c | 1349 | textStreaming = true; |
mkarlsso | 2:298679fff37c | 1350 | break; |
mkarlsso | 2:298679fff37c | 1351 | case 2: |
mkarlsso | 2:298679fff37c | 1352 | textStreaming = false; |
mkarlsso | 2:298679fff37c | 1353 | break; |
mkarlsso | 2:298679fff37c | 1354 | case 3: |
mkarlsso | 2:298679fff37c | 1355 | broadCastStateChanges = true; |
mkarlsso | 2:298679fff37c | 1356 | break; |
mkarlsso | 2:298679fff37c | 1357 | case 4: |
mkarlsso | 2:298679fff37c | 1358 | broadCastStateChanges = false; |
mkarlsso | 2:298679fff37c | 1359 | break; |
mkarlsso | 2:298679fff37c | 1360 | } |
mkarlsso | 2:298679fff37c | 1361 | } |
mkarlsso | 2:298679fff37c | 1362 | } |
mkarlsso | 2:298679fff37c | 1363 | |
mkarlsso | 2:298679fff37c | 1364 | eventQueue::eventQueue(digitalPort** portVectorInput, uint32_t* timeKeeperSlaveInput): |
mkarlsso | 2:298679fff37c | 1365 | portVector(portVectorInput), |
mkarlsso | 2:298679fff37c | 1366 | timeKeeperPtr(timeKeeperSlaveInput){ |
mkarlsso | 2:298679fff37c | 1367 | |
mkarlsso | 2:298679fff37c | 1368 | globalTimeKeeperPtr = timeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1369 | queueItem blankEvent; |
mkarlsso | 2:298679fff37c | 1370 | blankEvent.timeToExecute = 0; |
mkarlsso | 2:298679fff37c | 1371 | blankEvent.eventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1372 | queueSize = 100; |
mkarlsso | 2:298679fff37c | 1373 | events.resize(queueSize,blankEvent); |
mkarlsso | 2:298679fff37c | 1374 | |
mkarlsso | 2:298679fff37c | 1375 | |
mkarlsso | 2:298679fff37c | 1376 | } |
mkarlsso | 2:298679fff37c | 1377 | |
mkarlsso | 2:298679fff37c | 1378 | void eventQueue::addEventToQueue(event* eventInput, uint32_t delay) { |
mkarlsso | 2:298679fff37c | 1379 | //*updateSlavePtr = false; |
mkarlsso | 2:298679fff37c | 1380 | uint32_t eventTime = *timeKeeperPtr + delay; |
mkarlsso | 2:298679fff37c | 1381 | //*updateSlavePtr = true; |
mkarlsso | 2:298679fff37c | 1382 | //std::vector<queueItem>::size_type sz = events.size(); |
mkarlsso | 2:298679fff37c | 1383 | //Look for the first empty slot in the queue and place the event there. |
mkarlsso | 2:298679fff37c | 1384 | //This means that the events in the queue are out of order, but |
mkarlsso | 2:298679fff37c | 1385 | //it prevents us from having to push_pack and pop off all the time. |
mkarlsso | 2:298679fff37c | 1386 | for (unsigned i = 0; i < queueSize; i++) { |
mkarlsso | 2:298679fff37c | 1387 | if (events[i].eventPtr == NULL) { |
mkarlsso | 2:298679fff37c | 1388 | events[i].eventPtr = eventInput; |
mkarlsso | 2:298679fff37c | 1389 | events[i].timeToExecute = eventTime; |
mkarlsso | 2:298679fff37c | 1390 | break; |
mkarlsso | 2:298679fff37c | 1391 | } |
mkarlsso | 2:298679fff37c | 1392 | } |
mkarlsso | 2:298679fff37c | 1393 | } |
mkarlsso | 2:298679fff37c | 1394 | |
mkarlsso | 2:298679fff37c | 1395 | void eventQueue::eraseQueue() { |
mkarlsso | 2:298679fff37c | 1396 | //Erase all events in the queue |
mkarlsso | 2:298679fff37c | 1397 | std::vector<queueItem>::size_type sz = events.size(); |
mkarlsso | 2:298679fff37c | 1398 | for (unsigned i = 0; i < sz; i++) { |
mkarlsso | 2:298679fff37c | 1399 | events[i].eventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1400 | |
mkarlsso | 2:298679fff37c | 1401 | } |
mkarlsso | 2:298679fff37c | 1402 | } |
mkarlsso | 2:298679fff37c | 1403 | |
mkarlsso | 2:298679fff37c | 1404 | |
mkarlsso | 2:298679fff37c | 1405 | //check if any of the events in the queue are up for execution |
mkarlsso | 2:298679fff37c | 1406 | void eventQueue::check(void) { |
mkarlsso | 2:298679fff37c | 1407 | |
mkarlsso | 2:298679fff37c | 1408 | //*updateSlavePtr = false; |
mkarlsso | 2:298679fff37c | 1409 | uint32_t currentTime = *timeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1410 | //*updateSlavePtr = true; |
mkarlsso | 2:298679fff37c | 1411 | //std::vector<queueItem>::size_type sz = events.size(); |
mkarlsso | 2:298679fff37c | 1412 | for (unsigned i = 0; i < queueSize; i++) { |
mkarlsso | 2:298679fff37c | 1413 | if (events[i].eventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 1414 | if(events[i].timeToExecute <= currentTime) { |
mkarlsso | 2:298679fff37c | 1415 | if (!events[i].eventPtr->hasWhileLoop) { |
mkarlsso | 2:298679fff37c | 1416 | //this is not a while loop, so no need to check if the condition is still true |
mkarlsso | 2:298679fff37c | 1417 | events[i].eventPtr->execute(); |
mkarlsso | 2:298679fff37c | 1418 | } else if (events[i].eventPtr->isConditionTrue()){ |
mkarlsso | 2:298679fff37c | 1419 | //The event is part of a while loop, so recheck the condition before executing |
mkarlsso | 2:298679fff37c | 1420 | events[i].eventPtr->execute(); |
mkarlsso | 2:298679fff37c | 1421 | //if (events[i].eventPtr->isConditionTrue()) { //is the condition still true? |
mkarlsso | 2:298679fff37c | 1422 | int nextTime; |
mkarlsso | 2:298679fff37c | 1423 | int tmpPeriod; |
mkarlsso | 2:298679fff37c | 1424 | if (events[i].eventPtr->whileLoopPeriodIsConstant) { |
mkarlsso | 2:298679fff37c | 1425 | nextTime = (events[i].timeToExecute + events[i].eventPtr->whileLoopPeriod); |
mkarlsso | 2:298679fff37c | 1426 | tmpPeriod = events[i].eventPtr->whileLoopPeriod; |
mkarlsso | 2:298679fff37c | 1427 | } else { |
mkarlsso | 2:298679fff37c | 1428 | tmpPeriod = *events[i].eventPtr->whileLoopPeriodVar; |
mkarlsso | 2:298679fff37c | 1429 | if (tmpPeriod < 0) { |
mkarlsso | 2:298679fff37c | 1430 | tmpPeriod = 0; |
mkarlsso | 2:298679fff37c | 1431 | } |
mkarlsso | 2:298679fff37c | 1432 | nextTime = (events[i].timeToExecute + tmpPeriod); |
mkarlsso | 2:298679fff37c | 1433 | |
mkarlsso | 2:298679fff37c | 1434 | } |
mkarlsso | 2:298679fff37c | 1435 | //Just in case we are not keeping up, execute the event until we have cought up |
mkarlsso | 2:298679fff37c | 1436 | while ((nextTime-*timeKeeperPtr <= 0) && (events[i].eventPtr->isConditionTrue())) { |
mkarlsso | 2:298679fff37c | 1437 | events[i].eventPtr->execute(); |
mkarlsso | 2:298679fff37c | 1438 | nextTime = nextTime+tmpPeriod; |
mkarlsso | 2:298679fff37c | 1439 | |
mkarlsso | 2:298679fff37c | 1440 | } |
mkarlsso | 2:298679fff37c | 1441 | nextTime = nextTime - *timeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1442 | if (nextTime > 0) { |
mkarlsso | 2:298679fff37c | 1443 | //we add the event to the queue (but the condition is rechecked first) |
mkarlsso | 2:298679fff37c | 1444 | //if the condition is false, the 'then' statement is added to the queue instead |
mkarlsso | 2:298679fff37c | 1445 | events[i].eventPtr->addToQueue(nextTime); |
mkarlsso | 2:298679fff37c | 1446 | } else { |
mkarlsso | 2:298679fff37c | 1447 | //if we are having trouble keeping up, just add the next event in 1 ms |
mkarlsso | 2:298679fff37c | 1448 | events[i].eventPtr->addToQueue(1); |
mkarlsso | 2:298679fff37c | 1449 | } |
mkarlsso | 2:298679fff37c | 1450 | /* |
mkarlsso | 2:298679fff37c | 1451 | } else { |
mkarlsso | 2:298679fff37c | 1452 | if (events[i].eventPtr->nextElseEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 1453 | events[i].eventPtr->nextElseEventPtr->addToQueue(); |
mkarlsso | 2:298679fff37c | 1454 | } |
mkarlsso | 2:298679fff37c | 1455 | }*/ |
mkarlsso | 2:298679fff37c | 1456 | |
mkarlsso | 2:298679fff37c | 1457 | } else { |
mkarlsso | 2:298679fff37c | 1458 | if (events[i].eventPtr->nextElseEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 1459 | events[i].eventPtr->nextElseEventPtr->addToQueue(); |
mkarlsso | 2:298679fff37c | 1460 | } |
mkarlsso | 2:298679fff37c | 1461 | } |
mkarlsso | 2:298679fff37c | 1462 | events[i].eventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1463 | } |
mkarlsso | 2:298679fff37c | 1464 | } |
mkarlsso | 2:298679fff37c | 1465 | } |
mkarlsso | 2:298679fff37c | 1466 | } |
mkarlsso | 2:298679fff37c | 1467 | |
mkarlsso | 2:298679fff37c | 1468 | |
mkarlsso | 2:298679fff37c | 1469 | event::event(): |
mkarlsso | 2:298679fff37c | 1470 | timeLag(0), |
mkarlsso | 2:298679fff37c | 1471 | queuePtr(&mainQueue) { |
mkarlsso | 2:298679fff37c | 1472 | nextElseEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1473 | conditionToCheck = NULL; |
mkarlsso | 2:298679fff37c | 1474 | blockType = 0; |
mkarlsso | 2:298679fff37c | 1475 | whileLoopPeriod = 0; |
mkarlsso | 2:298679fff37c | 1476 | numConditions = 0; |
mkarlsso | 2:298679fff37c | 1477 | numActions = 0; |
mkarlsso | 2:298679fff37c | 1478 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1479 | timeLagVar = NULL; |
mkarlsso | 2:298679fff37c | 1480 | timeLagIsConstant = true; |
mkarlsso | 2:298679fff37c | 1481 | whileLoopPeriodIsConstant = true; |
mkarlsso | 2:298679fff37c | 1482 | hasWhileLoop = false; |
mkarlsso | 2:298679fff37c | 1483 | whileLoopPeriodVar = NULL; |
mkarlsso | 2:298679fff37c | 1484 | |
mkarlsso | 2:298679fff37c | 1485 | } |
mkarlsso | 2:298679fff37c | 1486 | |
mkarlsso | 2:298679fff37c | 1487 | event::event(eventQueue* queueInput): |
mkarlsso | 2:298679fff37c | 1488 | timeLag(0), |
mkarlsso | 2:298679fff37c | 1489 | queuePtr(&mainQueue) { |
mkarlsso | 2:298679fff37c | 1490 | nextElseEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1491 | conditionToCheck = NULL; |
mkarlsso | 2:298679fff37c | 1492 | blockType = 0; |
mkarlsso | 2:298679fff37c | 1493 | whileLoopPeriod = 0; |
mkarlsso | 2:298679fff37c | 1494 | numConditions = 0; |
mkarlsso | 2:298679fff37c | 1495 | numActions = 0; |
mkarlsso | 2:298679fff37c | 1496 | isUsed = true; |
mkarlsso | 2:298679fff37c | 1497 | timeLagVar = NULL; |
mkarlsso | 2:298679fff37c | 1498 | timeLagIsConstant = true; |
mkarlsso | 2:298679fff37c | 1499 | whileLoopPeriodIsConstant = true; |
mkarlsso | 2:298679fff37c | 1500 | hasWhileLoop = false; |
mkarlsso | 2:298679fff37c | 1501 | whileLoopPeriodVar = NULL; |
mkarlsso | 2:298679fff37c | 1502 | |
mkarlsso | 2:298679fff37c | 1503 | } |
mkarlsso | 2:298679fff37c | 1504 | |
mkarlsso | 2:298679fff37c | 1505 | event::~event() { |
mkarlsso | 2:298679fff37c | 1506 | /* |
mkarlsso | 2:298679fff37c | 1507 | while (!conditionArray.empty()) |
mkarlsso | 2:298679fff37c | 1508 | { |
mkarlsso | 2:298679fff37c | 1509 | delete conditionArray.back(); |
mkarlsso | 2:298679fff37c | 1510 | conditionArray.pop_back(); |
mkarlsso | 2:298679fff37c | 1511 | } |
mkarlsso | 2:298679fff37c | 1512 | |
mkarlsso | 2:298679fff37c | 1513 | while (!actionArray.empty()) |
mkarlsso | 2:298679fff37c | 1514 | { |
mkarlsso | 2:298679fff37c | 1515 | delete actionArray.back(); |
mkarlsso | 2:298679fff37c | 1516 | actionArray.pop_back(); |
mkarlsso | 2:298679fff37c | 1517 | } |
mkarlsso | 2:298679fff37c | 1518 | |
mkarlsso | 2:298679fff37c | 1519 | delete nextElseEventPtr; |
mkarlsso | 2:298679fff37c | 1520 | */ |
mkarlsso | 2:298679fff37c | 1521 | |
mkarlsso | 2:298679fff37c | 1522 | } |
mkarlsso | 2:298679fff37c | 1523 | |
mkarlsso | 2:298679fff37c | 1524 | void event::release() { |
mkarlsso | 2:298679fff37c | 1525 | |
mkarlsso | 2:298679fff37c | 1526 | for (int i = 0; i < numActions; i++) { |
mkarlsso | 2:298679fff37c | 1527 | actionArray[i]->release(); |
mkarlsso | 2:298679fff37c | 1528 | } |
mkarlsso | 2:298679fff37c | 1529 | |
mkarlsso | 2:298679fff37c | 1530 | if (conditionToCheck != NULL) { |
mkarlsso | 2:298679fff37c | 1531 | conditionToCheck->release(); |
mkarlsso | 2:298679fff37c | 1532 | conditionToCheck = NULL; |
mkarlsso | 2:298679fff37c | 1533 | } |
mkarlsso | 2:298679fff37c | 1534 | |
mkarlsso | 2:298679fff37c | 1535 | if (nextElseEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 1536 | nextElseEventPtr->release(); |
mkarlsso | 2:298679fff37c | 1537 | } |
mkarlsso | 2:298679fff37c | 1538 | timeLag = 0; |
mkarlsso | 2:298679fff37c | 1539 | nextElseEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 1540 | blockType = 0; |
mkarlsso | 2:298679fff37c | 1541 | whileLoopPeriod = 0; |
mkarlsso | 2:298679fff37c | 1542 | numConditions = 0; |
mkarlsso | 2:298679fff37c | 1543 | numActions = 0; |
mkarlsso | 2:298679fff37c | 1544 | isUsed = false; |
mkarlsso | 2:298679fff37c | 1545 | timeLagVar = NULL; |
mkarlsso | 2:298679fff37c | 1546 | timeLagIsConstant = true; |
mkarlsso | 2:298679fff37c | 1547 | whileLoopPeriodIsConstant = true; |
mkarlsso | 2:298679fff37c | 1548 | hasWhileLoop = false; |
mkarlsso | 2:298679fff37c | 1549 | whileLoopPeriodVar = NULL; |
mkarlsso | 2:298679fff37c | 1550 | } |
mkarlsso | 2:298679fff37c | 1551 | |
mkarlsso | 2:298679fff37c | 1552 | void event::setTimeLag(uint32_t timeLagInput) { |
mkarlsso | 2:298679fff37c | 1553 | timeLag = timeLagInput; |
mkarlsso | 2:298679fff37c | 1554 | timeLagIsConstant = true; |
mkarlsso | 2:298679fff37c | 1555 | } |
mkarlsso | 2:298679fff37c | 1556 | |
mkarlsso | 2:298679fff37c | 1557 | void event::setTimeLag(int* timeLagInput) { |
mkarlsso | 2:298679fff37c | 1558 | timeLagVar = timeLagInput; |
mkarlsso | 2:298679fff37c | 1559 | timeLagIsConstant = false; //time lag is not defined by a constant |
mkarlsso | 2:298679fff37c | 1560 | } |
mkarlsso | 2:298679fff37c | 1561 | |
mkarlsso | 2:298679fff37c | 1562 | void event::setWhileLoopPeriod(uint32_t period) { |
mkarlsso | 2:298679fff37c | 1563 | whileLoopPeriodIsConstant = true; |
mkarlsso | 2:298679fff37c | 1564 | hasWhileLoop = true; |
mkarlsso | 2:298679fff37c | 1565 | whileLoopPeriod = period; |
mkarlsso | 2:298679fff37c | 1566 | |
mkarlsso | 2:298679fff37c | 1567 | } |
mkarlsso | 2:298679fff37c | 1568 | |
mkarlsso | 2:298679fff37c | 1569 | void event::setWhileLoopPeriod(int* period) { |
mkarlsso | 2:298679fff37c | 1570 | whileLoopPeriodIsConstant = false; |
mkarlsso | 2:298679fff37c | 1571 | hasWhileLoop = true; |
mkarlsso | 2:298679fff37c | 1572 | whileLoopPeriodVar = period; |
mkarlsso | 2:298679fff37c | 1573 | |
mkarlsso | 2:298679fff37c | 1574 | } |
mkarlsso | 2:298679fff37c | 1575 | |
mkarlsso | 2:298679fff37c | 1576 | void event::addCondition(condition* conditionInput) { |
mkarlsso | 2:298679fff37c | 1577 | if (conditionToCheck != NULL) { |
mkarlsso | 2:298679fff37c | 1578 | conditionToCheck->release(); |
mkarlsso | 2:298679fff37c | 1579 | } |
mkarlsso | 2:298679fff37c | 1580 | conditionToCheck = conditionInput; |
mkarlsso | 2:298679fff37c | 1581 | |
mkarlsso | 2:298679fff37c | 1582 | //conditionArray.push_back(conditionInput); |
mkarlsso | 2:298679fff37c | 1583 | } |
mkarlsso | 2:298679fff37c | 1584 | |
mkarlsso | 2:298679fff37c | 1585 | void event::addAction(action* actionInput) { |
mkarlsso | 2:298679fff37c | 1586 | actionArray[numActions] = actionInput; |
mkarlsso | 2:298679fff37c | 1587 | numActions++; |
mkarlsso | 2:298679fff37c | 1588 | //actionArray.push_back(actionInput); |
mkarlsso | 2:298679fff37c | 1589 | } |
mkarlsso | 2:298679fff37c | 1590 | |
mkarlsso | 2:298679fff37c | 1591 | bool event::isConditionTrue(void) { |
mkarlsso | 2:298679fff37c | 1592 | //if statement (can be left empty, which is interpreted as 'true') |
mkarlsso | 2:298679fff37c | 1593 | //queuePtr->pcPtr->printf("Checking condition...\r\n"); |
mkarlsso | 2:298679fff37c | 1594 | bool result = true; |
mkarlsso | 2:298679fff37c | 1595 | /* |
mkarlsso | 2:298679fff37c | 1596 | std::vector<condition*>::size_type sz = conditionArray.size(); |
mkarlsso | 2:298679fff37c | 1597 | for (unsigned i = 0; i < sz; i++) { |
mkarlsso | 2:298679fff37c | 1598 | if (!conditionArray[i]->isTrue()) { |
mkarlsso | 2:298679fff37c | 1599 | result = false; |
mkarlsso | 2:298679fff37c | 1600 | //queuePtr->pcPtr->printf("Consition false\r\n"); |
mkarlsso | 2:298679fff37c | 1601 | } //else {queuePtr->pcPtr->printf("Consition true\r\n");} |
mkarlsso | 2:298679fff37c | 1602 | } |
mkarlsso | 2:298679fff37c | 1603 | */ |
mkarlsso | 2:298679fff37c | 1604 | |
mkarlsso | 2:298679fff37c | 1605 | if ((conditionToCheck!=NULL)&&(!conditionToCheck->isTrue())) { |
mkarlsso | 2:298679fff37c | 1606 | result = false; |
mkarlsso | 2:298679fff37c | 1607 | } |
mkarlsso | 2:298679fff37c | 1608 | |
mkarlsso | 2:298679fff37c | 1609 | return result; |
mkarlsso | 2:298679fff37c | 1610 | } |
mkarlsso | 2:298679fff37c | 1611 | |
mkarlsso | 2:298679fff37c | 1612 | void event::execute(void) { |
mkarlsso | 2:298679fff37c | 1613 | //called from the event queue. The condition is bypassed because it was already checked |
mkarlsso | 2:298679fff37c | 1614 | |
mkarlsso | 2:298679fff37c | 1615 | |
mkarlsso | 2:298679fff37c | 1616 | uint32_t timeAtStartExec = *globalTimeKeeperPtr; |
mkarlsso | 2:298679fff37c | 1617 | //std::vector<action*>::size_type sz = actionArray.size(); |
mkarlsso | 2:298679fff37c | 1618 | |
mkarlsso | 2:298679fff37c | 1619 | /* |
mkarlsso | 2:298679fff37c | 1620 | std::deque<action*>::size_type sz = actionArray.size(); |
mkarlsso | 2:298679fff37c | 1621 | |
mkarlsso | 2:298679fff37c | 1622 | for (unsigned i = 0; i < sz; i++) { |
mkarlsso | 2:298679fff37c | 1623 | actionArray[i]->execute(timeAtStartExec); |
mkarlsso | 2:298679fff37c | 1624 | |
mkarlsso | 2:298679fff37c | 1625 | } |
mkarlsso | 2:298679fff37c | 1626 | */ |
mkarlsso | 2:298679fff37c | 1627 | for (unsigned i = 0; i < numActions; i++) { |
mkarlsso | 2:298679fff37c | 1628 | actionArray[i]->execute(timeAtStartExec); |
mkarlsso | 2:298679fff37c | 1629 | |
mkarlsso | 2:298679fff37c | 1630 | } |
mkarlsso | 2:298679fff37c | 1631 | |
mkarlsso | 2:298679fff37c | 1632 | } |
mkarlsso | 2:298679fff37c | 1633 | |
mkarlsso | 2:298679fff37c | 1634 | //Attach an 'else' statement to the event |
mkarlsso | 2:298679fff37c | 1635 | void event::setNextElseEvent(event* eventInput) { |
mkarlsso | 2:298679fff37c | 1636 | nextElseEventPtr = eventInput; |
mkarlsso | 2:298679fff37c | 1637 | } |
mkarlsso | 2:298679fff37c | 1638 | |
mkarlsso | 2:298679fff37c | 1639 | |
mkarlsso | 2:298679fff37c | 1640 | //When we we call addToQueue() the condition is checked. If true, the event is added |
mkarlsso | 2:298679fff37c | 1641 | //to the queue, otherwise we check if there was an 'else' statement attached to the event. |
mkarlsso | 2:298679fff37c | 1642 | void event::addToQueue(void) { |
mkarlsso | 2:298679fff37c | 1643 | if (isConditionTrue()) { |
mkarlsso | 2:298679fff37c | 1644 | if ((timeLagIsConstant)&&(timeLag == 0)) { |
mkarlsso | 2:298679fff37c | 1645 | execute(); |
mkarlsso | 2:298679fff37c | 1646 | |
mkarlsso | 2:298679fff37c | 1647 | } else if (timeLagIsConstant) { |
mkarlsso | 2:298679fff37c | 1648 | queuePtr->addEventToQueue(this, this->timeLag); |
mkarlsso | 2:298679fff37c | 1649 | } else if ((!timeLagIsConstant)&&(*timeLagVar <= 0)) { |
mkarlsso | 2:298679fff37c | 1650 | execute(); |
mkarlsso | 2:298679fff37c | 1651 | } else { |
mkarlsso | 2:298679fff37c | 1652 | queuePtr->addEventToQueue(this, *timeLagVar); |
mkarlsso | 2:298679fff37c | 1653 | } |
mkarlsso | 2:298679fff37c | 1654 | } else if ((this->nextElseEventPtr != NULL)&&(whileLoopPeriod == 0)) { |
mkarlsso | 2:298679fff37c | 1655 | this->nextElseEventPtr->addToQueue(); |
mkarlsso | 2:298679fff37c | 1656 | } |
mkarlsso | 2:298679fff37c | 1657 | } |
mkarlsso | 2:298679fff37c | 1658 | |
mkarlsso | 2:298679fff37c | 1659 | //We can override the timeLag field and use another delay |
mkarlsso | 2:298679fff37c | 1660 | void event::addToQueue(uint32_t delay) { |
mkarlsso | 2:298679fff37c | 1661 | if (this->isConditionTrue()) { |
mkarlsso | 2:298679fff37c | 1662 | //if ((delay == 0) && (whileLoopPeriod == 0)) { |
mkarlsso | 2:298679fff37c | 1663 | if ((delay == 0)) { |
mkarlsso | 2:298679fff37c | 1664 | this->execute(); |
mkarlsso | 2:298679fff37c | 1665 | } else { |
mkarlsso | 2:298679fff37c | 1666 | queuePtr->addEventToQueue(this, delay); |
mkarlsso | 2:298679fff37c | 1667 | } |
mkarlsso | 2:298679fff37c | 1668 | } else if ((this->nextElseEventPtr != NULL)) { //&&(!hasWhileLoop)) { |
mkarlsso | 2:298679fff37c | 1669 | this->nextElseEventPtr->addToQueue(); |
mkarlsso | 2:298679fff37c | 1670 | } |
mkarlsso | 2:298679fff37c | 1671 | } |
mkarlsso | 2:298679fff37c | 1672 | |
mkarlsso | 2:298679fff37c | 1673 | |
mkarlsso | 2:298679fff37c | 1674 | |
mkarlsso | 2:298679fff37c | 1675 | functionItem::functionItem(action* actionInput, string tagInput): |
mkarlsso | 2:298679fff37c | 1676 | tag(tagInput), |
mkarlsso | 2:298679fff37c | 1677 | actionPtr(actionInput) { |
mkarlsso | 2:298679fff37c | 1678 | } |
mkarlsso | 2:298679fff37c | 1679 | |
mkarlsso | 2:298679fff37c | 1680 | functionItem::~functionItem() { |
mkarlsso | 2:298679fff37c | 1681 | delete actionPtr; |
mkarlsso | 2:298679fff37c | 1682 | } |
mkarlsso | 2:298679fff37c | 1683 | |
mkarlsso | 4:34aca2142df9 | 1684 | blockBuffer::blockBuffer() { |
mkarlsso | 4:34aca2142df9 | 1685 | bufferWritePos = 0; |
mkarlsso | 4:34aca2142df9 | 1686 | bufferWritePos = 0; |
mkarlsso | 4:34aca2142df9 | 1687 | _linesAvailable = 0; |
mkarlsso | 4:34aca2142df9 | 1688 | |
mkarlsso | 4:34aca2142df9 | 1689 | } |
mkarlsso | 4:34aca2142df9 | 1690 | |
mkarlsso | 4:34aca2142df9 | 1691 | bool blockBuffer::addLine(char *input, int numChars) { |
mkarlsso | 4:34aca2142df9 | 1692 | |
mkarlsso | 4:34aca2142df9 | 1693 | if (bufferWritePos+numChars >= INPUTCHARBUFFERSIZE) { |
mkarlsso | 4:34aca2142df9 | 1694 | return false; |
mkarlsso | 4:34aca2142df9 | 1695 | } |
mkarlsso | 4:34aca2142df9 | 1696 | for(int i=0;i<numChars;i++) { |
mkarlsso | 4:34aca2142df9 | 1697 | charBuffer[bufferWritePos] = input[i]; |
mkarlsso | 4:34aca2142df9 | 1698 | bufferWritePos++; |
mkarlsso | 4:34aca2142df9 | 1699 | } |
mkarlsso | 4:34aca2142df9 | 1700 | _linesAvailable++; |
mkarlsso | 4:34aca2142df9 | 1701 | return true; |
mkarlsso | 4:34aca2142df9 | 1702 | } |
mkarlsso | 4:34aca2142df9 | 1703 | |
mkarlsso | 4:34aca2142df9 | 1704 | string blockBuffer::getNextLine() { |
mkarlsso | 4:34aca2142df9 | 1705 | |
mkarlsso | 4:34aca2142df9 | 1706 | string outputLine; |
mkarlsso | 4:34aca2142df9 | 1707 | int endOfLinePos = bufferReadPos; |
mkarlsso | 4:34aca2142df9 | 1708 | bool endOfLineFound = false; |
mkarlsso | 4:34aca2142df9 | 1709 | if (_linesAvailable > 0) { |
mkarlsso | 4:34aca2142df9 | 1710 | //Find the end of the next line |
mkarlsso | 4:34aca2142df9 | 1711 | while (endOfLinePos < INPUTCHARBUFFERSIZE) { |
mkarlsso | 4:34aca2142df9 | 1712 | if (charBuffer[endOfLinePos] == '\0') { |
mkarlsso | 4:34aca2142df9 | 1713 | endOfLineFound = true; |
mkarlsso | 4:34aca2142df9 | 1714 | break; |
mkarlsso | 4:34aca2142df9 | 1715 | } |
mkarlsso | 4:34aca2142df9 | 1716 | endOfLinePos++; |
mkarlsso | 4:34aca2142df9 | 1717 | } |
mkarlsso | 4:34aca2142df9 | 1718 | |
mkarlsso | 4:34aca2142df9 | 1719 | //If the end was found, copy to output string |
mkarlsso | 4:34aca2142df9 | 1720 | if (endOfLineFound) { |
mkarlsso | 4:34aca2142df9 | 1721 | outputLine.append(charBuffer+bufferReadPos,endOfLinePos-bufferReadPos); |
mkarlsso | 4:34aca2142df9 | 1722 | bufferReadPos = endOfLinePos+1; |
mkarlsso | 4:34aca2142df9 | 1723 | _linesAvailable--; |
mkarlsso | 4:34aca2142df9 | 1724 | } |
mkarlsso | 4:34aca2142df9 | 1725 | } |
mkarlsso | 4:34aca2142df9 | 1726 | if (_linesAvailable == 0) { |
mkarlsso | 4:34aca2142df9 | 1727 | //we have read out all of the lines, so reset the buffer for the next block. |
mkarlsso | 4:34aca2142df9 | 1728 | resetBuffer(); |
mkarlsso | 4:34aca2142df9 | 1729 | } |
mkarlsso | 4:34aca2142df9 | 1730 | return outputLine; |
mkarlsso | 4:34aca2142df9 | 1731 | |
mkarlsso | 4:34aca2142df9 | 1732 | } |
mkarlsso | 4:34aca2142df9 | 1733 | |
mkarlsso | 4:34aca2142df9 | 1734 | int16_t blockBuffer::linesAvailable() { |
mkarlsso | 4:34aca2142df9 | 1735 | return _linesAvailable; |
mkarlsso | 4:34aca2142df9 | 1736 | } |
mkarlsso | 4:34aca2142df9 | 1737 | |
mkarlsso | 4:34aca2142df9 | 1738 | void blockBuffer::resetBuffer() { |
mkarlsso | 4:34aca2142df9 | 1739 | _linesAvailable = 0; |
mkarlsso | 4:34aca2142df9 | 1740 | bufferReadPos = 0; |
mkarlsso | 4:34aca2142df9 | 1741 | bufferWritePos = 0; |
mkarlsso | 4:34aca2142df9 | 1742 | } |
mkarlsso | 4:34aca2142df9 | 1743 | |
mkarlsso | 4:34aca2142df9 | 1744 | bool blockBuffer::empty() { |
mkarlsso | 4:34aca2142df9 | 1745 | return (_linesAvailable == 0); |
mkarlsso | 4:34aca2142df9 | 1746 | } |
mkarlsso | 4:34aca2142df9 | 1747 | |
mkarlsso | 4:34aca2142df9 | 1748 | |
mkarlsso | 2:298679fff37c | 1749 | scriptStream::scriptStream(Serial* serialInput, digitalPort** portVectorInput, int numPortsInput, eventQueue* queueInput): |
mkarlsso | 2:298679fff37c | 1750 | portVector(portVectorInput), |
mkarlsso | 2:298679fff37c | 1751 | numPorts(numPortsInput), |
mkarlsso | 2:298679fff37c | 1752 | pcPtr(serialInput), |
mkarlsso | 2:298679fff37c | 1753 | queuePtr(queueInput) { |
mkarlsso | 2:298679fff37c | 1754 | |
mkarlsso | 2:298679fff37c | 1755 | currentPort = -1; |
mkarlsso | 2:298679fff37c | 1756 | currentTriggerPort = -1; |
mkarlsso | 2:298679fff37c | 1757 | currentTriggerDir = 1; |
mkarlsso | 2:298679fff37c | 1758 | currentFunction = -1; |
mkarlsso | 4:34aca2142df9 | 1759 | |
mkarlsso | 4:34aca2142df9 | 1760 | randomSeedCounter = 0; //used for seeding random numbers |
mkarlsso | 2:298679fff37c | 1761 | |
mkarlsso | 2:298679fff37c | 1762 | lineError = false; |
mkarlsso | 2:298679fff37c | 1763 | blockDepth = 0; |
mkarlsso | 2:298679fff37c | 1764 | ifBlockInit = false; |
mkarlsso | 2:298679fff37c | 1765 | whileBlockInit = false; |
mkarlsso | 2:298679fff37c | 1766 | elseFlag = false; |
mkarlsso | 4:34aca2142df9 | 1767 | thenFlag = false; |
mkarlsso | 2:298679fff37c | 1768 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 1769 | |
mkarlsso | 4:34aca2142df9 | 1770 | |
mkarlsso | 2:298679fff37c | 1771 | } |
mkarlsso | 2:298679fff37c | 1772 | |
mkarlsso | 4:34aca2142df9 | 1773 | |
mkarlsso | 2:298679fff37c | 1774 | void scriptStream::addLineToCurrentBlock(char* lineInput) { |
mkarlsso | 2:298679fff37c | 1775 | |
mkarlsso | 2:298679fff37c | 1776 | bool compile = false; |
mkarlsso | 2:298679fff37c | 1777 | bool keep = false; |
mkarlsso | 4:34aca2142df9 | 1778 | int numCharInLine = 0; |
mkarlsso | 4:34aca2142df9 | 1779 | //A line ending with ';' then carriage return initiates the compile sequence |
mkarlsso | 4:34aca2142df9 | 1780 | //Otherwise, add the line to the buffer and compile later |
mkarlsso | 4:34aca2142df9 | 1781 | for (int i = 0; i < 256; i++) { |
mkarlsso | 4:34aca2142df9 | 1782 | numCharInLine++; |
mkarlsso | 2:298679fff37c | 1783 | if (lineInput[i] == ';') { |
mkarlsso | 2:298679fff37c | 1784 | compile = true; |
mkarlsso | 2:298679fff37c | 1785 | } else if (lineInput[i] == ' ') { |
mkarlsso | 2:298679fff37c | 1786 | continue; |
mkarlsso | 2:298679fff37c | 1787 | } else if (lineInput[i] == '\0') { |
mkarlsso | 2:298679fff37c | 1788 | break; |
mkarlsso | 2:298679fff37c | 1789 | } else { |
mkarlsso | 4:34aca2142df9 | 1790 | keep = true; |
mkarlsso | 2:298679fff37c | 1791 | compile = false; |
mkarlsso | 2:298679fff37c | 1792 | } |
mkarlsso | 4:34aca2142df9 | 1793 | |
mkarlsso | 2:298679fff37c | 1794 | } |
mkarlsso | 4:34aca2142df9 | 1795 | //if (keep) currentBlock.insert(currentBlock.begin(),string(lineInput)); |
mkarlsso | 4:34aca2142df9 | 1796 | if (keep) { |
mkarlsso | 4:34aca2142df9 | 1797 | if (!currentBlock.addLine(lineInput,numCharInLine)) { |
mkarlsso | 4:34aca2142df9 | 1798 | pcPtr->printf("Error: script input buffer full. The block is too long.\r\n"); |
mkarlsso | 4:34aca2142df9 | 1799 | currentBlock.resetBuffer(); |
mkarlsso | 4:34aca2142df9 | 1800 | compile = false; |
mkarlsso | 4:34aca2142df9 | 1801 | } |
mkarlsso | 4:34aca2142df9 | 1802 | } |
mkarlsso | 4:34aca2142df9 | 1803 | if (compile) { |
mkarlsso | 4:34aca2142df9 | 1804 | parseBlock(); |
mkarlsso | 4:34aca2142df9 | 1805 | } |
mkarlsso | 2:298679fff37c | 1806 | |
mkarlsso | 2:298679fff37c | 1807 | } |
mkarlsso | 2:298679fff37c | 1808 | |
mkarlsso | 2:298679fff37c | 1809 | |
mkarlsso | 2:298679fff37c | 1810 | //SCRIPT PARSING - all script commands are defined here. |
mkarlsso | 2:298679fff37c | 1811 | //------------------------------------------------------- |
mkarlsso | 2:298679fff37c | 1812 | void scriptStream::parseBlock() { |
mkarlsso | 2:298679fff37c | 1813 | |
mkarlsso | 4:34aca2142df9 | 1814 | |
mkarlsso | 2:298679fff37c | 1815 | lineError = false; |
mkarlsso | 2:298679fff37c | 1816 | blockDepth = 0; |
mkarlsso | 2:298679fff37c | 1817 | ifBlockInit = false; |
mkarlsso | 2:298679fff37c | 1818 | whileBlockInit = false; |
mkarlsso | 2:298679fff37c | 1819 | elseFlag = false; |
mkarlsso | 2:298679fff37c | 1820 | thenFlag = false; |
mkarlsso | 2:298679fff37c | 1821 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 1822 | |
mkarlsso | 4:34aca2142df9 | 1823 | |
mkarlsso | 2:298679fff37c | 1824 | std::size_t stringInd = 0; |
mkarlsso | 2:298679fff37c | 1825 | |
mkarlsso | 2:298679fff37c | 1826 | bool wholeLineEvaluated = false; |
mkarlsso | 2:298679fff37c | 1827 | |
mkarlsso | 2:298679fff37c | 1828 | //pcPtr->printf("\r\n"); |
mkarlsso | 2:298679fff37c | 1829 | while (!currentBlock.empty()) { |
mkarlsso | 4:34aca2142df9 | 1830 | |
mkarlsso | 4:34aca2142df9 | 1831 | |
mkarlsso | 2:298679fff37c | 1832 | wholeLineEvaluated = false; |
mkarlsso | 4:34aca2142df9 | 1833 | //tmpLine = currentBlock.back(); |
mkarlsso | 4:34aca2142df9 | 1834 | tmpLine = currentBlock.getNextLine(); |
mkarlsso | 4:34aca2142df9 | 1835 | |
mkarlsso | 2:298679fff37c | 1836 | lineError = false; |
mkarlsso | 2:298679fff37c | 1837 | //remove tabs |
mkarlsso | 2:298679fff37c | 1838 | std::size_t found = tmpLine.find_first_of(9); //tab |
mkarlsso | 2:298679fff37c | 1839 | while (found!=std::string::npos) { |
mkarlsso | 2:298679fff37c | 1840 | tmpLine[found]= ' '; |
mkarlsso | 2:298679fff37c | 1841 | found=tmpLine.find_first_of(9,found+1); |
mkarlsso | 2:298679fff37c | 1842 | } |
mkarlsso | 2:298679fff37c | 1843 | |
mkarlsso | 2:298679fff37c | 1844 | found = tmpLine.find_first_of(37); //% for commenting |
mkarlsso | 2:298679fff37c | 1845 | if (found!=std::string::npos) { |
mkarlsso | 2:298679fff37c | 1846 | for (int p=found; p<tmpLine.size() ;p++) { |
mkarlsso | 2:298679fff37c | 1847 | tmpLine[p]= ' '; |
mkarlsso | 2:298679fff37c | 1848 | } |
mkarlsso | 2:298679fff37c | 1849 | } |
mkarlsso | 2:298679fff37c | 1850 | |
mkarlsso | 2:298679fff37c | 1851 | //break up the line into tokens separated by spaces |
mkarlsso | 2:298679fff37c | 1852 | tokenize(tmpLine,tokens," ;"); |
mkarlsso | 2:298679fff37c | 1853 | |
mkarlsso | 2:298679fff37c | 1854 | std::vector<string>::size_type sz = tokens.size(); |
mkarlsso | 2:298679fff37c | 1855 | |
mkarlsso | 2:298679fff37c | 1856 | for (unsigned i = 0; i < sz; i++) { |
mkarlsso | 2:298679fff37c | 1857 | //pcPtr->printf("%s", tokens[i].c_str()); |
mkarlsso | 2:298679fff37c | 1858 | |
mkarlsso | 2:298679fff37c | 1859 | |
mkarlsso | 2:298679fff37c | 1860 | //end statement signals the end of a block----------------------------------------- |
mkarlsso | 2:298679fff37c | 1861 | if (tokens[i].compare("end") == 0) { //ends the current block |
mkarlsso | 2:298679fff37c | 1862 | |
mkarlsso | 2:298679fff37c | 1863 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 1864 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 1865 | lineError = true; |
mkarlsso | 2:298679fff37c | 1866 | } |
mkarlsso | 2:298679fff37c | 1867 | |
mkarlsso | 2:298679fff37c | 1868 | ifBlockInit = false; |
mkarlsso | 2:298679fff37c | 1869 | whileBlockInit = false; |
mkarlsso | 2:298679fff37c | 1870 | elseFlag = false; |
mkarlsso | 2:298679fff37c | 1871 | |
mkarlsso | 2:298679fff37c | 1872 | if (blockDepth > 0) { |
mkarlsso | 2:298679fff37c | 1873 | if (blockDepth == 1) { |
mkarlsso | 2:298679fff37c | 1874 | |
mkarlsso | 2:298679fff37c | 1875 | |
mkarlsso | 2:298679fff37c | 1876 | //pcPtr->printf("Close trigger block for port %d\r\n",currentTriggerPort); |
mkarlsso | 2:298679fff37c | 1877 | currentTriggerPort = -1; |
mkarlsso | 2:298679fff37c | 1878 | |
mkarlsso | 2:298679fff37c | 1879 | |
mkarlsso | 2:298679fff37c | 1880 | |
mkarlsso | 2:298679fff37c | 1881 | blockDepth = 0; |
mkarlsso | 2:298679fff37c | 1882 | } else if (blockDepth > 1) { |
mkarlsso | 2:298679fff37c | 1883 | //pcPtr->printf("Close block\r\n"); |
mkarlsso | 2:298679fff37c | 1884 | blockDepth = blockDepth - 1; |
mkarlsso | 2:298679fff37c | 1885 | } |
mkarlsso | 2:298679fff37c | 1886 | |
mkarlsso | 2:298679fff37c | 1887 | while ((tmpEventPtrArray.back()->blockType == 3) || (tmpEventPtrArray.back()->blockType == 4)){ |
mkarlsso | 2:298679fff37c | 1888 | tmpEventPtrArray.pop_back(); //recursively remove the pointers to all else blocks |
mkarlsso | 2:298679fff37c | 1889 | } |
mkarlsso | 2:298679fff37c | 1890 | tmpEventPtrArray.pop_back(); //remove the pointer to the finished block |
mkarlsso | 4:34aca2142df9 | 1891 | |
mkarlsso | 2:298679fff37c | 1892 | } else { |
mkarlsso | 2:298679fff37c | 1893 | pcPtr->printf("Error: End statement without block\r\n"); |
mkarlsso | 2:298679fff37c | 1894 | lineError = true; |
mkarlsso | 2:298679fff37c | 1895 | } |
mkarlsso | 2:298679fff37c | 1896 | |
mkarlsso | 2:298679fff37c | 1897 | |
mkarlsso | 2:298679fff37c | 1898 | |
mkarlsso | 2:298679fff37c | 1899 | //sound statement used to play wave files------------------------------------------------ |
mkarlsso | 2:298679fff37c | 1900 | //example: sound('soundfile') |
mkarlsso | 2:298679fff37c | 1901 | // sound(stop) |
mkarlsso | 2:298679fff37c | 1902 | } else if (tokens[i].find("sound(") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 1903 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 1904 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 1905 | lineError = true; |
mkarlsso | 2:298679fff37c | 1906 | } |
mkarlsso | 2:298679fff37c | 1907 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 1908 | int pos1 = tmpLine.find("sound(")+6; |
mkarlsso | 2:298679fff37c | 1909 | int pos2 = tmpLine.find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 1910 | string dispVar = tmpLine.substr(pos1,pos2-pos1); |
mkarlsso | 2:298679fff37c | 1911 | |
mkarlsso | 2:298679fff37c | 1912 | int* tmpVar = findIntVariable(dispVar); |
mkarlsso | 2:298679fff37c | 1913 | bool isText = false; |
mkarlsso | 2:298679fff37c | 1914 | bool stopSignal = false; |
mkarlsso | 2:298679fff37c | 1915 | bool resetSignal = false; |
mkarlsso | 2:298679fff37c | 1916 | if (tmpVar == NULL) { |
mkarlsso | 2:298679fff37c | 1917 | if ((tmpLine.compare(pos1,1,"'")==0) && (tmpLine.compare(pos2-1,1,"'")==0)) { |
mkarlsso | 2:298679fff37c | 1918 | isText = true; |
mkarlsso | 2:298679fff37c | 1919 | } else if (dispVar.compare("stop") == 0) { |
mkarlsso | 2:298679fff37c | 1920 | stopSignal = true; |
mkarlsso | 2:298679fff37c | 1921 | } else if (dispVar.compare("reset") == 0) { |
mkarlsso | 2:298679fff37c | 1922 | resetSignal = true; |
mkarlsso | 2:298679fff37c | 1923 | } else { |
mkarlsso | 2:298679fff37c | 1924 | pcPtr->printf("Error: variable input to sound() does not exist\r\n"); |
mkarlsso | 2:298679fff37c | 1925 | lineError = true; |
mkarlsso | 2:298679fff37c | 1926 | } |
mkarlsso | 2:298679fff37c | 1927 | } |
mkarlsso | 2:298679fff37c | 1928 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 1929 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 1930 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 1931 | lineError = true; |
mkarlsso | 2:298679fff37c | 1932 | } |
mkarlsso | 2:298679fff37c | 1933 | if (!lineError && (blockDepth == 0)) { |
mkarlsso | 2:298679fff37c | 1934 | //we are not inside a block structure, so play sound now |
mkarlsso | 2:298679fff37c | 1935 | if (stopSignal) { |
mkarlsso | 2:298679fff37c | 1936 | soundControl S; |
mkarlsso | 2:298679fff37c | 1937 | S.setPlayback(false); |
mkarlsso | 2:298679fff37c | 1938 | S.execute(); |
mkarlsso | 2:298679fff37c | 1939 | } else if (resetSignal) { |
mkarlsso | 2:298679fff37c | 1940 | soundControl S; |
mkarlsso | 2:298679fff37c | 1941 | S.setReset(); |
mkarlsso | 2:298679fff37c | 1942 | S.execute(); |
mkarlsso | 2:298679fff37c | 1943 | } else if (isText) { |
mkarlsso | 2:298679fff37c | 1944 | |
mkarlsso | 2:298679fff37c | 1945 | if (pos2-pos1-2 <= 20) { |
mkarlsso | 2:298679fff37c | 1946 | |
mkarlsso | 2:298679fff37c | 1947 | soundControl S; |
mkarlsso | 2:298679fff37c | 1948 | S.setFile(tmpLine.substr(pos1+1,pos2-pos1-2)); |
mkarlsso | 2:298679fff37c | 1949 | S.execute(); |
mkarlsso | 2:298679fff37c | 1950 | } else { |
mkarlsso | 2:298679fff37c | 1951 | pcPtr->printf("Error: sound file names must be 20 characters or less.\r\n"); |
mkarlsso | 2:298679fff37c | 1952 | lineError = true; |
mkarlsso | 2:298679fff37c | 1953 | } |
mkarlsso | 2:298679fff37c | 1954 | } else { |
mkarlsso | 2:298679fff37c | 1955 | pcPtr->printf("Error: variable input to sound() not yet supported. Enter a string in single quotes.\r\n"); |
mkarlsso | 2:298679fff37c | 1956 | lineError = true; |
mkarlsso | 2:298679fff37c | 1957 | } |
mkarlsso | 2:298679fff37c | 1958 | |
mkarlsso | 2:298679fff37c | 1959 | } else if (!lineError && (blockDepth > 0) ){ |
mkarlsso | 2:298679fff37c | 1960 | //the disp function was put inside a block |
mkarlsso | 2:298679fff37c | 1961 | if (stopSignal) { |
mkarlsso | 2:298679fff37c | 1962 | soundControl* sPtr = new soundControl(); |
mkarlsso | 2:298679fff37c | 1963 | sPtr->setPlayback(false); |
mkarlsso | 2:298679fff37c | 1964 | //action* tmpAction = new action(sPtr); |
mkarlsso | 2:298679fff37c | 1965 | tmpAction->set(sPtr); |
mkarlsso | 2:298679fff37c | 1966 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 1967 | } else if (resetSignal) { |
mkarlsso | 2:298679fff37c | 1968 | soundControl* sPtr = new soundControl(); |
mkarlsso | 2:298679fff37c | 1969 | sPtr->setReset(); |
mkarlsso | 2:298679fff37c | 1970 | //action* tmpAction = new action(sPtr); |
mkarlsso | 2:298679fff37c | 1971 | tmpAction->set(sPtr); |
mkarlsso | 2:298679fff37c | 1972 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 1973 | } else if (isText) { |
mkarlsso | 2:298679fff37c | 1974 | |
mkarlsso | 2:298679fff37c | 1975 | if (pos2-pos1-2 <= 20) { |
mkarlsso | 2:298679fff37c | 1976 | soundControl* sPtr = new soundControl(); |
mkarlsso | 2:298679fff37c | 1977 | sPtr->setFile(tmpLine.substr(pos1+1,pos2-pos1-2)); |
mkarlsso | 2:298679fff37c | 1978 | //action* tmpAction = new action(sPtr); |
mkarlsso | 2:298679fff37c | 1979 | tmpAction->set(sPtr); |
mkarlsso | 2:298679fff37c | 1980 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 1981 | } else { |
mkarlsso | 2:298679fff37c | 1982 | pcPtr->printf("Error: sound file names must be 20 characters or less.\r\n"); |
mkarlsso | 2:298679fff37c | 1983 | lineError = true; |
mkarlsso | 2:298679fff37c | 1984 | } |
mkarlsso | 2:298679fff37c | 1985 | } else { |
mkarlsso | 2:298679fff37c | 1986 | pcPtr->printf("Error: variable input to sound() not yet supported. Enter a string in single quotes.\r\n"); |
mkarlsso | 2:298679fff37c | 1987 | lineError = true; |
mkarlsso | 2:298679fff37c | 1988 | } |
mkarlsso | 2:298679fff37c | 1989 | |
mkarlsso | 2:298679fff37c | 1990 | //pcPtr->printf("Sound action\r\n"); |
mkarlsso | 2:298679fff37c | 1991 | } |
mkarlsso | 2:298679fff37c | 1992 | |
mkarlsso | 2:298679fff37c | 1993 | } else if (tokens[i].find("volume(") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 1994 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 1995 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 1996 | lineError = true; |
mkarlsso | 2:298679fff37c | 1997 | } |
mkarlsso | 2:298679fff37c | 1998 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 1999 | int pos1 = tmpLine.find("volume(")+7; |
mkarlsso | 2:298679fff37c | 2000 | int pos2 = tmpLine.find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 2001 | string dispVar = tmpLine.substr(pos1,pos2-pos1); |
mkarlsso | 2:298679fff37c | 2002 | |
mkarlsso | 2:298679fff37c | 2003 | int* tmpVar = findIntVariable(dispVar); |
mkarlsso | 2:298679fff37c | 2004 | bool isText = false; |
mkarlsso | 2:298679fff37c | 2005 | if (tmpVar == NULL) { |
mkarlsso | 2:298679fff37c | 2006 | if (isNumber(dispVar)) { |
mkarlsso | 2:298679fff37c | 2007 | isText = true; |
mkarlsso | 2:298679fff37c | 2008 | } else { |
mkarlsso | 2:298679fff37c | 2009 | pcPtr->printf("Error: variable input to volume() does not exist\r\n"); |
mkarlsso | 2:298679fff37c | 2010 | lineError = true; |
mkarlsso | 2:298679fff37c | 2011 | } |
mkarlsso | 2:298679fff37c | 2012 | } |
mkarlsso | 2:298679fff37c | 2013 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2014 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2015 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2016 | lineError = true; |
mkarlsso | 2:298679fff37c | 2017 | } |
mkarlsso | 2:298679fff37c | 2018 | if (!lineError && (blockDepth == 0)) { |
mkarlsso | 2:298679fff37c | 2019 | //we are not inside a block structure, so play sound now |
mkarlsso | 2:298679fff37c | 2020 | if (isText) { |
mkarlsso | 2:298679fff37c | 2021 | int newVolume = atoi(dispVar.data()); |
mkarlsso | 2:298679fff37c | 2022 | if ((newVolume >=0)&&(newVolume <= 255)) { |
mkarlsso | 2:298679fff37c | 2023 | soundControl S; |
mkarlsso | 2:298679fff37c | 2024 | S.setVolume(newVolume); |
mkarlsso | 2:298679fff37c | 2025 | S.execute(); |
mkarlsso | 2:298679fff37c | 2026 | } else { |
mkarlsso | 2:298679fff37c | 2027 | pcPtr->printf("Error: sound volume must be between 0 and 255 .\r\n"); |
mkarlsso | 2:298679fff37c | 2028 | lineError = true; |
mkarlsso | 2:298679fff37c | 2029 | } |
mkarlsso | 2:298679fff37c | 2030 | } else { |
mkarlsso | 2:298679fff37c | 2031 | soundControl S; |
mkarlsso | 2:298679fff37c | 2032 | S.setVolume(tmpVar); |
mkarlsso | 2:298679fff37c | 2033 | S.execute(); |
mkarlsso | 2:298679fff37c | 2034 | } |
mkarlsso | 2:298679fff37c | 2035 | |
mkarlsso | 2:298679fff37c | 2036 | } else if (!lineError && (blockDepth > 0) ){ |
mkarlsso | 2:298679fff37c | 2037 | //the disp function was put inside a block |
mkarlsso | 2:298679fff37c | 2038 | if (isText) { |
mkarlsso | 2:298679fff37c | 2039 | int newVolume = atoi(dispVar.data()); |
mkarlsso | 2:298679fff37c | 2040 | |
mkarlsso | 2:298679fff37c | 2041 | soundControl* sPtr = new soundControl(); |
mkarlsso | 2:298679fff37c | 2042 | sPtr->setVolume(newVolume); |
mkarlsso | 2:298679fff37c | 2043 | |
mkarlsso | 2:298679fff37c | 2044 | //action* tmpAction = new action(sPtr); |
mkarlsso | 2:298679fff37c | 2045 | tmpAction->set(sPtr); |
mkarlsso | 2:298679fff37c | 2046 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2047 | |
mkarlsso | 2:298679fff37c | 2048 | } else { |
mkarlsso | 2:298679fff37c | 2049 | soundControl* sPtr = new soundControl(); |
mkarlsso | 2:298679fff37c | 2050 | sPtr->setVolume(tmpVar); |
mkarlsso | 2:298679fff37c | 2051 | //action* tmpAction = new action(sPtr); |
mkarlsso | 2:298679fff37c | 2052 | tmpAction->set(sPtr); |
mkarlsso | 2:298679fff37c | 2053 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2054 | } |
mkarlsso | 2:298679fff37c | 2055 | |
mkarlsso | 2:298679fff37c | 2056 | //pcPtr->printf("Volume action\r\n"); |
mkarlsso | 2:298679fff37c | 2057 | } |
mkarlsso | 2:298679fff37c | 2058 | //clock statement used to is used to control the clock------------------------- |
mkarlsso | 2:298679fff37c | 2059 | //example: clock(reset); clock(slave); clock(standalone) |
mkarlsso | 2:298679fff37c | 2060 | } else if (tokens[i].find("clock(") != std::string::npos) { //clock commands |
mkarlsso | 2:298679fff37c | 2061 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2062 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2063 | lineError = true; |
mkarlsso | 2:298679fff37c | 2064 | } |
mkarlsso | 2:298679fff37c | 2065 | if (blockDepth > 0) { |
mkarlsso | 2:298679fff37c | 2066 | pcPtr->printf("Error: clock commands only allowed outside of block structure\r\n"); |
mkarlsso | 2:298679fff37c | 2067 | lineError = true; |
mkarlsso | 2:298679fff37c | 2068 | } |
mkarlsso | 2:298679fff37c | 2069 | |
mkarlsso | 2:298679fff37c | 2070 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2071 | int pos1 = tmpLine.find("clock(")+6; |
mkarlsso | 2:298679fff37c | 2072 | int pos2 = tmpLine.find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 2073 | string dispVar = tmpLine.substr(pos1,pos2-pos1); |
mkarlsso | 2:298679fff37c | 2074 | |
mkarlsso | 2:298679fff37c | 2075 | |
mkarlsso | 2:298679fff37c | 2076 | if (blockDepth == 0) { |
mkarlsso | 2:298679fff37c | 2077 | if (dispVar.compare("reset") == 0) { |
mkarlsso | 2:298679fff37c | 2078 | resetTimer = true; |
mkarlsso | 2:298679fff37c | 2079 | queuePtr->eraseQueue(); |
mkarlsso | 2:298679fff37c | 2080 | textDisplay.send(string("Clock reset to 0\r\n")); |
mkarlsso | 2:298679fff37c | 2081 | //pcPtr->printf("Clock reset to 0\r\n"); |
mkarlsso | 2:298679fff37c | 2082 | } else if (dispVar.compare("slave") == 0) { |
mkarlsso | 2:298679fff37c | 2083 | if (!clockSlave) { |
mkarlsso | 2:298679fff37c | 2084 | changeToSlave = true; |
mkarlsso | 2:298679fff37c | 2085 | textDisplay.send(string("Slave mode\r\n")); |
mkarlsso | 2:298679fff37c | 2086 | //pcPtr->printf("Slave mode\r\n"); |
mkarlsso | 2:298679fff37c | 2087 | } |
mkarlsso | 2:298679fff37c | 2088 | } else if (dispVar.compare("standalone") == 0) { |
mkarlsso | 2:298679fff37c | 2089 | if (clockSlave) { |
mkarlsso | 2:298679fff37c | 2090 | changeToStandAlone = true; |
mkarlsso | 2:298679fff37c | 2091 | textDisplay.send(string("Standalone mode\r\n")); |
mkarlsso | 2:298679fff37c | 2092 | //pcPtr->printf("Standalone mode\r\n"); |
mkarlsso | 2:298679fff37c | 2093 | } |
mkarlsso | 2:298679fff37c | 2094 | } else { |
mkarlsso | 2:298679fff37c | 2095 | pcPtr->printf("Clock control statement not understood\r\n"); |
mkarlsso | 2:298679fff37c | 2096 | lineError = true; |
mkarlsso | 2:298679fff37c | 2097 | } |
mkarlsso | 2:298679fff37c | 2098 | } |
mkarlsso | 2:298679fff37c | 2099 | } |
mkarlsso | 2:298679fff37c | 2100 | |
mkarlsso | 2:298679fff37c | 2101 | //disp command used to display text via serial--------------------------------------- |
mkarlsso | 2:298679fff37c | 2102 | //example: disp('hello'); disp(myVar) |
mkarlsso | 2:298679fff37c | 2103 | } else if (tokens[i].find("disp(") != std::string::npos) { //display value of variable |
mkarlsso | 2:298679fff37c | 2104 | |
mkarlsso | 2:298679fff37c | 2105 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2106 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2107 | lineError = true; |
mkarlsso | 2:298679fff37c | 2108 | } |
mkarlsso | 2:298679fff37c | 2109 | |
mkarlsso | 2:298679fff37c | 2110 | //int pos1 = tokens[i].find("disp(")+5; |
mkarlsso | 2:298679fff37c | 2111 | //int pos2 = tokens[i].find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 2112 | //string dispVar = tokens[i].substr(pos1,pos2-pos1); |
mkarlsso | 2:298679fff37c | 2113 | |
mkarlsso | 2:298679fff37c | 2114 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2115 | int pos1 = tmpLine.find("disp(")+5; |
mkarlsso | 2:298679fff37c | 2116 | int pos2 = tmpLine.find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 2117 | string dispVar = tmpLine.substr(pos1,pos2-pos1); |
mkarlsso | 2:298679fff37c | 2118 | |
mkarlsso | 2:298679fff37c | 2119 | int* tmpVar = findIntVariable(dispVar); |
mkarlsso | 2:298679fff37c | 2120 | bool isText = false; |
mkarlsso | 2:298679fff37c | 2121 | if (tmpVar == NULL) { |
mkarlsso | 2:298679fff37c | 2122 | if ((tmpLine.compare(pos1,1,"'")==0) && (tmpLine.compare(pos2-1,1,"'")==0)) { |
mkarlsso | 2:298679fff37c | 2123 | isText = true; |
mkarlsso | 2:298679fff37c | 2124 | } else { |
mkarlsso | 2:298679fff37c | 2125 | pcPtr->printf("Error: variable to display does not exist\r\n"); |
mkarlsso | 2:298679fff37c | 2126 | lineError = true; |
mkarlsso | 2:298679fff37c | 2127 | } |
mkarlsso | 2:298679fff37c | 2128 | } |
mkarlsso | 2:298679fff37c | 2129 | displayAction* dPtr = findFirstUnUsed(displayActionBlock, NUMDISPLAYACTIONS); |
mkarlsso | 2:298679fff37c | 2130 | if (dPtr == NULL) { |
mkarlsso | 2:298679fff37c | 2131 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2132 | lineError = true; |
mkarlsso | 2:298679fff37c | 2133 | } |
mkarlsso | 2:298679fff37c | 2134 | if (!lineError && (blockDepth == 0)) { |
mkarlsso | 2:298679fff37c | 2135 | //we are not inside a block structure, so display now |
mkarlsso | 2:298679fff37c | 2136 | |
mkarlsso | 2:298679fff37c | 2137 | if (isText) { |
mkarlsso | 2:298679fff37c | 2138 | //displayAction* dPtr = new displayAction(tmpLine.substr(pos1+1,pos2-pos1-2), pcPtr); |
mkarlsso | 2:298679fff37c | 2139 | dPtr->set(tmpLine.substr(pos1+1,pos2-pos1-2), pcPtr); |
mkarlsso | 2:298679fff37c | 2140 | dPtr->execute(); |
mkarlsso | 2:298679fff37c | 2141 | //delete dPtr; |
mkarlsso | 2:298679fff37c | 2142 | dPtr->release(); |
mkarlsso | 2:298679fff37c | 2143 | } else { |
mkarlsso | 2:298679fff37c | 2144 | //displayAction* dPtr = new displayAction(tmpVar, dispVar, pcPtr); |
mkarlsso | 2:298679fff37c | 2145 | dPtr->set(tmpVar, dispVar, pcPtr); |
mkarlsso | 2:298679fff37c | 2146 | dPtr->execute(); |
mkarlsso | 2:298679fff37c | 2147 | //delete dPtr; |
mkarlsso | 2:298679fff37c | 2148 | dPtr->release(); |
mkarlsso | 2:298679fff37c | 2149 | } |
mkarlsso | 2:298679fff37c | 2150 | |
mkarlsso | 2:298679fff37c | 2151 | } else if (!lineError && (blockDepth > 0) ){ |
mkarlsso | 2:298679fff37c | 2152 | //the disp function was put inside a block |
mkarlsso | 2:298679fff37c | 2153 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2154 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2155 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2156 | lineError = true; |
mkarlsso | 2:298679fff37c | 2157 | } |
mkarlsso | 2:298679fff37c | 2158 | if (!lineError && isText) { |
mkarlsso | 2:298679fff37c | 2159 | //displayAction* dPtr = new displayAction(tmpLine.substr(pos1+1,pos2-pos1-2), pcPtr); |
mkarlsso | 2:298679fff37c | 2160 | dPtr->set(tmpLine.substr(pos1+1,pos2-pos1-2), pcPtr); |
mkarlsso | 2:298679fff37c | 2161 | tmpAction->set(dPtr); |
mkarlsso | 2:298679fff37c | 2162 | //action* tmpAction = new action(dPtr); |
mkarlsso | 2:298679fff37c | 2163 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2164 | } else if (!lineError) { |
mkarlsso | 2:298679fff37c | 2165 | //displayAction* dPtr = new displayAction(tmpVar, dispVar, pcPtr); |
mkarlsso | 2:298679fff37c | 2166 | dPtr->set(tmpVar, dispVar, pcPtr); |
mkarlsso | 2:298679fff37c | 2167 | tmpAction->set(dPtr); |
mkarlsso | 2:298679fff37c | 2168 | //action* tmpAction = new action(dPtr); |
mkarlsso | 2:298679fff37c | 2169 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2170 | } |
mkarlsso | 2:298679fff37c | 2171 | |
mkarlsso | 2:298679fff37c | 2172 | //pcPtr->printf("Display action\r\n"); |
mkarlsso | 2:298679fff37c | 2173 | } |
mkarlsso | 2:298679fff37c | 2174 | |
mkarlsso | 2:298679fff37c | 2175 | |
mkarlsso | 2:298679fff37c | 2176 | //int is used to decalar new variables. Only allowed outside of callbacks------------------- |
mkarlsso | 2:298679fff37c | 2177 | //example: int a; int b = 9 |
mkarlsso | 2:298679fff37c | 2178 | } else if (tokens[i].compare("int") == 0) { //define a new integer variable |
mkarlsso | 2:298679fff37c | 2179 | |
mkarlsso | 2:298679fff37c | 2180 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2181 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2182 | lineError = true; |
mkarlsso | 2:298679fff37c | 2183 | } |
mkarlsso | 2:298679fff37c | 2184 | tmpString = ""; |
mkarlsso | 2:298679fff37c | 2185 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2186 | int spacesBeforeEqualSign = 0; |
mkarlsso | 2:298679fff37c | 2187 | bool countSpaces = true; |
mkarlsso | 2:298679fff37c | 2188 | //combine the tokens without whitespaces |
mkarlsso | 2:298679fff37c | 2189 | for (unsigned j = i+1; j < sz; j++) { |
mkarlsso | 2:298679fff37c | 2190 | tmpString.append(tokens[j]); |
mkarlsso | 2:298679fff37c | 2191 | if (tokens[j].find_first_of("=") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 2192 | if (tokens[j].find_first_of("=") > 0) spacesBeforeEqualSign++; |
mkarlsso | 2:298679fff37c | 2193 | countSpaces = false; |
mkarlsso | 2:298679fff37c | 2194 | } else if (countSpaces) { |
mkarlsso | 2:298679fff37c | 2195 | spacesBeforeEqualSign++; |
mkarlsso | 2:298679fff37c | 2196 | } |
mkarlsso | 2:298679fff37c | 2197 | } |
mkarlsso | 2:298679fff37c | 2198 | |
mkarlsso | 2:298679fff37c | 2199 | if (blockDepth > 0) { |
mkarlsso | 2:298679fff37c | 2200 | pcPtr->printf("Error: Variables can only be first declared outside of callbacks.\r\n"); |
mkarlsso | 2:298679fff37c | 2201 | lineError = true; |
mkarlsso | 2:298679fff37c | 2202 | } |
mkarlsso | 2:298679fff37c | 2203 | |
mkarlsso | 2:298679fff37c | 2204 | if ((!lineError) && (spacesBeforeEqualSign > 1)) { |
mkarlsso | 2:298679fff37c | 2205 | pcPtr->printf("Error: Variable can't have a space in it.\r\n"); |
mkarlsso | 2:298679fff37c | 2206 | lineError = true; |
mkarlsso | 2:298679fff37c | 2207 | } |
mkarlsso | 2:298679fff37c | 2208 | stringInd = tmpString.find_first_of("="); |
mkarlsso | 2:298679fff37c | 2209 | |
mkarlsso | 2:298679fff37c | 2210 | bool variableCreated = false; |
mkarlsso | 2:298679fff37c | 2211 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2212 | if (((stringInd == std::string::npos) && (sz == 2)) || (stringInd != std::string::npos)) { |
mkarlsso | 2:298679fff37c | 2213 | if (createIntVariable(tmpString.substr(0,stringInd))) { |
mkarlsso | 2:298679fff37c | 2214 | //pcPtr->printf("Created variable: %s\r\n", tmpString.substr(0,stringInd).data()); |
mkarlsso | 2:298679fff37c | 2215 | variableCreated = true; |
mkarlsso | 2:298679fff37c | 2216 | } else { |
mkarlsso | 2:298679fff37c | 2217 | int* tmpVar = findIntVariable(tmpString.substr(0,stringInd)); |
mkarlsso | 2:298679fff37c | 2218 | *tmpVar = 0; |
mkarlsso | 2:298679fff37c | 2219 | //pcPtr->printf("Reset variable %s to 0\r\n", tmpString.substr(0,stringInd).data()); |
mkarlsso | 2:298679fff37c | 2220 | //lineError = true; |
mkarlsso | 2:298679fff37c | 2221 | } |
mkarlsso | 2:298679fff37c | 2222 | } else { |
mkarlsso | 2:298679fff37c | 2223 | pcPtr->printf("Error: variable declaration not understood.\r\n", tmpString.substr(0,stringInd).data()); |
mkarlsso | 2:298679fff37c | 2224 | lineError = true; |
mkarlsso | 2:298679fff37c | 2225 | } |
mkarlsso | 2:298679fff37c | 2226 | } |
mkarlsso | 2:298679fff37c | 2227 | |
mkarlsso | 2:298679fff37c | 2228 | if ((!lineError) && (stringInd != std::string::npos)) { //evaluate the expression |
mkarlsso | 2:298679fff37c | 2229 | action* tmpAction = evaluateAssignmentForAction(tmpString); |
mkarlsso | 2:298679fff37c | 2230 | if (tmpAction != NULL) { |
mkarlsso | 2:298679fff37c | 2231 | tmpAction->execute(); |
mkarlsso | 2:298679fff37c | 2232 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 2233 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 2234 | } else { |
mkarlsso | 2:298679fff37c | 2235 | lineError = true; |
mkarlsso | 2:298679fff37c | 2236 | if (variableCreated) { |
mkarlsso | 2:298679fff37c | 2237 | delete globalVariables.back(); |
mkarlsso | 2:298679fff37c | 2238 | globalVariables.pop_back(); |
mkarlsso | 2:298679fff37c | 2239 | } |
mkarlsso | 2:298679fff37c | 2240 | } |
mkarlsso | 2:298679fff37c | 2241 | } |
mkarlsso | 2:298679fff37c | 2242 | |
mkarlsso | 2:298679fff37c | 2243 | //serial command is used to toggle whether or not to buffer up output text---------------- |
mkarlsso | 2:298679fff37c | 2244 | //examples: serial buffer; serial send |
mkarlsso | 2:298679fff37c | 2245 | } else if (tokens[i].compare("serial") == 0) { |
mkarlsso | 2:298679fff37c | 2246 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2247 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2248 | lineError = true; |
mkarlsso | 2:298679fff37c | 2249 | } |
mkarlsso | 2:298679fff37c | 2250 | bool stream = true; |
mkarlsso | 2:298679fff37c | 2251 | if ((!lineError)&&(i+1 < sz)){ |
mkarlsso | 2:298679fff37c | 2252 | if (tokens[i+1].compare("buffer") == 0) { |
mkarlsso | 2:298679fff37c | 2253 | stream = false; |
mkarlsso | 2:298679fff37c | 2254 | } else if (tokens[i+1].compare("send") == 0) { |
mkarlsso | 2:298679fff37c | 2255 | stream = true; |
mkarlsso | 2:298679fff37c | 2256 | } else { |
mkarlsso | 2:298679fff37c | 2257 | pcPtr->printf("Error: 'serial' useage: 'serial buffer' or 'serial send'\r\n"); |
mkarlsso | 2:298679fff37c | 2258 | lineError = true; |
mkarlsso | 2:298679fff37c | 2259 | } |
mkarlsso | 2:298679fff37c | 2260 | } |
mkarlsso | 2:298679fff37c | 2261 | i++; |
mkarlsso | 2:298679fff37c | 2262 | if ((!lineError) && (blockDepth == 0)) { |
mkarlsso | 2:298679fff37c | 2263 | if (stream) { |
mkarlsso | 2:298679fff37c | 2264 | textStreaming = true; |
mkarlsso | 2:298679fff37c | 2265 | } else { |
mkarlsso | 2:298679fff37c | 2266 | textStreaming = false; |
mkarlsso | 2:298679fff37c | 2267 | } |
mkarlsso | 2:298679fff37c | 2268 | } else if ((!lineError) && (blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 2269 | if (stream) { |
mkarlsso | 2:298679fff37c | 2270 | //action* tmpAction = new action(1); //code 1 = turn on text streaming |
mkarlsso | 2:298679fff37c | 2271 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2272 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2273 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2274 | lineError = true; |
mkarlsso | 2:298679fff37c | 2275 | } else { |
mkarlsso | 2:298679fff37c | 2276 | tmpAction->set(1); |
mkarlsso | 2:298679fff37c | 2277 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2278 | } |
mkarlsso | 2:298679fff37c | 2279 | |
mkarlsso | 2:298679fff37c | 2280 | } else { |
mkarlsso | 2:298679fff37c | 2281 | //action* tmpAction = new action(2); //code 2 = turn on text buffering |
mkarlsso | 2:298679fff37c | 2282 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2283 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2284 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2285 | lineError = true; |
mkarlsso | 2:298679fff37c | 2286 | } else { |
mkarlsso | 2:298679fff37c | 2287 | tmpAction->set(2); |
mkarlsso | 2:298679fff37c | 2288 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2289 | } |
mkarlsso | 2:298679fff37c | 2290 | } |
mkarlsso | 2:298679fff37c | 2291 | } |
mkarlsso | 2:298679fff37c | 2292 | |
mkarlsso | 2:298679fff37c | 2293 | //updates command toggles the DIO update messages upon a change------------------ |
mkarlsso | 2:298679fff37c | 2294 | //examples: updates on; updates off |
mkarlsso | 2:298679fff37c | 2295 | } else if (tokens[i].compare("updates") == 0) { |
mkarlsso | 2:298679fff37c | 2296 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2297 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2298 | lineError = true; |
mkarlsso | 2:298679fff37c | 2299 | } |
mkarlsso | 2:298679fff37c | 2300 | bool stream = true; |
mkarlsso | 2:298679fff37c | 2301 | if ((!lineError)&&(i+1 < sz)){ |
mkarlsso | 2:298679fff37c | 2302 | if (tokens[i+1].compare("on") == 0) { |
mkarlsso | 2:298679fff37c | 2303 | stream = true; |
mkarlsso | 2:298679fff37c | 2304 | } else if (tokens[i+1].compare("off") == 0) { |
mkarlsso | 2:298679fff37c | 2305 | stream = false; |
mkarlsso | 2:298679fff37c | 2306 | } else { |
mkarlsso | 2:298679fff37c | 2307 | pcPtr->printf("Error: 'updates' useage: 'updates on' or 'updates off'\r\n"); |
mkarlsso | 2:298679fff37c | 2308 | lineError = true; |
mkarlsso | 2:298679fff37c | 2309 | } |
mkarlsso | 2:298679fff37c | 2310 | } |
mkarlsso | 2:298679fff37c | 2311 | i++; |
mkarlsso | 2:298679fff37c | 2312 | if ((!lineError) && (blockDepth == 0)) { |
mkarlsso | 2:298679fff37c | 2313 | if (stream) { |
mkarlsso | 2:298679fff37c | 2314 | broadCastStateChanges = true; |
mkarlsso | 2:298679fff37c | 2315 | } else { |
mkarlsso | 2:298679fff37c | 2316 | broadCastStateChanges = false; |
mkarlsso | 2:298679fff37c | 2317 | } |
mkarlsso | 2:298679fff37c | 2318 | } else if ((!lineError) && (blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 2319 | if (stream) { |
mkarlsso | 2:298679fff37c | 2320 | //action* tmpAction = new action(3); //code 3 = turn on updates |
mkarlsso | 2:298679fff37c | 2321 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2322 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2323 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2324 | lineError = true; |
mkarlsso | 2:298679fff37c | 2325 | } else { |
mkarlsso | 2:298679fff37c | 2326 | tmpAction->set(3); |
mkarlsso | 2:298679fff37c | 2327 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2328 | } |
mkarlsso | 2:298679fff37c | 2329 | } else { |
mkarlsso | 2:298679fff37c | 2330 | //action* tmpAction = new action(4); //code 4 = turn off updates |
mkarlsso | 2:298679fff37c | 2331 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2332 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2333 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2334 | lineError = true; |
mkarlsso | 2:298679fff37c | 2335 | } else { |
mkarlsso | 2:298679fff37c | 2336 | tmpAction->set(4); |
mkarlsso | 2:298679fff37c | 2337 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2338 | } |
mkarlsso | 2:298679fff37c | 2339 | |
mkarlsso | 2:298679fff37c | 2340 | } |
mkarlsso | 2:298679fff37c | 2341 | } |
mkarlsso | 2:298679fff37c | 2342 | |
mkarlsso | 2:298679fff37c | 2343 | //clear is used to clear things from memory--------------------------------- |
mkarlsso | 2:298679fff37c | 2344 | //examples: clear all; clear callbacks; clear queue |
mkarlsso | 2:298679fff37c | 2345 | } else if (tokens[i].compare("ram") == 0) { |
mkarlsso | 2:298679fff37c | 2346 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2347 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2348 | lineError = true; |
mkarlsso | 2:298679fff37c | 2349 | } |
mkarlsso | 2:298679fff37c | 2350 | if ((!lineError) && (blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 2351 | pcPtr->printf("Error: ram statement is not allowed inside a block.\r\n"); |
mkarlsso | 2:298679fff37c | 2352 | lineError = true; |
mkarlsso | 2:298679fff37c | 2353 | } |
mkarlsso | 2:298679fff37c | 2354 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2355 | DisplayRAMBanks(); |
mkarlsso | 2:298679fff37c | 2356 | } |
mkarlsso | 2:298679fff37c | 2357 | } else if (tokens[i].compare("memory") == 0) { |
mkarlsso | 2:298679fff37c | 2358 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2359 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2360 | lineError = true; |
mkarlsso | 2:298679fff37c | 2361 | } |
mkarlsso | 2:298679fff37c | 2362 | if ((!lineError) && (blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 2363 | pcPtr->printf("Error: memory statement is not allowed inside a block.\r\n"); |
mkarlsso | 2:298679fff37c | 2364 | lineError = true; |
mkarlsso | 2:298679fff37c | 2365 | } |
mkarlsso | 2:298679fff37c | 2366 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2367 | displayMemoryLeft(); |
mkarlsso | 2:298679fff37c | 2368 | } |
mkarlsso | 2:298679fff37c | 2369 | |
mkarlsso | 2:298679fff37c | 2370 | |
mkarlsso | 2:298679fff37c | 2371 | } else if (tokens[i].compare("clear") == 0) { //delete all created events and variables |
mkarlsso | 2:298679fff37c | 2372 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2373 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2374 | lineError = true; |
mkarlsso | 2:298679fff37c | 2375 | } |
mkarlsso | 2:298679fff37c | 2376 | int clearMode = 0; |
mkarlsso | 2:298679fff37c | 2377 | if ((!lineError)&&(i+1 < sz)){ |
mkarlsso | 2:298679fff37c | 2378 | if (tokens[i+1].compare("all") == 0) { |
mkarlsso | 2:298679fff37c | 2379 | clearMode = 1; |
mkarlsso | 2:298679fff37c | 2380 | } else if (tokens[i+1].compare("blocks") == 0) { |
mkarlsso | 2:298679fff37c | 2381 | clearMode = 2; |
mkarlsso | 2:298679fff37c | 2382 | } else if (tokens[i+1].compare("queue") == 0) { |
mkarlsso | 2:298679fff37c | 2383 | clearMode = 3; |
mkarlsso | 2:298679fff37c | 2384 | } else { |
mkarlsso | 2:298679fff37c | 2385 | pcPtr->printf("Error: clear what: all, blocks, or queue? \r\n"); |
mkarlsso | 2:298679fff37c | 2386 | lineError = true; |
mkarlsso | 2:298679fff37c | 2387 | } |
mkarlsso | 2:298679fff37c | 2388 | } else { |
mkarlsso | 2:298679fff37c | 2389 | pcPtr->printf("Error: clear what: all, blocks, or queue? \r\n"); |
mkarlsso | 2:298679fff37c | 2390 | lineError = true; |
mkarlsso | 2:298679fff37c | 2391 | } |
mkarlsso | 2:298679fff37c | 2392 | |
mkarlsso | 2:298679fff37c | 2393 | |
mkarlsso | 2:298679fff37c | 2394 | if ((!lineError) && (clearMode < 3) && (blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 2395 | pcPtr->printf("Error: 'clear all' and 'clear blocks' only allowed outside of block structures\r\n"); |
mkarlsso | 2:298679fff37c | 2396 | lineError = true; |
mkarlsso | 2:298679fff37c | 2397 | } |
mkarlsso | 2:298679fff37c | 2398 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2399 | i++; |
mkarlsso | 2:298679fff37c | 2400 | //clear variables |
mkarlsso | 2:298679fff37c | 2401 | if (clearMode == 1) { |
mkarlsso | 2:298679fff37c | 2402 | while (!globalVariables.empty()) { |
mkarlsso | 2:298679fff37c | 2403 | globalVariables.pop_back(); |
mkarlsso | 2:298679fff37c | 2404 | } |
mkarlsso | 2:298679fff37c | 2405 | } |
mkarlsso | 2:298679fff37c | 2406 | |
mkarlsso | 2:298679fff37c | 2407 | //clear callbacks, functions, and queue |
mkarlsso | 2:298679fff37c | 2408 | if (clearMode < 3) { |
mkarlsso | 2:298679fff37c | 2409 | for (int pNum = 1; pNum <= numPorts; pNum++) { |
mkarlsso | 2:298679fff37c | 2410 | //delete portVector[pNum]->triggerUpEventPtr; |
mkarlsso | 2:298679fff37c | 2411 | if (portVector[pNum]->triggerUpEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 2412 | portVector[pNum]->triggerUpEventPtr->release(); |
mkarlsso | 2:298679fff37c | 2413 | } |
mkarlsso | 2:298679fff37c | 2414 | portVector[pNum]->triggerUpEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 2415 | |
mkarlsso | 2:298679fff37c | 2416 | //delete portVector[pNum]->triggerDownEventPtr; |
mkarlsso | 2:298679fff37c | 2417 | if (portVector[pNum]->triggerDownEventPtr != NULL) { |
mkarlsso | 2:298679fff37c | 2418 | portVector[pNum]->triggerDownEventPtr->release(); |
mkarlsso | 2:298679fff37c | 2419 | } |
mkarlsso | 2:298679fff37c | 2420 | portVector[pNum]->triggerDownEventPtr = NULL; |
mkarlsso | 2:298679fff37c | 2421 | while (!functionArray.empty()) { |
mkarlsso | 2:298679fff37c | 2422 | delete functionArray.back(); |
mkarlsso | 2:298679fff37c | 2423 | functionArray.pop_back(); |
mkarlsso | 2:298679fff37c | 2424 | } |
mkarlsso | 2:298679fff37c | 2425 | } |
mkarlsso | 2:298679fff37c | 2426 | |
mkarlsso | 2:298679fff37c | 2427 | queuePtr->eraseQueue(); |
mkarlsso | 2:298679fff37c | 2428 | } |
mkarlsso | 2:298679fff37c | 2429 | |
mkarlsso | 2:298679fff37c | 2430 | if (clearMode == 4) { |
mkarlsso | 2:298679fff37c | 2431 | if (blockDepth > 0) { //we are inside a block |
mkarlsso | 2:298679fff37c | 2432 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2433 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2434 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2435 | lineError = true; |
mkarlsso | 2:298679fff37c | 2436 | } else { |
mkarlsso | 2:298679fff37c | 2437 | |
mkarlsso | 2:298679fff37c | 2438 | int8_t code = 0; |
mkarlsso | 2:298679fff37c | 2439 | tmpAction->set(code); |
mkarlsso | 2:298679fff37c | 2440 | //action* tmpAction = new action(code); //code 0 = clear queue |
mkarlsso | 2:298679fff37c | 2441 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2442 | } |
mkarlsso | 2:298679fff37c | 2443 | } else { |
mkarlsso | 2:298679fff37c | 2444 | //clear queue now |
mkarlsso | 2:298679fff37c | 2445 | queuePtr->eraseQueue(); |
mkarlsso | 2:298679fff37c | 2446 | } |
mkarlsso | 2:298679fff37c | 2447 | } |
mkarlsso | 2:298679fff37c | 2448 | |
mkarlsso | 2:298679fff37c | 2449 | |
mkarlsso | 2:298679fff37c | 2450 | } |
mkarlsso | 2:298679fff37c | 2451 | |
mkarlsso | 2:298679fff37c | 2452 | //do starts a block--------------------------------------------------------- |
mkarlsso | 2:298679fff37c | 2453 | //example: do in 500 |
mkarlsso | 2:298679fff37c | 2454 | // ... |
mkarlsso | 2:298679fff37c | 2455 | // end |
mkarlsso | 2:298679fff37c | 2456 | |
mkarlsso | 2:298679fff37c | 2457 | } else if (tokens[i].compare("do") == 0) { //the start of a block |
mkarlsso | 2:298679fff37c | 2458 | |
mkarlsso | 2:298679fff37c | 2459 | if (!ifBlockInit && !whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2460 | |
mkarlsso | 2:298679fff37c | 2461 | if ((currentTriggerPort > 0) || (currentFunction > 0)) { //check to make sure we are inside a trigger block |
mkarlsso | 2:298679fff37c | 2462 | //pcPtr->printf("Start new block\r\n"); |
mkarlsso | 2:298679fff37c | 2463 | |
mkarlsso | 2:298679fff37c | 2464 | } else { |
mkarlsso | 2:298679fff37c | 2465 | pcPtr->printf("Error: a statement block must be placed inside a callback or function.\r\n"); |
mkarlsso | 2:298679fff37c | 2466 | lineError = true; |
mkarlsso | 2:298679fff37c | 2467 | } |
mkarlsso | 2:298679fff37c | 2468 | |
mkarlsso | 2:298679fff37c | 2469 | } |
mkarlsso | 2:298679fff37c | 2470 | tmpEvent = findFirstUnUsed(eventBlock, NUMEVENTS); |
mkarlsso | 2:298679fff37c | 2471 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2472 | bool eventReserved = false; |
mkarlsso | 2:298679fff37c | 2473 | if ((tmpEvent == NULL)||(tmpAction == NULL)) { |
mkarlsso | 2:298679fff37c | 2474 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2475 | lineError = true; |
mkarlsso | 2:298679fff37c | 2476 | |
mkarlsso | 2:298679fff37c | 2477 | } else { |
mkarlsso | 2:298679fff37c | 2478 | |
mkarlsso | 2:298679fff37c | 2479 | |
mkarlsso | 2:298679fff37c | 2480 | } |
mkarlsso | 2:298679fff37c | 2481 | |
mkarlsso | 2:298679fff37c | 2482 | if (i+2 < sz) { //a time delay in the block |
mkarlsso | 2:298679fff37c | 2483 | |
mkarlsso | 2:298679fff37c | 2484 | if ((!lineError) && (tokens[i+1].compare("in") == 0) && (isNumber(tokens[i+2]))) { |
mkarlsso | 2:298679fff37c | 2485 | |
mkarlsso | 2:298679fff37c | 2486 | currentDelay = atoi(tokens[i+2].data()); |
mkarlsso | 2:298679fff37c | 2487 | if (currentDelay < 0) { |
mkarlsso | 2:298679fff37c | 2488 | pcPtr->printf("Error: block delay time must be a positive integer\r\n"); |
mkarlsso | 2:298679fff37c | 2489 | lineError = true; |
mkarlsso | 2:298679fff37c | 2490 | } else if (!ifBlockInit) { //a standalone do block |
mkarlsso | 2:298679fff37c | 2491 | //tmpEvent = new event(queuePtr); |
mkarlsso | 2:298679fff37c | 2492 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2493 | eventReserved = true; |
mkarlsso | 2:298679fff37c | 2494 | tmpEvent->setTimeLag(currentDelay); |
mkarlsso | 2:298679fff37c | 2495 | |
mkarlsso | 2:298679fff37c | 2496 | if ((!elseFlag) && ((!thenFlag))) { |
mkarlsso | 2:298679fff37c | 2497 | //tmpEventPtrArray.back()->addAction(new action(tmpEvent)); |
mkarlsso | 2:298679fff37c | 2498 | tmpAction->set(tmpEvent); |
mkarlsso | 2:298679fff37c | 2499 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2500 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2501 | tmpEventPtrArray.back()->blockType = 2; //this is a do block |
mkarlsso | 2:298679fff37c | 2502 | blockDepth = blockDepth+1; |
mkarlsso | 2:298679fff37c | 2503 | } else if (elseFlag) { |
mkarlsso | 2:298679fff37c | 2504 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2505 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2506 | tmpEventPtrArray.back()->blockType = 4; //an else block |
mkarlsso | 2:298679fff37c | 2507 | } else if (thenFlag) { |
mkarlsso | 2:298679fff37c | 2508 | |
mkarlsso | 2:298679fff37c | 2509 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2510 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2511 | tmpEventPtrArray.back()->blockType = 8; //a then block |
mkarlsso | 2:298679fff37c | 2512 | } |
mkarlsso | 2:298679fff37c | 2513 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2514 | } else { //an if block |
mkarlsso | 2:298679fff37c | 2515 | tmpEventPtrArray.back()->setTimeLag(currentDelay); |
mkarlsso | 2:298679fff37c | 2516 | |
mkarlsso | 2:298679fff37c | 2517 | if (!elseFlag && !thenFlag) { |
mkarlsso | 2:298679fff37c | 2518 | if (blockDepth > 1) { //this is a nested block, so add it as an action to the parent block |
mkarlsso | 2:298679fff37c | 2519 | tmpAction->set(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2520 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2521 | //tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(new action(tmpEventPtrArray.back())); |
mkarlsso | 2:298679fff37c | 2522 | } |
mkarlsso | 2:298679fff37c | 2523 | } else if (elseFlag){ |
mkarlsso | 2:298679fff37c | 2524 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->setNextElseEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2525 | } else if (thenFlag){ |
mkarlsso | 2:298679fff37c | 2526 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->setNextElseEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2527 | |
mkarlsso | 2:298679fff37c | 2528 | } |
mkarlsso | 2:298679fff37c | 2529 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2530 | |
mkarlsso | 2:298679fff37c | 2531 | } |
mkarlsso | 2:298679fff37c | 2532 | |
mkarlsso | 2:298679fff37c | 2533 | } else if ((!lineError) && (tokens[i+1].compare("in") == 0) && (findIntVariable(tokens[i+2])!=NULL)) { |
mkarlsso | 2:298679fff37c | 2534 | |
mkarlsso | 2:298679fff37c | 2535 | int* delayVar = findIntVariable(tokens[i+2]); |
mkarlsso | 2:298679fff37c | 2536 | //currentDelay = atoi(tokens[i+2].data()); |
mkarlsso | 2:298679fff37c | 2537 | if (!ifBlockInit) { //a standalone do block |
mkarlsso | 2:298679fff37c | 2538 | //tmpEvent = new event(queuePtr); |
mkarlsso | 2:298679fff37c | 2539 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2540 | eventReserved = true; |
mkarlsso | 2:298679fff37c | 2541 | tmpEvent->setTimeLag(delayVar); |
mkarlsso | 2:298679fff37c | 2542 | |
mkarlsso | 2:298679fff37c | 2543 | if ((!elseFlag) && ((!thenFlag))) { |
mkarlsso | 2:298679fff37c | 2544 | //tmpEventPtrArray.back()->addAction(new action(tmpEvent)); |
mkarlsso | 2:298679fff37c | 2545 | tmpAction->set(tmpEvent); |
mkarlsso | 2:298679fff37c | 2546 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2547 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2548 | tmpEventPtrArray.back()->blockType = 2; //this is a do block |
mkarlsso | 2:298679fff37c | 2549 | blockDepth = blockDepth+1; |
mkarlsso | 2:298679fff37c | 2550 | } else if (elseFlag) { |
mkarlsso | 2:298679fff37c | 2551 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2552 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2553 | tmpEventPtrArray.back()->blockType = 4; //an else block |
mkarlsso | 2:298679fff37c | 2554 | } else if (thenFlag) { |
mkarlsso | 2:298679fff37c | 2555 | |
mkarlsso | 2:298679fff37c | 2556 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2557 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2558 | tmpEventPtrArray.back()->blockType = 8; //a then block |
mkarlsso | 2:298679fff37c | 2559 | } |
mkarlsso | 2:298679fff37c | 2560 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2561 | } else { //an if block |
mkarlsso | 2:298679fff37c | 2562 | tmpEventPtrArray.back()->setTimeLag(delayVar); |
mkarlsso | 2:298679fff37c | 2563 | |
mkarlsso | 2:298679fff37c | 2564 | if (!elseFlag && !thenFlag) { |
mkarlsso | 2:298679fff37c | 2565 | if (blockDepth > 1) { //this is a nested block, so add it as an action to the parent block |
mkarlsso | 2:298679fff37c | 2566 | tmpAction->set(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2567 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2568 | //tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(new action(tmpEventPtrArray.back())); |
mkarlsso | 2:298679fff37c | 2569 | } |
mkarlsso | 2:298679fff37c | 2570 | } else if (elseFlag){ |
mkarlsso | 2:298679fff37c | 2571 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->setNextElseEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2572 | } else if (thenFlag){ |
mkarlsso | 2:298679fff37c | 2573 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->setNextElseEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2574 | |
mkarlsso | 2:298679fff37c | 2575 | } |
mkarlsso | 2:298679fff37c | 2576 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2577 | |
mkarlsso | 2:298679fff37c | 2578 | } |
mkarlsso | 2:298679fff37c | 2579 | |
mkarlsso | 2:298679fff37c | 2580 | } else { |
mkarlsso | 2:298679fff37c | 2581 | pcPtr->printf("Error: block delay time must be a positive integer or a variable\r\n"); |
mkarlsso | 2:298679fff37c | 2582 | lineError = true; |
mkarlsso | 2:298679fff37c | 2583 | } |
mkarlsso | 2:298679fff37c | 2584 | } else if (!lineError && !ifBlockInit) { //no time delay given, standalone do |
mkarlsso | 2:298679fff37c | 2585 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 2586 | //tmpEvent = new event(queuePtr); |
mkarlsso | 2:298679fff37c | 2587 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2588 | eventReserved = true; |
mkarlsso | 2:298679fff37c | 2589 | tmpEvent->setTimeLag(currentDelay); |
mkarlsso | 2:298679fff37c | 2590 | if (!elseFlag && !thenFlag) { |
mkarlsso | 2:298679fff37c | 2591 | tmpAction->set(tmpEvent); |
mkarlsso | 2:298679fff37c | 2592 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2593 | //tmpEventPtrArray.back()->addAction(new action(tmpEvent)); |
mkarlsso | 2:298679fff37c | 2594 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2595 | blockDepth = blockDepth+1; |
mkarlsso | 2:298679fff37c | 2596 | } else if (elseFlag) { |
mkarlsso | 2:298679fff37c | 2597 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2598 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2599 | tmpEventPtrArray.back()->blockType = 4; //an else block |
mkarlsso | 2:298679fff37c | 2600 | } else if (thenFlag) { |
mkarlsso | 2:298679fff37c | 2601 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2602 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2603 | tmpEventPtrArray.back()->blockType = 8; //a then block |
mkarlsso | 2:298679fff37c | 2604 | } |
mkarlsso | 2:298679fff37c | 2605 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2606 | } else if (!lineError) { //no time delay, if block |
mkarlsso | 2:298679fff37c | 2607 | |
mkarlsso | 2:298679fff37c | 2608 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 2609 | tmpEventPtrArray.back()->setTimeLag(currentDelay); |
mkarlsso | 2:298679fff37c | 2610 | |
mkarlsso | 2:298679fff37c | 2611 | |
mkarlsso | 2:298679fff37c | 2612 | if (!elseFlag && !thenFlag) { |
mkarlsso | 2:298679fff37c | 2613 | if (blockDepth > 1) { |
mkarlsso | 2:298679fff37c | 2614 | tmpAction->set(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2615 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2616 | //tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->addAction(new action(tmpEventPtrArray.back())); |
mkarlsso | 2:298679fff37c | 2617 | } |
mkarlsso | 2:298679fff37c | 2618 | } else { |
mkarlsso | 2:298679fff37c | 2619 | tmpEventPtrArray.at(tmpEventPtrArray.size()-2)->setNextElseEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2620 | } |
mkarlsso | 2:298679fff37c | 2621 | |
mkarlsso | 2:298679fff37c | 2622 | |
mkarlsso | 2:298679fff37c | 2623 | //pcPtr->printf("Block delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2624 | } |
mkarlsso | 2:298679fff37c | 2625 | if (lineError && eventReserved) { |
mkarlsso | 2:298679fff37c | 2626 | tmpEvent->release(); |
mkarlsso | 2:298679fff37c | 2627 | } |
mkarlsso | 2:298679fff37c | 2628 | //close block initiation |
mkarlsso | 2:298679fff37c | 2629 | ifBlockInit = false; |
mkarlsso | 2:298679fff37c | 2630 | whileBlockInit = false; |
mkarlsso | 2:298679fff37c | 2631 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2632 | elseFlag = false; |
mkarlsso | 2:298679fff37c | 2633 | thenFlag = false; |
mkarlsso | 2:298679fff37c | 2634 | |
mkarlsso | 2:298679fff37c | 2635 | //callback starts a callback block------------------------------------------ |
mkarlsso | 2:298679fff37c | 2636 | //exmaple: callback portin(1) down |
mkarlsso | 2:298679fff37c | 2637 | // ... |
mkarlsso | 2:298679fff37c | 2638 | // end |
mkarlsso | 2:298679fff37c | 2639 | } else if (tokens[i].compare("callback") == 0) { //a new callback block |
mkarlsso | 2:298679fff37c | 2640 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2641 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2642 | lineError = true; |
mkarlsso | 2:298679fff37c | 2643 | } |
mkarlsso | 2:298679fff37c | 2644 | if (blockDepth != 0) { |
mkarlsso | 2:298679fff37c | 2645 | pcPtr->printf("Error: Can't declare a callback block within another block\r\n"); |
mkarlsso | 2:298679fff37c | 2646 | lineError = true; |
mkarlsso | 2:298679fff37c | 2647 | } |
mkarlsso | 2:298679fff37c | 2648 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2649 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2650 | if (i+2 < sz) { |
mkarlsso | 2:298679fff37c | 2651 | if ((tokens[i+1].find("portin[") != std::string::npos) && (tokens[i+1].size() > 8) ) { //callback for a digital port |
mkarlsso | 2:298679fff37c | 2652 | int pos1 = tokens[i+1].find("portin[")+7; |
mkarlsso | 2:298679fff37c | 2653 | int pos2 = tokens[i+1].find_first_of("]",pos1); |
mkarlsso | 2:298679fff37c | 2654 | currentTriggerPort = atoi(tokens[i+1].substr(pos1,pos2-pos1).data()); |
mkarlsso | 2:298679fff37c | 2655 | |
mkarlsso | 2:298679fff37c | 2656 | if (currentTriggerPort <= 0) { |
mkarlsso | 2:298679fff37c | 2657 | currentTriggerPort = -1; |
mkarlsso | 2:298679fff37c | 2658 | pcPtr->printf("Error: Not a valid port number\r\n"); |
mkarlsso | 2:298679fff37c | 2659 | lineError = true; |
mkarlsso | 2:298679fff37c | 2660 | } |
mkarlsso | 2:298679fff37c | 2661 | } else { |
mkarlsso | 2:298679fff37c | 2662 | pcPtr->printf("Error: Not a valid callback input\r\n"); |
mkarlsso | 2:298679fff37c | 2663 | lineError = true; |
mkarlsso | 2:298679fff37c | 2664 | } |
mkarlsso | 2:298679fff37c | 2665 | if (tokens[i+2].compare("up") == 0) { |
mkarlsso | 2:298679fff37c | 2666 | currentTriggerDir = 1; |
mkarlsso | 2:298679fff37c | 2667 | } else if (tokens[i+2].compare("down") == 0) { |
mkarlsso | 2:298679fff37c | 2668 | currentTriggerDir = -1; |
mkarlsso | 2:298679fff37c | 2669 | } else { |
mkarlsso | 2:298679fff37c | 2670 | pcPtr->printf("Error: No trigger direction given\r\n"); |
mkarlsso | 2:298679fff37c | 2671 | lineError = true; |
mkarlsso | 2:298679fff37c | 2672 | } |
mkarlsso | 2:298679fff37c | 2673 | |
mkarlsso | 2:298679fff37c | 2674 | } else { |
mkarlsso | 2:298679fff37c | 2675 | if (!lineError) pcPtr->printf("Error: Not enough arguments for callback statement\r\n"); |
mkarlsso | 2:298679fff37c | 2676 | lineError = true; |
mkarlsso | 2:298679fff37c | 2677 | } |
mkarlsso | 2:298679fff37c | 2678 | if (sz > 3) { |
mkarlsso | 2:298679fff37c | 2679 | if (!((sz == 4) && (tokens[i+3].compare("do") == 0))) { |
mkarlsso | 2:298679fff37c | 2680 | pcPtr->printf("Error: Too many arguments in callback statement\r\n"); |
mkarlsso | 2:298679fff37c | 2681 | lineError = true; |
mkarlsso | 2:298679fff37c | 2682 | } |
mkarlsso | 2:298679fff37c | 2683 | } |
mkarlsso | 2:298679fff37c | 2684 | |
mkarlsso | 2:298679fff37c | 2685 | tmpEvent = findFirstUnUsed(eventBlock, NUMEVENTS); |
mkarlsso | 2:298679fff37c | 2686 | if (tmpEvent != NULL) { |
mkarlsso | 2:298679fff37c | 2687 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2688 | |
mkarlsso | 2:298679fff37c | 2689 | } else { |
mkarlsso | 2:298679fff37c | 2690 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2691 | lineError = true; |
mkarlsso | 2:298679fff37c | 2692 | } |
mkarlsso | 2:298679fff37c | 2693 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2694 | //pcPtr->printf("Current port: %d\r\n", currentTriggerPort); |
mkarlsso | 2:298679fff37c | 2695 | blockDepth = 1; |
mkarlsso | 2:298679fff37c | 2696 | i = i+2; |
mkarlsso | 2:298679fff37c | 2697 | //create new event and attach it to the port |
mkarlsso | 2:298679fff37c | 2698 | //tmpEventPtrArray.push_back(new event(queuePtr)); |
mkarlsso | 2:298679fff37c | 2699 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2700 | if (currentTriggerDir == 1) { |
mkarlsso | 2:298679fff37c | 2701 | |
mkarlsso | 2:298679fff37c | 2702 | portVector[currentTriggerPort]->setTriggerUpEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2703 | } else { |
mkarlsso | 2:298679fff37c | 2704 | |
mkarlsso | 2:298679fff37c | 2705 | portVector[currentTriggerPort]->setTriggerDownEvent(tmpEventPtrArray.back()); |
mkarlsso | 2:298679fff37c | 2706 | } |
mkarlsso | 2:298679fff37c | 2707 | |
mkarlsso | 2:298679fff37c | 2708 | } |
mkarlsso | 2:298679fff37c | 2709 | } |
mkarlsso | 2:298679fff37c | 2710 | |
mkarlsso | 2:298679fff37c | 2711 | //if starts an if block---------------------------------------------- |
mkarlsso | 2:298679fff37c | 2712 | //examples: if x < 10 && y == 1 do; if a==1 do in 1000 |
mkarlsso | 2:298679fff37c | 2713 | } else if (tokens[i].compare("if") == 0) { //a new if block |
mkarlsso | 2:298679fff37c | 2714 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2715 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2716 | lineError = true; |
mkarlsso | 2:298679fff37c | 2717 | } |
mkarlsso | 2:298679fff37c | 2718 | |
mkarlsso | 2:298679fff37c | 2719 | ifBlockInit = true; |
mkarlsso | 2:298679fff37c | 2720 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 2721 | bool eventDefined = false; |
mkarlsso | 2:298679fff37c | 2722 | tmpEvent = findFirstUnUsed(eventBlock, NUMEVENTS); |
mkarlsso | 2:298679fff37c | 2723 | if (tmpEvent != NULL) { |
mkarlsso | 2:298679fff37c | 2724 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2725 | eventDefined = true; |
mkarlsso | 2:298679fff37c | 2726 | } else { |
mkarlsso | 2:298679fff37c | 2727 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2728 | lineError = true; |
mkarlsso | 2:298679fff37c | 2729 | } |
mkarlsso | 2:298679fff37c | 2730 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2731 | //this is a regular event |
mkarlsso | 2:298679fff37c | 2732 | //tmpEventPtrArray.push_back(new event(queuePtr)); |
mkarlsso | 2:298679fff37c | 2733 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2734 | |
mkarlsso | 2:298679fff37c | 2735 | if ((!elseFlag) && ((!thenFlag))) { |
mkarlsso | 2:298679fff37c | 2736 | tmpEventPtrArray.back()->blockType = 1; //this is an if block |
mkarlsso | 2:298679fff37c | 2737 | blockDepth = blockDepth + 1; |
mkarlsso | 2:298679fff37c | 2738 | } else if (elseFlag) { |
mkarlsso | 2:298679fff37c | 2739 | tmpEventPtrArray.back()->blockType = 3; //this is an else if block |
mkarlsso | 2:298679fff37c | 2740 | } else if (thenFlag) { |
mkarlsso | 2:298679fff37c | 2741 | tmpEventPtrArray.back()->blockType = 7; //this is a then if block |
mkarlsso | 2:298679fff37c | 2742 | } |
mkarlsso | 2:298679fff37c | 2743 | } |
mkarlsso | 2:298679fff37c | 2744 | |
mkarlsso | 2:298679fff37c | 2745 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2746 | //combine the condition tokens without whitespaces |
mkarlsso | 2:298679fff37c | 2747 | tmpString = ""; |
mkarlsso | 2:298679fff37c | 2748 | for (unsigned j = i+1; j < sz; j++) { |
mkarlsso | 2:298679fff37c | 2749 | if (tokens[j].compare("do") != 0) { |
mkarlsso | 2:298679fff37c | 2750 | i++; |
mkarlsso | 2:298679fff37c | 2751 | tmpString.append(tokens[j]); |
mkarlsso | 2:298679fff37c | 2752 | } else { |
mkarlsso | 2:298679fff37c | 2753 | break; |
mkarlsso | 2:298679fff37c | 2754 | } |
mkarlsso | 2:298679fff37c | 2755 | } |
mkarlsso | 2:298679fff37c | 2756 | //adds the conditions to the current event |
mkarlsso | 2:298679fff37c | 2757 | |
mkarlsso | 2:298679fff37c | 2758 | if (!evaluateConditions(tmpString, tmpEventPtrArray.back())) lineError = true; |
mkarlsso | 2:298679fff37c | 2759 | } |
mkarlsso | 2:298679fff37c | 2760 | |
mkarlsso | 2:298679fff37c | 2761 | if (lineError && eventDefined) { |
mkarlsso | 2:298679fff37c | 2762 | tmpEvent->release(); |
mkarlsso | 2:298679fff37c | 2763 | } |
mkarlsso | 2:298679fff37c | 2764 | |
mkarlsso | 2:298679fff37c | 2765 | |
mkarlsso | 2:298679fff37c | 2766 | //else starts an else block------------------------------------- |
mkarlsso | 2:298679fff37c | 2767 | //examples: else do in 500; else if x==7 do |
mkarlsso | 2:298679fff37c | 2768 | } else if (tokens[i].compare("else") == 0) { //an else block |
mkarlsso | 2:298679fff37c | 2769 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2770 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2771 | lineError = true; |
mkarlsso | 2:298679fff37c | 2772 | } |
mkarlsso | 2:298679fff37c | 2773 | |
mkarlsso | 2:298679fff37c | 2774 | //trigger blocks can't have else conditions |
mkarlsso | 2:298679fff37c | 2775 | if ((!lineError) && (blockDepth < 2) && (currentTriggerPort > -1)) { |
mkarlsso | 2:298679fff37c | 2776 | pcPtr->printf("Error: else statement can not occur after a trigger block or outside a block\r\n"); |
mkarlsso | 2:298679fff37c | 2777 | lineError = true; |
mkarlsso | 2:298679fff37c | 2778 | } |
mkarlsso | 2:298679fff37c | 2779 | |
mkarlsso | 2:298679fff37c | 2780 | //check to make sure we are in an 'if' block |
mkarlsso | 2:298679fff37c | 2781 | if ((!lineError) && (tmpEventPtrArray.back()->blockType != 1) && (tmpEventPtrArray.back()->blockType != 3)) { //not currently in an 'if' or 'else if' block |
mkarlsso | 2:298679fff37c | 2782 | pcPtr->printf("Error: else statement can only occur in an 'if' block\r\n"); |
mkarlsso | 2:298679fff37c | 2783 | lineError = true; |
mkarlsso | 2:298679fff37c | 2784 | } |
mkarlsso | 2:298679fff37c | 2785 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2786 | elseFlag = true; |
mkarlsso | 2:298679fff37c | 2787 | |
mkarlsso | 2:298679fff37c | 2788 | } |
mkarlsso | 2:298679fff37c | 2789 | } else if (tokens[i].compare("then") == 0) { //a then block |
mkarlsso | 2:298679fff37c | 2790 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2791 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2792 | lineError = true; |
mkarlsso | 2:298679fff37c | 2793 | } |
mkarlsso | 2:298679fff37c | 2794 | |
mkarlsso | 2:298679fff37c | 2795 | //trigger blocks can't have else conditions |
mkarlsso | 2:298679fff37c | 2796 | if ((!lineError) && (blockDepth < 2) && (currentTriggerPort > -1)) { |
mkarlsso | 2:298679fff37c | 2797 | pcPtr->printf("Error: 'then' statement can only occur after a 'while' block\r\n"); |
mkarlsso | 2:298679fff37c | 2798 | lineError = true; |
mkarlsso | 2:298679fff37c | 2799 | } |
mkarlsso | 2:298679fff37c | 2800 | |
mkarlsso | 2:298679fff37c | 2801 | //check to make sure we are in a 'while' block |
mkarlsso | 2:298679fff37c | 2802 | if ((!lineError) && (tmpEventPtrArray.back()->blockType != 5)) { //not currently in a while block |
mkarlsso | 2:298679fff37c | 2803 | pcPtr->printf("Error: 'then' statement can only occur in a 'while' block\r\n"); |
mkarlsso | 2:298679fff37c | 2804 | lineError = true; |
mkarlsso | 2:298679fff37c | 2805 | } |
mkarlsso | 2:298679fff37c | 2806 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2807 | thenFlag = true; |
mkarlsso | 2:298679fff37c | 2808 | |
mkarlsso | 2:298679fff37c | 2809 | } |
mkarlsso | 2:298679fff37c | 2810 | //while starts a while block---------------------------------------- |
mkarlsso | 2:298679fff37c | 2811 | //example: while x<10 do every 100 |
mkarlsso | 2:298679fff37c | 2812 | // ... |
mkarlsso | 2:298679fff37c | 2813 | // end |
mkarlsso | 2:298679fff37c | 2814 | } else if (tokens[i].compare("while") == 0) { //a new while block |
mkarlsso | 2:298679fff37c | 2815 | if (ifBlockInit || whileBlockInit) { |
mkarlsso | 2:298679fff37c | 2816 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2817 | lineError = true; |
mkarlsso | 2:298679fff37c | 2818 | } |
mkarlsso | 2:298679fff37c | 2819 | |
mkarlsso | 2:298679fff37c | 2820 | if ((currentTriggerPort > 0) || (currentFunction > 0)) { //check to make sure we are inside a trigger block |
mkarlsso | 2:298679fff37c | 2821 | //pcPtr->printf("Start new block\r\n"); |
mkarlsso | 2:298679fff37c | 2822 | |
mkarlsso | 2:298679fff37c | 2823 | } else { |
mkarlsso | 2:298679fff37c | 2824 | pcPtr->printf("Error: a statement block must be placed inside a callback or function.\r\n"); |
mkarlsso | 2:298679fff37c | 2825 | lineError = true; |
mkarlsso | 2:298679fff37c | 2826 | } |
mkarlsso | 2:298679fff37c | 2827 | //whileBlockInit = true; |
mkarlsso | 2:298679fff37c | 2828 | currentDelay = 0; |
mkarlsso | 2:298679fff37c | 2829 | |
mkarlsso | 2:298679fff37c | 2830 | tmpEvent = findFirstUnUsed(eventBlock, NUMEVENTS); |
mkarlsso | 2:298679fff37c | 2831 | if (tmpEvent != NULL) { |
mkarlsso | 2:298679fff37c | 2832 | tmpEvent->isUsed = true; |
mkarlsso | 2:298679fff37c | 2833 | tmpEvent->setTimeLag(currentDelay); |
mkarlsso | 2:298679fff37c | 2834 | } else { |
mkarlsso | 2:298679fff37c | 2835 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2836 | lineError = true; |
mkarlsso | 2:298679fff37c | 2837 | } |
mkarlsso | 2:298679fff37c | 2838 | |
mkarlsso | 2:298679fff37c | 2839 | //tmpEvent = new event(queuePtr); |
mkarlsso | 2:298679fff37c | 2840 | |
mkarlsso | 2:298679fff37c | 2841 | |
mkarlsso | 2:298679fff37c | 2842 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2843 | //combine the condition tokens without whitespaces |
mkarlsso | 2:298679fff37c | 2844 | tmpString = ""; |
mkarlsso | 2:298679fff37c | 2845 | for (unsigned j = i+1; j < sz; j++) { |
mkarlsso | 2:298679fff37c | 2846 | if (tokens[j].compare("do") != 0) { |
mkarlsso | 2:298679fff37c | 2847 | i++; |
mkarlsso | 2:298679fff37c | 2848 | tmpString.append(tokens[j]); |
mkarlsso | 2:298679fff37c | 2849 | } else { |
mkarlsso | 2:298679fff37c | 2850 | break; |
mkarlsso | 2:298679fff37c | 2851 | } |
mkarlsso | 2:298679fff37c | 2852 | } |
mkarlsso | 2:298679fff37c | 2853 | //adds the conditions to the current event |
mkarlsso | 2:298679fff37c | 2854 | if (!evaluateConditions(tmpString, tmpEvent)) lineError = true; |
mkarlsso | 2:298679fff37c | 2855 | } |
mkarlsso | 2:298679fff37c | 2856 | |
mkarlsso | 2:298679fff37c | 2857 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2858 | if ((i+3) < sz) { |
mkarlsso | 2:298679fff37c | 2859 | if ((tokens[i+1].compare("do") == 0) && (tokens[i+2].compare("every") == 0)) { |
mkarlsso | 2:298679fff37c | 2860 | |
mkarlsso | 2:298679fff37c | 2861 | if (isNumber(tokens[i+3])) { |
mkarlsso | 2:298679fff37c | 2862 | uint32_t period = atoi(tokens[i+3].data()); |
mkarlsso | 2:298679fff37c | 2863 | if (period > 0) { |
mkarlsso | 2:298679fff37c | 2864 | //pcPtr->printf("While block\r\n"); |
mkarlsso | 2:298679fff37c | 2865 | |
mkarlsso | 2:298679fff37c | 2866 | //tmpEvent->whileLoopPeriod = period; |
mkarlsso | 2:298679fff37c | 2867 | tmpEvent->setWhileLoopPeriod(period); |
mkarlsso | 2:298679fff37c | 2868 | if (!elseFlag) { |
mkarlsso | 2:298679fff37c | 2869 | tmpEvent->blockType = 5; //this is a while block |
mkarlsso | 2:298679fff37c | 2870 | |
mkarlsso | 2:298679fff37c | 2871 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2872 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2873 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2874 | lineError = true; |
mkarlsso | 2:298679fff37c | 2875 | } else { |
mkarlsso | 2:298679fff37c | 2876 | tmpAction->set(tmpEvent); |
mkarlsso | 2:298679fff37c | 2877 | } |
mkarlsso | 2:298679fff37c | 2878 | //tmpEventPtrArray.back()->addAction(new action(tmpEvent)); |
mkarlsso | 2:298679fff37c | 2879 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2880 | |
mkarlsso | 2:298679fff37c | 2881 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2882 | blockDepth = blockDepth+1; |
mkarlsso | 2:298679fff37c | 2883 | } else { |
mkarlsso | 2:298679fff37c | 2884 | tmpEvent->blockType = 6; //this is an else while block |
mkarlsso | 2:298679fff37c | 2885 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2886 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2887 | } |
mkarlsso | 2:298679fff37c | 2888 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2889 | } else { |
mkarlsso | 2:298679fff37c | 2890 | pcPtr->printf("Error: loop period must be a positive integer.\r\n"); |
mkarlsso | 2:298679fff37c | 2891 | lineError = true; |
mkarlsso | 2:298679fff37c | 2892 | } |
mkarlsso | 2:298679fff37c | 2893 | } else if (findIntVariable(tokens[i+3])!=NULL) { |
mkarlsso | 2:298679fff37c | 2894 | |
mkarlsso | 2:298679fff37c | 2895 | int* period = findIntVariable(tokens[i+3]); |
mkarlsso | 2:298679fff37c | 2896 | //tmpEvent->whileLoopPeriodVar = period; |
mkarlsso | 2:298679fff37c | 2897 | tmpEvent->setWhileLoopPeriod(period); |
mkarlsso | 2:298679fff37c | 2898 | if (!elseFlag) { |
mkarlsso | 2:298679fff37c | 2899 | tmpEvent->blockType = 5; //this is a while block |
mkarlsso | 2:298679fff37c | 2900 | |
mkarlsso | 2:298679fff37c | 2901 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 2902 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 2903 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 2904 | lineError = true; |
mkarlsso | 2:298679fff37c | 2905 | } else { |
mkarlsso | 2:298679fff37c | 2906 | tmpAction->set(tmpEvent); |
mkarlsso | 2:298679fff37c | 2907 | } |
mkarlsso | 2:298679fff37c | 2908 | //tmpEventPtrArray.back()->addAction(new action(tmpEvent)); |
mkarlsso | 2:298679fff37c | 2909 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2910 | |
mkarlsso | 2:298679fff37c | 2911 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2912 | blockDepth = blockDepth+1; |
mkarlsso | 2:298679fff37c | 2913 | } else { |
mkarlsso | 2:298679fff37c | 2914 | tmpEvent->blockType = 6; //this is an else while block |
mkarlsso | 2:298679fff37c | 2915 | tmpEventPtrArray.back()->setNextElseEvent(tmpEvent); |
mkarlsso | 2:298679fff37c | 2916 | tmpEventPtrArray.push_back(tmpEvent); |
mkarlsso | 2:298679fff37c | 2917 | } |
mkarlsso | 2:298679fff37c | 2918 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2919 | } |
mkarlsso | 2:298679fff37c | 2920 | } else { |
mkarlsso | 2:298679fff37c | 2921 | pcPtr->printf("Error: expected a 'do every' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2922 | lineError = true; |
mkarlsso | 2:298679fff37c | 2923 | } |
mkarlsso | 2:298679fff37c | 2924 | } else { |
mkarlsso | 2:298679fff37c | 2925 | pcPtr->printf("Error: expected a 'do every' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2926 | lineError = true; |
mkarlsso | 2:298679fff37c | 2927 | } |
mkarlsso | 2:298679fff37c | 2928 | } |
mkarlsso | 2:298679fff37c | 2929 | |
mkarlsso | 2:298679fff37c | 2930 | //if the line contains an '=' sign,the equality is evaulated------------------------- |
mkarlsso | 2:298679fff37c | 2931 | //examples: a = 1; a = b + 5; a = random(100); portout[2] = 1; portout[2] = flip |
mkarlsso | 2:298679fff37c | 2932 | } else if ((tmpLine.find_first_of("=") != std::string::npos) ) { //an expression |
mkarlsso | 2:298679fff37c | 2933 | if (ifBlockInit || whileBlockInit || elseFlag) { |
mkarlsso | 2:298679fff37c | 2934 | pcPtr->printf("Error: expected a 'do' statement\r\n"); |
mkarlsso | 2:298679fff37c | 2935 | lineError = true; |
mkarlsso | 2:298679fff37c | 2936 | } |
mkarlsso | 2:298679fff37c | 2937 | |
mkarlsso | 2:298679fff37c | 2938 | wholeLineEvaluated = true; |
mkarlsso | 2:298679fff37c | 2939 | tmpString = ""; |
mkarlsso | 2:298679fff37c | 2940 | int spacesBeforeEqualSign = 0; |
mkarlsso | 2:298679fff37c | 2941 | bool countSpaces = true; |
mkarlsso | 2:298679fff37c | 2942 | //combine the tokens without whitespaces |
mkarlsso | 2:298679fff37c | 2943 | for (unsigned j = i; j < sz; j++) { |
mkarlsso | 2:298679fff37c | 2944 | tmpString.append(tokens[j]); |
mkarlsso | 2:298679fff37c | 2945 | if (tokens[j].find_first_of("=") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 2946 | if (tokens[j].find_first_of("=") > 0) spacesBeforeEqualSign++; |
mkarlsso | 2:298679fff37c | 2947 | countSpaces = false; |
mkarlsso | 2:298679fff37c | 2948 | } else if (countSpaces) { |
mkarlsso | 2:298679fff37c | 2949 | spacesBeforeEqualSign++; |
mkarlsso | 2:298679fff37c | 2950 | } |
mkarlsso | 2:298679fff37c | 2951 | } |
mkarlsso | 2:298679fff37c | 2952 | if (!lineError && spacesBeforeEqualSign > 1) { |
mkarlsso | 2:298679fff37c | 2953 | pcPtr->printf("Error: Variable can't have a space in it.\r\n"); |
mkarlsso | 2:298679fff37c | 2954 | lineError = true; |
mkarlsso | 2:298679fff37c | 2955 | } |
mkarlsso | 2:298679fff37c | 2956 | |
mkarlsso | 2:298679fff37c | 2957 | if (!lineError) { |
mkarlsso | 2:298679fff37c | 2958 | if (blockDepth > 0) { |
mkarlsso | 2:298679fff37c | 2959 | action* tmpAction = evaluateAssignmentForAction(tmpString); |
mkarlsso | 2:298679fff37c | 2960 | if (tmpAction != NULL) { |
mkarlsso | 2:298679fff37c | 2961 | tmpEventPtrArray.back()->addAction(tmpAction); |
mkarlsso | 2:298679fff37c | 2962 | //pcPtr->printf("Added action with delay: %d\r\n", tmpEventPtrArray.back()->timeLag); |
mkarlsso | 2:298679fff37c | 2963 | |
mkarlsso | 2:298679fff37c | 2964 | } else { |
mkarlsso | 2:298679fff37c | 2965 | lineError = true; |
mkarlsso | 2:298679fff37c | 2966 | } |
mkarlsso | 2:298679fff37c | 2967 | |
mkarlsso | 2:298679fff37c | 2968 | } else { //assignment was written outside of any block structure, so execute now |
mkarlsso | 2:298679fff37c | 2969 | |
mkarlsso | 2:298679fff37c | 2970 | |
mkarlsso | 2:298679fff37c | 2971 | action* tmpAction = evaluateAssignmentForAction(tmpString); |
mkarlsso | 2:298679fff37c | 2972 | |
mkarlsso | 3:ae33b7f5a7c1 | 2973 | if (tmpAction != NULL) { |
mkarlsso | 2:298679fff37c | 2974 | tmpAction->execute(); |
mkarlsso | 3:ae33b7f5a7c1 | 2975 | |
mkarlsso | 2:298679fff37c | 2976 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 2977 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 2978 | } else { |
mkarlsso | 2:298679fff37c | 2979 | lineError = true; |
mkarlsso | 2:298679fff37c | 2980 | } |
mkarlsso | 2:298679fff37c | 2981 | } |
mkarlsso | 2:298679fff37c | 2982 | } |
mkarlsso | 2:298679fff37c | 2983 | } else { |
mkarlsso | 2:298679fff37c | 2984 | //if there was no match to any of the above, an error is given |
mkarlsso | 2:298679fff37c | 2985 | pcPtr->printf("Error: statement not understood.\r\n"); |
mkarlsso | 2:298679fff37c | 2986 | lineError = true; |
mkarlsso | 2:298679fff37c | 2987 | } |
mkarlsso | 2:298679fff37c | 2988 | |
mkarlsso | 2:298679fff37c | 2989 | if (lineError) break; //stop parsing the rest of the line if an error was detected |
mkarlsso | 2:298679fff37c | 2990 | |
mkarlsso | 2:298679fff37c | 2991 | if (wholeLineEvaluated) { //some of the tokens forces the whole line to be avaluated at once |
mkarlsso | 2:298679fff37c | 2992 | i = sz; //skip ahead to end of the line |
mkarlsso | 2:298679fff37c | 2993 | } |
mkarlsso | 2:298679fff37c | 2994 | |
mkarlsso | 2:298679fff37c | 2995 | } |
mkarlsso | 2:298679fff37c | 2996 | |
mkarlsso | 2:298679fff37c | 2997 | //if there was an error, we quit compiling the code |
mkarlsso | 2:298679fff37c | 2998 | if (lineError) { |
mkarlsso | 2:298679fff37c | 2999 | pcPtr->printf("Line text: "); |
mkarlsso | 2:298679fff37c | 3000 | while (!tokens.empty()) { |
mkarlsso | 2:298679fff37c | 3001 | pcPtr->printf("%s ",tokens.front().data()); |
mkarlsso | 2:298679fff37c | 3002 | tokens.erase(tokens.begin()); |
mkarlsso | 2:298679fff37c | 3003 | } |
mkarlsso | 2:298679fff37c | 3004 | pcPtr->printf("\r\n"); |
mkarlsso | 4:34aca2142df9 | 3005 | /* |
mkarlsso | 2:298679fff37c | 3006 | while (!currentBlock.empty()) { |
mkarlsso | 2:298679fff37c | 3007 | currentBlock.pop_back(); |
mkarlsso | 4:34aca2142df9 | 3008 | }*/ |
mkarlsso | 4:34aca2142df9 | 3009 | currentBlock.resetBuffer(); |
mkarlsso | 4:34aca2142df9 | 3010 | while (!tokens.empty()) { |
mkarlsso | 4:34aca2142df9 | 3011 | tokens.pop_back(); |
mkarlsso | 2:298679fff37c | 3012 | } |
mkarlsso | 2:298679fff37c | 3013 | delete tmpEvent; |
mkarlsso | 2:298679fff37c | 3014 | } else { |
mkarlsso | 2:298679fff37c | 3015 | |
mkarlsso | 2:298679fff37c | 3016 | while (!tokens.empty()) { |
mkarlsso | 2:298679fff37c | 3017 | tokens.pop_back(); |
mkarlsso | 2:298679fff37c | 3018 | } |
mkarlsso | 4:34aca2142df9 | 3019 | //currentBlock.pop_back(); |
mkarlsso | 4:34aca2142df9 | 3020 | |
mkarlsso | 2:298679fff37c | 3021 | } |
mkarlsso | 2:298679fff37c | 3022 | |
mkarlsso | 2:298679fff37c | 3023 | } |
mkarlsso | 2:298679fff37c | 3024 | |
mkarlsso | 2:298679fff37c | 3025 | //make sure that all blocks have a matching end statement |
mkarlsso | 4:34aca2142df9 | 3026 | |
mkarlsso | 4:34aca2142df9 | 3027 | |
mkarlsso | 2:298679fff37c | 3028 | if ((!lineError)&&(blockDepth > 0)) { |
mkarlsso | 2:298679fff37c | 3029 | pcPtr->printf("Error: Missing 1 or more end statements\r\n"); |
mkarlsso | 2:298679fff37c | 3030 | lineError = true; |
mkarlsso | 4:34aca2142df9 | 3031 | currentBlock.resetBuffer(); |
mkarlsso | 4:34aca2142df9 | 3032 | } |
mkarlsso | 4:34aca2142df9 | 3033 | |
mkarlsso | 4:34aca2142df9 | 3034 | if ((!lineError)&&(blockDepth == 0)) { |
mkarlsso | 4:34aca2142df9 | 3035 | textDisplay.send("~~~\r\n"); |
mkarlsso | 4:34aca2142df9 | 3036 | } |
mkarlsso | 4:34aca2142df9 | 3037 | |
mkarlsso | 2:298679fff37c | 3038 | //displayMemoryLeft(); |
mkarlsso | 2:298679fff37c | 3039 | //DisplayRAMBanks(); |
mkarlsso | 2:298679fff37c | 3040 | |
mkarlsso | 2:298679fff37c | 3041 | } |
mkarlsso | 2:298679fff37c | 3042 | |
mkarlsso | 2:298679fff37c | 3043 | |
mkarlsso | 2:298679fff37c | 3044 | //used to return a pointer to a variable, if it exists |
mkarlsso | 2:298679fff37c | 3045 | int* scriptStream::findIntVariable(string nameInput) { |
mkarlsso | 2:298679fff37c | 3046 | |
mkarlsso | 2:298679fff37c | 3047 | int* outPtr = NULL; |
mkarlsso | 2:298679fff37c | 3048 | bool foundIt = false; |
mkarlsso | 2:298679fff37c | 3049 | |
mkarlsso | 2:298679fff37c | 3050 | if (nameInput.find("portout") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 3051 | int pos1 = nameInput.find("portout[")+8; |
mkarlsso | 2:298679fff37c | 3052 | int pos2 = nameInput.find_first_of("]",pos1); |
mkarlsso | 2:298679fff37c | 3053 | int portnum = atoi(nameInput.substr(pos1,pos2-pos1).data()); |
mkarlsso | 2:298679fff37c | 3054 | int portVal = 0; |
mkarlsso | 2:298679fff37c | 3055 | if ((portnum > 0) && (portnum <= numPorts)) { |
mkarlsso | 2:298679fff37c | 3056 | outPtr = &portVector[portnum]->outState; |
mkarlsso | 2:298679fff37c | 3057 | foundIt = true; |
mkarlsso | 2:298679fff37c | 3058 | } |
mkarlsso | 2:298679fff37c | 3059 | } else if (nameInput.find("portin") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 3060 | int pos1 = nameInput.find("portin[")+7; |
mkarlsso | 2:298679fff37c | 3061 | int pos2 = nameInput.find_first_of("]",pos1); |
mkarlsso | 2:298679fff37c | 3062 | int portnum = atoi(nameInput.substr(pos1,pos2-pos1).data()); |
mkarlsso | 2:298679fff37c | 3063 | int portVal = 0; |
mkarlsso | 2:298679fff37c | 3064 | if ((portnum > 0) && (portnum <= numPorts)) { |
mkarlsso | 2:298679fff37c | 3065 | outPtr = &portVector[portnum]->inState; |
mkarlsso | 2:298679fff37c | 3066 | foundIt = true; |
mkarlsso | 2:298679fff37c | 3067 | } |
mkarlsso | 2:298679fff37c | 3068 | } |
mkarlsso | 2:298679fff37c | 3069 | |
mkarlsso | 2:298679fff37c | 3070 | if (!foundIt) { |
mkarlsso | 2:298679fff37c | 3071 | std::vector<intVariable*>::size_type sz = globalVariables.size(); |
mkarlsso | 2:298679fff37c | 3072 | for (unsigned i = 0; i < sz; i++) { |
mkarlsso | 2:298679fff37c | 3073 | if (nameInput.compare(globalVariables[i]->tag) == 0) { |
mkarlsso | 2:298679fff37c | 3074 | outPtr = &globalVariables[i]->value; |
mkarlsso | 2:298679fff37c | 3075 | break; |
mkarlsso | 2:298679fff37c | 3076 | } |
mkarlsso | 2:298679fff37c | 3077 | } |
mkarlsso | 2:298679fff37c | 3078 | } |
mkarlsso | 2:298679fff37c | 3079 | |
mkarlsso | 2:298679fff37c | 3080 | return outPtr; |
mkarlsso | 2:298679fff37c | 3081 | } |
mkarlsso | 2:298679fff37c | 3082 | |
mkarlsso | 2:298679fff37c | 3083 | bool scriptStream::createIntVariable(string nameInput) { |
mkarlsso | 2:298679fff37c | 3084 | if (findIntVariable(nameInput) == NULL) { |
mkarlsso | 2:298679fff37c | 3085 | globalVariables.push_back(new intVariable(nameInput, 0)); |
mkarlsso | 2:298679fff37c | 3086 | return true; |
mkarlsso | 2:298679fff37c | 3087 | } else { |
mkarlsso | 2:298679fff37c | 3088 | return false; |
mkarlsso | 2:298679fff37c | 3089 | } |
mkarlsso | 2:298679fff37c | 3090 | } |
mkarlsso | 2:298679fff37c | 3091 | |
mkarlsso | 2:298679fff37c | 3092 | |
mkarlsso | 2:298679fff37c | 3093 | action* scriptStream::evaluateAssignmentForAction(string expression) { |
mkarlsso | 2:298679fff37c | 3094 | |
mkarlsso | 2:298679fff37c | 3095 | //action* tmpAction = new action(); //create a new action |
mkarlsso | 2:298679fff37c | 3096 | action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS); |
mkarlsso | 2:298679fff37c | 3097 | if (tmpAction == NULL) { |
mkarlsso | 2:298679fff37c | 3098 | pcPtr->printf("Error: no action memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3099 | return NULL; |
mkarlsso | 2:298679fff37c | 3100 | } |
mkarlsso | 2:298679fff37c | 3101 | std::size_t stringInd; |
mkarlsso | 2:298679fff37c | 3102 | std::size_t stringInd2; |
mkarlsso | 2:298679fff37c | 3103 | string afterEqual; |
mkarlsso | 2:298679fff37c | 3104 | string beforeEqual; |
mkarlsso | 2:298679fff37c | 3105 | //The expression might have up to three variables |
mkarlsso | 2:298679fff37c | 3106 | int* tmpVar; |
mkarlsso | 2:298679fff37c | 3107 | int* tmpVar2; |
mkarlsso | 2:298679fff37c | 3108 | int* tmpVar3; |
mkarlsso | 2:298679fff37c | 3109 | stringInd = expression.find_first_of("="); //location of = sign, if it exists |
mkarlsso | 2:298679fff37c | 3110 | beforeEqual = expression.substr(0,stringInd); // the string after the = sign |
mkarlsso | 2:298679fff37c | 3111 | afterEqual = expression.substr(stringInd+1,std::string::npos); // the string after the = sign |
mkarlsso | 2:298679fff37c | 3112 | stringInd2 = afterEqual.find_first_of("+-"); //location of +/- sign (only one allowed) |
mkarlsso | 2:298679fff37c | 3113 | tmpVar = findIntVariable(expression.substr(0,stringInd)); //returns pointer to the variable |
mkarlsso | 2:298679fff37c | 3114 | |
mkarlsso | 2:298679fff37c | 3115 | if (beforeEqual.find("portout[") != std::string::npos) { //set the output of a digital port |
mkarlsso | 2:298679fff37c | 3116 | int pos1 = beforeEqual.find("portout[")+8; |
mkarlsso | 2:298679fff37c | 3117 | int pos2 = beforeEqual.find_first_of("]",pos1); |
mkarlsso | 2:298679fff37c | 3118 | int portnum = atoi(beforeEqual.substr(pos1,pos2-pos1).data()); |
mkarlsso | 2:298679fff37c | 3119 | int* tmpVar = findIntVariable(beforeEqual.substr(pos1,pos2-pos1)); //returns pointer to the variable, if given |
mkarlsso | 2:298679fff37c | 3120 | int portVal = 0; |
mkarlsso | 2:298679fff37c | 3121 | if ((tmpVar != NULL)||((portnum > 0) && (portnum <= numPorts))) { |
mkarlsso | 2:298679fff37c | 3122 | if (isNumber(afterEqual)) { //a simple numeric assign |
mkarlsso | 2:298679fff37c | 3123 | portVal = atoi(afterEqual.data()); |
mkarlsso | 2:298679fff37c | 3124 | if ((portVal == 0) || (portVal == 1)) { |
mkarlsso | 2:298679fff37c | 3125 | //portMessage* tmpMessage = new portMessage(portVector[portnum],1,portVal); |
mkarlsso | 2:298679fff37c | 3126 | portMessage* tmpMessage = findFirstUnUsed(portMessageBlock, NUMPORTMESSAGES); |
mkarlsso | 2:298679fff37c | 3127 | if (tmpMessage == NULL) { |
mkarlsso | 2:298679fff37c | 3128 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3129 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3130 | return NULL; |
mkarlsso | 2:298679fff37c | 3131 | } else { |
mkarlsso | 2:298679fff37c | 3132 | //tmpMessage->setMessage(portVector[portnum],1,portVal); |
mkarlsso | 2:298679fff37c | 3133 | if (tmpVar == NULL) { //a constant port number was given |
mkarlsso | 2:298679fff37c | 3134 | tmpMessage->setMessage(NULL,portnum,portVal); |
mkarlsso | 2:298679fff37c | 3135 | } else { |
mkarlsso | 2:298679fff37c | 3136 | tmpMessage->setMessage(tmpVar,0,portVal); |
mkarlsso | 2:298679fff37c | 3137 | } |
mkarlsso | 2:298679fff37c | 3138 | } |
mkarlsso | 2:298679fff37c | 3139 | |
mkarlsso | 2:298679fff37c | 3140 | |
mkarlsso | 2:298679fff37c | 3141 | tmpAction->set(tmpMessage); |
mkarlsso | 2:298679fff37c | 3142 | //pcPtr->printf("Action: digital port %d set to %d\r\n",portnum,portVal); |
mkarlsso | 2:298679fff37c | 3143 | } else { |
mkarlsso | 2:298679fff37c | 3144 | pcPtr->printf("Error: portouts can only be directly assigned a 1, 0 or 'flip'\r\n"); |
mkarlsso | 2:298679fff37c | 3145 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3146 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3147 | return NULL; |
mkarlsso | 2:298679fff37c | 3148 | } |
mkarlsso | 2:298679fff37c | 3149 | } else if (afterEqual.compare("flip") == 0) { |
mkarlsso | 2:298679fff37c | 3150 | //portMessage* tmpMessage = new portMessage(portVector[portnum],1,-1); |
mkarlsso | 2:298679fff37c | 3151 | portMessage* tmpMessage = findFirstUnUsed(portMessageBlock, NUMPORTMESSAGES); |
mkarlsso | 2:298679fff37c | 3152 | if (tmpMessage == NULL) { |
mkarlsso | 2:298679fff37c | 3153 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3154 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3155 | return NULL; |
mkarlsso | 2:298679fff37c | 3156 | } else { |
mkarlsso | 2:298679fff37c | 3157 | //tmpMessage->setMessage(portVector[portnum],1,-1); |
mkarlsso | 2:298679fff37c | 3158 | if (tmpVar == NULL) { //a constant port number was given |
mkarlsso | 3:ae33b7f5a7c1 | 3159 | tmpMessage->setMessage(NULL,portnum,-1); |
mkarlsso | 2:298679fff37c | 3160 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3161 | tmpMessage->setMessage(tmpVar,0,-1); |
mkarlsso | 2:298679fff37c | 3162 | } |
mkarlsso | 2:298679fff37c | 3163 | } |
mkarlsso | 2:298679fff37c | 3164 | tmpAction->set(tmpMessage); |
mkarlsso | 2:298679fff37c | 3165 | } else { |
mkarlsso | 2:298679fff37c | 3166 | pcPtr->printf("Error: portouts can only be directly assigned a 1, 0, or 'flip'\r\n"); |
mkarlsso | 2:298679fff37c | 3167 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3168 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3169 | return NULL; |
mkarlsso | 2:298679fff37c | 3170 | } |
mkarlsso | 2:298679fff37c | 3171 | } else { |
mkarlsso | 2:298679fff37c | 3172 | pcPtr->printf("Port number not found (must be between 1 and %d, or an existing variable)\r\n", numPorts); |
mkarlsso | 2:298679fff37c | 3173 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3174 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3175 | return NULL; |
mkarlsso | 2:298679fff37c | 3176 | } |
mkarlsso | 2:298679fff37c | 3177 | } else if (beforeEqual.find("portin") != std::string::npos) { |
mkarlsso | 2:298679fff37c | 3178 | pcPtr->printf("Error: portins can not be set\r\n"); |
mkarlsso | 2:298679fff37c | 3179 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3180 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3181 | return NULL; |
mkarlsso | 2:298679fff37c | 3182 | } else if (tmpVar != NULL) { |
mkarlsso | 2:298679fff37c | 3183 | intOperation* tmpOp; |
mkarlsso | 2:298679fff37c | 3184 | intOperation* tmpOp2; |
mkarlsso | 2:298679fff37c | 3185 | if (isNumber(afterEqual)) { //a simple numeric assign |
mkarlsso | 2:298679fff37c | 3186 | //tmpOp = new intOperation(tmpVar, "=", atoi(afterEqual.data())); |
mkarlsso | 2:298679fff37c | 3187 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3188 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3189 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3190 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3191 | return NULL; |
mkarlsso | 2:298679fff37c | 3192 | } else { |
mkarlsso | 2:298679fff37c | 3193 | tmpOp->set(tmpVar, "=", atoi(afterEqual.data())); |
mkarlsso | 2:298679fff37c | 3194 | } |
mkarlsso | 2:298679fff37c | 3195 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3196 | //pcPtr->printf("Action: set variable to constant numeric value\r\n"); |
mkarlsso | 2:298679fff37c | 3197 | } else if ((stringInd2 == std::string::npos)&&(afterEqual.find("random") != std::string::npos)) { |
mkarlsso | 2:298679fff37c | 3198 | |
mkarlsso | 2:298679fff37c | 3199 | int highVal = getRandomParam(afterEqual); |
mkarlsso | 2:298679fff37c | 3200 | |
mkarlsso | 2:298679fff37c | 3201 | if (highVal > 0) { |
mkarlsso | 2:298679fff37c | 3202 | //tmpOp = new intOperation(highVal, "=", tmpVar); //for random assignment, we reverse the input order (because of overloading uniqueness) |
mkarlsso | 2:298679fff37c | 3203 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3204 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3205 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3206 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3207 | return NULL; |
mkarlsso | 2:298679fff37c | 3208 | } else { |
mkarlsso | 2:298679fff37c | 3209 | tmpOp->set(highVal, "=", tmpVar); //for random assignment, we reverse the input order (because of overloading uniqueness) |
mkarlsso | 2:298679fff37c | 3210 | } |
mkarlsso | 2:298679fff37c | 3211 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3212 | //pcPtr->printf("Action: set variable to random value up to %d\r\n", highVal); |
mkarlsso | 2:298679fff37c | 3213 | } else { |
mkarlsso | 2:298679fff37c | 3214 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3215 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3216 | return NULL; |
mkarlsso | 2:298679fff37c | 3217 | } |
mkarlsso | 2:298679fff37c | 3218 | |
mkarlsso | 2:298679fff37c | 3219 | } else if (stringInd2 != std::string::npos) { //a +/- operation is there |
mkarlsso | 2:298679fff37c | 3220 | string multiplier("+"); |
mkarlsso | 2:298679fff37c | 3221 | int multiplierInt = 1; |
mkarlsso | 2:298679fff37c | 3222 | if (afterEqual[stringInd2] == '-') { |
mkarlsso | 2:298679fff37c | 3223 | multiplier = "-"; |
mkarlsso | 2:298679fff37c | 3224 | multiplierInt = -1; |
mkarlsso | 2:298679fff37c | 3225 | } |
mkarlsso | 2:298679fff37c | 3226 | tmpVar2 = findIntVariable(afterEqual.substr(0,stringInd2)); //before the +/- sign |
mkarlsso | 2:298679fff37c | 3227 | tmpVar3 = findIntVariable(afterEqual.substr(stringInd2+1,std::string::npos)); //after the +/- sign |
mkarlsso | 2:298679fff37c | 3228 | |
mkarlsso | 2:298679fff37c | 3229 | if ((tmpVar2 != NULL) && isNumber(afterEqual.substr(stringInd2+1,std::string::npos))) { //variable +/- number |
mkarlsso | 2:298679fff37c | 3230 | if (tmpVar2 == tmpVar) { |
mkarlsso | 2:298679fff37c | 3231 | multiplier.append("="); //final sign is += or -= |
mkarlsso | 2:298679fff37c | 3232 | //tmpOp = new intOperation(tmpVar, multiplier.data(), atoi(afterEqual.substr(stringInd2+1,std::string::npos).data())); |
mkarlsso | 2:298679fff37c | 3233 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3234 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3235 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3236 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3237 | return NULL; |
mkarlsso | 2:298679fff37c | 3238 | } else { |
mkarlsso | 2:298679fff37c | 3239 | tmpOp->set(tmpVar, multiplier.data(), atoi(afterEqual.substr(stringInd2+1,std::string::npos).data())); |
mkarlsso | 2:298679fff37c | 3240 | } |
mkarlsso | 2:298679fff37c | 3241 | tmpAction->set(tmpOp); |
mkarlsso | 4:34aca2142df9 | 3242 | //pcPtr->printf("Action: change variable by constant amount\r\n"); |
mkarlsso | 2:298679fff37c | 3243 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3244 | |
mkarlsso | 2:298679fff37c | 3245 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3246 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3247 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3248 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3249 | return NULL; |
mkarlsso | 2:298679fff37c | 3250 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3251 | |
mkarlsso | 2:298679fff37c | 3252 | tmpOp2->set(tmpVar2,multiplier.data(), atoi(afterEqual.substr(stringInd2+1,std::string::npos).data())); |
mkarlsso | 3:ae33b7f5a7c1 | 3253 | //tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 3:ae33b7f5a7c1 | 3254 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3255 | |
mkarlsso | 3:ae33b7f5a7c1 | 3256 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3257 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3258 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3259 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3260 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3261 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3262 | } else { |
mkarlsso | 2:298679fff37c | 3263 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 3:ae33b7f5a7c1 | 3264 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3265 | |
mkarlsso | 2:298679fff37c | 3266 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3267 | //pcPtr->printf("Action: variable equals expression\r\n"); |
mkarlsso | 2:298679fff37c | 3268 | } |
mkarlsso | 2:298679fff37c | 3269 | |
mkarlsso | 2:298679fff37c | 3270 | } else if ((tmpVar3 != NULL) && isNumber(afterEqual.substr(0,stringInd2))) { //number +/- variable |
mkarlsso | 2:298679fff37c | 3271 | if (tmpVar3 == tmpVar) { |
mkarlsso | 3:ae33b7f5a7c1 | 3272 | multiplier.append("="); //makes "+=" or "-=" |
mkarlsso | 2:298679fff37c | 3273 | //tmpOp = new intOperation(tmpVar, multiplier.data(), atoi(afterEqual.substr(0,stringInd2).data())); |
mkarlsso | 2:298679fff37c | 3274 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3275 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3276 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3277 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3278 | return NULL; |
mkarlsso | 2:298679fff37c | 3279 | } else { |
mkarlsso | 2:298679fff37c | 3280 | tmpOp->set(tmpVar, multiplier.data(), atoi(afterEqual.substr(0,stringInd2).data())); |
mkarlsso | 2:298679fff37c | 3281 | } |
mkarlsso | 2:298679fff37c | 3282 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3283 | //pcPtr->printf("Action: change variable by constant amount\r\n"); |
mkarlsso | 2:298679fff37c | 3284 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3285 | |
mkarlsso | 2:298679fff37c | 3286 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3287 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3288 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3289 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3290 | return NULL; |
mkarlsso | 2:298679fff37c | 3291 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3292 | tmpOp2->set(tmpVar3, multiplier.data(), atoi(afterEqual.substr(0, stringInd2).data())); |
mkarlsso | 3:ae33b7f5a7c1 | 3293 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3294 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3295 | |
mkarlsso | 3:ae33b7f5a7c1 | 3296 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3297 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3298 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3299 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3300 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3301 | } else { |
mkarlsso | 2:298679fff37c | 3302 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3303 | } |
mkarlsso | 2:298679fff37c | 3304 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3305 | //pcPtr->printf("Action: variable equals expression\r\n"); |
mkarlsso | 2:298679fff37c | 3306 | } |
mkarlsso | 2:298679fff37c | 3307 | |
mkarlsso | 2:298679fff37c | 3308 | } else if ((tmpVar2 != NULL) && (tmpVar3 != NULL)) { //variable +/- variable |
mkarlsso | 3:ae33b7f5a7c1 | 3309 | |
mkarlsso | 2:298679fff37c | 3310 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3311 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3312 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3313 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3314 | return NULL; |
mkarlsso | 2:298679fff37c | 3315 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3316 | tmpOp2->set(tmpVar2, multiplier.data(), tmpVar3); |
mkarlsso | 3:ae33b7f5a7c1 | 3317 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3318 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3319 | |
mkarlsso | 3:ae33b7f5a7c1 | 3320 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3321 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3322 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3323 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3324 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3325 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3326 | |
mkarlsso | 2:298679fff37c | 3327 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3328 | } |
mkarlsso | 2:298679fff37c | 3329 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3330 | //pcPtr->printf("Action: set variable to operation involving two variables\r\n"); |
mkarlsso | 2:298679fff37c | 3331 | //tmpVar->value = tmpVar2->value + (multiplier * tmpVar3->value); |
mkarlsso | 2:298679fff37c | 3332 | } else if ( isNumber(afterEqual.substr(stringInd2+1,std::string::npos)) && isNumber(afterEqual.substr(0,stringInd2)) ) { //number +/- number |
mkarlsso | 2:298679fff37c | 3333 | //tmpOp = new intOperation(tmpVar, "=", atoi(afterEqual.substr(0,stringInd2).data()) + (multiplierInt * atoi(afterEqual.substr(stringInd2+1,std::string::npos).data()))); |
mkarlsso | 2:298679fff37c | 3334 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3335 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3336 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3337 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3338 | return NULL; |
mkarlsso | 2:298679fff37c | 3339 | } else { |
mkarlsso | 2:298679fff37c | 3340 | tmpOp->set(tmpVar, "=", atoi(afterEqual.substr(0,stringInd2).data()) + (multiplierInt * atoi(afterEqual.substr(stringInd2+1,std::string::npos).data()))); |
mkarlsso | 2:298679fff37c | 3341 | } |
mkarlsso | 2:298679fff37c | 3342 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3343 | //pcPtr->printf("Action: set variable to constant numeric value\r\n"); |
mkarlsso | 2:298679fff37c | 3344 | |
mkarlsso | 2:298679fff37c | 3345 | } else if ((afterEqual.substr(0,stringInd2).find("random") != std::string::npos) && isNumber(afterEqual.substr(stringInd2+1,std::string::npos))) { //random +/- number |
mkarlsso | 2:298679fff37c | 3346 | int highVal = getRandomParam(afterEqual.substr(0,stringInd2)); |
mkarlsso | 2:298679fff37c | 3347 | |
mkarlsso | 2:298679fff37c | 3348 | if (highVal > 0) { |
mkarlsso | 3:ae33b7f5a7c1 | 3349 | |
mkarlsso | 3:ae33b7f5a7c1 | 3350 | |
mkarlsso | 2:298679fff37c | 3351 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3352 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3353 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3354 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3355 | return NULL; |
mkarlsso | 2:298679fff37c | 3356 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3357 | tmpOp2->set(highVal, multiplier.data(), atoi(afterEqual.substr(stringInd2+1,std::string::npos).data())); |
mkarlsso | 3:ae33b7f5a7c1 | 3358 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3359 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3360 | |
mkarlsso | 3:ae33b7f5a7c1 | 3361 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3362 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3363 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3364 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3365 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3366 | } else { |
mkarlsso | 2:298679fff37c | 3367 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3368 | } |
mkarlsso | 2:298679fff37c | 3369 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3370 | //pcPtr->printf("Action: set variable to random value plus number %d\r\n", highVal); |
mkarlsso | 2:298679fff37c | 3371 | } else { |
mkarlsso | 2:298679fff37c | 3372 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3373 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3374 | return NULL; |
mkarlsso | 2:298679fff37c | 3375 | } |
mkarlsso | 2:298679fff37c | 3376 | } else if ((afterEqual.substr(0,stringInd2).find("random") != std::string::npos) && (tmpVar3 != NULL)) { //random +/- variable |
mkarlsso | 2:298679fff37c | 3377 | int highVal = getRandomParam(afterEqual.substr(0,stringInd2)); |
mkarlsso | 2:298679fff37c | 3378 | |
mkarlsso | 2:298679fff37c | 3379 | if (highVal > 0) { |
mkarlsso | 3:ae33b7f5a7c1 | 3380 | |
mkarlsso | 3:ae33b7f5a7c1 | 3381 | |
mkarlsso | 2:298679fff37c | 3382 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3383 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3384 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3385 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3386 | return NULL; |
mkarlsso | 2:298679fff37c | 3387 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3388 | tmpOp2->set(highVal, multiplier.data(), tmpVar3); |
mkarlsso | 3:ae33b7f5a7c1 | 3389 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3390 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3391 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3392 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3393 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3394 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3395 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3396 | } else { |
mkarlsso | 2:298679fff37c | 3397 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3398 | } |
mkarlsso | 2:298679fff37c | 3399 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3400 | //pcPtr->printf("Action: set variable to random value plus number %d\r\n", highVal); |
mkarlsso | 2:298679fff37c | 3401 | } else { |
mkarlsso | 2:298679fff37c | 3402 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3403 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3404 | return NULL; |
mkarlsso | 2:298679fff37c | 3405 | } |
mkarlsso | 2:298679fff37c | 3406 | |
mkarlsso | 2:298679fff37c | 3407 | |
mkarlsso | 2:298679fff37c | 3408 | } else if ((afterEqual.substr(stringInd2+1,std::string::npos).find("random") != std::string::npos) && isNumber(afterEqual.substr(0,stringInd2))) { //random +/- number |
mkarlsso | 2:298679fff37c | 3409 | int highVal = getRandomParam(afterEqual.substr(stringInd2+1,std::string::npos)); |
mkarlsso | 2:298679fff37c | 3410 | |
mkarlsso | 2:298679fff37c | 3411 | if (highVal > 0) { |
mkarlsso | 3:ae33b7f5a7c1 | 3412 | |
mkarlsso | 3:ae33b7f5a7c1 | 3413 | |
mkarlsso | 2:298679fff37c | 3414 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3415 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3416 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3417 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3418 | return NULL; |
mkarlsso | 2:298679fff37c | 3419 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3420 | tmpOp2->set(highVal, multiplier.data(), atoi(afterEqual.substr(0, stringInd2).data())); |
mkarlsso | 3:ae33b7f5a7c1 | 3421 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3422 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3423 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3424 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3425 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3426 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3427 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3428 | } else { |
mkarlsso | 2:298679fff37c | 3429 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3430 | } |
mkarlsso | 2:298679fff37c | 3431 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3432 | //pcPtr->printf("Action: set variable to random value plus number %d\r\n", highVal); |
mkarlsso | 2:298679fff37c | 3433 | } else { |
mkarlsso | 2:298679fff37c | 3434 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3435 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3436 | return NULL; |
mkarlsso | 2:298679fff37c | 3437 | } |
mkarlsso | 2:298679fff37c | 3438 | } else if ((afterEqual.substr(stringInd2+1,std::string::npos).find("random") != std::string::npos) && (tmpVar2 != NULL)) { //random +/- number |
mkarlsso | 2:298679fff37c | 3439 | int highVal = getRandomParam(afterEqual.substr(stringInd2+1,std::string::npos)); |
mkarlsso | 2:298679fff37c | 3440 | |
mkarlsso | 2:298679fff37c | 3441 | if (highVal > 0) { |
mkarlsso | 3:ae33b7f5a7c1 | 3442 | |
mkarlsso | 3:ae33b7f5a7c1 | 3443 | |
mkarlsso | 2:298679fff37c | 3444 | tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3445 | if (tmpOp2 == NULL) { |
mkarlsso | 2:298679fff37c | 3446 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3447 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3448 | return NULL; |
mkarlsso | 2:298679fff37c | 3449 | } else { |
mkarlsso | 3:ae33b7f5a7c1 | 3450 | tmpOp2->set(highVal, multiplier.data(), tmpVar2); |
mkarlsso | 3:ae33b7f5a7c1 | 3451 | } |
mkarlsso | 3:ae33b7f5a7c1 | 3452 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 3:ae33b7f5a7c1 | 3453 | if (tmpOp == NULL) { |
mkarlsso | 3:ae33b7f5a7c1 | 3454 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 3:ae33b7f5a7c1 | 3455 | tmpOp2->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3456 | tmpAction->release(); |
mkarlsso | 3:ae33b7f5a7c1 | 3457 | return NULL; |
mkarlsso | 3:ae33b7f5a7c1 | 3458 | } else { |
mkarlsso | 2:298679fff37c | 3459 | tmpOp->set(tmpVar, tmpOp2); |
mkarlsso | 2:298679fff37c | 3460 | } |
mkarlsso | 2:298679fff37c | 3461 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3462 | //pcPtr->printf("Action: set variable to random value plus number %d\r\n", highVal); |
mkarlsso | 2:298679fff37c | 3463 | } else { |
mkarlsso | 2:298679fff37c | 3464 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3465 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3466 | return NULL; |
mkarlsso | 2:298679fff37c | 3467 | } |
mkarlsso | 2:298679fff37c | 3468 | } else { |
mkarlsso | 2:298679fff37c | 3469 | pcPtr->printf("Expression not understood: %s\r\n",afterEqual.data()); |
mkarlsso | 2:298679fff37c | 3470 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3471 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3472 | return NULL; |
mkarlsso | 2:298679fff37c | 3473 | } |
mkarlsso | 2:298679fff37c | 3474 | |
mkarlsso | 2:298679fff37c | 3475 | } else if (findIntVariable(afterEqual) != NULL) { //assign value of another variable |
mkarlsso | 2:298679fff37c | 3476 | //tmpOp = new intOperation(tmpVar, "=", findIntVariable(afterEqual)); |
mkarlsso | 2:298679fff37c | 3477 | tmpOp = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS); |
mkarlsso | 2:298679fff37c | 3478 | if (tmpOp == NULL) { |
mkarlsso | 2:298679fff37c | 3479 | pcPtr->printf("Error: no memory slots available.\r\n"); |
mkarlsso | 2:298679fff37c | 3480 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3481 | return NULL; |
mkarlsso | 2:298679fff37c | 3482 | } else { |
mkarlsso | 2:298679fff37c | 3483 | tmpOp->set(tmpVar, "=", findIntVariable(afterEqual)); |
mkarlsso | 2:298679fff37c | 3484 | } |
mkarlsso | 2:298679fff37c | 3485 | tmpAction->set(tmpOp); |
mkarlsso | 2:298679fff37c | 3486 | //pcPtr->printf("Action: set variable to value of another\r\n"); |
mkarlsso | 2:298679fff37c | 3487 | } else { |
mkarlsso | 2:298679fff37c | 3488 | pcPtr->printf("Variable not found: %s\r\n",afterEqual.data()); |
mkarlsso | 2:298679fff37c | 3489 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3490 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3491 | return NULL; |
mkarlsso | 2:298679fff37c | 3492 | } |
mkarlsso | 2:298679fff37c | 3493 | |
mkarlsso | 2:298679fff37c | 3494 | } else { |
mkarlsso | 2:298679fff37c | 3495 | pcPtr->printf("Variable not found\r\n"); |
mkarlsso | 2:298679fff37c | 3496 | //delete tmpAction; |
mkarlsso | 2:298679fff37c | 3497 | tmpAction->release(); |
mkarlsso | 2:298679fff37c | 3498 | return NULL; |
mkarlsso | 2:298679fff37c | 3499 | } |
mkarlsso | 2:298679fff37c | 3500 | return tmpAction; |
mkarlsso | 2:298679fff37c | 3501 | } |
mkarlsso | 2:298679fff37c | 3502 | |
mkarlsso | 2:298679fff37c | 3503 | bool scriptStream::isOutsideParenth(string expression,std::size_t foundItem) { |
mkarlsso | 2:298679fff37c | 3504 | |
mkarlsso | 2:298679fff37c | 3505 | int pDepth = 0; // How many nested parentheses |
mkarlsso | 2:298679fff37c | 3506 | //pcPtr->printf("Check outside parentheses..."); |
mkarlsso | 2:298679fff37c | 3507 | if (foundItem < expression.length()) { |
mkarlsso | 2:298679fff37c | 3508 | for (int i = 0; i <= foundItem; i++) { |
mkarlsso | 2:298679fff37c | 3509 | if (expression[i] == '(') { |
mkarlsso | 2:298679fff37c | 3510 | pDepth++; |
mkarlsso | 2:298679fff37c | 3511 | } else if (expression[i] == ')') { |
mkarlsso | 2:298679fff37c | 3512 | pDepth--; |
mkarlsso | 2:298679fff37c | 3513 | } |
mkarlsso | 2:298679fff37c | 3514 | } |
mkarlsso | 2:298679fff37c | 3515 | if (pDepth<=0) { |
mkarlsso | 2:298679fff37c | 3516 | //pcPtr->printf("yes."); |
mkarlsso | 2:298679fff37c | 3517 | return true; |
mkarlsso | 2:298679fff37c | 3518 | } else { |
mkarlsso | 2:298679fff37c | 3519 | //pcPtr->printf("no."); |
mkarlsso | 2:298679fff37c | 3520 | return false; |
mkarlsso | 2:298679fff37c | 3521 | } |
mkarlsso | 2:298679fff37c | 3522 | } else { |
mkarlsso | 2:298679fff37c | 3523 | return true; |
mkarlsso | 2:298679fff37c | 3524 | } |
mkarlsso | 2:298679fff37c | 3525 | |
mkarlsso | 2:298679fff37c | 3526 | } |
mkarlsso | 2:298679fff37c | 3527 | |
mkarlsso | 2:298679fff37c | 3528 | std::size_t scriptStream::findFirstOrOutsideParenth(string expression) { |
mkarlsso | 2:298679fff37c | 3529 | |
mkarlsso | 2:298679fff37c | 3530 | std::size_t foundItem = expression.find("||"); |
mkarlsso | 2:298679fff37c | 3531 | while (foundItem != std::string::npos) { |
mkarlsso | 2:298679fff37c | 3532 | if (isOutsideParenth(expression,foundItem)) { |
mkarlsso | 2:298679fff37c | 3533 | break; |
mkarlsso | 2:298679fff37c | 3534 | } |
mkarlsso | 2:298679fff37c | 3535 | foundItem = expression.find("||",foundItem+1); |
mkarlsso | 2:298679fff37c | 3536 | } |
mkarlsso | 2:298679fff37c | 3537 | return foundItem; |
mkarlsso | 2:298679fff37c | 3538 | } |
mkarlsso | 2:298679fff37c | 3539 | |
mkarlsso | 2:298679fff37c | 3540 | std::size_t scriptStream::findFirstAndOutsideParenth(string expression) { |
mkarlsso | 2:298679fff37c | 3541 | |
mkarlsso | 2:298679fff37c | 3542 | std::size_t foundItem = expression.find("&&"); |
mkarlsso | 2:298679fff37c | 3543 | while (foundItem != std::string::npos) { |
mkarlsso | 2:298679fff37c | 3544 | if (isOutsideParenth(expression,foundItem)){ |
mkarlsso | 2:298679fff37c | 3545 | break; |
mkarlsso | 2:298679fff37c | 3546 | } |
mkarlsso | 2:298679fff37c | 3547 | foundItem = expression.find("&&",foundItem+1); |
mkarlsso | 2:298679fff37c | 3548 | } |
mkarlsso | 2:298679fff37c | 3549 | return foundItem; |
mkarlsso | 2:298679fff37c | 3550 | } |
mkarlsso | 2:298679fff37c | 3551 | |
mkarlsso | 2:298679fff37c | 3552 | condition* scriptStream::parseConditions(string expression) { |
mkarlsso | 2:298679fff37c | 3553 | //This function is used to parse a condition string |
mkarlsso | 2:298679fff37c | 3554 | //such as (x < y && x != z) || (y == 2) |
mkarlsso | 2:298679fff37c | 3555 | //This function first identifies the root node of the logic tree |
mkarlsso | 2:298679fff37c | 3556 | //based on operator precedence ( () > && > || ), and then recursively calls itself |
mkarlsso | 2:298679fff37c | 3557 | //to find the nodes of the branches. The basic building blocks of |
mkarlsso | 2:298679fff37c | 3558 | //the final condition object are arithmatic comparitors (a > b) and |
mkarlsso | 2:298679fff37c | 3559 | //other condition objects. |
mkarlsso | 2:298679fff37c | 3560 | |
mkarlsso | 2:298679fff37c | 3561 | |
mkarlsso | 2:298679fff37c | 3562 | //pcPtr->printf("Parsing condition: %s\r\n", expression.data()); |
mkarlsso | 2:298679fff37c | 3563 | condition* newCondition = NULL; |
mkarlsso | 2:298679fff37c | 3564 | bool singleCondition = false; //no compound conditions |
mkarlsso | 2:298679fff37c | 3565 | string afterComparator; |
mkarlsso | 2:298679fff37c | 3566 | string beforeComparator; |
mkarlsso | 2:298679fff37c | 3567 | |
mkarlsso | 2:298679fff37c | 3568 | std::size_t found; |
mkarlsso | 2:298679fff37c | 3569 | |
mkarlsso | 2:298679fff37c | 3570 | //To make a parse tree, we start by looking for operators with the lowest precendence |
mkarlsso | 2:298679fff37c | 3571 | //so we look for OR conditions first |
mkarlsso | 2:298679fff37c | 3572 | char currentOperator = OR_CONDITION; |
mkarlsso | 2:298679fff37c | 3573 | //pcPtr->printf("Looking for OR condition..."); |
mkarlsso | 2:298679fff37c | 3574 | found = findFirstOrOutsideParenth(expression); |
mkarlsso | 2:298679fff37c | 3575 | if (found==std::string::npos) { //no or conditions outside parentheses found, so we look for AND conditions |
mkarlsso | 2:298679fff37c | 3576 | currentOperator = AND_CONDITION; |
mkarlsso | 2:298679fff37c | 3577 | //pcPtr->printf("Looking for AND condition..."); |
mkarlsso | 2:298679fff37c | 3578 | found = findFirstAndOutsideParenth(expression); |
mkarlsso | 2:298679fff37c | 3579 | } |
mkarlsso | 2:298679fff37c | 3580 | if (found==std::string::npos) { //no or/and conditions outside parentheses found |
mkarlsso | 2:298679fff37c | 3581 | //if the expression is encapsulated in parentheses, remove the parentheses |
mkarlsso | 2:298679fff37c | 3582 | bool removedParenth = false; |
mkarlsso | 2:298679fff37c | 3583 | if ((expression[0] == '(') && (expression[expression.length()-1] == ')')) { |
mkarlsso | 2:298679fff37c | 3584 | //pcPtr->printf("Remove parentheses"); |
mkarlsso | 2:298679fff37c | 3585 | expression = expression.substr(1,expression.length()-2); |
mkarlsso | 2:298679fff37c | 3586 | removedParenth = true; |
mkarlsso | 2:298679fff37c | 3587 | } |
mkarlsso | 2:298679fff37c | 3588 | if (removedParenth) { //we removed parentheses, so try again |
mkarlsso | 2:298679fff37c | 3589 | return parseConditions(expression); |
mkarlsso | 2:298679fff37c | 3590 | } else { |
mkarlsso | 2:298679fff37c | 3591 | singleCondition = true; //we assume that the condition is non-compound, i.e., a>b |
mkarlsso | 2:298679fff37c | 3592 | } |
mkarlsso | 2:298679fff37c | 3593 | } |
mkarlsso | 2:298679fff37c | 3594 | |
mkarlsso | 2:298679fff37c | 3595 | if (singleCondition) { //no compound conditions found |
mkarlsso | 2:298679fff37c | 3596 | //pcPtr->printf("Single condition"); |
mkarlsso | 2:298679fff37c | 3597 | std::size_t equalStringInd; |
mkarlsso | 2:298679fff37c | 3598 | std::size_t greaterOrEqualStringInd; |
mkarlsso | 2:298679fff37c | 3599 | std::size_t lessThanOrEqualStringInd; |
mkarlsso | 2:298679fff37c | 3600 | std::size_t notEqualStringInd; |
mkarlsso | 2:298679fff37c | 3601 | std::size_t greaterThanStringInd; |
mkarlsso | 2:298679fff37c | 3602 | std::size_t lessThanStringInd; |
mkarlsso | 2:298679fff37c | 3603 | std::size_t generalCompareStringInd; |
mkarlsso | 2:298679fff37c | 3604 | |
mkarlsso | 2:298679fff37c | 3605 | |
mkarlsso | 2:298679fff37c | 3606 | string tmpCondition = expression; |
mkarlsso | 2:298679fff37c | 3607 | string compareString; |
mkarlsso | 2:298679fff37c | 3608 | //The expression might have up to three variables |
mkarlsso | 2:298679fff37c | 3609 | int* tmpVar; |
mkarlsso | 2:298679fff37c | 3610 | int* tmpVar2; |
mkarlsso | 2:298679fff37c | 3611 | int* tmpVar3; |
mkarlsso | 2:298679fff37c | 3612 | |
mkarlsso | 2:298679fff37c | 3613 | int offset = 0; |
mkarlsso | 2:298679fff37c | 3614 | equalStringInd = tmpCondition.find("=="); //location of comparator |
mkarlsso | 2:298679fff37c | 3615 | greaterOrEqualStringInd = tmpCondition.find(">="); //location of comparator |
mkarlsso | 2:298679fff37c | 3616 | lessThanOrEqualStringInd = tmpCondition.find("<="); //location of comparator |
mkarlsso | 2:298679fff37c | 3617 | notEqualStringInd = tmpCondition.find("!="); //location of comparator |
mkarlsso | 2:298679fff37c | 3618 | greaterThanStringInd = tmpCondition.find_first_of(">"); //location of comparator |
mkarlsso | 2:298679fff37c | 3619 | lessThanStringInd = tmpCondition.find_first_of("<"); //location of comparator |
mkarlsso | 2:298679fff37c | 3620 | |
mkarlsso | 2:298679fff37c | 3621 | if ((equalStringInd != std::string::npos) && (greaterOrEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3622 | (lessThanOrEqualStringInd == std::string::npos) && (notEqualStringInd == std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3623 | |
mkarlsso | 2:298679fff37c | 3624 | generalCompareStringInd = equalStringInd; |
mkarlsso | 2:298679fff37c | 3625 | compareString = "=="; |
mkarlsso | 2:298679fff37c | 3626 | } else if ((equalStringInd == std::string::npos) && (greaterOrEqualStringInd != std::string::npos) && |
mkarlsso | 2:298679fff37c | 3627 | (lessThanOrEqualStringInd == std::string::npos) && (notEqualStringInd == std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3628 | |
mkarlsso | 2:298679fff37c | 3629 | generalCompareStringInd = greaterOrEqualStringInd; |
mkarlsso | 2:298679fff37c | 3630 | compareString = ">="; |
mkarlsso | 2:298679fff37c | 3631 | } else if ((equalStringInd == std::string::npos) && (greaterOrEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3632 | (lessThanOrEqualStringInd != std::string::npos) && (notEqualStringInd == std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3633 | |
mkarlsso | 2:298679fff37c | 3634 | generalCompareStringInd = lessThanOrEqualStringInd; |
mkarlsso | 2:298679fff37c | 3635 | compareString = "<="; |
mkarlsso | 2:298679fff37c | 3636 | } else if ((equalStringInd == std::string::npos) && (greaterOrEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3637 | (lessThanOrEqualStringInd == std::string::npos) && (notEqualStringInd != std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3638 | |
mkarlsso | 2:298679fff37c | 3639 | generalCompareStringInd = notEqualStringInd; |
mkarlsso | 2:298679fff37c | 3640 | compareString = "!="; |
mkarlsso | 2:298679fff37c | 3641 | } else if ((equalStringInd == std::string::npos) && (greaterOrEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3642 | (lessThanOrEqualStringInd == std::string::npos) && (notEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3643 | (greaterThanStringInd != std::string::npos) && (lessThanStringInd == std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3644 | |
mkarlsso | 2:298679fff37c | 3645 | generalCompareStringInd = greaterThanStringInd; |
mkarlsso | 2:298679fff37c | 3646 | compareString = ">"; |
mkarlsso | 2:298679fff37c | 3647 | offset = 1; |
mkarlsso | 2:298679fff37c | 3648 | } else if ((equalStringInd == std::string::npos) && (greaterOrEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3649 | (lessThanOrEqualStringInd == std::string::npos) && (notEqualStringInd == std::string::npos) && |
mkarlsso | 2:298679fff37c | 3650 | (greaterThanStringInd == std::string::npos) && (lessThanStringInd != std::string::npos)){ |
mkarlsso | 2:298679fff37c | 3651 | |
mkarlsso | 2:298679fff37c | 3652 | generalCompareStringInd = lessThanStringInd; |
mkarlsso | 2:298679fff37c | 3653 | compareString = "<"; |
mkarlsso | 2:298679fff37c | 3654 | offset = 1; |
mkarlsso | 2:298679fff37c | 3655 | |
mkarlsso | 2:298679fff37c | 3656 | }else { |
mkarlsso | 2:298679fff37c | 3657 | pcPtr->printf("Condition not understood: %s\r\n", expression.data()); |
mkarlsso | 2:298679fff37c | 3658 | return 0; |
mkarlsso | 2:298679fff37c | 3659 | } |
mkarlsso | 2:298679fff37c | 3660 | |
mkarlsso | 2:298679fff37c | 3661 | intCompare* newCompare = findFirstUnUsed(intCompareBlock, NUMINTCOMPARE); |
mkarlsso | 2:298679fff37c | 3662 | if (newCompare == NULL) { |
mkarlsso | 2:298679fff37c | 3663 | pcPtr->printf("Error: No memory slots available."); |
mkarlsso | 2:298679fff37c | 3664 | return NULL; |
mkarlsso | 2:298679fff37c | 3665 | } |
mkarlsso | 2:298679fff37c | 3666 | newCondition = findFirstUnUsed(conditionBlock, NUMCONDITIONS); |
mkarlsso | 2:298679fff37c | 3667 | if (newCondition == NULL) { |
mkarlsso | 2:298679fff37c | 3668 | pcPtr->printf("Error: No memory slots available."); |
mkarlsso | 2:298679fff37c | 3669 | return NULL; |
mkarlsso | 2:298679fff37c | 3670 | } |
mkarlsso | 2:298679fff37c | 3671 | afterComparator = tmpCondition.substr(generalCompareStringInd+2-offset,std::string::npos); |
mkarlsso | 2:298679fff37c | 3672 | |
mkarlsso | 2:298679fff37c | 3673 | beforeComparator = tmpCondition.substr(0,generalCompareStringInd); |
mkarlsso | 2:298679fff37c | 3674 | tmpVar = findIntVariable(beforeComparator); //returns pointer to the variable |
mkarlsso | 2:298679fff37c | 3675 | if (tmpVar != NULL) { //before the comparator is a single variable |
mkarlsso | 2:298679fff37c | 3676 | tmpVar2 = findIntVariable(afterComparator); //returns pointer to the variable |
mkarlsso | 2:298679fff37c | 3677 | if (tmpVar2 != NULL) { //we are comapring a single variable to another |
mkarlsso | 2:298679fff37c | 3678 | //intCompare* newCompare = new intCompare(tmpVar,compareString.data(),tmpVar2); |
mkarlsso | 2:298679fff37c | 3679 | //currentEvent->addCondition(new condition(newCompare)); |
mkarlsso | 2:298679fff37c | 3680 | newCompare->set(tmpVar,compareString.data(),tmpVar2); |
mkarlsso | 2:298679fff37c | 3681 | newCondition->set(newCompare); |
mkarlsso | 2:298679fff37c | 3682 | |
mkarlsso | 2:298679fff37c | 3683 | //pcPtr->printf("Var vs. Var condition added: %s\r\n", tmpCondition.data()); |
mkarlsso | 2:298679fff37c | 3684 | } else if (isNumber(afterComparator)) { |
mkarlsso | 2:298679fff37c | 3685 | //intCompare* newCompare = new intCompare(tmpVar,compareString.data(),atoi(afterComparator.data())); |
mkarlsso | 2:298679fff37c | 3686 | //currentEvent->addCondition(new condition(newCompare)); |
mkarlsso | 2:298679fff37c | 3687 | newCompare->set(tmpVar,compareString.data(),atoi(afterComparator.data())); |
mkarlsso | 2:298679fff37c | 3688 | newCondition->set(newCompare); |
mkarlsso | 2:298679fff37c | 3689 | |
mkarlsso | 2:298679fff37c | 3690 | //pcPtr->printf("Var vs. Int condition added: %s\r\n", tmpCondition.data()); |
mkarlsso | 2:298679fff37c | 3691 | } //more here |
mkarlsso | 2:298679fff37c | 3692 | |
mkarlsso | 2:298679fff37c | 3693 | } else { |
mkarlsso | 2:298679fff37c | 3694 | pcPtr->printf("Condition not understood: %s\r\n", expression.data()); |
mkarlsso | 2:298679fff37c | 3695 | |
mkarlsso | 2:298679fff37c | 3696 | return NULL; |
mkarlsso | 2:298679fff37c | 3697 | } |
mkarlsso | 2:298679fff37c | 3698 | |
mkarlsso | 2:298679fff37c | 3699 | } else { //this is a compound condition (with either && or ||) |
mkarlsso | 2:298679fff37c | 3700 | //pcPtr->printf("Compound condition"); |
mkarlsso | 2:298679fff37c | 3701 | afterComparator = expression.substr(found+2,std::string::npos); |
mkarlsso | 2:298679fff37c | 3702 | beforeComparator = expression.substr(0,found); |
mkarlsso | 2:298679fff37c | 3703 | newCondition = findFirstUnUsed(conditionBlock, NUMCONDITIONS); |
mkarlsso | 2:298679fff37c | 3704 | if (newCondition == NULL) { |
mkarlsso | 2:298679fff37c | 3705 | pcPtr->printf("Error: No memory slots available."); |
mkarlsso | 2:298679fff37c | 3706 | return NULL; |
mkarlsso | 2:298679fff37c | 3707 | } else { |
mkarlsso | 2:298679fff37c | 3708 | newCondition->isUsed = true; //reserve the condition slot; |
mkarlsso | 2:298679fff37c | 3709 | } |
mkarlsso | 2:298679fff37c | 3710 | //recursively call this function to parse the sub conditions |
mkarlsso | 2:298679fff37c | 3711 | condition* cond1 = parseConditions(beforeComparator); |
mkarlsso | 2:298679fff37c | 3712 | if (cond1 == NULL) { |
mkarlsso | 2:298679fff37c | 3713 | newCondition->release(); |
mkarlsso | 2:298679fff37c | 3714 | return NULL; |
mkarlsso | 2:298679fff37c | 3715 | } |
mkarlsso | 2:298679fff37c | 3716 | condition* cond2 = parseConditions(afterComparator); |
mkarlsso | 2:298679fff37c | 3717 | if (cond2 == NULL) { |
mkarlsso | 2:298679fff37c | 3718 | newCondition->release(); |
mkarlsso | 2:298679fff37c | 3719 | cond1->release(); |
mkarlsso | 2:298679fff37c | 3720 | return NULL; |
mkarlsso | 2:298679fff37c | 3721 | } |
mkarlsso | 2:298679fff37c | 3722 | newCondition->set(cond1,currentOperator, cond2); |
mkarlsso | 2:298679fff37c | 3723 | |
mkarlsso | 2:298679fff37c | 3724 | } |
mkarlsso | 2:298679fff37c | 3725 | |
mkarlsso | 2:298679fff37c | 3726 | return newCondition; //all went well, so return the newly made condition |
mkarlsso | 2:298679fff37c | 3727 | |
mkarlsso | 2:298679fff37c | 3728 | } |
mkarlsso | 2:298679fff37c | 3729 | |
mkarlsso | 2:298679fff37c | 3730 | bool scriptStream::evaluateConditions(string expression, event* currentEvent) { |
mkarlsso | 2:298679fff37c | 3731 | //calls the function to parse the condition string. The condition pointer is then |
mkarlsso | 2:298679fff37c | 3732 | //attached to the event |
mkarlsso | 2:298679fff37c | 3733 | |
mkarlsso | 2:298679fff37c | 3734 | condition* newCondition = NULL; |
mkarlsso | 2:298679fff37c | 3735 | newCondition = parseConditions(expression); |
mkarlsso | 2:298679fff37c | 3736 | if (newCondition == NULL) { |
mkarlsso | 2:298679fff37c | 3737 | return false; |
mkarlsso | 2:298679fff37c | 3738 | } else { |
mkarlsso | 2:298679fff37c | 3739 | currentEvent->addCondition(newCondition); |
mkarlsso | 2:298679fff37c | 3740 | return true; |
mkarlsso | 2:298679fff37c | 3741 | } |
mkarlsso | 2:298679fff37c | 3742 | } |
mkarlsso | 2:298679fff37c | 3743 | |
mkarlsso | 2:298679fff37c | 3744 | int scriptStream::getRandomParam(string expression) { |
mkarlsso | 2:298679fff37c | 3745 | |
mkarlsso | 2:298679fff37c | 3746 | int pos1 = expression.find("random(")+7; |
mkarlsso | 2:298679fff37c | 3747 | int pos2 = expression.find_first_of(")",pos1); |
mkarlsso | 2:298679fff37c | 3748 | int highVal = atoi(expression.substr(pos1,pos2-pos1).data()); |
mkarlsso | 2:298679fff37c | 3749 | |
mkarlsso | 2:298679fff37c | 3750 | if ((highVal > 0)) { |
mkarlsso | 2:298679fff37c | 3751 | return highVal; |
mkarlsso | 2:298679fff37c | 3752 | } else { |
mkarlsso | 2:298679fff37c | 3753 | pcPtr->printf("Error: random parameter must be 1 or more\r\n"); |
mkarlsso | 2:298679fff37c | 3754 | return 0; |
mkarlsso | 2:298679fff37c | 3755 | } |
mkarlsso | 2:298679fff37c | 3756 | } |
mkarlsso | 2:298679fff37c | 3757 | |
mkarlsso | 2:298679fff37c | 3758 | |
mkarlsso | 2:298679fff37c | 3759 | outputStream::outputStream(int bufferSizeIn): |
mkarlsso | 2:298679fff37c | 3760 | readHead(0), |
mkarlsso | 2:298679fff37c | 3761 | writeHead(0), |
mkarlsso | 2:298679fff37c | 3762 | totalWriteHead(0), |
mkarlsso | 2:298679fff37c | 3763 | totalReadHead(0), |
mkarlsso | 2:298679fff37c | 3764 | bufferSize(bufferSizeIn), |
mkarlsso | 2:298679fff37c | 3765 | unsentData(false) { |
mkarlsso | 2:298679fff37c | 3766 | |
mkarlsso | 2:298679fff37c | 3767 | outputBuffer = new char[bufferSize]; |
mkarlsso | 2:298679fff37c | 3768 | |
mkarlsso | 2:298679fff37c | 3769 | } |
mkarlsso | 2:298679fff37c | 3770 | |
mkarlsso | 2:298679fff37c | 3771 | outputStream::~outputStream() { |
mkarlsso | 2:298679fff37c | 3772 | delete[] outputBuffer; |
mkarlsso | 2:298679fff37c | 3773 | } |
mkarlsso | 2:298679fff37c | 3774 | |
mkarlsso | 2:298679fff37c | 3775 | //adds text to the buffer |
mkarlsso | 2:298679fff37c | 3776 | void outputStream::send(string outputString) { |
mkarlsso | 2:298679fff37c | 3777 | int strLen = outputString.size(); |
mkarlsso | 2:298679fff37c | 3778 | |
mkarlsso | 2:298679fff37c | 3779 | int total = 0; |
mkarlsso | 2:298679fff37c | 3780 | int chunk = 0; |
mkarlsso | 2:298679fff37c | 3781 | if (!(totalWriteHead+strLen > (totalReadHead + bufferSize))) { |
mkarlsso | 2:298679fff37c | 3782 | while (strLen - total > 0) { |
mkarlsso | 2:298679fff37c | 3783 | chunk = min((bufferSize - writeHead), strLen - total); |
mkarlsso | 2:298679fff37c | 3784 | outputString.copy(outputBuffer + writeHead, chunk, total); |
mkarlsso | 2:298679fff37c | 3785 | writeHead = (writeHead + chunk) % bufferSize; |
mkarlsso | 2:298679fff37c | 3786 | totalWriteHead += chunk; |
mkarlsso | 2:298679fff37c | 3787 | total += chunk; |
mkarlsso | 2:298679fff37c | 3788 | } |
mkarlsso | 2:298679fff37c | 3789 | if (total > 0) { |
mkarlsso | 2:298679fff37c | 3790 | unsentData = true; |
mkarlsso | 2:298679fff37c | 3791 | } |
mkarlsso | 2:298679fff37c | 3792 | } |
mkarlsso | 2:298679fff37c | 3793 | } |
mkarlsso | 2:298679fff37c | 3794 | |
mkarlsso | 2:298679fff37c | 3795 | //the main loop gets one character per loop and write it to the serial port |
mkarlsso | 2:298679fff37c | 3796 | char outputStream::getNextChar() { |
mkarlsso | 2:298679fff37c | 3797 | |
mkarlsso | 2:298679fff37c | 3798 | |
mkarlsso | 2:298679fff37c | 3799 | if (totalReadHead < totalWriteHead) { |
mkarlsso | 2:298679fff37c | 3800 | tmpOut = *(outputBuffer+readHead); |
mkarlsso | 2:298679fff37c | 3801 | readHead = (readHead+1) % bufferSize; |
mkarlsso | 2:298679fff37c | 3802 | totalReadHead++; |
mkarlsso | 2:298679fff37c | 3803 | if (totalReadHead >= totalWriteHead) { |
mkarlsso | 2:298679fff37c | 3804 | unsentData = false; |
mkarlsso | 2:298679fff37c | 3805 | } |
mkarlsso | 2:298679fff37c | 3806 | } |
mkarlsso | 2:298679fff37c | 3807 | return tmpOut; |
mkarlsso | 2:298679fff37c | 3808 | |
mkarlsso | 2:298679fff37c | 3809 | } |
mkarlsso | 2:298679fff37c | 3810 |