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:
7:5fe7329751d4
Parent:
4:abee20c0bf2a
Child:
8:872b843a3053
diff -r 6a6761a47951 -r 5fe7329751d4 behave.h
--- a/behave.h	Fri Jun 10 21:22:34 2016 +0000
+++ b/behave.h	Tue Feb 07 18:45:25 2017 +0000
@@ -56,6 +56,89 @@
     bool triggered;
 };*/
 
+class AbstractPort {
+
+public:
+    AbstractPort();
+
+    enum Direction{in,out,none};
+    enum DataType{digital, analog};
+
+    Direction portDir;
+    DataType portType;
+
+    virtual void write(int outVal);
+    virtual int  read() = 0; //Must be defined in inheriting class
+    virtual void setThresh(int threshVal);
+
+    uint32_t lastChangeTime;
+    uint32_t lastOutChangeTime;
+
+    bool update(); //called from the main loop
+    void setTriggerUpEvent(event* eventInput); //attahces a routine to an upward change
+    void setTriggerDownEvent(event* eventInput); //attahces a routine to a downward change
+    void clearTriggerEvents();
+    void setReportUpdates(bool report);
+    int getLastChangeState();
+    uint32_t getTimeSinceLastChange();
+
+    int state;
+    bool outStateChanged;
+
+    event* triggerUpEventPtr;
+    event* triggerDownEventPtr;
+
+protected:
+    virtual bool checkForChange();
+    bool hasTriggerFunction;
+    bool reportUpdates;
+
+    int lastInState;
+    int thresh;
+    uint32_t lastChangeInterval;
+
+private:
+
+};
+
+class AnalogPort: public AbstractPort {
+public:
+    AnalogPort();
+
+    void init(sAnalogIn* IP);
+    void init(sAnalogOut* OP);
+
+    void set(int outVal);
+    int  read();
+    void setThresh(int threshVal);
+    void write(int outVal);
+
+private:
+    sAnalogOut* outPin;
+    sAnalogIn*  inPin;
+
+};
+
+class DigitalPort: public AbstractPort {
+public:
+    DigitalPort();
+
+    void init(sDigitalIn* IP);
+    void init(sDigitalOut* OP);
+
+    void set(int outVal);
+    int  read();
+    void write(int outVal);
+
+protected:
+    bool checkForChange();
+
+private:
+    sDigitalOut* outPin;
+    sDigitalIn*  inPin;
+
+};
+
 
 //The digitalPort object directly controls and keeps data about the port. Each port has
 //one digital out and one digital in.
@@ -98,6 +181,8 @@
 };
 
 
+
+
 //an intVariable contains an integer value and the name of that variable within the script
 class intVariable {
 
@@ -214,7 +299,7 @@
 
 };
 
-//portMessage is an action to change a digital port.  So far, You can only change the digital out (0 or 1)
+//portMessage is an action to change a port state.
 class portMessage {
 public:
 
@@ -222,7 +307,7 @@
     //portMessage(digitalPort* portIn, int whichToSet, int value); //whichToSet: 1 DigitalOut; 2 State
     //void setMessage(digitalPort* portIn, int whichToSet, int value); //whichToSet: 1 DigitalOut; 2 State
     //portMessage(int* portIn, int whichToSet, int value); //whichToSet:
-    void setMessage(int* portIn, int whichToSet, int value, digitalPort* portVector); //whichToSet:
+    void setMessage(int* portIn, int whichToSet, int value, AbstractPort** portVector, int portVectorLength); //whichToSet:
 
     void execute();
     void release();
@@ -232,8 +317,9 @@
     int whichToSet; //hard coded port number
     int* port; //alternative variable port number
     int value;
+    int vectorLength;
 
-    digitalPort* portVector;
+    AbstractPort** portVector;
 
 };
 
@@ -291,19 +377,19 @@
 
 public:
     intCompare();
-    intCompare(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse);
-    intCompare(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse);
+    intCompare(AbstractPort* portInput, const char* cmpString, int cmpValInput, int whichToUse);
+    intCompare(AbstractPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse);
     intCompare(int* intVarInput, const char* cmpString, int cmpValInput);
     intCompare(int* intVarInput, const char* cmpString, int* cmpIntVarInput);
     intCompare(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput);
-    intCompare(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse);
+    intCompare(AbstractPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse);
 
-    void set(digitalPort* portInput, const char* cmpString, int cmpValInput, int whichToUse);
-    void set(digitalPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse);
+    void set(AbstractPort* portInput, const char* cmpString, int cmpValInput, int whichToUse);
+    void set(AbstractPort* portInput, const char* cmpString, int* cmpIntVarInput, int whichToUse);
     void set(int* intVarInput, const char* cmpString, int cmpValInput);
     void set(int* intVarInput, const char* cmpString, int* cmpIntVarInput);
     void set(int* intVarInput, const char* cmpString, intOperation* cmpIntOpInput);
-    void set(digitalPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse);
+    void set(AbstractPort* portInput, const char* cmpString, intOperation* cmpIntOpInput, int whichToUse);
 
     void release();
 
@@ -312,7 +398,7 @@
     bool isUsed;
 
 private:
-    digitalPort* port;
+    AbstractPort* port;
     int* portValPtr;
     int* cmpVal;
     int* intVal;
@@ -480,7 +566,7 @@
 //Only the final line in a callback block should have a semicolon.
 class scriptStream {
 public:
-    scriptStream(digitalPort* portVectorInput, int numPortsInput, eventQueue* queueInput, sSystem* system);
+    scriptStream(AbstractPort** portVectorInput, int numPortsInput, eventQueue* queueInput, sSystem* system);
     void parseBlock(); //Parses everything since the last semicolon was found
     void addLineToCurrentBlock(char* lineInput); // if the line did not end with a semicolon, add it to the current block
 
@@ -531,12 +617,27 @@
     //vector<functionItem*> functionArray; //any blocks declared outsite callback blocks are stored here
     //list<string> currentBlock;
     blockBuffer currentBlock;
-    digitalPort* portVector;
     sSystem* system;
 
+    AbstractPort** portVector;
+    int numPorts;
 
+    AbstractPort* digInPortVector[NUMDIGINPORTS];
+    uint8_t digInLookup[NUMDIGINPORTS];
+    int numDigInPorts;
 
-    int numPorts;
+    AbstractPort* digOutPortVector[NUMDIGOUTPORTS];
+    uint8_t digOutLookup[NUMDIGOUTPORTS];
+    int numDigOutPorts;
+
+    AbstractPort* anInPortVector[NUMANINPORTS];
+    uint8_t anInLookup[NUMANINPORTS];
+    int numAnInPorts;
+
+    AbstractPort* anOutPortVector[NUMANOUTPORTS];
+    uint8_t anOutLookup[NUMANOUTPORTS];
+    int numAnOutPorts;
+
     eventQueue* queuePtr;
 
 };
@@ -558,8 +659,13 @@
     sSystem *hardware; //hardware interface
     sSerialPort *pc; //communication to computer
     char buffer[256];
-    digitalPort ports[NUMPORTS];
+    //digitalPort ports[NUMPORTS];
 
+    DigitalPort digInPorts[NUMDIGINPORTS];
+    DigitalPort digOutPorts[NUMDIGOUTPORTS];
+    AnalogPort anInPorts[NUMANINPORTS];
+    AnalogPort anOutPorts[NUMANOUTPORTS];
 
+    AbstractPort* ports[NUMDIGINPORTS+NUMDIGOUTPORTS+NUMANINPORTS+NUMANOUTPORTS];
 
 };