perturb room

Dependencies:   SMARTWAV mbed

Fork of stateScript by Mattias Karlsson

Files at this revision

API Documentation at this revision

Comitter:
alustig3
Date:
Mon May 18 01:17:58 2015 +0000
Parent:
4:34aca2142df9
Commit message:
perturb room

Changed in this revision

behave.cpp Show annotated file Show diff for this revision Revisions of this file
behave.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
test.lib Show diff for this revision Revisions of this file
diff -r 34aca2142df9 -r 4d6575e3a07e behave.cpp
--- 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);
diff -r 34aca2142df9 -r 4d6575e3a07e behave.h
--- a/behave.h	Thu Sep 25 23:42:30 2014 +0000
+++ b/behave.h	Mon May 18 01:17:58 2015 +0000
@@ -21,10 +21,9 @@
 #define OR_CONDITION    1
 #define AND_CONDITION    2
 
-#define NUMPORTS 8
+#define NUMPORTS 10
 
-#define INPUTCHARBUFFERSIZE 3072
-
+extern "C" void mbed_reset();//MAX and ANDY
 class event; //we foreward declare this because of class interdependencies
 
 //used in the digital port class to organize digital change events
@@ -38,6 +37,7 @@
 class digitalPort {
 public:
     digitalPort(DigitalOut* DOP, DigitalIn* DIP);
+    digitalPort(DigitalOut* DOP);
     void setDigitalOut(int outVal);
     //int  getDigitalOut();
     int  getDigitalIn();
@@ -386,25 +386,6 @@
     action* actionPtr;
 };
 
-class blockBuffer {
-    
-public:
-    blockBuffer();
-    bool addLine(char* input, int numChars);
-    string getNextLine();
-    int16_t linesAvailable();
-    bool empty();
-    void resetBuffer();
-    
-private:
-    //__attribute((section("AHBSRAM1"),aligned)) char charBuffer[INPUTCHARBUFFERSIZE];
-    char charBuffer[INPUTCHARBUFFERSIZE];       
-    int16_t bufferWritePos;
-    int16_t bufferReadPos;
-    int16_t _linesAvailable;
-    
-};
-
 //Parser for the incoming text.  The parser is called when a line terminates with a semicolon (;).
 //Only the final line in a callback block should have a semicolon.  
 class scriptStream {
@@ -423,15 +404,13 @@
     
     int getRandomParam(string expression);
     
-    
-    
 private:
 
     int currentTriggerPort;
     int currentTriggerDir;
     int currentPort;
     int currentFunction;
-        
+    
     string tmpLine; 
     vector<string> tokens;
     
@@ -448,8 +427,7 @@
     vector<intVariable*> globalVariables;
     vector<event*> tmpEventPtrArray;
     vector<functionItem*> functionArray; //any blocks declared outsite callback blocks are stored here
-    //list<string> currentBlock;
-    blockBuffer currentBlock;
+    list<string> currentBlock;
     digitalPort** portVector;
     
     
diff -r 34aca2142df9 -r 4d6575e3a07e main.cpp
--- a/main.cpp	Thu Sep 25 23:42:30 2014 +0000
+++ b/main.cpp	Mon May 18 01:17:58 2015 +0000
@@ -14,7 +14,7 @@
 
 //static char buf1[0x2000] __attribute__((section("AHBSRAM0")));
 __attribute((section("AHBSRAM0"),aligned)) outputStream textDisplay(512);
-__attribute((section("AHBSRAM0"),aligned)) char buffer[256];
+__attribute((section("AHBSRAM0"),aligned)) char buffer[128];
 
 
 __attribute((section("AHBSRAM1"),aligned)) event eventBlock[NUMEVENTS];
@@ -56,60 +56,72 @@
 //Define the digial ports
 
 //Pins for clock syncing
-InterruptIn clockResetInt(p5);
-DigitalOut clockOutSync(p6);
-DigitalOut clockOutSignal(p7);
-InterruptIn clockExternalIncrement(p8);
-
-//Camera trigger signal
-//DigitalOut camera30Hz(p27);
+//InterruptIn clockResetInt(p24);
+DigitalOut clockOutSync(p19);
+DigitalOut clockOutSignal(p29);
+InterruptIn clockExternalIncrement(p30);
 
 
 //Pins for digital ports.  Each port has 1 out and 1 in
 //DigitalOut out1(LED1); //route to LED for debugging
-DigitalOut out1(p11);
-DigitalIn in1(p12);
-InterruptIn int1(p12);
+//1A,1B
+DigitalOut out1(p5);
+DigitalIn in1(p6);
+InterruptIn int1(p6);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port1(&out1, &in1);
-
-DigitalOut out2(p13);
-DigitalIn in2(p14);
-InterruptIn int2(p14);
+//1C,1D
+DigitalOut out2(p7);
+DigitalIn in2(p8);
+InterruptIn int2(p8);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port2(&out2, &in2);
-
-
-DigitalOut out3(p15);
-DigitalIn in3(p16);
-InterruptIn int3(p16);
+//2A,2B
+DigitalOut out3(p17);
+DigitalIn in3(p18);
+InterruptIn int3(p18);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port3(&out3, &in3);
-
-DigitalOut out4(p18);
-DigitalIn in4(p17);
-InterruptIn int4(p17);
+//2C,2D
+DigitalOut out4(p11);
+DigitalIn in4(p12);
+InterruptIn int4(p12);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port4(&out4, &in4);
-
+//3A,3B
 DigitalOut out5(p21);
 DigitalIn in5(p22);
 InterruptIn int5(p22);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port5(&out5, &in5);
-
-DigitalOut out6(p23);
-DigitalIn in6(p24);
-InterruptIn int6(p24);
+//3C,3D
+DigitalOut out6(p15);
+DigitalIn in6(p16);
+InterruptIn int6(p16);
 __attribute((section("AHBSRAM0"),aligned)) digitalPort port6(&out6, &in6);
+//4A,4B
+//DigitalOut out7(p9);
+//DigitalIn in7(p10);
+//InterruptIn int7(p10);
+//__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
+//5A,5B
+DigitalOut out7(p13);
+DigitalIn in7(p14);
+InterruptIn int7(p14);
+__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
+//6A,6B
+DigitalOut out8(p23);
+DigitalIn in8(p24);
+InterruptIn int8(p24);
+__attribute((section("AHBSRAM0"),aligned)) digitalPort port8(&out8, &in8);
+//Pump1
+DigitalOut out9(p25);
+__attribute((section("AHBSRAM0"),aligned)) digitalPort port9(&out9);
+//Pump2
+DigitalOut out10(p26);
+__attribute((section("AHBSRAM0"),aligned)) digitalPort port10(&out10);
 
-DigitalOut out7(p25);
-DigitalIn in7(p26);
-InterruptIn int7(p26);
-__attribute((section("AHBSRAM0"),aligned)) digitalPort port7(&out7, &in7);
 
-DigitalOut out8(p29);
-DigitalIn in8(p30);
-InterruptIn int8(p30);
-__attribute((section("AHBSRAM0"),aligned)) digitalPort port8(&out8, &in8);
+
 //Serial communication
 Serial pc(USBTX, USBRX); // tx, rx
 
+
 //Main event queue
 eventQueue mainQueue(portVector, &timeKeeper);
 
@@ -117,8 +129,8 @@
 scriptStream parser(&pc, portVector, NUMPORTS, &mainQueue);
 
 //The sound output uses a SmartWav device and their simple serial library
-SMARTWAV sWav(p9,p10,p19);    //(TX,RX,Reset);
-
+SMARTWAV sWav(p28,p27,p20);    //(TX,RX,Reset);
+Serial device(p9,p10);
 //Erases the input buffer for serial input
 void eraseBuffer(char* buffer,int numToErase) {
     for (int i = 0; i < numToErase; i++) {
@@ -200,7 +212,6 @@
             currentBroadcastBit = 0;
         }
     
-                
         //Every second, we broadcast out the current time 
         if ((timeKeeper % 1000) == 0) {
             currentBroadcastTime = timeKeeper;
@@ -268,6 +279,9 @@
 void callback_port7_fall(void) { int_callback(7, 0); }
 void callback_port8_rise(void) { int_callback(8, 1); }
 void callback_port8_fall(void) { int_callback(8, 0); }
+//void callback_port9_rise(void) { int_callback(9, 1); }
+//void callback_port9_fall(void) { int_callback(9, 0); }
+
 
 //This function is attached to an interrupt pin for external clock reset
 void callback_clockReset(void) {
@@ -281,11 +295,12 @@
 
 int main() {
     timeKeeper = 0; //set main clock to 0;
-    sWav.reset(); 
+//    sWav.reset(); 
     pc.baud(115200);
+    device.baud(4800);
     //pc.baud(9600);
    
-    for (int i = 0; i < 9; i++) {
+    for (int i = 0; i <NUMPORTS+1; i++) {
         portVector[i] = NULL;
     }
     //We keep portVector 1-based to eliminate confusion
@@ -297,6 +312,9 @@
     portVector[6] = &port6;
     portVector[7] = &port7;
     portVector[8] = &port8;
+    portVector[9] = &port9;
+    portVector[10] = &port10;
+    //portVector[11] = &port11;  
    
     //Callback to update the main clock 
     //timeTick1.attach_us(&incrementTime, 100);
@@ -321,9 +339,11 @@
     int7.fall(&callback_port7_fall);
     int8.rise(&callback_port8_rise);
     int8.fall(&callback_port8_fall);
+//   int9.rise(&callback_port9_rise);
+//   int9.fall(&callback_port9_fall);
     
-    clockResetInt.rise(&callback_clockReset);
-    clockResetInt.mode(PullDown);
+    //clockResetInt.rise(&callback_clockReset);
+    //clockResetInt.mode(PullDown);
     
     clockExternalIncrement.mode(PullDown);
       
@@ -336,11 +356,12 @@
     in6.mode(PullDown);
     in7.mode(PullDown);
     in8.mode(PullDown);
+//    in9.mode(PullDown);
      
     //Set up input buffer for the serial port
     //char buffer[128];
     int bufferPos = 0;
-    eraseBuffer(buffer,256);
+    eraseBuffer(buffer,128);
     
     ostringstream timeConvert;   // stream used for the conversion
     ostringstream stateConvert; 
@@ -363,14 +384,14 @@
             if ((tmpChar == 13) || (tmpChar == 10)) { //carrriage return
                 parser.addLineToCurrentBlock(buffer);                          
                 bufferPos = 0;
-                eraseBuffer(buffer,256);             
+                eraseBuffer(buffer,128);             
             }            
             //pc.putc(tmpChar);
         } while (tmpChar != EOF);
         
         buffer[bufferPos] = 59;
         parser.addLineToCurrentBlock(buffer);
-        eraseBuffer(buffer,256);  
+        eraseBuffer(buffer,128);  
         fclose(fp);
     } else {
         pc.printf("No startup script found.\r\n"); 
@@ -381,6 +402,10 @@
        //check the main event queue to see if anything needs to be done
        mainQueue.check();
        
+       //https://developer.mbed.org/handbook/Serial
+        if(device.readable()) {
+            pc.putc(device.getc());
+        }
        //check if anything has been written to the serial input
        if (pc.readable()) {
                 
diff -r 34aca2142df9 -r 4d6575e3a07e test.lib
--- a/test.lib	Thu Sep 25 23:42:30 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://mbed.org/users/simon/code/HelloWorld/#03c191369089