[chronojump] All the code for running with wireless photocells



commit 33522aa1526e96d7fad19d6da167b5c0cb76815f
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Jul 2 15:49:32 2021 +0200

    All the code for running with wireless photocells

 src/execute/arduinoCapture.cs | 78 +++++++++++++++++++++++++++++++------------
 src/execute/run.cs            | 27 ++++++++-------
 src/gui/app1/chronojump.cs    | 16 +++++++--
 3 files changed, 85 insertions(+), 36 deletions(-)
---
diff --git a/src/execute/arduinoCapture.cs b/src/execute/arduinoCapture.cs
index d9592ac99..2e0b63558 100644
--- a/src/execute/arduinoCapture.cs
+++ b/src/execute/arduinoCapture.cs
@@ -29,6 +29,7 @@ public abstract class ArduinoCapture
        protected string portName;
        protected int bauds;
        protected SerialPort port;
+       protected bool portOpened;
        protected int readedPos; //position already readed from list
 
        // public stuff ---->
@@ -58,6 +59,28 @@ public abstract class ArduinoCapture
                emptyList();
        }
 
+       protected bool portConnect()
+       {
+               port = new SerialPort(portName, bauds);
+
+               try {
+                       port.Open();
+               }
+               catch (System.IO.IOException)
+               {
+                       LogB.Information("Error: could not open port");
+                       return false;
+               }
+
+               LogB.Information("port successfully opened");
+
+               //TODO: Val, caldria que quedés clar a la interficie que estem esperant aquest temps, a veure 
com ho fa el sensor de força, ...
+               //just print on gui somthing like "please, wait, ..."
+               //
+               Thread.Sleep(3000); //sleep to let arduino start reading serial event
+               return true;
+       }
+
        protected bool sendCommand (string command, string errorMessage)
        {
                try {
@@ -70,6 +93,7 @@ public abstract class ArduinoCapture
                        {
                                LogB.Information("error: " + errorMessage);
                                port.Close();
+                               portOpened = false;
                                return false;
                        }
                        //throw;
@@ -114,13 +138,31 @@ public abstract class ArduinoCapture
        }
 
        protected abstract void emptyList();
+
+       public void Disconnect()
+       {
+               port.Close();
+               portOpened = false;
+       }
+
+       public bool PortOpened
+       {
+               get { return portOpened; }
+       }
 }
 
 public class PhotocellWirelessCapture: ArduinoCapture
 {
        private List<PhotocellWirelessEvent> list = new List<PhotocellWirelessEvent>();
 
+       //constructor
        public PhotocellWirelessCapture (string portName)
+       {
+               Reset(portName);
+       }
+
+       //after a first capture, put variales to zero
+       public void Reset (string portName)
        {
                this.bauds = 115200;
                initialize(portName, bauds);
@@ -128,27 +170,15 @@ public class PhotocellWirelessCapture: ArduinoCapture
 
        public override bool CaptureStart()
        {
-               LogB.Information(string.Format("arduinoCapture portName: {0}, bauds: {1}", portName, bauds));
-               port = new SerialPort(portName, bauds);
-
-               try {
-                       port.Open();
-               } 
-               catch (System.IO.IOException)
-               {
-                       LogB.Information("Error: could not open port");
-                       return false;
-               }
+               LogB.Information("portOpened: " + portOpened.ToString());
+               // 0 connect if needed
+               if(! portOpened)
+                       if(! portConnect())
+                               return false;
 
-               LogB.Information("port successfully opened");
+               portOpened = true;
 
-               //TODO: Val, caldria que quedés clar a la interficie que estem esperant aquest temps, a veure 
com ho fa el sensor de força, ...
-               //just print on gui somthing like "please, wait, ..."
-               //-----------------------------
-               //el que cal és el connect !!!!
-               //-----------------------------
-               //
-               Thread.Sleep(3000); //sleep to let arduino start reading serial event
+               LogB.Information(string.Format("arduinoCapture portName: {0}, bauds: {1}", portName, bauds));
 
                if (! sendCommand("start_capture:", "Catched at start_capture:"))
                        return false;
@@ -193,7 +223,10 @@ public class PhotocellWirelessCapture: ArduinoCapture
                waitResponse("Capture ended");
                LogB.Information("AT Capture: STOPPED");
 
+               /*
                port.Close();
+               portOpened = false;
+               */
 
                return true;
        }
@@ -250,7 +283,8 @@ public class PhotocellWirelessCapture: ArduinoCapture
                if( strFull.Length != 3 ||
                                ! Util.IsNumber(strFull[0], false) ||
                                ! Util.IsNumber(strFull[1], false) ||
-                               (strFull[2] != "O" && strFull[2] != "I")
+                               //(strFull[2] != "O" && strFull[2] != "I")
+                               (strFull[2] != "0" && strFull[2] != "1")
                                )
                        return false;
 
@@ -279,9 +313,9 @@ public class PhotocellWirelessEvent
        {
                this.photocell = photocell;
                this.timeMs = timeMs;
-               if(status == "O")
+               if(status == "1")
                        this.status = Chronopic.Plataforma.OFF;
-               else //(status == "I")
+               else //(status == "0")
                        this.status = Chronopic.Plataforma.ON;
        }
 
diff --git a/src/execute/run.cs b/src/execute/run.cs
index bc50a8180..7fc224989 100644
--- a/src/execute/run.cs
+++ b/src/execute/run.cs
@@ -76,6 +76,8 @@ public class RunExecute : EventExecute
        protected Gtk.Image image_run_execute_running;
        protected Gtk.Image image_run_execute_photocell;
 
+       protected PhotocellWirelessCapture photocellWirelessCapture;
+
 //     protected bool firstTrackDone;
 
        public RunExecute() {
@@ -84,7 +86,7 @@ public class RunExecute : EventExecute
        //run execution
        //if wireless: string wirelessPort, wirelessBauds will be used instead of Chronopic cp
        public RunExecute(int personID, int sessionID, string type, double distance,   
-                       Chronopic cp,
+                       Chronopic cp, PhotocellWirelessCapture photocellWirelessCapture,
                        string wirelessPort, int wirelessBauds,
                        int pDN, bool metersSecondsPreferred,
                        bool volumeOn, Preferences.GstreamerTypes gstreamer,
@@ -101,6 +103,7 @@ public class RunExecute : EventExecute
                this.distance = distance;
                
                this.cp = cp;
+               this.photocellWirelessCapture = photocellWirelessCapture;
                this.wirelessPort = wirelessPort;
                this.wirelessBauds = wirelessBauds;
                wireless = (wirelessPort != "" && wirelessBauds > 0);
@@ -310,14 +313,15 @@ public class RunExecute : EventExecute
                                checkDoubleContactTime
                                );
 
-LogB.Information("going to call pwc.CaptureStart ()");
-               PhotocellWirelessCapture pwc = null;
+LogB.Information("going to call photocellWirelessCapture.CaptureStart ()");
+               //PhotocellWirelessCapture pwc = null;
                if(wireless)
                {
                        feedbackMessage = Catalog.GetString("Please, wait!");
                        needShowFeedbackMessage = true;
-                       pwc = new PhotocellWirelessCapture(wirelessPort);
-                       pwc.CaptureStart ();
+                       //photocellWirelessCapture = new PhotocellWirelessCapture(wirelessPort);
+                       photocellWirelessCapture.Reset(wirelessPort);
+                       photocellWirelessCapture.CaptureStart ();
 
                        manageIniWireless();
                }
@@ -327,14 +331,14 @@ LogB.Information("going to call pwc.CaptureStart ()");
                do {
                        if (wireless)
                        {
-                               if(! pwc.CaptureLine())
+                               if(! photocellWirelessCapture.CaptureLine())
                                        cancel = true; //problem reading line (capturing)
 
                                ok = false;
-                               if(pwc.CanRead())
+                               if(photocellWirelessCapture.CanRead())
                                {
                                        LogB.Information("waitEvent 3");
-                                       PhotocellWirelessEvent pwe = pwc.PhotocellWirelessCaptureReadNext();
+                                       PhotocellWirelessEvent pwe = 
photocellWirelessCapture.PhotocellWirelessCaptureReadNext();
                                        LogB.Information("wait_event pwe: " + pwe.ToString());
                                        //TODO: photocell = pwe.photocell;
                                        timestamp = pwe.timeMs - timestampAccumulated; //photocell does not 
send splittime, sends absolute time
@@ -510,9 +514,7 @@ LogB.Information("going to call pwc.CaptureStart ()");
                } while ( ! exitWaitEventBucle );
 
                if(wireless)
-               {
-                       pwc.Stop();
-               }
+                       photocellWirelessCapture.Stop();
 
                onlyInterval_FinishWaitEventWrite();
        }
@@ -902,7 +904,7 @@ public class RunIntervalExecute : RunExecute
 
        //run execution
        public RunIntervalExecute(int personID, int sessionID, string type, double distanceInterval, double 
limitAsDouble, bool tracksLimited,  
-                       Chronopic cp,
+                       Chronopic cp, PhotocellWirelessCapture photocellWirelessCapture,
                        string wirelessPort, int wirelessBauds,
                        int pDN, bool metersSecondsPreferred,
                        bool volumeOn, Preferences.GstreamerTypes gstreamer,
@@ -937,6 +939,7 @@ public class RunIntervalExecute : RunExecute
                
                
                this.cp = cp;
+               this.photocellWirelessCapture = photocellWirelessCapture;
                this.wirelessPort = wirelessPort;
                this.wirelessBauds = wirelessBauds;
                wireless = (wirelessPort != "" && wirelessBauds > 0);
diff --git a/src/gui/app1/chronojump.cs b/src/gui/app1/chronojump.cs
index dbfbb3beb..853d84da8 100644
--- a/src/gui/app1/chronojump.cs
+++ b/src/gui/app1/chronojump.cs
@@ -501,6 +501,7 @@ public partial class ChronoJumpWindow
 
        ChronopicRegister chronopicRegister;
        Chronopic2016 cp2016;
+       private PhotocellWirelessCapture photocellWirelessCapture;
        private Threshold threshold;
 
        RestTime restTime;
@@ -2571,6 +2572,8 @@ public partial class ChronoJumpWindow
                        forceOtherThread.Abort();
                if(portFSOpened)
                        portFS.Close();
+               if(photocellWirelessCapture != null && photocellWirelessCapture.PortOpened)
+                       photocellWirelessCapture.Disconnect();
 
                //cancel runEncoder capture process
                if(capturingRunEncoder == arduinoCaptureStatus.STARTING || capturingRunEncoder == 
arduinoCaptureStatus.CAPTURING)
@@ -3146,6 +3149,9 @@ public partial class ChronoJumpWindow
                if(portFSOpened)
                        forceSensorDisconnect();
 
+               if(photocellWirelessCapture != null && photocellWirelessCapture.PortOpened)
+                       photocellWirelessCapture.Disconnect();
+
                //run simple will be the only one with its drawing are
                frame_run_simple_double_contacts.Visible = false;
 
@@ -4948,10 +4954,13 @@ public partial class ChronoJumpWindow
                event_execute_ButtonUpdate.Clicked += new EventHandler(on_update_clicked);
                */
 
+               if(photocellWirelessCapture == null)
+                       photocellWirelessCapture = new PhotocellWirelessCapture(wirelessPort);
+
                currentEventExecute = new RunExecute(
                                currentPerson.UniqueID, currentSession.UniqueID, 
                                currentRunType.Name, myDistance, 
-                               cp2016.CP, wirelessPort, wirelessBauds,
+                               cp2016.CP, photocellWirelessCapture, wirelessPort, wirelessBauds,
                                preferences.digitsNumber, preferences.metersSecondsPreferred,
                                preferences.volumeOn, preferences.gstreamer,
                                progressbarLimit, egd,
@@ -5096,10 +5105,13 @@ public partial class ChronoJumpWindow
                event_execute_ButtonUpdate.Clicked += new EventHandler(on_update_clicked);
                */
 
+               if(photocellWirelessCapture == null)
+                       photocellWirelessCapture = new PhotocellWirelessCapture(wirelessPort);
+
                currentEventExecute = new RunIntervalExecute(
                                currentPerson.UniqueID, currentSession.UniqueID, currentRunIntervalType.Name, 
                                distanceInterval, progressbarLimit, currentRunIntervalType.TracksLimited, 
-                               cp2016.CP, wirelessPort, wirelessBauds,
+                               cp2016.CP, photocellWirelessCapture, wirelessPort, wirelessBauds,
                                preferences.digitsNumber, preferences.metersSecondsPreferred,
                                preferences.volumeOn, preferences.gstreamer,
                                repetitiveConditionsWin,


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