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