A scripting environment used to define precise output/input temporal relationships.

Dependencies:   SMARTWAV mbed HelloWorld

Dependents:   perturbRoom_legacy

Fork of HelloWorld by Simon Ford

Committer:
mkarlsso
Date:
Thu Sep 25 23:42:30 2014 +0000
Revision:
4:34aca2142df9
Parent:
2:298679fff37c
Longer script loading without hanging due to memory limitations

Who changed what in which revision?

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