Add I2CSlave to ov580 master.

Dependencies:   MorseGenerator2

Revision:
8:10ffd42a3921
Parent:
7:9c77eaad5102
Child:
9:69e0bbfdb62b
--- a/main.cpp	Mon Oct 29 00:04:10 2018 +0000
+++ b/main.cpp	Tue Oct 30 19:33:05 2018 +0000
@@ -111,7 +111,8 @@
 EventQueue queue;
 
 /*          TIMOUT          */
-//Timeout timeout;
+Timeout dot_timeout;
+Timeout flood_timeout;
 
 /*          INTERRUPTS      */
 //create interupts
@@ -134,9 +135,6 @@
 DigitalIn vcselFault(p25,PullNone);
 DigitalIn killVcsel(p26,PullNone);  // vcselFault is 1V8 instead of 3V3
 
-DigitalInOut ovsda(ov580_sda);
-
-
 
 /*          REGISTERS       */
 static uint8_t LM36011_addr = 0x64 << 1;  //0xC8
@@ -179,11 +177,14 @@
 bool stacked = false;
 bool emitter_status_dot = false;
 char rcv_buffer[3] = {0,0,0};
+int dot_counter = 0;
 bool dot_on = false;
+int flood_counter = 0;
 bool flood_on = false;
 bool once = false;
-//bool in_app = true;
-//int stack_counter = 0;
+bool in_app = false;
+int app_counts_required = 10;
+int stacked_counter = 0;
 
 /*          FUNCTIONS       */
 
@@ -194,18 +195,21 @@
     flood_I2C.write(LM36011_addr,lm_off,2,false);
 }
 
+// WAI
 void write_dot()
 {
     flood_I2C.write(LM36011_addr,lm_off,2,false);
     dot_I2C.write(LM36011_addr,lm_on,2,false);
 }
 
+// WAI
 void write_flood()
 {
     dot_I2C.write(LM36011_addr,lm_off,2,false);
     flood_I2C.write(LM36011_addr,lm_on,2,false);
 }
 
+// WAI
 void write_pulsed()
 {
     if(emitter_status_dot) {
@@ -215,80 +219,146 @@
     }
 }
 
+// WAI
 void write_once()
 {
-    if(stacked) {
-        write_pulsed();
-    } else {
-        if(dot_on) {
-            write_dot();
-        } else if(flood_on) {
-            write_flood();
+    if(in_app) {
+        if(stacked_counter > app_counts_required) {
+            write_pulsed();
+            write_pulsed();
         } else {
-            write_off();
+            if(dot_on) {
+                write_dot();
+                write_dot();
+            } else if(flood_on) {
+                write_flood();
+                write_flood();
+            } else {
+                write_off();
+                write_off();
+            }
         }
+    } else {
+        write_off();
+        write_off();
     }
-    
-    write_pulsed();
-    dot_on = false;
-    flood_on = false;
 }
 
-// WAI
-void stack_check()
+void dot_watchdog()
 {
-    stacked = dot_on && flood_on;
+    dot_on = false;
+    dot_counter = 0;
+    stacked_counter = 0;
+    stacked = false;
+    queue.call(&write_once);
+}
+
+void flood_watchdog()
+{
+    flood_on = false;
+    flood_counter = 0;
+    stacked_counter = 0;
+    stacked = false;
+    queue.call(&write_once);
+}
+
+void dot_falling_edge()
+{
 
     if(stacked) {
-        // toggle dot / flood indicator on PWM 0
         emitter_status_dot = !emitter_status_dot;
     } else {
         emitter_status_dot = dot_on;
     }
+    // indicate status dot
+    pwm_0_output = emitter_status_dot;
+    // indicate VCSEL fault if it exists
+    pwm_1_output = !vcselFault.read();
+
+    // write once
+    queue.call(&write_once);
+
+    // timeout for app exit
+    dot_timeout.attach(&dot_watchdog,.05);
 }
 
-void lightsOn()
+void flood_falling_edge()
 {
-    // toggle once
-    once = !once;
-    if(once) {
-        // check the status of what strobes are firing
-        stack_check();
-        // change dot/flood indicator
-        pwm_0_output = emitter_status_dot;
-        // indicate VCSEL fault if it exists
-        pwm_1_output = !vcselFault.read();
-        // timeout for app exit
-        //timeout.attach(&write_off,.5);
-        // write once
-        queue.call(&write_once);
+    if (!stacked) {
+        emitter_status_dot = dot_on;
     }
-}
+    // indicate status dot
+    pwm_0_output = emitter_status_dot;
+    // indicate VCSEL fault if it exists
+    pwm_1_output = !vcselFault.read();
+    // timeout for app exit
+    //timeout.attach(&write_off,.5);
+    // write once
+    queue.call(&write_once);
 
+    // timeout for app exit
+    flood_timeout.attach(&flood_watchdog,.05);
 
-
+}
 
 void dot_check()
 {
-    dot_on = true;
+    dot_timeout.detach();
 
-    once = false;
-    //timeout.detach();
+    dot_counter ++;
+    if(dot_counter > app_counts_required) {
+        dot_on = true;
+    } else {
+        dot_on = false;
+    }
+    stacked = strobe0.read();
+    if(stacked) {
+        stacked_counter++;
+    } else {
+        stacked_counter = 0;
+        flood_counter = 0;
+        flood_on = false;
+    }
+    if(!in_app) {
+        if (stacked_counter > app_counts_required) {
+            in_app = true;
+        }
+    }
 }
 
 
 void flood_check()
 {
-    flood_on = true;
+    flood_timeout.detach();
 
-    once = false;
-    //timeout.detach();
+    flood_counter ++;
+    if(flood_counter > app_counts_required) {
+        flood_on = true;
+    } else {
+        flood_on = false;
+    }
+    stacked = strobe1.read();
+    if(stacked) {
+        stacked_counter++;
+    } else {
+        stacked_counter = 0;
+        dot_counter = 0;
+        dot_on = false;
+    }
+    if(!in_app) {
+        if (stacked_counter > app_counts_required) {
+            in_app = true;
+        }
+    }
 }
 
 //     TODOS    //
 //  DONE    P0  Get illumination working
-//  P0  Get in app working
-//  P0  Get watchdog time working
+//  P0  Get OV580 I2C to look correct. Suspect open drain culprit
+//  P0  Ensure that current implementation meets the needs of data collection
+//  DONE    P0  Get in app working
+//  DONE    P0  Get watchdog time working
+//  DONE    P0      Check watchdog for working on dot and flood
 //  P1  Bluetooth OTA updates
 //  P2  Get writing working
 //  P2  Get reading working
@@ -297,9 +367,8 @@
 // main() runs in its own thread in the OS
 int main()
 {
-    ovsda.output();
-    ovsda.mode(OpenDrain);
-    wait(5);
+
+    //wait(5);
 
     Thread eventThread(osPriorityHigh);;
     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
@@ -307,16 +376,22 @@
     // set interrupts
     int_strobe_dot.rise(&dot_check);
     int_strobe_flood.rise(&flood_check);
-    int_strobe_dot.fall(&lightsOn);
-    int_strobe_flood.fall(&lightsOn);
+    int_strobe_dot.fall(&dot_falling_edge);
+    int_strobe_flood.fall(&flood_falling_edge);
+
+    while(!in_app) {
+        red = 0;
+    }
+    green = !in_app;
+    red = in_app;
 
     // set I2C Frequency to 400kHz
     //flood_I2C.frequency(400000);      not needed cause of set defaults
     //dot_I2C.frequency(400000);        not needed cause of set defaults
-    ov_I2C.frequency(400000);
+    //ov_I2C.frequency(400000);
 
     //TODO get i2c writes working
-    ov_I2C.address(0xC0);
+    //ov_I2C.address(0xC0);
 
     // write safety
     flood_I2C.write(LM36011_addr,lmSafety,2,false);
@@ -326,34 +401,10 @@
     flood_I2C.write(LM36011_addr,flashBrightness_flood,2,false);
     dot_I2C.write(LM36011_addr,flashBrightness_dot,2,false);
 
-    char read_buff[2] = {0,0};
-
     while (true) {
 
-        
-            int i = ov_I2C.receive();
-
-            switch(i) {
-                case I2CSlave::ReadAddressed:
-                    ov_I2C.write(read_buff[1]);
-                    break;
-                case I2CSlave::WriteGeneral:
-                    ov_I2C.read(rcv_buffer, 3);
-                    break;
-                case I2CSlave::WriteAddressed:
-                    ov_I2C.read(rcv_buffer, 3);
-                    green = 0;
-                    //dot_I2C.write(LM36011_addr,lm_on,2,false);
-                    break;
-            }
-
-            // clear buffer
-            for (int k = 0;
-                    k < 3;
-                    k++) rcv_buffer[i]=0;
-        
-        //green = !stacked;
-        //red = stacked;
+        green = !in_app;
+        red = in_app;
 
     }
 }