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