[chronojump] MultiChronopic implemented again. TODO: Need tests and read "BUGGY" message



commit 753b7d8cef3e4e72da4123bb470b9c2136838f4c
Author: Xavier de Blas <xaviblas gmail com>
Date:   Sat Dec 24 11:13:42 2016 +0100

    MultiChronopic implemented again. TODO: Need tests and read "BUGGY" message

 src/chronopic2016.cs     |   86 +++++++++++++++++++++++++++++----
 src/chronopicRegister.cs |   22 +++++++++
 src/gui/chronojump.cs    |  116 ++++++++++++++++++++++------------------------
 src/gui/guiTests.cs      |    2 +-
 4 files changed, 153 insertions(+), 73 deletions(-)
---
diff --git a/src/chronopic2016.cs b/src/chronopic2016.cs
index 0cdd787..0c21873 100644
--- a/src/chronopic2016.cs
+++ b/src/chronopic2016.cs
@@ -39,6 +39,11 @@ public class Chronopic2016
        private Chronopic cp;
        private SerialPort sp;
        private Chronopic.Plataforma platformState;
+
+       //multichronopic
+       private Chronopic cp2;
+       private SerialPort sp2;
+       private int cpDoing; //2 is for the second chronopic on multichronopic
        
        private string lastConnectedRealPort = "";
        private string lastConnectedRealSerialNumber = "";
@@ -92,7 +97,11 @@ public class Chronopic2016
 
        private void on_button_cancel_clicked(object o, EventArgs args)
        {
-               cp.AbortFlush = true;
+               if(cpDoing == 1)
+                       cp.AbortFlush = true;
+               else //(cpDoing == 2)
+                       cp2.AbortFlush = true;
+
                chronopicInit.CancelledByUser = true;
        }
        private void on_delete_event (object o, DeleteEventArgs args)
@@ -104,12 +113,15 @@ public class Chronopic2016
                on_button_cancel_clicked(new object(), new EventArgs());
        }
 
-       public void ConnectContactsReal(Gtk.Window app1, ChronopicRegisterPort crp, string labelStr)
+       public void ConnectContactsReal(Gtk.Window app1, ChronopicRegisterPort crp,
+                       int cpCount, string labelStr) //cpCount 2 is for 2nd chronopic on multichronopic
        {
                createGui(app1, labelStr);
 
                crpConnectContactsRealThread = crp;
 
+               cpDoing = cpCount;
+
                connectContactsRealThread = new Thread (new ThreadStart (connectContactsRealDo));
                GLib.Idle.Add (new GLib.IdleHandler (pulseConnectContactsReal));
 
@@ -123,11 +135,21 @@ public class Chronopic2016
 
                string message = "";
                bool success = false;
+               bool connected = false;
 
-               sp = new SerialPort(crp.Port);
-               chronopicInit = new ChronopicInit();
-               bool connected = chronopicInit.Do(1, out cp, out sp,
-                               platformState, crp.Port, out message, out success);
+               if(cpDoing == 1)
+               {
+                       sp = new SerialPort(crp.Port);
+                       chronopicInit = new ChronopicInit();
+                       connected = chronopicInit.Do(1, out cp, out sp,
+                                       platformState, crp.Port, out message, out success);
+               } else //(cpDoing == 2)
+               {
+                       sp2 = new SerialPort(crp.Port);
+                       chronopicInit = new ChronopicInit();
+                       connected = chronopicInit.Do(2, out cp2, out sp2,
+                                       platformState, crp.Port, out message, out success);
+               }
 
                LogB.Information("Ended chronopicInit.Do()");
 
@@ -216,7 +238,11 @@ public class Chronopic2016
        {
                ChronopicAuto ca = new ChronopicAutoCheck();
 
-               string str = ca.Read(sp);
+               string str = "";
+               if(cpDoing == 1)
+                       str = ca.Read(sp);
+               else
+                       str = ca.Read(sp2);
 
                isChronopicAuto = ca.IsChronopicAuto;
 
@@ -228,15 +254,22 @@ public class Chronopic2016
        // ----- change multitest firmware START ----->
 
        //change debounce time automatically on change menuitem mode (if multitest firmware)
-       public bool ChangeMultitestFirmwarePre(int thresholdValue)
+       public bool ChangeMultitestFirmwarePre(int thresholdValue, int cpCount)
        {
                LogB.Information("ChangeMultitestFirmwareMaybe (A)");
 
+               cpDoing = cpCount;
+
                //---- 1
                //bool ok = cp.Read_platform(out platformState);
                //seems better to have a new platformState:
                Chronopic.Plataforma ps;
-               bool ok = cp.Read_platform(out ps);
+               bool ok;
+               if(cpDoing == 1)
+                       ok = cp.Read_platform(out ps);
+               else
+                       ok = cp2.Read_platform(out ps);
+
                if(! ok) {
                        LogB.Information("Chronopic has been disconnected");
                        //createChronopicWindow(true, "");
@@ -288,7 +321,10 @@ public class Chronopic2016
                try {
                        ChronopicAuto ca = new ChronopicAutoChangeDebounce();
                        //write change
-                       ca.Write(sp, debounceChange);
+                       if(cpDoing == 1)
+                               ca.Write(sp, debounceChange);
+                       else
+                               ca.Write(sp2, debounceChange);
 
                        string ms = "";
                        bool success = false;
@@ -296,7 +332,11 @@ public class Chronopic2016
                        do {
                                //read if ok
                                ca = new ChronopicAutoCheckDebounce();
-                               ms = ca.Read(sp); //ms wil be eg. "50 ms"
+
+                               if(cpDoing == 1)
+                                       ms = ca.Read(sp); //ms wil be eg. "50 ms"
+                               else
+                                       ms = ca.Read(sp2); //ms wil be eg. "50 ms"
                                LogB.Information("ChronopicAutoCheckDebounce: " + ms);
 
                                if(ms.Length == 0)
@@ -340,6 +380,17 @@ public class Chronopic2016
                        LogB.Information("Disposing cp to see if helps on OSX port busy");
                        cp = null;
                }
+
+               if(sp2 != null && sp2.IsOpen) {
+                       LogB.Information("Closing sp2");
+                       sp2.Close();
+
+                       LogB.Information("Flushing cp2 to see if helps on OSX port busy");
+                       cp2.FlushByTimeOut();
+
+                       LogB.Information("Disposing cp2 to see if helps on OSX port busy");
+                       cp2 = null;
+               }
        }
 
        public Chronopic CP
@@ -351,4 +402,17 @@ public class Chronopic2016
        {
                get { return sp; }
        }
+
+       //multichronopic
+       public Chronopic CP2
+       {
+               get { return cp2; }
+       }
+
+       //connectContactsRealDo() uses 1 or 2 cpDoing. This has to be known on gui/chronojump.cs
+       //to call cp2016.ChangeMultitestFirmwarePre with 1 or 2
+       public int CpDoing
+       {
+               get { return cpDoing; }
+       }
 }
diff --git a/src/chronopicRegister.cs b/src/chronopicRegister.cs
index 46f44ea..e5444ce 100644
--- a/src/chronopicRegister.cs
+++ b/src/chronopicRegister.cs
@@ -255,6 +255,28 @@ public abstract class ChronopicRegister
                return null;
        }
 
+       //multichronopic
+       public List<ChronopicRegisterPort> GetTwoContactsConnected()
+       {
+               //create a list for two contacts crpl
+               List<ChronopicRegisterPort> l = new List<ChronopicRegisterPort>();
+               int count = 0;
+
+               foreach (ChronopicRegisterPort crp in crpl.L)
+               {
+                       if (crp.Type == ChronopicRegisterPort.Types.CONTACTS && crp.Port != "")
+                       {
+                               l.Add(crp);
+                               count ++;
+                       }
+               }
+
+               if(count == 2)
+                       return l;
+               else
+                       return null;
+       }
+
        public bool UnknownFound()
        {
                if(NumConnectedOfType(ChronopicRegisterPort.Types.UNKNOWN) > 0)
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index b6037e1..bf90beb 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -3837,13 +3837,13 @@ public partial class ChronoJumpWindow
                label_threshold.Text = threshold.GetLabel();
        }
 
-       private void changeMultitestFirmwareIfNeeded()
+       private void changeMultitestFirmwareIfNeeded(int cpCount)
        {
                //change multitest stuff
                threshold.UpdateAtDatabaseIfNeeded(getMenuItemMode());
                if(threshold.ShouldUpdateChronopicFirmware())
                {
-                       bool ok = cp2016.ChangeMultitestFirmwarePre(threshold.GetT);
+                       bool ok = cp2016.ChangeMultitestFirmwarePre(threshold.GetT, cpCount);
                        if(ok)
                                threshold.ChronopicFirmwareUpdated();
                }
@@ -3856,32 +3856,60 @@ public partial class ChronoJumpWindow
                chronopicRegisterUpdate(false);
 
                int numContacts = chronopicRegister.NumConnectedOfType(ChronopicRegisterPort.Types.CONTACTS);
-               //store a boolean in order to read info faster
-               cp2016.StoredCanCaptureContacts = (numContacts == 1);
                LogB.Information("numContacts: " + numContacts);
 
                //check if chronopics have changed
-               if(numContacts > 1)
+               if(numContacts >= 2 && radio_mode_multi_chronopic_small.Active)
                {
-                       new DialogMessage(Constants.MessageTypes.WARNING, "More than 1 Chronopic for 
jumps/runs are connected.");
+                       //will get two or null
+                       List<ChronopicRegisterPort> crpMultiList = 
chronopicRegister.GetTwoContactsConnected();
+                       //store a boolean in order to read info faster
+                       cp2016.StoredCanCaptureContacts = (crpMultiList.Count == 2);
+
+                       foreach(ChronopicRegisterPort crp in crpMultiList)
+                       {
+                               //TODO: Note this code can be BUGGY in the foreach because two iteration can 
happen while waiting user interaction
+                               int count = 1;
+                               if(cp2016.IsLastConnectedReal(crp))
+                               {
+                                       LogB.Information("Already Connected real! cp = " + count.ToString());
+                                       changeMultitestFirmwareIfNeeded(count);
+                                       //on_button_execute_test_accepted();
+                               } else {
+                                       cp2016.FakeButtonContactsRealDone.Clicked +=
+                                               new EventHandler(on_connection_contacts_real_done);
+
+                                       cp2016.ConnectContactsReal(app1, crp, count,
+                                                       "Press TEST button on Chronopic to stablish initial 
communication"); //TODO: translate this
+
+                                       /* this will start a thread and if succeeds, then will call:
+                                        * changeMultitestFirmwareIfNeeded();
+                                        * on_button_execute_test_accepted();
+                                        */
+                               }
+                               count ++;
+                       }
+
                        return;
                }
-
-               if(numContacts == 1)
+               else if(numContacts >= 1) //will get first
                {
                        ChronopicRegisterPort crp = 
chronopicRegister.ConnectedOfType(ChronopicRegisterPort.Types.CONTACTS);
+                       //store a boolean in order to read info faster
+                       cp2016.StoredCanCaptureContacts = true;
+
                        LogB.Information("Checking if Connected real!");
                        if(cp2016.IsLastConnectedReal(crp))
                        {
                                LogB.Information("Already Connected real!");
-                               changeMultitestFirmwareIfNeeded();
+                               changeMultitestFirmwareIfNeeded(1);
                                on_button_execute_test_accepted();
                        } else
                        {
                                cp2016.FakeButtonContactsRealDone.Clicked +=
                                        new EventHandler(on_connection_contacts_real_done);
 
-                               cp2016.ConnectContactsReal(app1, crp,
+                               cp2016.ConnectContactsReal(app1, crp, 1,
                                                "Press TEST button on Chronopic to stablish initial 
communication"); //TODO: translate this
 
                                /* this will start a thread and if succeeds, then will call:
@@ -3892,9 +3920,11 @@ public partial class ChronoJumpWindow
 
                        return;
                }
-
-               if(numContacts == 0)
+               else //(numContacts == 0)
                {
+                       //store a boolean in order to read info faster
+                       cp2016.StoredCanCaptureContacts = false;
+
                        /*
                         * if serial port gets opened, then a new USB connection will use different ttyUSB on 
Linux
                         * and maybe is the cause for blocking the port on OSX
@@ -3921,7 +3951,7 @@ public partial class ChronoJumpWindow
                        if(testsActive)
                                chronojumpWindowTestsNext();
                        else {
-                               changeMultitestFirmwareIfNeeded();
+                               changeMultitestFirmwareIfNeeded(cp2016.CpDoing);
                                on_button_execute_test_accepted();
                        }
                } else
@@ -5142,10 +5172,9 @@ public partial class ChronoJumpWindow
 
        private void on_multi_chronopic_start_clicked (bool canCaptureC)
        {
-               new DialogMessage(Constants.MessageTypes.WARNING, "Disabled on version 1.6.3.");
-               return;
+               //new DialogMessage(Constants.MessageTypes.WARNING, "Disabled on version 1.6.3.");
+               //return;
 
-               /*
                LogB.Information("multi chronopic accepted");
                
                bool syncAvailable = false;
@@ -5165,7 +5194,7 @@ public partial class ChronoJumpWindow
 
                //show the event doing window
                event_execute_initializeVariables(
-                       ! chronopicWin.Connected,       //is simulated
+                       ! canCaptureC,  //is simulated
                        currentPerson.UniqueID, 
                        currentPerson.Name, 
                        Catalog.GetString("Changes"),     //name of the different moments
@@ -5189,47 +5218,14 @@ public partial class ChronoJumpWindow
                //if(currentMultiChronopicType.SyncAvailable && extra_window_check_multichronopic_sync.Active)
                //      syncAvailable = true;
 
-               int numConnected = chronopicWin.NumConnected();
-
-               if(numConnected == 1)
-                       currentEventExecute = new MultiChronopicExecute(
-                                       currentPerson.UniqueID, currentPerson.Name, 
-                                       currentSession.UniqueID, currentMultiChronopicType.Name, 
-                                       chronopicWin.CP, 
-                                       syncAvailable, extra_window_check_multichronopic_delete_first.Active, 
-                                       extra_window_spin_run_analysis_distance.Value.ToString(),
-                                       app1, egd
-                                       );
-               else if(numConnected == 2)
-                       currentEventExecute = new MultiChronopicExecute(
-                                       currentPerson.UniqueID, currentPerson.Name, 
-                                       currentSession.UniqueID, currentMultiChronopicType.Name,  
-                                       chronopicWin.CP, chronopicWin.CP2, 
-                                       syncAvailable, extra_window_check_multichronopic_delete_first.Active, 
-                                       extra_window_spin_run_analysis_distance.Value.ToString(),
-                                       app1, egd
-                                       );
-               else if(numConnected == 3)
-                       currentEventExecute = new MultiChronopicExecute(
-                                       currentPerson.UniqueID, currentPerson.Name, 
-                                       currentSession.UniqueID, currentMultiChronopicType.Name,
-                                       chronopicWin.CP, chronopicWin.CP2, chronopicWin.CP3, 
-                                       syncAvailable, extra_window_check_multichronopic_delete_first.Active, 
-                                       extra_window_spin_run_analysis_distance.Value.ToString(),
-                                       app1, egd
-                                       );
-               else if(numConnected == 4)
-                       currentEventExecute = new MultiChronopicExecute(
-                                       currentPerson.UniqueID, currentPerson.Name, 
-                                       currentSession.UniqueID, currentMultiChronopicType.Name,
-                                       chronopicWin.CP, chronopicWin.CP2, chronopicWin.CP3, chronopicWin.CP4,
-                                       syncAvailable, extra_window_check_multichronopic_delete_first.Active, 
-                                       extra_window_spin_run_analysis_distance.Value.ToString(),
-                                       app1, egd
-                                       );
-
-               //if(!chronopicWin.Connected)   
-               //      currentEventExecute.SimulateInitValues(rand);
+               currentEventExecute = new MultiChronopicExecute(
+                               currentPerson.UniqueID, currentPerson.Name,
+                               currentSession.UniqueID, currentMultiChronopicType.Name,
+                               cp2016.CP, cp2016.CP2,
+                               syncAvailable, extra_window_check_multichronopic_delete_first.Active,
+                               extra_window_spin_run_analysis_distance.Value.ToString(),
+                               app1, egd
+                               );
 
 
                //mark to only get inside on_multi_chronopic_finished one time
@@ -5242,12 +5238,11 @@ public partial class ChronoJumpWindow
 //             currentEventExecute.FakeButtonRunATouchPlatform.Clicked += new 
EventHandler(on_event_execute_RunATouchPlatform);
                currentEventExecute.FakeButtonFinished.Clicked += new 
EventHandler(on_multi_chronopic_finished);
                currentEventExecute.FakeButtonThreadDyed.Clicked += new 
EventHandler(on_test_finished_can_touch_gtk);
-               */
        }
 
+       bool multiFinishing;
        private void on_multi_chronopic_finished (object o, EventArgs args)
        {
-               /*
                if(multiFinishing)
                        return;
                else
@@ -5306,7 +5301,6 @@ LogB.Debug("X");
                }
                else if( currentEventExecute.ChronopicDisconnected )
                        chronopicDisconnectedWhileExecuting();
-                       */
        }
                
 
diff --git a/src/gui/guiTests.cs b/src/gui/guiTests.cs
index 65ca207..efe0d9b 100644
--- a/src/gui/guiTests.cs
+++ b/src/gui/guiTests.cs
@@ -388,7 +388,7 @@ public partial class ChronoJumpWindow
                else {
                        cp2016.FakeButtonContactsRealDone.Clicked +=
                                new EventHandler(on_connection_contacts_real_done);
-                       cp2016.ConnectContactsReal(app1, crp,
+                       cp2016.ConnectContactsReal(app1, crp, 1,
                                        "Press TEST button on Chronopic to stablish initial communication"); 
//TODO: translate this
                }
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]