![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
statescriptMP
Dependencies: SMARTWAV USBDevice mbed
behave.h@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 "mbed.h" |
alustig3 | 0:9d97f34c6f46 | 2 | #include <stdint.h> |
alustig3 | 0:9d97f34c6f46 | 3 | #include <string.h> |
alustig3 | 0:9d97f34c6f46 | 4 | #include <string> |
alustig3 | 0:9d97f34c6f46 | 5 | #include <vector> |
alustig3 | 0:9d97f34c6f46 | 6 | #include <list> |
alustig3 | 0:9d97f34c6f46 | 7 | #include <deque> |
alustig3 | 0:9d97f34c6f46 | 8 | #include <queue> |
alustig3 | 0:9d97f34c6f46 | 9 | #include "soundControl.h" |
alustig3 | 0:9d97f34c6f46 | 10 | |
alustig3 | 0:9d97f34c6f46 | 11 | |
alustig3 | 0:9d97f34c6f46 | 12 | #define NUMEVENTS 50 |
alustig3 | 0:9d97f34c6f46 | 13 | #define NUMCONDITIONS 150 |
alustig3 | 0:9d97f34c6f46 | 14 | #define NUMINTCOMPARE 150 |
alustig3 | 0:9d97f34c6f46 | 15 | #define NUMACTIONS 150 |
alustig3 | 0:9d97f34c6f46 | 16 | #define NUMPORTMESSAGES 150 |
alustig3 | 0:9d97f34c6f46 | 17 | #define NUMINTOPERATIONS 150 |
alustig3 | 0:9d97f34c6f46 | 18 | #define NUMDISPLAYACTIONS 30 |
alustig3 | 0:9d97f34c6f46 | 19 | |
alustig3 | 0:9d97f34c6f46 | 20 | #define ARITHMATIC_CONDITION 0 |
alustig3 | 0:9d97f34c6f46 | 21 | #define OR_CONDITION 1 |
alustig3 | 0:9d97f34c6f46 | 22 | #define AND_CONDITION 2 |
alustig3 | 0:9d97f34c6f46 | 23 | |
alustig3 | 0:9d97f34c6f46 | 24 | #define NUMPORTS 10 |
alustig3 | 0:9d97f34c6f46 | 25 | |
alustig3 | 0:9d97f34c6f46 | 26 | extern "C" void mbed_reset();//MAX and ANDY |
alustig3 | 0:9d97f34c6f46 | 27 | class event; //we foreward declare this because of class interdependencies |
alustig3 | 0:9d97f34c6f46 | 28 | |
alustig3 | 0:9d97f34c6f46 | 29 | //used in the digital port class to organize digital change events |
alustig3 | 0:9d97f34c6f46 | 30 | struct changeEvent { |
alustig3 | 0:9d97f34c6f46 | 31 | uint32_t timeStamp; |
alustig3 | 0:9d97f34c6f46 | 32 | bool triggered; |
alustig3 | 0:9d97f34c6f46 | 33 | }; |
alustig3 | 0:9d97f34c6f46 | 34 | |
alustig3 | 0:9d97f34c6f46 | 35 | //The digitalPort object directly controls and keeps data about the port. Each port has |
alustig3 | 0:9d97f34c6f46 | 36 | //one digital out and one digital in. |
alustig3 | 0:9d97f34c6f46 | 37 | class digitalPort { |
alustig3 | 0:9d97f34c6f46 | 38 | public: |
alustig3 | 0:9d97f34c6f46 | 39 | digitalPort(DigitalOut* DOP, DigitalIn* DIP); |
alustig3 | 0:9d97f34c6f46 | 40 | digitalPort(DigitalOut* DOP); |
alustig3 | 0:9d97f34c6f46 | 41 | void setDigitalOut(int outVal); |
alustig3 | 0:9d97f34c6f46 | 42 | //int getDigitalOut(); |
alustig3 | 0:9d97f34c6f46 | 43 | int getDigitalIn(); |
alustig3 | 0:9d97f34c6f46 | 44 | int getLastChangeState(); |
alustig3 | 0:9d97f34c6f46 | 45 | uint32_t getTimeSinceLastChange(); |
alustig3 | 0:9d97f34c6f46 | 46 | uint32_t lastChangeTime; |
alustig3 | 0:9d97f34c6f46 | 47 | uint32_t lastOutChangeTime; |
alustig3 | 0:9d97f34c6f46 | 48 | |
alustig3 | 0:9d97f34c6f46 | 49 | void setTriggerUpEvent(event* eventInput); //attahces a routine to an upward change |
alustig3 | 0:9d97f34c6f46 | 50 | void setTriggerDownEvent(event* eventInput); //attahces a routine to a downward change |
alustig3 | 0:9d97f34c6f46 | 51 | void addStateChange(int newState, uint32_t timeStamp); |
alustig3 | 0:9d97f34c6f46 | 52 | |
alustig3 | 0:9d97f34c6f46 | 53 | bool update(); //called from the main loop |
alustig3 | 0:9d97f34c6f46 | 54 | |
alustig3 | 0:9d97f34c6f46 | 55 | int inState; |
alustig3 | 0:9d97f34c6f46 | 56 | int outState; |
alustig3 | 0:9d97f34c6f46 | 57 | |
alustig3 | 0:9d97f34c6f46 | 58 | bool outStateChanged; |
alustig3 | 0:9d97f34c6f46 | 59 | |
alustig3 | 0:9d97f34c6f46 | 60 | event* triggerUpEventPtr; |
alustig3 | 0:9d97f34c6f46 | 61 | event* triggerDownEventPtr; |
alustig3 | 0:9d97f34c6f46 | 62 | |
alustig3 | 0:9d97f34c6f46 | 63 | private: |
alustig3 | 0:9d97f34c6f46 | 64 | |
alustig3 | 0:9d97f34c6f46 | 65 | DigitalOut* outPin; |
alustig3 | 0:9d97f34c6f46 | 66 | DigitalIn* inPin; |
alustig3 | 0:9d97f34c6f46 | 67 | int lastInState; |
alustig3 | 0:9d97f34c6f46 | 68 | uint32_t lastChangeInterval; |
alustig3 | 0:9d97f34c6f46 | 69 | |
alustig3 | 0:9d97f34c6f46 | 70 | changeEvent lastUpEvent; |
alustig3 | 0:9d97f34c6f46 | 71 | changeEvent lastDownEvent; |
alustig3 | 0:9d97f34c6f46 | 72 | }; |
alustig3 | 0:9d97f34c6f46 | 73 | |
alustig3 | 0:9d97f34c6f46 | 74 | |
alustig3 | 0:9d97f34c6f46 | 75 | //an intVariable contains an integer value and the name of that variable within the script |
alustig3 | 0:9d97f34c6f46 | 76 | class intVariable { |
alustig3 | 0:9d97f34c6f46 | 77 | |
alustig3 | 0:9d97f34c6f46 | 78 | public: |
alustig3 | 0:9d97f34c6f46 | 79 | intVariable(); |
alustig3 | 0:9d97f34c6f46 | 80 | intVariable(string tagInput, int initialValue); |
alustig3 | 0:9d97f34c6f46 | 81 | void set(string tagInput, int initialValue); |
alustig3 | 0:9d97f34c6f46 | 82 | int value; |
alustig3 | 0:9d97f34c6f46 | 83 | string tag; |
alustig3 | 0:9d97f34c6f46 | 84 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 85 | |
alustig3 | 0:9d97f34c6f46 | 86 | }; |
alustig3 | 0:9d97f34c6f46 | 87 | |
alustig3 | 0:9d97f34c6f46 | 88 | |
alustig3 | 0:9d97f34c6f46 | 89 | //ACTION SECTION-- an 'action' is a command in the script. It can be a single command, |
alustig3 | 0:9d97f34c6f46 | 90 | //or a block containing a set of actions |
alustig3 | 0:9d97f34c6f46 | 91 | //------------------------------------------------------------------------------------ |
alustig3 | 0:9d97f34c6f46 | 92 | |
alustig3 | 0:9d97f34c6f46 | 93 | //display actions are used to output text messages via the serial port. The user can display |
alustig3 | 0:9d97f34c6f46 | 94 | //either a static text string or the value of a single variable. |
alustig3 | 0:9d97f34c6f46 | 95 | class displayAction { |
alustig3 | 0:9d97f34c6f46 | 96 | |
alustig3 | 0:9d97f34c6f46 | 97 | public: |
alustig3 | 0:9d97f34c6f46 | 98 | displayAction(); |
alustig3 | 0:9d97f34c6f46 | 99 | displayAction(int* variable, string varNameInput, Serial* pcPtrInput); |
alustig3 | 0:9d97f34c6f46 | 100 | displayAction(string text, Serial* pcPtrInput); |
alustig3 | 0:9d97f34c6f46 | 101 | void set(int* variable, string varNameInput, Serial* pcPtrInput); |
alustig3 | 0:9d97f34c6f46 | 102 | void set(string text, Serial* pcPtrInput); |
alustig3 | 0:9d97f34c6f46 | 103 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 104 | void execute(); |
alustig3 | 0:9d97f34c6f46 | 105 | void release(); |
alustig3 | 0:9d97f34c6f46 | 106 | |
alustig3 | 0:9d97f34c6f46 | 107 | private: |
alustig3 | 0:9d97f34c6f46 | 108 | int* dVariable; |
alustig3 | 0:9d97f34c6f46 | 109 | string dText; |
alustig3 | 0:9d97f34c6f46 | 110 | Serial* pcPtr; |
alustig3 | 0:9d97f34c6f46 | 111 | }; |
alustig3 | 0:9d97f34c6f46 | 112 | |
alustig3 | 0:9d97f34c6f46 | 113 | //intOpertaion is an action that does addition or subtraction of integers and returns/stores the result |
alustig3 | 0:9d97f34c6f46 | 114 | //these operation are very limited so far (only + or - allowed, and only one operation per object, |
alustig3 | 0:9d97f34c6f46 | 115 | //for example a = b + b works but a = b + c + d does not. The output value can also be set to a random number. |
alustig3 | 0:9d97f34c6f46 | 116 | class intOperation { |
alustig3 | 0:9d97f34c6f46 | 117 | |
alustig3 | 0:9d97f34c6f46 | 118 | public: |
alustig3 | 0:9d97f34c6f46 | 119 | intOperation(); |
alustig3 | 0:9d97f34c6f46 | 120 | intOperation(int randParam, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 121 | intOperation(int randParam, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 122 | intOperation(int* intVarInput, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 123 | intOperation(int* intVarInput, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 124 | intOperation(int* intVarInput, intOperation* operationInput); |
alustig3 | 0:9d97f34c6f46 | 125 | |
alustig3 | 0:9d97f34c6f46 | 126 | ~intOperation(); |
alustig3 | 0:9d97f34c6f46 | 127 | |
alustig3 | 0:9d97f34c6f46 | 128 | void set(int randParam, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 129 | void set(int randParam, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 130 | void set(int* intVarInput, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 131 | void set(int* intVarInput, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 132 | void set(int* intVarInput, intOperation* operationInput); |
alustig3 | 0:9d97f34c6f46 | 133 | void release(); |
alustig3 | 0:9d97f34c6f46 | 134 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 135 | int execute(); |
alustig3 | 0:9d97f34c6f46 | 136 | |
alustig3 | 0:9d97f34c6f46 | 137 | private: |
alustig3 | 0:9d97f34c6f46 | 138 | int randHigh; |
alustig3 | 0:9d97f34c6f46 | 139 | int* cmpVal; |
alustig3 | 0:9d97f34c6f46 | 140 | int* intVal; |
alustig3 | 0:9d97f34c6f46 | 141 | intOperation* opPtr; |
alustig3 | 0:9d97f34c6f46 | 142 | bool cmpValGlobal; |
alustig3 | 0:9d97f34c6f46 | 143 | int (intOperation::*executePtr)(); |
alustig3 | 0:9d97f34c6f46 | 144 | int addAndStore(); |
alustig3 | 0:9d97f34c6f46 | 145 | int subtractAndStore(); |
alustig3 | 0:9d97f34c6f46 | 146 | int add(); |
alustig3 | 0:9d97f34c6f46 | 147 | int subtract(); |
alustig3 | 0:9d97f34c6f46 | 148 | int equals(); |
alustig3 | 0:9d97f34c6f46 | 149 | |
alustig3 | 0:9d97f34c6f46 | 150 | }; |
alustig3 | 0:9d97f34c6f46 | 151 | |
alustig3 | 0:9d97f34c6f46 | 152 | //portMessage is an action to change a digital port. So far, You can only change the digital out (0 or 1) |
alustig3 | 0:9d97f34c6f46 | 153 | class portMessage { |
alustig3 | 0:9d97f34c6f46 | 154 | public: |
alustig3 | 0:9d97f34c6f46 | 155 | |
alustig3 | 0:9d97f34c6f46 | 156 | portMessage(); |
alustig3 | 0:9d97f34c6f46 | 157 | //portMessage(digitalPort* portIn, int whichToSet, int value); //whichToSet: 1 DigitalOut; 2 State |
alustig3 | 0:9d97f34c6f46 | 158 | //void setMessage(digitalPort* portIn, int whichToSet, int value); //whichToSet: 1 DigitalOut; 2 State |
alustig3 | 0:9d97f34c6f46 | 159 | portMessage(int* portIn, int whichToSet, int value); //whichToSet: |
alustig3 | 0:9d97f34c6f46 | 160 | void setMessage(int* portIn, int whichToSet, int value); //whichToSet: |
alustig3 | 0:9d97f34c6f46 | 161 | |
alustig3 | 0:9d97f34c6f46 | 162 | void execute(); |
alustig3 | 0:9d97f34c6f46 | 163 | void release(); |
alustig3 | 0:9d97f34c6f46 | 164 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 165 | |
alustig3 | 0:9d97f34c6f46 | 166 | private: |
alustig3 | 0:9d97f34c6f46 | 167 | int whichToSet; //hard coded port number |
alustig3 | 0:9d97f34c6f46 | 168 | int* port; //alternative variable port number |
alustig3 | 0:9d97f34c6f46 | 169 | int value; |
alustig3 | 0:9d97f34c6f46 | 170 | //digitalPort* port; |
alustig3 | 0:9d97f34c6f46 | 171 | |
alustig3 | 0:9d97f34c6f46 | 172 | }; |
alustig3 | 0:9d97f34c6f46 | 173 | |
alustig3 | 0:9d97f34c6f46 | 174 | //holder class for all possible actions. This include general system commands. |
alustig3 | 0:9d97f34c6f46 | 175 | class action { |
alustig3 | 0:9d97f34c6f46 | 176 | public: |
alustig3 | 0:9d97f34c6f46 | 177 | |
alustig3 | 0:9d97f34c6f46 | 178 | action(); |
alustig3 | 0:9d97f34c6f46 | 179 | ~action(); |
alustig3 | 0:9d97f34c6f46 | 180 | action(intOperation* opInput); |
alustig3 | 0:9d97f34c6f46 | 181 | action(portMessage* messageInput); |
alustig3 | 0:9d97f34c6f46 | 182 | action(event* eventInput); |
alustig3 | 0:9d97f34c6f46 | 183 | //action(event* eventInput, uint32_t delay); |
alustig3 | 0:9d97f34c6f46 | 184 | action(displayAction* displayInput); |
alustig3 | 0:9d97f34c6f46 | 185 | action(soundControl* soundInput); |
alustig3 | 0:9d97f34c6f46 | 186 | action(int8_t sysCommandInput); //for general system commands |
alustig3 | 0:9d97f34c6f46 | 187 | |
alustig3 | 0:9d97f34c6f46 | 188 | void set(intOperation* opInput); |
alustig3 | 0:9d97f34c6f46 | 189 | void set(portMessage* messageInput); |
alustig3 | 0:9d97f34c6f46 | 190 | void set(event* eventInput); |
alustig3 | 0:9d97f34c6f46 | 191 | //void set(event* eventInput, uint32_t delay); |
alustig3 | 0:9d97f34c6f46 | 192 | |
alustig3 | 0:9d97f34c6f46 | 193 | void set(displayAction* displayInput); |
alustig3 | 0:9d97f34c6f46 | 194 | void set(soundControl* soundInput); |
alustig3 | 0:9d97f34c6f46 | 195 | void set(int8_t sysCommandInput); |
alustig3 | 0:9d97f34c6f46 | 196 | void execute(); |
alustig3 | 0:9d97f34c6f46 | 197 | void execute(uint32_t blockExecTime); |
alustig3 | 0:9d97f34c6f46 | 198 | void release(); |
alustig3 | 0:9d97f34c6f46 | 199 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 200 | |
alustig3 | 0:9d97f34c6f46 | 201 | private: |
alustig3 | 0:9d97f34c6f46 | 202 | intOperation* op; |
alustig3 | 0:9d97f34c6f46 | 203 | portMessage* message; |
alustig3 | 0:9d97f34c6f46 | 204 | event* eventToCreate; |
alustig3 | 0:9d97f34c6f46 | 205 | displayAction* displayActionPtr; |
alustig3 | 0:9d97f34c6f46 | 206 | soundControl* sound; |
alustig3 | 0:9d97f34c6f46 | 207 | //uint32_t eventDelay; |
alustig3 | 0:9d97f34c6f46 | 208 | int8_t sysCommand; |
alustig3 | 0:9d97f34c6f46 | 209 | char actionType; |
alustig3 | 0:9d97f34c6f46 | 210 | |
alustig3 | 0:9d97f34c6f46 | 211 | }; |
alustig3 | 0:9d97f34c6f46 | 212 | //----------------------------------------------------- |
alustig3 | 0:9d97f34c6f46 | 213 | |
alustig3 | 0:9d97f34c6f46 | 214 | //CONDITION SECTION-- a 'condition' is used in the beginning of a block (if-else blocks or while blocks) |
alustig3 | 0:9d97f34c6f46 | 215 | //If the condition is true, the block is exectuted during a callback |
alustig3 | 0:9d97f34c6f46 | 216 | //------------------------------------------------------------------------------------ |
alustig3 | 0:9d97f34c6f46 | 217 | |
alustig3 | 0:9d97f34c6f46 | 218 | |
alustig3 | 0:9d97f34c6f46 | 219 | //intCompare is a condition class that compares the state value of a port or |
alustig3 | 0:9d97f34c6f46 | 220 | //an integer variable to another integer variable or operation output |
alustig3 | 0:9d97f34c6f46 | 221 | class intCompare { |
alustig3 | 0:9d97f34c6f46 | 222 | |
alustig3 | 0:9d97f34c6f46 | 223 | public: |
alustig3 | 0:9d97f34c6f46 | 224 | intCompare(); |
alustig3 | 0:9d97f34c6f46 | 225 | intCompare(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 226 | intCompare(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 227 | intCompare(int* intVarInput, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 228 | intCompare(int* intVarInput, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 229 | intCompare(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput); |
alustig3 | 0:9d97f34c6f46 | 230 | intCompare(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 231 | |
alustig3 | 0:9d97f34c6f46 | 232 | void set(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 233 | void set(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 234 | void set(int* intVarInput, const char* cmpString, int cmpValInput); |
alustig3 | 0:9d97f34c6f46 | 235 | void set(int* intVarInput, const char* cmpString, int* cmpIntVarInput); |
alustig3 | 0:9d97f34c6f46 | 236 | void set(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput); |
alustig3 | 0:9d97f34c6f46 | 237 | void set(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse); |
alustig3 | 0:9d97f34c6f46 | 238 | |
alustig3 | 0:9d97f34c6f46 | 239 | void release(); |
alustig3 | 0:9d97f34c6f46 | 240 | |
alustig3 | 0:9d97f34c6f46 | 241 | ~intCompare(); |
alustig3 | 0:9d97f34c6f46 | 242 | bool isTrue(); |
alustig3 | 0:9d97f34c6f46 | 243 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 244 | |
alustig3 | 0:9d97f34c6f46 | 245 | private: |
alustig3 | 0:9d97f34c6f46 | 246 | digitalPort* port; |
alustig3 | 0:9d97f34c6f46 | 247 | int* portValPtr; |
alustig3 | 0:9d97f34c6f46 | 248 | int* cmpVal; |
alustig3 | 0:9d97f34c6f46 | 249 | int* intVal; |
alustig3 | 0:9d97f34c6f46 | 250 | intOperation* intOp; |
alustig3 | 0:9d97f34c6f46 | 251 | void setPointer(const char* cmpString); |
alustig3 | 0:9d97f34c6f46 | 252 | void setPointer_operation(const char* cmpString); |
alustig3 | 0:9d97f34c6f46 | 253 | bool (intCompare::*isTruePtr)(); |
alustig3 | 0:9d97f34c6f46 | 254 | bool cmpValGlobal; |
alustig3 | 0:9d97f34c6f46 | 255 | bool greaterThan(); |
alustig3 | 0:9d97f34c6f46 | 256 | bool greaterOrEqual(); |
alustig3 | 0:9d97f34c6f46 | 257 | bool lessThan(); |
alustig3 | 0:9d97f34c6f46 | 258 | bool lessOrEqual(); |
alustig3 | 0:9d97f34c6f46 | 259 | bool equal(); |
alustig3 | 0:9d97f34c6f46 | 260 | bool notEqual(); |
alustig3 | 0:9d97f34c6f46 | 261 | bool greaterThan_op(); |
alustig3 | 0:9d97f34c6f46 | 262 | bool greaterOrEqual_op(); |
alustig3 | 0:9d97f34c6f46 | 263 | bool lessThan_op(); |
alustig3 | 0:9d97f34c6f46 | 264 | bool lessOrEqual_op(); |
alustig3 | 0:9d97f34c6f46 | 265 | bool equal_op(); |
alustig3 | 0:9d97f34c6f46 | 266 | bool notEqual_op(); |
alustig3 | 0:9d97f34c6f46 | 267 | }; |
alustig3 | 0:9d97f34c6f46 | 268 | |
alustig3 | 0:9d97f34c6f46 | 269 | |
alustig3 | 0:9d97f34c6f46 | 270 | //holder class for all possible conditions (so far only intCompare) |
alustig3 | 0:9d97f34c6f46 | 271 | class condition { |
alustig3 | 0:9d97f34c6f46 | 272 | public: |
alustig3 | 0:9d97f34c6f46 | 273 | |
alustig3 | 0:9d97f34c6f46 | 274 | condition(); |
alustig3 | 0:9d97f34c6f46 | 275 | condition(intCompare* compareInput); |
alustig3 | 0:9d97f34c6f46 | 276 | condition(condition* condition1, char condType, condition* condition2); |
alustig3 | 0:9d97f34c6f46 | 277 | ~condition(); |
alustig3 | 0:9d97f34c6f46 | 278 | void set(intCompare* compareInput); |
alustig3 | 0:9d97f34c6f46 | 279 | void set(condition* condition1, char condType, condition* condition2); |
alustig3 | 0:9d97f34c6f46 | 280 | bool isTrue(); |
alustig3 | 0:9d97f34c6f46 | 281 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 282 | void release(); //called when the event is no longer being used; |
alustig3 | 0:9d97f34c6f46 | 283 | private: |
alustig3 | 0:9d97f34c6f46 | 284 | |
alustig3 | 0:9d97f34c6f46 | 285 | //char conditionType; //1 for intCompare |
alustig3 | 0:9d97f34c6f46 | 286 | intCompare* intCmp; |
alustig3 | 0:9d97f34c6f46 | 287 | condition* conditionPtrs[2]; |
alustig3 | 0:9d97f34c6f46 | 288 | char conditionType; |
alustig3 | 0:9d97f34c6f46 | 289 | |
alustig3 | 0:9d97f34c6f46 | 290 | |
alustig3 | 0:9d97f34c6f46 | 291 | }; |
alustig3 | 0:9d97f34c6f46 | 292 | //-------------------------------------------- |
alustig3 | 0:9d97f34c6f46 | 293 | |
alustig3 | 0:9d97f34c6f46 | 294 | |
alustig3 | 0:9d97f34c6f46 | 295 | //queueItem connects a pre-defined event with an exectution time. |
alustig3 | 0:9d97f34c6f46 | 296 | //They are placed in the eventQueue |
alustig3 | 0:9d97f34c6f46 | 297 | struct queueItem { |
alustig3 | 0:9d97f34c6f46 | 298 | uint32_t timeToExecute; |
alustig3 | 0:9d97f34c6f46 | 299 | event* eventPtr; |
alustig3 | 0:9d97f34c6f46 | 300 | }; |
alustig3 | 0:9d97f34c6f46 | 301 | |
alustig3 | 0:9d97f34c6f46 | 302 | |
alustig3 | 0:9d97f34c6f46 | 303 | //Organizes events in a temporal queue. check() is called from the main loop. |
alustig3 | 0:9d97f34c6f46 | 304 | //If the execution time of the event has passed, then the event is exectuted. |
alustig3 | 0:9d97f34c6f46 | 305 | class eventQueue { |
alustig3 | 0:9d97f34c6f46 | 306 | public: |
alustig3 | 0:9d97f34c6f46 | 307 | eventQueue(digitalPort** portVectorInput, uint32_t* timeKeeperSlaveInput); |
alustig3 | 0:9d97f34c6f46 | 308 | void addEventToQueue(event* eventInput, uint32_t delay); |
alustig3 | 0:9d97f34c6f46 | 309 | void eraseQueue(); //clear all future events |
alustig3 | 0:9d97f34c6f46 | 310 | void check(void); |
alustig3 | 0:9d97f34c6f46 | 311 | |
alustig3 | 0:9d97f34c6f46 | 312 | private: |
alustig3 | 0:9d97f34c6f46 | 313 | std::vector<queueItem> events; |
alustig3 | 0:9d97f34c6f46 | 314 | digitalPort** portVector; |
alustig3 | 0:9d97f34c6f46 | 315 | uint32_t* timeKeeperPtr; |
alustig3 | 0:9d97f34c6f46 | 316 | int queueSize; |
alustig3 | 0:9d97f34c6f46 | 317 | |
alustig3 | 0:9d97f34c6f46 | 318 | }; |
alustig3 | 0:9d97f34c6f46 | 319 | |
alustig3 | 0:9d97f34c6f46 | 320 | //An 'event' is a block of 'actions' that can be gated with a boolean 'condition' set. All |
alustig3 | 0:9d97f34c6f46 | 321 | //conditions in the set must be true for the block of actions to be executed. Right now, |
alustig3 | 0:9d97f34c6f46 | 322 | //there is no OR logic (||), only AND (&&). |
alustig3 | 0:9d97f34c6f46 | 323 | //The entire event is placed on the event queue to be executed at a given delay. |
alustig3 | 0:9d97f34c6f46 | 324 | //At that future time, the condition is checked and if true, the block of actions |
alustig3 | 0:9d97f34c6f46 | 325 | //is exectuted. Note: an 'action' can be another event (or even the parent event), allowing |
alustig3 | 0:9d97f34c6f46 | 326 | //nested 'if' and 'while' statements. |
alustig3 | 0:9d97f34c6f46 | 327 | class event { |
alustig3 | 0:9d97f34c6f46 | 328 | public: |
alustig3 | 0:9d97f34c6f46 | 329 | |
alustig3 | 0:9d97f34c6f46 | 330 | event(); |
alustig3 | 0:9d97f34c6f46 | 331 | event(eventQueue* queueInput); |
alustig3 | 0:9d97f34c6f46 | 332 | ~event(); |
alustig3 | 0:9d97f34c6f46 | 333 | void setTimeLag(uint32_t timeLagInput); //the event will be exectuted at this time from now |
alustig3 | 0:9d97f34c6f46 | 334 | void setTimeLag(int* timeLagInput); //the event will be exectuted at this time from now |
alustig3 | 0:9d97f34c6f46 | 335 | void setWhileLoopPeriod(uint32_t period); |
alustig3 | 0:9d97f34c6f46 | 336 | void setWhileLoopPeriod(int* period); |
alustig3 | 0:9d97f34c6f46 | 337 | void addCondition(condition* conditionInput); //contains a set of conditions to check and a truth table |
alustig3 | 0:9d97f34c6f46 | 338 | bool isConditionTrue(void); //checks if the condition is true |
alustig3 | 0:9d97f34c6f46 | 339 | void addAction(action* actionInput); //called during script parsing, when the block is being defined |
alustig3 | 0:9d97f34c6f46 | 340 | void addToQueue(void); //places the event on the event queue with default time lag. When the time |
alustig3 | 0:9d97f34c6f46 | 341 | //lag has expired, the the block is executed |
alustig3 | 0:9d97f34c6f46 | 342 | void addToQueue(uint32_t delay); |
alustig3 | 0:9d97f34c6f46 | 343 | void execute(void); //Execute without checking the condition. Called only from the event queue |
alustig3 | 0:9d97f34c6f46 | 344 | void setNextElseEvent(event* eventInput); //allows for else event block |
alustig3 | 0:9d97f34c6f46 | 345 | uint32_t timeLag; //default time from now when the event will be executed (this is ignored once placed in event queue) |
alustig3 | 0:9d97f34c6f46 | 346 | int* timeLagVar; //exectution time lab defined by a variable |
alustig3 | 0:9d97f34c6f46 | 347 | eventQueue* queuePtr; |
alustig3 | 0:9d97f34c6f46 | 348 | void release(); //called when the event is no longer being used; |
alustig3 | 0:9d97f34c6f46 | 349 | |
alustig3 | 0:9d97f34c6f46 | 350 | char blockType; //0 callback |
alustig3 | 0:9d97f34c6f46 | 351 | //1 if ... do block (with conditions) |
alustig3 | 0:9d97f34c6f46 | 352 | //2 do block (no conditions) |
alustig3 | 0:9d97f34c6f46 | 353 | //3 else if ... do block |
alustig3 | 0:9d97f34c6f46 | 354 | //4 else do (no conditions) |
alustig3 | 0:9d97f34c6f46 | 355 | //5 while ... do every ... block |
alustig3 | 0:9d97f34c6f46 | 356 | //6 else while ... do every ... block |
alustig3 | 0:9d97f34c6f46 | 357 | //7 then if ... do block |
alustig3 | 0:9d97f34c6f46 | 358 | //8 then do (no conditions) |
alustig3 | 0:9d97f34c6f46 | 359 | |
alustig3 | 0:9d97f34c6f46 | 360 | uint32_t whileLoopPeriod; //if non-zero, the block is a while loop (executed at regular intervals) |
alustig3 | 0:9d97f34c6f46 | 361 | int* whileLoopPeriodVar; |
alustig3 | 0:9d97f34c6f46 | 362 | event* nextElseEventPtr; |
alustig3 | 0:9d97f34c6f46 | 363 | bool isUsed; |
alustig3 | 0:9d97f34c6f46 | 364 | bool timeLagIsConstant; |
alustig3 | 0:9d97f34c6f46 | 365 | bool whileLoopPeriodIsConstant; |
alustig3 | 0:9d97f34c6f46 | 366 | bool hasWhileLoop; |
alustig3 | 0:9d97f34c6f46 | 367 | |
alustig3 | 0:9d97f34c6f46 | 368 | private: |
alustig3 | 0:9d97f34c6f46 | 369 | int numConditions; |
alustig3 | 0:9d97f34c6f46 | 370 | int numActions; |
alustig3 | 0:9d97f34c6f46 | 371 | condition* conditionToCheck; |
alustig3 | 0:9d97f34c6f46 | 372 | action* actionArray[20]; |
alustig3 | 0:9d97f34c6f46 | 373 | |
alustig3 | 0:9d97f34c6f46 | 374 | //if statement (can be left empty, which is interpreted as 'true') |
alustig3 | 0:9d97f34c6f46 | 375 | //std::vector<condition*> conditionArray; |
alustig3 | 0:9d97f34c6f46 | 376 | //std::deque<action*> actionArray; |
alustig3 | 0:9d97f34c6f46 | 377 | |
alustig3 | 0:9d97f34c6f46 | 378 | }; |
alustig3 | 0:9d97f34c6f46 | 379 | |
alustig3 | 0:9d97f34c6f46 | 380 | //each functionItem help a poiter to an action, and the name of the function |
alustig3 | 0:9d97f34c6f46 | 381 | class functionItem { |
alustig3 | 0:9d97f34c6f46 | 382 | public: |
alustig3 | 0:9d97f34c6f46 | 383 | functionItem(action* actionInput, string tagInput); |
alustig3 | 0:9d97f34c6f46 | 384 | ~functionItem(); |
alustig3 | 0:9d97f34c6f46 | 385 | string tag; |
alustig3 | 0:9d97f34c6f46 | 386 | action* actionPtr; |
alustig3 | 0:9d97f34c6f46 | 387 | }; |
alustig3 | 0:9d97f34c6f46 | 388 | |
alustig3 | 0:9d97f34c6f46 | 389 | //Parser for the incoming text. The parser is called when a line terminates with a semicolon (;). |
alustig3 | 0:9d97f34c6f46 | 390 | //Only the final line in a callback block should have a semicolon. |
alustig3 | 0:9d97f34c6f46 | 391 | class scriptStream { |
alustig3 | 0:9d97f34c6f46 | 392 | public: |
alustig3 | 0:9d97f34c6f46 | 393 | scriptStream(Serial* serialInput, digitalPort** portVectorInput, int numPortsInput, eventQueue* queueInput); |
alustig3 | 0:9d97f34c6f46 | 394 | void parseBlock(); |
alustig3 | 0:9d97f34c6f46 | 395 | void addLineToCurrentBlock(char* lineInput); // if the line did not end with a semicolon, add it to the current block |
alustig3 | 0:9d97f34c6f46 | 396 | int* findIntVariable(string nameInput); //used to retrieve the pointer to the designated variable if it exists |
alustig3 | 0:9d97f34c6f46 | 397 | bool createIntVariable(string nameInput); // creates a new interger variable |
alustig3 | 0:9d97f34c6f46 | 398 | action* evaluateAssignmentForAction(string expression); //parses a numerical assignment or operation (a = b - c) |
alustig3 | 0:9d97f34c6f46 | 399 | bool evaluateConditions(string expression, event* currentEvent); //parses a condition statement (a == b && c > d) |
alustig3 | 0:9d97f34c6f46 | 400 | condition* parseConditions(string expression); //parses a condition statement (a == b && c > d) |
alustig3 | 0:9d97f34c6f46 | 401 | std::size_t findFirstOrOutsideParenth(string expression); |
alustig3 | 0:9d97f34c6f46 | 402 | std::size_t findFirstAndOutsideParenth(string expression); |
alustig3 | 0:9d97f34c6f46 | 403 | bool isOutsideParenth(string expression,std::size_t foundItem); |
alustig3 | 0:9d97f34c6f46 | 404 | |
alustig3 | 0:9d97f34c6f46 | 405 | int getRandomParam(string expression); |
alustig3 | 0:9d97f34c6f46 | 406 | |
alustig3 | 0:9d97f34c6f46 | 407 | private: |
alustig3 | 0:9d97f34c6f46 | 408 | |
alustig3 | 0:9d97f34c6f46 | 409 | int currentTriggerPort; |
alustig3 | 0:9d97f34c6f46 | 410 | int currentTriggerDir; |
alustig3 | 0:9d97f34c6f46 | 411 | int currentPort; |
alustig3 | 0:9d97f34c6f46 | 412 | int currentFunction; |
alustig3 | 0:9d97f34c6f46 | 413 | |
alustig3 | 0:9d97f34c6f46 | 414 | string tmpLine; |
alustig3 | 0:9d97f34c6f46 | 415 | vector<string> tokens; |
alustig3 | 0:9d97f34c6f46 | 416 | |
alustig3 | 0:9d97f34c6f46 | 417 | bool lineError; |
alustig3 | 0:9d97f34c6f46 | 418 | int blockDepth; |
alustig3 | 0:9d97f34c6f46 | 419 | bool ifBlockInit; |
alustig3 | 0:9d97f34c6f46 | 420 | bool whileBlockInit; |
alustig3 | 0:9d97f34c6f46 | 421 | bool elseFlag; |
alustig3 | 0:9d97f34c6f46 | 422 | bool thenFlag; |
alustig3 | 0:9d97f34c6f46 | 423 | int currentDelay; |
alustig3 | 0:9d97f34c6f46 | 424 | event* tmpEvent; |
alustig3 | 0:9d97f34c6f46 | 425 | string tmpString; |
alustig3 | 0:9d97f34c6f46 | 426 | |
alustig3 | 0:9d97f34c6f46 | 427 | vector<intVariable*> globalVariables; |
alustig3 | 0:9d97f34c6f46 | 428 | vector<event*> tmpEventPtrArray; |
alustig3 | 0:9d97f34c6f46 | 429 | vector<functionItem*> functionArray; //any blocks declared outsite callback blocks are stored here |
alustig3 | 0:9d97f34c6f46 | 430 | list<string> currentBlock; |
alustig3 | 0:9d97f34c6f46 | 431 | digitalPort** portVector; |
alustig3 | 0:9d97f34c6f46 | 432 | |
alustig3 | 0:9d97f34c6f46 | 433 | |
alustig3 | 0:9d97f34c6f46 | 434 | int numPorts; |
alustig3 | 0:9d97f34c6f46 | 435 | Serial* pcPtr; |
alustig3 | 0:9d97f34c6f46 | 436 | eventQueue* queuePtr; |
alustig3 | 0:9d97f34c6f46 | 437 | |
alustig3 | 0:9d97f34c6f46 | 438 | }; |
alustig3 | 0:9d97f34c6f46 | 439 | |
alustig3 | 0:9d97f34c6f46 | 440 | |
alustig3 | 0:9d97f34c6f46 | 441 | //Used to buffer output text. Used mainly for 'display' commands within the script, |
alustig3 | 0:9d97f34c6f46 | 442 | //and alloed the reset of the block to execute quickly instead. The text is then streamed out |
alustig3 | 0:9d97f34c6f46 | 443 | //slowly via serial (one character per main loop execution). outputStream is a simple circular |
alustig3 | 0:9d97f34c6f46 | 444 | //buffer that cannot be resized after initiation. |
alustig3 | 0:9d97f34c6f46 | 445 | class outputStream { |
alustig3 | 0:9d97f34c6f46 | 446 | |
alustig3 | 0:9d97f34c6f46 | 447 | public: |
alustig3 | 0:9d97f34c6f46 | 448 | outputStream(int bufferSizeIn); |
alustig3 | 0:9d97f34c6f46 | 449 | ~outputStream(); |
alustig3 | 0:9d97f34c6f46 | 450 | void send(string outputString); |
alustig3 | 0:9d97f34c6f46 | 451 | char getNextChar(); |
alustig3 | 0:9d97f34c6f46 | 452 | bool unsentData; |
alustig3 | 0:9d97f34c6f46 | 453 | |
alustig3 | 0:9d97f34c6f46 | 454 | private: |
alustig3 | 0:9d97f34c6f46 | 455 | int readHead; |
alustig3 | 0:9d97f34c6f46 | 456 | int writeHead; |
alustig3 | 0:9d97f34c6f46 | 457 | int totalWriteHead; |
alustig3 | 0:9d97f34c6f46 | 458 | int totalReadHead; |
alustig3 | 0:9d97f34c6f46 | 459 | int bufferSize; |
alustig3 | 0:9d97f34c6f46 | 460 | char tmpOut; |
alustig3 | 0:9d97f34c6f46 | 461 | char* outputBuffer; |
alustig3 | 0:9d97f34c6f46 | 462 | }; |