Program that uses the EOF sequence of Vizualeyez motion tracker to trigger an output.

Dependencies:   mbed

Revision:
1:381c49f999b8
Parent:
0:6190ebfd8112
Child:
2:7509f2c717aa
--- a/main.cpp	Thu Oct 27 07:15:49 2016 +0000
+++ b/main.cpp	Thu Oct 27 13:40:52 2016 +0000
@@ -6,8 +6,84 @@
 InterruptIn trig(PTD1);
 Timer trigtimer;
 Timeout output_timeout;
+Timeout input_timeout;
 
 
+typedef enum {TRIG_DETECT_IDLE, TRIG_LOW, TRIG_FIRST_HIGH} trig_enum_t;
+typedef enum {TRIG_EVENT_RISING, TRIG_EVENT_FALLING, TRIG_EVENT_TIMEOUT} trig_event_t;
+
+trig_enum_t trigger = TRIG_DETECT_IDLE;
+
+//Function declarations
+void input_timeout_handler(void);
+void get_new_state(trig_enum_t * trig, trig_event_t event);
+void set_output(void);
+void trig_rise_event(void);
+void trig_fall_event(void);
+
+
+
+void input_timeout_handler(void)
+{
+    get_new_state(&trigger, TRIG_EVENT_TIMEOUT);
+}
+
+void get_new_state(trig_enum_t * trig, trig_event_t event)
+{
+    const int jitter = 5;
+    switch(*trig)
+    {
+    case TRIG_DETECT_IDLE:
+    {
+        if(event == TRIG_EVENT_FALLING)
+        {
+            *trig = TRIG_LOW;
+            trigtimer.reset();
+        }
+        else // should not happen
+            *trig = TRIG_DETECT_IDLE;
+        break;
+    }
+    case TRIG_LOW:
+    {
+        if(event == TRIG_EVENT_RISING)
+        {
+            if(trigtimer.read_us() > 30 - jitter && trigtimer.read_us() < 30+jitter) //if first low period detected
+            {
+                *trig = TRIG_FIRST_HIGH;
+                input_timeout.attach_us(input_timeout_handler, 60);
+            }
+            else //pulse too short or too long
+                *trig = TRIG_DETECT_IDLE;      
+        }
+        
+        else // should not happen
+            *trig = TRIG_DETECT_IDLE;
+        break;
+    }
+    case TRIG_FIRST_HIGH:
+    {
+        if(event == TRIG_EVENT_TIMEOUT)
+        {
+           gpo.write(0);
+           output_timeout.attach_us(set_output, 1000);
+           *trig = TRIG_DETECT_IDLE;
+        }
+        
+        else // should not happen, maybe falling edge due to TOP684
+        {
+            *trig = TRIG_DETECT_IDLE;
+            input_timeout.detach();
+        }
+        break;
+    } 
+    default:
+        break;
+    }
+        
+}
+     
+
 void set_output(void)
 {
     gpo.write(1);
@@ -16,22 +92,24 @@
 
 void trig_rise_event(void)
 {
-    trigtimer.reset();
+    //trigtimer.reset();
+     get_new_state(&trigger, TRIG_EVENT_RISING);
 }
 
 
 void trig_fall_event(void)
 {
-    if(trigtimer.read_us() > 400)
-    {
-        gpo.write(0);
-        output_timeout.attach_us(set_output, 1000);
-    }
+    get_new_state(&trigger, TRIG_EVENT_FALLING);
+    //if(trigtimer.read_us() > 400)
+    //{
+    //    gpo.write(0);
+    //    output_timeout.attach_us(set_output, 1000);
+    //}
 }    
 
 int main()
 {
-    trigtimer.start();
+    //trigtimer.start();
     trig.rise(trig_rise_event);
     trig.fall(trig_fall_event);
     while (true) {