perturb room

Dependencies:   SMARTWAV mbed

Fork of stateScript by Mattias Karlsson

Revision:
5:4d6575e3a07e
Parent:
4:34aca2142df9
--- a/behave.cpp	Thu Sep 25 23:42:30 2014 +0000
+++ b/behave.cpp	Mon May 18 01:17:58 2015 +0000
@@ -2,8 +2,6 @@
 #include <ctype.h>
 #include <sstream> 
 
-int16_t randomSeedCounter;  //used for seeding random numbers
-
 // These external symbols are maintained by the linker to indicate the
 // location of various regions in the device's memory.  They will be used by
 // DisplayRAMBanks() to dump the size of each RAM bank to stdout.
@@ -19,6 +17,7 @@
 extern digitalPort* portVector[];
 
 extern Serial pc;
+extern Serial device; //ANDY
 extern bool resetTimer;
 extern bool clockSlave;
 extern bool changeToSlave;
@@ -132,6 +131,19 @@
     triggerDownEventPtr = NULL;
     outStateChanged = false;
 }
+digitalPort::digitalPort(DigitalOut* DOP):  //added by me
+    outPin(DOP),
+    outState(0){
+    
+    lastChangeTime = 0;
+    lastOutChangeTime = 0;
+    lastChangeInterval = 0;
+    lastDownEvent.triggered = false;
+    lastUpEvent.triggered = false;
+    triggerUpEventPtr = NULL;
+    triggerDownEventPtr = NULL;
+    outStateChanged = false;
+}
 
 void digitalPort::setTriggerUpEvent(event* eventInput) {
     if (triggerUpEventPtr != NULL) {
@@ -912,8 +924,7 @@
         return *intVal;    
     } else if (cmpVal != NULL){
         
-        srand(*globalTimeKeeperPtr+randomSeedCounter);
-        randomSeedCounter++; //for seeding the next rand call, just in case it happens before the clock advances     
+        srand(*globalTimeKeeperPtr);     
         *cmpVal = (rand() % (randHigh+1)); //this is how we assign a random number to variable
         return *cmpVal;
         
@@ -1681,71 +1692,6 @@
     delete actionPtr;
  }
  
- blockBuffer::blockBuffer() {
-    bufferWritePos = 0;
-    bufferWritePos = 0;
-    _linesAvailable = 0;
-    
- }
-
- bool blockBuffer::addLine(char *input, int numChars) {
-    
-    if (bufferWritePos+numChars >= INPUTCHARBUFFERSIZE) {
-        return false;
-    }
-    for(int i=0;i<numChars;i++) {
-       charBuffer[bufferWritePos] = input[i];
-       bufferWritePos++;            
-    }
-    _linesAvailable++;
-    return true;      
- }
- 
- string blockBuffer::getNextLine() {
- 
-    string outputLine;
-    int endOfLinePos = bufferReadPos;
-    bool endOfLineFound = false;
-    if (_linesAvailable > 0) {
-        //Find the end of the next line
-        while (endOfLinePos < INPUTCHARBUFFERSIZE) {
-            if (charBuffer[endOfLinePos] == '\0') {               
-                endOfLineFound = true;
-                break;
-            }
-            endOfLinePos++;
-        }
-        
-        //If the end was found, copy to output string
-        if (endOfLineFound) {           
-            outputLine.append(charBuffer+bufferReadPos,endOfLinePos-bufferReadPos);
-            bufferReadPos = endOfLinePos+1; 
-            _linesAvailable--;            
-        }
-    }
-    if (_linesAvailable == 0) {
-        //we have read out all of the lines, so reset the buffer for the next block.
-        resetBuffer();
-    }
-    return outputLine;
-        
- }
- 
- int16_t blockBuffer::linesAvailable() {
-     return _linesAvailable;
- }
- 
- void blockBuffer::resetBuffer() {
-    _linesAvailable = 0;
-    bufferReadPos = 0;
-    bufferWritePos = 0;    
- }
- 
- bool blockBuffer::empty() {
-     return (_linesAvailable == 0);
- }
-     
- 
  scriptStream::scriptStream(Serial* serialInput, digitalPort** portVectorInput, int numPortsInput, eventQueue* queueInput):
     portVector(portVectorInput),
     numPorts(numPortsInput),
@@ -1756,9 +1702,42 @@
     currentTriggerPort = -1;
     currentTriggerDir = 1;
     currentFunction = -1;
+     
+    lineError = false;
+    blockDepth = 0;
+    ifBlockInit = false;
+    whileBlockInit = false;
+    elseFlag = false;
+    currentDelay = 0;
     
-    randomSeedCounter = 0;  //used for seeding random numbers
+ }
+ 
+ void scriptStream::addLineToCurrentBlock(char* lineInput) {
      
+    bool compile = false;
+    bool keep = false;
+    for (int i = 0; i < 128; i++) {
+        if (lineInput[i] == ';') {
+            compile = true;
+        } else if (lineInput[i] == ' ') {
+            continue;
+        } else if (lineInput[i] == '\0') {
+            break;
+        } else {
+            keep = true;
+            compile = false;
+        }
+    }
+    if (keep) currentBlock.insert(currentBlock.begin(),string(lineInput));
+    if (compile) parseBlock();
+    
+ }
+ 
+ 
+ //SCRIPT PARSING - all script commands are defined here.
+ //-------------------------------------------------------
+ void scriptStream::parseBlock() {
+          
     lineError = false;
     blockDepth = 0;
     ifBlockInit = false;
@@ -1767,72 +1746,14 @@
     thenFlag = false;
     currentDelay = 0;
     
-       
- }
- 
- 
- void scriptStream::addLineToCurrentBlock(char* lineInput) {
-     
-    bool compile = false;
-    bool keep = false;
-    int numCharInLine = 0;
-    //A line ending with ';' then carriage return initiates the compile sequence
-    //Otherwise, add the line to the buffer and compile later
-    for (int i = 0; i < 256; i++) {
-        numCharInLine++;
-        if (lineInput[i] == ';') {
-            compile = true;
-        } else if (lineInput[i] == ' ') {
-            continue;
-        } else if (lineInput[i] == '\0') {
-            break;
-        } else {
-            keep = true;            
-            compile = false;
-        }
-        
-    }
-    //if (keep) currentBlock.insert(currentBlock.begin(),string(lineInput));
-    if (keep) {
-        if (!currentBlock.addLine(lineInput,numCharInLine)) {
-            pcPtr->printf("Error: script input buffer full. The block is too long.\r\n");
-            currentBlock.resetBuffer();
-            compile = false;                                             
-        }        
-    }
-    if (compile) {   
-        parseBlock();
-    }
-    
- }
- 
- 
- //SCRIPT PARSING - all script commands are defined here.
- //-------------------------------------------------------
- void scriptStream::parseBlock() {
-          
-    
-    lineError = false;
-    blockDepth = 0;
-    ifBlockInit = false;
-    whileBlockInit = false;
-    elseFlag = false;
-    thenFlag = false;
-    currentDelay = 0;
-    
-    
     std::size_t stringInd = 0;
     
     bool wholeLineEvaluated = false;
    
     //pcPtr->printf("\r\n"); 
     while (!currentBlock.empty()) {
-        
-             
         wholeLineEvaluated = false;
-        //tmpLine = currentBlock.back();
-        tmpLine = currentBlock.getNextLine();
-                      
+        tmpLine = currentBlock.back();
         lineError = false;
         //remove tabs
         std::size_t found = tmpLine.find_first_of(9); //tab
@@ -1888,7 +1809,6 @@
                        tmpEventPtrArray.pop_back(); //recursively remove the pointers to all else blocks
                     }
                     tmpEventPtrArray.pop_back(); //remove the pointer to the finished block
-                    
                 } else {
                     pcPtr->printf("Error: End statement without block\r\n");
                     lineError = true;
@@ -2454,7 +2374,14 @@
             //              ...
             //          end
                                 
-            } else if (tokens[i].compare("do") == 0) { //the start of a block
+          }else if(tokens[i].compare("kaboom") == 0){//MAX ANDY
+            mbed_reset();            
+          }
+            
+            
+            
+            
+             else if (tokens[i].compare("do") == 0) { //the start of a block
                                       
                      if (!ifBlockInit && !whileBlockInit) {
                          
@@ -2980,7 +2907,17 @@
                         }                                        
                     }
                 }
-            } else {
+            } 
+            //ANDY
+            else if (tokens[i].find("opto(") != std::string::npos) {
+                wholeLineEvaluated = true;
+                int pos1 = tmpLine.find("opto(")+5;
+                int pos2 = tmpLine.find_first_of(")",pos1);
+                string dispVar = tmpLine.substr(pos1,pos2-pos1);
+                device.printf(dispVar.c_str());
+            }
+                
+            else {
                 //if there was no match to any of the above, an error is given
                 pcPtr->printf("Error: statement not understood.\r\n");
                 lineError = true;
@@ -3002,13 +2939,8 @@
                 tokens.erase(tokens.begin());
             }
             pcPtr->printf("\r\n"); 
-            /*
             while (!currentBlock.empty()) {
                 currentBlock.pop_back();
-            }*/
-            currentBlock.resetBuffer();
-            while (!tokens.empty()) {
-                tokens.pop_back();
             }
             delete tmpEvent;
         } else {
@@ -3016,25 +2948,18 @@
             while (!tokens.empty()) {
                 tokens.pop_back();
             }
-            //currentBlock.pop_back();
-            
+            currentBlock.pop_back();
         }
 
     }
     
     //make sure that all blocks have a matching end statement
-    
-    
     if ((!lineError)&&(blockDepth > 0)) {
         pcPtr->printf("Error: Missing 1 or more end statements\r\n");
         lineError = true;
-        currentBlock.resetBuffer();            
-    }
-        
-    if ((!lineError)&&(blockDepth == 0)) {
-        textDisplay.send("~~~\r\n");
-    }
-    
+    }    
+    //pcPtr->printf("~~~\r\n"); //signals that the code was compiled
+    textDisplay.send("~~~\r\n");
     //displayMemoryLeft();
     //DisplayRAMBanks();
         
@@ -3239,7 +3164,7 @@
                         tmpOp->set(tmpVar, multiplier.data(), atoi(afterEqual.substr(stringInd2+1,std::string::npos).data()));
                     }
                     tmpAction->set(tmpOp);
-                    //pcPtr->printf("Action: change variable by constant amount\r\n");
+                    pcPtr->printf("Action: change variable by constant amount\r\n");
                 } else {
                     
                     tmpOp2 = findFirstUnUsed(intOperationBlock, NUMINTOPERATIONS);