[chronojump] ReactionTime discriminative with a FlushByTime at sending signal



commit 0f85daf1d1d8742782bad5dbc8f1a7b79b8371a3
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri May 13 14:30:18 2016 +0200

    ReactionTime discriminative with a FlushByTime at sending signal

 src/chronopic.cs            |   27 ++++++++++++++++++++++
 src/chronopicDetect.cs      |    1 +
 src/execute/event.cs        |    8 ++++--
 src/execute/reactionTime.cs |   53 +++++++++++++++---------------------------
 src/gui/chronojump.cs       |   40 +++++++++++++++++++++++++-------
 5 files changed, 83 insertions(+), 46 deletions(-)
---
diff --git a/src/chronopic.cs b/src/chronopic.cs
index e63b53d..94c007d 100644
--- a/src/chronopic.cs
+++ b/src/chronopic.cs
@@ -373,9 +373,36 @@ public class Chronopic {
                }
        }
 
+       /*
+        * this will read what's in the serial port until time out
+       */      
+       private void flushByTimeOut()
+       {
+               byte[] buffer = new byte[256];
+
+               //try, catch done because mono-1.2.3 throws an exception when there's a timeout
+               //http://bugzilla.gnome.org/show_bug.cgi?id=420520
+               bool timeOut = false;
+               do{
+                       try{
+                               sp.Read(buffer,0,256);
+                               LogB.Debug(" spReaded ");
+                       } catch {
+                               LogB.Warning(" catchedTimeOut ");
+                               timeOut = true;
+                       }
+
+               } while(! timeOut);
+       }
+
+
        public void Flush() {
                flush();
        }
+       
+       public void FlushByTimeOut() {
+               flushByTimeOut();
+       }
 
        /**********************************/
        /* TIPOS PRIVADOS                 */
diff --git a/src/chronopicDetect.cs b/src/chronopicDetect.cs
index e8eca04..e67c8c3 100644
--- a/src/chronopicDetect.cs
+++ b/src/chronopicDetect.cs
@@ -339,6 +339,7 @@ public abstract class ChronopicAuto
                return true;
        }
        private void close(SerialPort sp) {
+               LogB.Information("closing port... ");
                sp.Close();
        }       
 
diff --git a/src/execute/event.cs b/src/execute/event.cs
index 82db9f3..c1d835a 100644
--- a/src/execute/event.cs
+++ b/src/execute/event.cs
@@ -154,9 +154,8 @@ public class EventExecute
        //for reaction time     
        //on animation lights and discriminative should be false
        public bool StartIn = true;
-       public string DiscriminativeCharToSend;
-       public double DiscriminativeStartTime;
-       public SerialPort SP;
+       public Gtk.Button FakeButtonReactionTimeStart;
+       
        
 
        //protected EventExecuteWindow eventExecuteWin;
@@ -490,6 +489,9 @@ public class EventExecute
                Util.PlaySound(Constants.SoundTypes.BAD, volumeOn);
        } 
        
+       public virtual void Manage2() {
+       }
+       
        //from confirm_window cancel button (thread has not started)
        //this is NOT called when a event has started and user click on "Cancel"
        protected void cancel_event_before_start(object o, EventArgs args)
diff --git a/src/execute/reactionTime.cs b/src/execute/reactionTime.cs
index ef4e2c9..f609900 100644
--- a/src/execute/reactionTime.cs
+++ b/src/execute/reactionTime.cs
@@ -35,7 +35,7 @@ public class ReactionTimeExecute : EventExecute
        private Chronopic cp;
 
        private ReactionTime reactionTimeDone;
-       
+                               
        public ReactionTimeExecute() {
        }
 
@@ -62,6 +62,8 @@ public class ReactionTimeExecute : EventExecute
                fakeButtonEventEnded = new Gtk.Button();
                fakeButtonFinished = new Gtk.Button();
                fakeButtonThreadDyed = new Gtk.Button();
+                               
+               FakeButtonReactionTimeStart = new Gtk.Button();
                
                simulated = false;
                        
@@ -100,9 +102,10 @@ public class ReactionTimeExecute : EventExecute
 
                if (simulated) 
                        platformState = Chronopic.Plataforma.ON;
-               else
+               else {
                        platformState = chronopicInitialValue(cp);
-       
+               }
+                       
                bool canStart = false;
                if ( 
                                (StartIn && platformState == Chronopic.Plataforma.ON) ||
@@ -129,13 +132,9 @@ public class ReactionTimeExecute : EventExecute
                        if (simulated) //TODO: check loggedState and StartIn
                                platformState = Chronopic.Plataforma.OFF;
                        
-                       //start thread
-                       //Log.Write("Start thread");
-                       thread = new Thread(new ThreadStart(waitEvent));
-                       GLib.Idle.Add (new GLib.IdleHandler (PulseGTK));
-                       
-                       LogB.ThreadStart(); 
-                       thread.Start(); 
+       
+                       //if discriminative, will fire the buttons      
+                       FakeButtonReactionTimeStart.Click();
                } 
                else if (! canStart && (platformState == Chronopic.Plataforma.ON || platformState == 
Chronopic.Plataforma.OFF) )
                {
@@ -156,33 +155,19 @@ public class ReactionTimeExecute : EventExecute
                }
        }
 
-       protected override void waitEvent ()
+       public override void Manage2() 
        {
-               if(DiscriminativeCharToSend != "") {
-                       Thread.Sleep(Convert.ToInt32(DiscriminativeStartTime * 1000)); //in ms
+               //start thread
+               //Log.Write("Start thread");
+               thread = new Thread(new ThreadStart(waitEvent));
+               GLib.Idle.Add (new GLib.IdleHandler (PulseGTK));
 
-                       ChronopicAuto cs = new ChronopicStartReactionTimeAnimation();
-                       cs.CharToSend = DiscriminativeCharToSend;
-                       cs.Write(SP, 0);
-
-                       LogB.Information("opening port");       
-                       SP.Open();
-
-                       LogB.Information("reading one");        
-                       byte[] buffer = new byte[256];
-                       SP.Read(buffer,0,256);
-                       LogB.Information("readed");     
-
-                       LogB.Information("reading two");        
-                       buffer = new byte[256];
-                       SP.Read(buffer,0,256);
-                       LogB.Information("readed");     
-
-                       LogB.Information("closing port");       
-                       SP.Close();
-                       //TODO: end discriminative stuff!!!!
-               }
+               LogB.ThreadStart(); 
+               thread.Start(); 
+       }
 
+       protected override void waitEvent ()
+       {
                double timestamp = 0;
                bool success = false;
                
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index d969771..e661300 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -32,6 +32,7 @@ using LongoMatch.Gui;
 using LongoMatch.Video.Capturer;
 using LongoMatch.Video.Common;
 using LongoMatch.Video.Utils;
+using System.Threading;
 
 public partial class ChronoJumpWindow 
 {
@@ -4527,14 +4528,9 @@ public partial class ChronoJumpWindow
                if(extra_window_radio_reaction_time_discriminative.Active) {
                        //TODO: do also for animation_lights and flickr
                        currentEventExecute.StartIn = false;
-
-                       currentEventExecute.DiscriminativeCharToSend = discriminativeCharToSend;
-                       currentEventExecute.DiscriminativeStartTime = discriminativeStartTime;
-                       currentEventExecute.SP = chronopicWin.SP;
-               } else {
-                       currentEventExecute.DiscriminativeCharToSend = "";
-                       currentEventExecute.DiscriminativeStartTime = 0;
                }
+               currentEventExecute.FakeButtonReactionTimeStart.Clicked += new 
EventHandler(on_event_execute_reaction_time_start);
+
                currentEventExecute.Manage(); //check that platform is ok
                
                currentEventExecute.FakeButtonUpdateGraph.Clicked += 
@@ -4544,6 +4540,34 @@ public partial class ChronoJumpWindow
                currentEventExecute.FakeButtonThreadDyed.Clicked += new 
EventHandler(on_test_finished_can_touch_gtk);
        }       
 
+       private void on_event_execute_reaction_time_start (object o, EventArgs args) 
+       {
+               currentEventExecute.FakeButtonReactionTimeStart.Clicked -= new 
EventHandler(on_event_execute_reaction_time_start);
+               
+               if(extra_window_radio_reaction_time_discriminative.Active) {
+                       Thread.Sleep(Convert.ToInt32(discriminativeStartTime * 1000)); //in ms
+
+                       ChronopicAuto cs = new ChronopicStartReactionTimeAnimation();
+                       cs.CharToSend = discriminativeCharToSend;
+                       cs.Write(chronopicWin.SP, 0);
+
+
+                       LogB.Information("opening port at gui/chronojump.cs");  
+                       chronopicWin.SP.Open();
+
+                       /*
+                        * some machines needed to flush
+                        * - my Linux laptop two bytes
+                        * - a linux guest on windows host (virtual box) don't need
+                        * Note this will not allow reaction time be lower than 100 ms (DefaultTimeout on 
chronopic.cs)
+                        */
+                       LogB.Information("Going to flush by time out"); //needed on some machines
+                       chronopicWin.CP.FlushByTimeOut();
+                       LogB.Information("flushed!");   
+               }
+
+               currentEventExecute.Manage2();
+       }
 
 
        private void on_reaction_time_finished (object o, EventArgs args)
@@ -4578,8 +4602,6 @@ public partial class ChronoJumpWindow
                        chronopicDisconnectedWhileExecuting();
        }
 
-
-
        /* ---------------------------------------------------------
         * ----------------  PULSES EXECUTION ----------------------
         *  --------------------------------------------------------


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