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:
6:6a6761a47951
Parent:
5:67d67d452545
Child:
7:5fe7329751d4
--- a/behave.cpp	Thu Jan 21 14:28:46 2016 +0000
+++ b/behave.cpp	Fri Jun 10 21:22:34 2016 +0000
@@ -456,16 +456,56 @@
     if ((timeKeeper - lastChangeTime) > 1) { //prevents flutter triggers when button is pressed
 
         //changed = (lastInState != inState);
-
-        changed = (inPin->lastUpEvent.triggered || inPin->lastDownEvent.triggered);
+        inPin->setUpdate(true); //Once we get the state of the pin, we buffer any pin changes until we are done checking
+
+        //Every ms, we first check if a hardware trigger occured
+        //in the opposite direction of the last remembered state
+        if (lastInState == 0) {
+            changed = inPin->lastUpEvent.triggered;
+            if (changed) inState = 1;
+        } else if (lastInState == 1) {
+            changed = inPin->lastDownEvent.triggered;
+            if (changed) inState = 0;
+        }
+
+        //if not, then we read the pin state to see if it is different than the
+        //remembered state.  This is important in order to report both edges of a
+        //fast up-down event.
+        if (!changed) {
+            inState = getDigitalIn();
+            changed = (lastInState != inState);
+        }
+
+
+        //changed = (inPin->lastUpEvent.triggered || inPin->lastDownEvent.triggered);
         if (changed) {
-            inPin->setUpdate(true); //Once we get the state of the pin, we buffer any pin changes until we are done checking
-
-            inState = getDigitalIn();
+            if (inState == 1) {
+
+                lastChangeInterval = inPin->lastUpEvent.timeStamp - lastChangeTime;
+                lastChangeTime = inPin->lastUpEvent.timeStamp;
+
+
+                if (triggerUpEventPtr != NULL && triggerUpEventPtr->isUsed) {triggerUpEventPtr->execute();}
+            } else if (inState == 0) {
+
+                lastChangeInterval = inPin->lastDownEvent.timeStamp - lastChangeTime;
+                lastChangeTime = inPin->lastDownEvent.timeStamp;
+
+
+                if (triggerDownEventPtr != NULL && triggerDownEventPtr->isUsed){triggerDownEventPtr->execute();}
+            }
+
+
+
+            //inPin->setUpdate(true); //Once we get the state of the pin, we buffer any pin changes until we are done checking
+
+            //inState = getDigitalIn();
 
             //We need to ignore flutter when levers/beam breaks are triggered. So
             //if the current state if different than the last logged state, we only
             //consider the first edge that brough us to the current state
+
+            /*
             if (lastInState != inState) {
 
                 if (inState == 1) {
@@ -473,31 +513,13 @@
                     lastChangeInterval = inPin->lastUpEvent.timeStamp - lastChangeTime;
                     lastChangeTime = inPin->lastUpEvent.timeStamp;
 
-                    /*
-                    if (inPin->lastUpEvent.triggered) {
-                        //there were hardware triggers since the last main loop.  We use that time
-                        lastChangeInterval = inPin->lastUpEvent.timeStamp - lastChangeTime;
-                        lastChangeTime = inPin->lastUpEvent.timeStamp;
-                    } else {
-                        //otherwise we use the current time
-                        lastChangeInterval = timeKeeper - lastChangeTime;
-                        lastChangeTime = timeKeeper;
-                    }
-                    */
+
                     if (triggerUpEventPtr != NULL && triggerUpEventPtr->isUsed) {triggerUpEventPtr->execute();}
                 } else if (inState == 0) {
 
                     lastChangeInterval = inPin->lastDownEvent.timeStamp - lastChangeTime;
                     lastChangeTime = inPin->lastDownEvent.timeStamp;
 
-                    /*
-                    if (inPin->lastDownEvent.triggered) {
-                        lastChangeInterval = inPin->lastDownEvent.timeStamp - lastChangeTime;
-                        lastChangeTime = inPin->lastDownEvent.timeStamp;
-                    } else {
-                        lastChangeInterval = timeKeeper - lastChangeTime;
-                        lastChangeTime = timeKeeper;
-                    }*/
 
                     if (triggerDownEventPtr != NULL && triggerDownEventPtr->isUsed){triggerDownEventPtr->execute();}
                 }
@@ -520,17 +542,19 @@
                     if (triggerDownEventPtr != NULL && triggerDownEventPtr->isUsed) {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
-
+            //inPin->setUpdate(false); //This also checks if there were any buffered changes that occured
+            
 
         }
 
-
+        inPin->setUpdate(false); //This also checks if there were any buffered changes that occured
+        
 
     }
 
@@ -2642,7 +2666,7 @@
                 if (!lineError) {
                     int funcNum = atoi(tmpLine.substr(pos1,pos2-pos1).data());
                     if ((funcNum > 0) && (funcNum < NUMFUNCTIONS+1)) {
-                        if (functionSpotTaken[funcNum-1] && functionEventArray[funcNum-1]->isUsed) {
+                        if (functionSpotTaken[funcNum-1] && functionEventArray[funcNum]->isUsed) {
 
 
                         } else {