Microcontroller firmware that uses a simple, yet powerful scripting language to control the timing of input and output events with high temporal resolution. Written by Mattias Karlsson

Dependencies:   SMARTWAV mbed

Revision:
5:67d67d452545
Parent:
4:abee20c0bf2a
Child:
6:6a6761a47951
--- a/behave.cpp	Fri Jan 15 22:13:23 2016 +0000
+++ b/behave.cpp	Thu Jan 21 14:28:46 2016 +0000
@@ -125,8 +125,8 @@
 
     int bufferPos = 0;
 
-    ostringstream timeConvert;   // stream used for the conversion
-    ostringstream stateConvert;
+    //ostringstream timeConvert;   // stream used for the conversion
+    //ostringstream stateConvert;
     int tmpChar;
     int junkChar;
 
@@ -239,7 +239,10 @@
                 }
             }
             if (ports[i].outStateChanged) {
-                digitalOutChanged = true;
+                if (!ignoreUpdatePorts[i]) {
+                     //Only trigger an output update to the serial port if ignore is false
+                    digitalOutChanged = true;
+                }
                 changeTime = min(changeTime,ports[i].lastOutChangeTime);
                 //The out state of all the ports is condensed into one number (each bit contains the info)
                 if (ports[i].outState == 1) {
@@ -2311,91 +2314,98 @@
                 wholeLineEvaluated = true;
                 int pos1 = tmpLine.find("sound(")+6;
                 int pos2 = tmpLine.find_first_of(")",pos1);
-                string dispVar = tmpLine.substr(pos1,pos2-pos1);
-
-                int* tmpVar = findIntVariable(dispVar);
-                bool isText = false;
-                bool stopSignal = false;
-                bool resetSignal = false;
-                if (tmpVar == NULL) {
-                    if ((tmpLine.compare(pos1,1,"'")==0) && (tmpLine.compare(pos2-1,1,"'")==0)) {
-                        isText = true;
-                    } else if (dispVar.compare("stop") == 0) {
-                        stopSignal = true;
-                    } else if (dispVar.compare("reset") == 0) {
-                        resetSignal = true;
-                    } else {
-                        textDisplay << "Error: variable input to sound() does not exist\r\n";
-                        lineError = true;
-                    }
-                }
-                action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS);
-                if (tmpAction == NULL) {
-                    textDisplay << "Error: no memory slots available.\r\n";
+                if (pos2 == std::string::npos) {
+                    textDisplay << "Syntax error: expected a ')'\r\n";
                     lineError = true;
                 }
-                if (!lineError && (blockDepth == 0)) {
-                    //we are not inside a block structure, so play sound now
-                    if (stopSignal) {
-                        sSound* S = system->createNewSoundAction();
-                        S->setPlayback(false);
-                        S->execute();
-                        delete S;
-                    } else if (resetSignal) {
-                        sSound* S = system->createNewSoundAction();
-                        S->setReset();
-                        S->execute();
-                        delete S;
-                    } else if (isText) {
-
-                        if (pos2-pos1-2 <= 20) {
-
-                            sSound* S = system->createNewSoundAction();
-                            S->setFile(tmpLine.substr(pos1+1,pos2-pos1-2));
-                            S->execute();
-                            delete S;
+
+                if (!lineError) {
+                    string dispVar = tmpLine.substr(pos1,pos2-pos1);
+
+                    int* tmpVar = findIntVariable(dispVar);
+                    bool isText = false;
+                    bool stopSignal = false;
+                    bool resetSignal = false;
+                    if (tmpVar == NULL) {
+                        if ((tmpLine.compare(pos1,1,"'")==0) && (tmpLine.compare(pos2-1,1,"'")==0)) {
+                            isText = true;
+                        } else if (dispVar.compare("stop") == 0) {
+                            stopSignal = true;
+                        } else if (dispVar.compare("reset") == 0) {
+                            resetSignal = true;
                         } else {
-                            textDisplay << "Error: sound file names must be 20 characters or less.\r\n";
+                            textDisplay << "Error: variable input to sound() does not exist\r\n";
                             lineError = true;
                         }
-                    } else {
-                        textDisplay << "Error: variable input to sound() not yet supported.  Enter a string in single quotes.\r\n";
+                    }
+                    action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS);
+                    if (tmpAction == NULL) {
+                        textDisplay << "Error: no memory slots available.\r\n";
                         lineError = true;
                     }
-
-                } else if (!lineError && (blockDepth > 0) ){
-                    //the disp function was put inside a block
-                    textDisplay.debug("Sound statement\r\n");
-                    if (stopSignal) {
-                        sSound* sPtr = system->createNewSoundAction();
-                        sPtr->setPlayback(false);
-                        //action* tmpAction = new action(sPtr);
-                        tmpAction->set(sPtr);
-                        tmpEventPtrArray.back()->addAction(tmpAction);
-                    } else if (resetSignal) {
-                        sSound* sPtr = system->createNewSoundAction();
-                        sPtr->setReset();
-                        //action* tmpAction = new action(sPtr);
-                        tmpAction->set(sPtr);
-                        tmpEventPtrArray.back()->addAction(tmpAction);
-                    } else if (isText) {
-
-                        if (pos2-pos1-2 <= 20) {
+                    if (!lineError && (blockDepth == 0)) {
+                        //we are not inside a block structure, so play sound now
+                        if (stopSignal) {
+                            sSound* S = system->createNewSoundAction();
+                            S->setPlayback(false);
+                            S->execute();
+                            delete S;
+                        } else if (resetSignal) {
+                            sSound* S = system->createNewSoundAction();
+                            S->setReset();
+                            S->execute();
+                            delete S;
+                        } else if (isText) {
+
+                            if (pos2-pos1-2 <= 20) {
+
+                                sSound* S = system->createNewSoundAction();
+                                S->setFile(tmpLine.substr(pos1+1,pos2-pos1-2));
+                                S->execute();
+                                delete S;
+                            } else {
+                                textDisplay << "Error: sound file names must be 20 characters or less.\r\n";
+                                lineError = true;
+                            }
+                        } else {
+                            textDisplay << "Error: variable input to sound() not yet supported.  Enter a string in single quotes.\r\n";
+                            lineError = true;
+                        }
+
+                    } else if (!lineError && (blockDepth > 0) ){
+                        //the disp function was put inside a block
+                        textDisplay.debug("Sound statement\r\n");
+                        if (stopSignal) {
                             sSound* sPtr = system->createNewSoundAction();
-                            sPtr->setFile(tmpLine.substr(pos1+1,pos2-pos1-2));
+                            sPtr->setPlayback(false);
                             //action* tmpAction = new action(sPtr);
                             tmpAction->set(sPtr);
                             tmpEventPtrArray.back()->addAction(tmpAction);
+                        } else if (resetSignal) {
+                            sSound* sPtr = system->createNewSoundAction();
+                            sPtr->setReset();
+                            //action* tmpAction = new action(sPtr);
+                            tmpAction->set(sPtr);
+                            tmpEventPtrArray.back()->addAction(tmpAction);
+                        } else if (isText) {
+
+                            if (pos2-pos1-2 <= 20) {
+                                sSound* sPtr = system->createNewSoundAction();
+                                sPtr->setFile(tmpLine.substr(pos1+1,pos2-pos1-2));
+                                //action* tmpAction = new action(sPtr);
+                                tmpAction->set(sPtr);
+                                tmpEventPtrArray.back()->addAction(tmpAction);
+                            } else {
+                                textDisplay << "Error: sound file names must be 20 characters or less.\r\n";
+                                lineError = true;
+                            }
                         } else {
-                            textDisplay << "Error: sound file names must be 20 characters or less.\r\n";
+                            textDisplay << "Error: variable input to sound() not yet supported.  Enter a string in single quotes.\r\n";
                             lineError = true;
                         }
-                    } else {
-                        textDisplay << "Error: variable input to sound() not yet supported.  Enter a string in single quotes.\r\n";
-                        lineError = true;
+
+
                     }
-
-
                 }
 
             } else if (tokens[i].find("volume(") != std::string::npos) {
@@ -2406,65 +2416,71 @@
                 wholeLineEvaluated = true;
                 int pos1 = tmpLine.find("volume(")+7;
                 int pos2 = tmpLine.find_first_of(")",pos1);
-                string dispVar = tmpLine.substr(pos1,pos2-pos1);
-
-                int* tmpVar = findIntVariable(dispVar);
-                bool isText = false;
-                if (tmpVar == NULL) {
-                    if (isNumber(dispVar)) {
-                        isText = true;
-                    } else {
-                        textDisplay << "Error: variable input to volume() does not exist\r\n";
-                        lineError = true;
-                    }
-                }
-                action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS);
-                if (tmpAction == NULL) {
-                    textDisplay << "Error: no memory slots available.\r\n";
+                if (pos2 == std::string::npos) {
+                    textDisplay << "Syntax error: expected a ')'\r\n";
                     lineError = true;
                 }
-                if (!lineError && (blockDepth == 0)) {
-                    //we are not inside a block structure, so play sound now
-                    if (isText) {
-                        int newVolume = atoi(dispVar.data());
-                        if ((newVolume >=0)&&(newVolume <= 255)) {
-                            sSound* S = system->createNewSoundAction();
-                            S->setVolume(newVolume);
-                            S->execute();
-                            delete S;
+                if (!lineError) {
+                    string dispVar = tmpLine.substr(pos1,pos2-pos1);
+
+                    int* tmpVar = findIntVariable(dispVar);
+                    bool isText = false;
+                    if (tmpVar == NULL) {
+                        if (isNumber(dispVar)) {
+                            isText = true;
                         } else {
-                            textDisplay << "Error: sound volume must be between 0 and 255 .\r\n";
+                            textDisplay << "Error: variable input to volume() does not exist\r\n";
                             lineError = true;
                         }
-                    } else {
-                        sSound* S = system->createNewSoundAction();
-                        S->setVolume(tmpVar);
-                        S->execute();
-                        delete S;
+                    }
+                    action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS);
+                    if (tmpAction == NULL) {
+                        textDisplay << "Error: no memory slots available.\r\n";
+                        lineError = true;
                     }
-
-                } else if (!lineError && (blockDepth > 0) ){
-                    //the disp function was put inside a block
-                    textDisplay.debug("Volume statement\r\n");
-                    if (isText) {
-                        int newVolume = atoi(dispVar.data());
-
-                        sSound* sPtr = system->createNewSoundAction();
-                        sPtr->setVolume(newVolume);
-
-                        //action* tmpAction = new action(sPtr);
-                        tmpAction->set(sPtr);
-                        tmpEventPtrArray.back()->addAction(tmpAction);
-
-                    } else {
-                        sSound* sPtr = system->createNewSoundAction();
-                        sPtr->setVolume(tmpVar);
-                        //action* tmpAction = new action(sPtr);
-                        tmpAction->set(sPtr);
-                        tmpEventPtrArray.back()->addAction(tmpAction);
+                    if (!lineError && (blockDepth == 0)) {
+                        //we are not inside a block structure, so play sound now
+                        if (isText) {
+                            int newVolume = atoi(dispVar.data());
+                            if ((newVolume >=0)&&(newVolume <= 255)) {
+                                sSound* S = system->createNewSoundAction();
+                                S->setVolume(newVolume);
+                                S->execute();
+                                delete S;
+                            } else {
+                                textDisplay << "Error: sound volume must be between 0 and 255 .\r\n";
+                                lineError = true;
+                            }
+                        } else {
+                            sSound* S = system->createNewSoundAction();
+                            S->setVolume(tmpVar);
+                            S->execute();
+                            delete S;
+                        }
+
+                    } else if (!lineError && (blockDepth > 0) ){
+                        //the disp function was put inside a block
+                        textDisplay.debug("Volume statement\r\n");
+                        if (isText) {
+                            int newVolume = atoi(dispVar.data());
+
+                            sSound* sPtr = system->createNewSoundAction();
+                            sPtr->setVolume(newVolume);
+
+                            //action* tmpAction = new action(sPtr);
+                            tmpAction->set(sPtr);
+                            tmpEventPtrArray.back()->addAction(tmpAction);
+
+                        } else {
+                            sSound* sPtr = system->createNewSoundAction();
+                            sPtr->setVolume(tmpVar);
+                            //action* tmpAction = new action(sPtr);
+                            tmpAction->set(sPtr);
+                            tmpEventPtrArray.back()->addAction(tmpAction);
+                        }
+
+
                     }
-
-
                 }
                 //clock statement used to is used to control the clock-------------------------
                 //example: clock(reset); clock(slave); clock(standalone)
@@ -2537,7 +2553,7 @@
                 int pos1 = tmpLine.find("disp(")+5;
                 int pos2 = tmpLine.find_first_of(")",pos1);
                 if (pos2 == std::string::npos) {
-                    textDisplay <<"Error: expected a ) character\r\n";
+                    textDisplay <<"Syntax error: expected a ')'\r\n";
                     lineError = true;
                 }
 
@@ -2626,7 +2642,7 @@
                 if (!lineError) {
                     int funcNum = atoi(tmpLine.substr(pos1,pos2-pos1).data());
                     if ((funcNum > 0) && (funcNum < NUMFUNCTIONS+1)) {
-                        if (functionSpotTaken[funcNum-1] && functionEventArray[funcNum]->isUsed) {
+                        if (functionSpotTaken[funcNum-1] && functionEventArray[funcNum-1]->isUsed) {
 
 
                         } else {
@@ -2802,6 +2818,7 @@
 
                 //updates command toggles the DIO update messages upon a change------------------
                 //examples: updates on; updates off
+                //examples: updates on 3; updates off 3
             } else if (tokens[i].compare("updates") == 0) {
                 if (ifBlockInit || whileBlockInit || elseFlag || expectingDoStatement) {
                     textDisplay << "Error: expected a 'do' statement\r\n";