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:
3:d7b0a0890d96
Parent:
2:35266b266eaa
Child:
4:abee20c0bf2a
--- a/behave.cpp	Wed Jun 03 23:41:16 2015 +0000
+++ b/behave.cpp	Sat Oct 10 22:37:17 2015 +0000
@@ -2,8 +2,8 @@
 #include "hardwareInterface.h"
 #include <ctype.h>
 #include <sstream>
-#include <string.h>
-#include <cstdlib>
+//#include <string.h>
+//#include <cstdlib>
 
 using namespace std;
 
@@ -119,6 +119,7 @@
 void mainLoop::exec() {
     bool digitalInChanged = false;
     bool digitalOutChanged = false;
+    bool *ignoreUpdatePorts = hardware->getIgnoreUpdates();
     uint32_t changeTime;
     timeKeeper = 0; //set main clock to 0;
 
@@ -218,11 +219,15 @@
         digitalInChanged = false;
         digitalOutChanged = false;
         changeTime = timeKeeper;
+
         for (int i = 0; i < NUMPORTS; i++) {
 
             if (ports[i].update()) {
 
-                digitalInChanged = true;
+                if (!ignoreUpdatePorts[i]) {
+                    //Only trigger an output update to the serial port if ignore is false
+                    digitalInChanged = true;
+                }
                 changeTime = min(changeTime,ports[i].lastChangeTime);
 
 
@@ -287,8 +292,10 @@
         //check for shortcut triggers
         numTriggersToProcess = hardware->getPendingFunctionTriggers(shortcutTriggers);
         for (int i = 0; i < numTriggersToProcess; i++) {
-            if (shortcutTriggers[i] < NUMTRIGGERACTIONS && functionSpotTaken[i]) {
-                functionEventArray[i]->execute();
+            textDisplay << "Trigger function " << shortcutTriggers[i]+1 << "\r\n";
+            if ((shortcutTriggers[i] < NUMTRIGGERACTIONS) && functionSpotTaken[shortcutTriggers[i]]) {
+                //textDisplay << "Executing function array index " << shortcutTriggers[i] << "\r\n";
+                functionEventArray[shortcutTriggers[i]]->execute();
             }
         }
 
@@ -510,16 +517,17 @@
                     if (triggerDownEventPtr != NULL) {triggerDownEventPtr->execute();}
                 }
             }
-            
+
             lastInState = inState;
             inPin->lastUpEvent.triggered = false;
             inPin->lastDownEvent.triggered = false;
-            
+
             inPin->setUpdate(false); //This also checks if there were any buffered changes that occured
 
+
         }
 
-        
+
 
     }
 
@@ -582,9 +590,9 @@
 void displayAction::execute() {
 
     if (dVariable != NULL) {
-        textDisplay << timeKeeper << " " << dText << " = " << *dVariable << "\r\n";
+        textDisplay << timeKeeper << " " << dText.c_str() << " = " << *dVariable << "\r\n";
     } else {
-        textDisplay << timeKeeper << " " << dText << "\r\n";
+        textDisplay << timeKeeper << " " << dText.c_str() << "\r\n";
     }
 }
 
@@ -2280,7 +2288,7 @@
                         blockDepth = blockDepth - 1;
                     }
 
-                    while ((tmpEventPtrArray.back()->blockType == 3) || (tmpEventPtrArray.back()->blockType == 4)){
+                    while ((tmpEventPtrArray.back()->blockType == 3) || (tmpEventPtrArray.back()->blockType == 4) || (tmpEventPtrArray.back()->blockType == 6) || (tmpEventPtrArray.back()->blockType == 7) || (tmpEventPtrArray.back()->blockType == 8)){
                         tmpEventPtrArray.pop_back(); //recursively remove the pointers to all else blocks
                     }
                     tmpEventPtrArray.pop_back(); //remove the pointer to the finished block
@@ -2526,10 +2534,10 @@
                     textDisplay <<"Error: expected a ) character\r\n";
                     lineError = true;
                 }
-                
+
                 if (!lineError) {
                     string dispVar = tmpLine.substr(pos1,pos2-pos1);
-                    
+
                     int* tmpVar = findIntVariable(dispVar);
                     bool isText = false;
                     if (tmpVar == NULL) {
@@ -2545,10 +2553,10 @@
                         textDisplay << "Error: no memory slots available.\r\n";
                         lineError = true;
                     }
-                    
+
                     if (!lineError && (blockDepth == 0)) {
                         //we are not inside a block structure, so display now
-                        
+
                         if (isText) {
                             //displayAction* dPtr = new displayAction(tmpLine.substr(pos1+1,pos2-pos1-2), pcPtr);
                             dPtr->set(tmpLine.substr(pos1+1,pos2-pos1-2));
@@ -2562,7 +2570,7 @@
                             //delete dPtr;
                             dPtr->release();
                         }
-                        
+
                     } else if (!lineError && (blockDepth > 0) ){
                         //the disp function was put inside a block
                         textDisplay.debug("Display statement\r\n");
@@ -2584,8 +2592,8 @@
                             //action* tmpAction = new action(dPtr);
                             tmpEventPtrArray.back()->addAction(tmpAction);
                         }
-                        
-                        
+
+
                     }
                 }
                 //----------------------------------------------
@@ -2788,6 +2796,7 @@
                     lineError = true;
                 }
                 bool stream = true;
+                int specifiedPort = -1;
                 if ((!lineError)&&(i+1 < sz)){
                     if (tokens[i+1].compare("on") == 0) {
                         stream = true;
@@ -2798,14 +2807,47 @@
                         lineError = true;
                     }
                 }
+                if ((!lineError) && (i+2 < sz)) {
+                    //There is a port specified
+                    //int pos1 = tmpLine.find("trigger(")+8;
+                    //int pos2 = tmpLine.find_first_of(")",pos1);
+                    int tempPort = atoi(tokens[i+2].data());
+                    
+                    if (tempPort > 0) {
+                        specifiedPort = tempPort-1;
+                    } else {
+                        textDisplay << "Error: 'updates' useage: 'updates on [port]' or 'updates off [port]'\r\n";
+                        lineError = true;
+                    }
+                    i++;
+                }
                 i++;
                 if ((!lineError) && (blockDepth == 0)) {
                     if (stream) {
+                        //applies to all;
                         broadCastStateChanges = true;
+                        if (specifiedPort > -1) {
+                            system->setPortUpdatesOn(specifiedPort);
+                        } else {
+                            for (int i=0;i<NUMPORTS;i++) {
+                                system->setPortUpdatesOn(i);
+                            }
+                        }
                     } else {
-                        broadCastStateChanges = false;
+                        if (specifiedPort > -1) {
+                            system->setPortUpdatesOff(specifiedPort);
+                        } else {
+                            //applies to all
+                            //broadCastStateChanges = false;
+                            for (int i=0;i<NUMPORTS;i++) {
+                                system->setPortUpdatesOff(i);
+                            }
+                        }
+
                     }
                 } else if ((!lineError) && (blockDepth > 0)) {
+                    //Inside a block-- current no support here to specify a port
+
                     if (stream) {
                         //action* tmpAction = new action(3); //code 3 = turn on updates
                         action* tmpAction = findFirstUnUsed(actionBlock, NUMACTIONS);
@@ -2882,6 +2924,10 @@
                             delete globalVariables.back();
                             globalVariables.pop_back();
                         }
+                        broadCastStateChanges = true;
+                        for (int i=0;i<NUMPORTS;i++) {
+                            system->setPortUpdatesOn(i);
+                        }
                     }
 
                     //clear callbacks, functions, and queue
@@ -3576,17 +3622,20 @@
 
         //if there was an error, we quit compiling the code
         if (lineError) {
+
             textDisplay << "Line text: ";
             while (!tokens.empty()) {
                 textDisplay << tokens.front()<< " ";
                 tokens.erase(tokens.begin());
             }
             textDisplay << "\r\n";
+
             currentBlock.resetBuffer();
             while (!tokens.empty()) {
                 tokens.pop_back();
             }
-            delete tmpEvent;
+            //delete tmpEvent;
+            tmpEvent->release();
         } else {
 
             while (!tokens.empty()) {
@@ -3766,7 +3815,7 @@
         multiplierInt = -1;
 
     }
-    
+
     tmpVar = findIntVariable(expression,0,beforeEqualLoc); //returns pointer to the variable
     if (findStringLoc(expression,"portout[",0,beforeEqualLoc) != -1) {  //set the output of a digital port
         textDisplay.debug("Portout assignment\r\n");