Add I2CSlave to ov580 master.

Dependencies:   MorseGenerator2

Revision:
6:85d9ad912c14
Parent:
5:60c20a7f7735
Child:
7:9c77eaad5102
--- a/main.cpp	Mon Oct 22 23:09:36 2018 +0000
+++ b/main.cpp	Thu Oct 25 23:06:06 2018 +0000
@@ -48,6 +48,21 @@
 
 */
 
+/*          TEST THIS AT SOME POINT     */
+
+/*
+if ( bEnable )
+{ NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
+NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
+}
+else
+{ NRF_TWI1->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
+ NRF_TWI0->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos; }
+*/
+
+// Probably something like this for TWIS
+// NRF_TWIS0->ENABLE = TWIS_ENABLE_ENABLE_Enabled << TWIS_ENABLE_ENABLE_Pos;
+
 /*          DEFINES         */
 
 #define version_number 0x70
@@ -61,8 +76,8 @@
 #define ov580_scl p31
 
 // define strobe pins
-#define strobe_dot p23
-#define strobe_flood p24
+#define strobe_flood p23      // strobe0
+#define strobe_dot p24    // strobe1
 
 // define pmw pins
 #define pwm_1_pin p29
@@ -73,13 +88,17 @@
 #define ledGreen p17
 #define ledBlue p13
 
+
 /*          THREAD          */
 EventQueue queue;
 
+/*          TIMOUT          */
+//Timeout timeout;
+
 /*          INTERRUPTS      */
 //create interupts
 InterruptIn int_strobe_dot(strobe_dot);
-//InterruptIn int_strobe_flood(strobe_flood);  // only need one interrupt in
+InterruptIn int_strobe_flood(strobe_flood);  // only need one interrupt in
 
 /*          I/O            */
 // initialize LEDs
@@ -129,71 +148,153 @@
 char flashBrightness_flood[2] = {brightness_reg,level_flood_max};
 
 /*          I2C             */
+//I2CSlave ov_I2C(ov580_sda,ov580_scl);
 I2C flood_I2C(flood_sda,flood_scl);
 I2C dot_I2C(dot_sda,dot_scl);
-I2CSlave ov_I2C(ov580_sda,ov580_scl);
+
 
 /*          VARIABLES       */
 bool stacked = false;
 bool emitter_status_dot = false;
-char rcv_buffer[3] = {0,0,0};
+//char rcv_buffer[3] = {0,0,0};
+bool dot_on = false;
+bool flood_on = false;
+bool once = false;
+//bool in_app = true;
+//int stack_counter = 0;
 
 /*          FUNCTIONS       */
 
-void stack_check()
+// WAI
+void write_off()
+{
+    dot_I2C.write(LM36011_addr,lm_off,2,false);
+    flood_I2C.write(LM36011_addr,lm_off,2,false);
+}
+
+void write_dot()
 {
-    // see if stacked
-    stacked = strobe1.read();
-    // toggle dot / flood indicator on PWM 0
-    emitter_status_dot = !emitter_status_dot;
+    flood_I2C.write(LM36011_addr,lm_off,2,false);
+    dot_I2C.write(LM36011_addr,lm_on,2,false);
+}
+
+void write_flood()
+{
+    dot_I2C.write(LM36011_addr,lm_off,2,false);
+    flood_I2C.write(LM36011_addr,lm_on,2,false);
+}
+
+void write_pulsed()
+{
+    if(emitter_status_dot) {
+        write_dot();
+    } else {
+        write_flood();
+    }
 }
 
 void write_once()
 {
+    // write dot
+    /*
     if(stacked) {
-        if(emitter_status_dot) {
-            flood_I2C.write(LM36011_addr,lm_off,2,false);
-            dot_I2C.write(LM36011_addr,lm_on,2,false);
+        write_pulsed();
+    } else {
+        if(dot_on) {
+            write_dot();
+        } else if(flood_on) {
+            write_flood();
         } else {
-            dot_I2C.write(LM36011_addr,lm_off,2,false);
-            flood_I2C.write(LM36011_addr,lm_on,2,false);
+            write_off();
         }
+    }
+    */
+    write_pulsed();
+    dot_on = false;
+    flood_on = false;
+}
+
+// WAI
+void stack_check()
+{
+    stacked = dot_on && flood_on;
+
+    if(stacked) {
+        // toggle dot / flood indicator on PWM 0
+        emitter_status_dot = !emitter_status_dot;
     } else {
-        dot_I2C.write(LM36011_addr,lm_off,2,false);
-        flood_I2C.write(LM36011_addr,lm_off,2,false);
+        emitter_status_dot = dot_on;
     }
 }
 
 void lightsOn()
 {
-    // change dot/flood indicator
-    pwm_0_output = emitter_status_dot;
-    // indicate VCSEL fault if it exists
-    pwm_1_output = !vcselFault.read();
-    queue.call(&write_once);
+    // 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);
+    }
 }
 
 
 
 
+void dot_check()
+{
+    dot_on = true;
+
+    once = false;
+    //timeout.detach();
+}
+
+
+void flood_check()
+{
+    flood_on = true;
+
+    once = false;
+    //timeout.detach();
+}
+
+//     TODOS    //
+//  P0  Get illumination working
+//  P0  Get in app working
+//  P0  Get watchdog time working
+//  P1  Bluetooth OTA updates
+//  P2  Get writing working
+//  P2  Get reading working
+//
+
 // main() runs in its own thread in the OS
 int main()
 {
+    wait(5);
+
     Thread eventThread(osPriorityHigh);;
     eventThread.start(callback(&queue, &EventQueue::dispatch_forever));
 
     // set interrupts
-    int_strobe_dot.rise(&stack_check);
+    int_strobe_dot.rise(&dot_check);
+    int_strobe_flood.rise(&flood_check);
     int_strobe_dot.fall(&lightsOn);
-
-    wait(3);
+    int_strobe_flood.fall(&lightsOn);
 
     // set I2C Frequency to 400kHz
     flood_I2C.frequency(400000);
     dot_I2C.frequency(400000);
-    ov_I2C.frequency(400000);
+    //ov_I2C.frequency(400000);
 
-    ov_I2C.address(0x20);
+    //TODO get i2c writes working
+    //ov_I2C.address(0x10);
 
     // write safety
     flood_I2C.write(LM36011_addr,lmSafety,2,false);
@@ -203,44 +304,34 @@
     flood_I2C.write(LM36011_addr,flashBrightness_flood,2,false);
     dot_I2C.write(LM36011_addr,flashBrightness_dot,2,false);
 
-    char read_buff[2] = {0,0};
+    //char read_buff[2] = {0,0};
 
     while (true) {
 
-        int i = ov_I2C.receive();
+        /*
+            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.stop();
-
-                ov_I2C.read(rcv_buffer, 3);
-                /*
-                if ((int)rcv_buffer[0] == 1 && (int)rcv_buffer[1] == 1) {
+            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;
-                    red = 1;
-                } else {
-                    red = 0;
-                    green = 1;
-                }
-                */
-                green = 0;
-                //dot_I2C.write(LM36011_addr,lm_on,2,false);
-                break;
-        }
+                    //dot_I2C.write(LM36011_addr,lm_on,2,false);
+                    break;
+            }
 
-        // clear buffer
-        for (int k = 0;
-                k < 3;
-                k++) rcv_buffer[i]=0;
-
-        //green = !emitter_status_dot;
-        //red = emitter_status_dot;
+            // clear buffer
+            for (int k = 0;
+                    k < 3;
+                    k++) rcv_buffer[i]=0;
+        */
+        green = !stacked;
+        red = stacked;
 
     }
 }