[chronojump] Autodetect Encoder and normal Chronopic on menu change 70%



commit 7a6d6b65694adc8ac9194edd5f5d6578eadc89da
Author: Xavier de Blas <xaviblas gmail com>
Date:   Fri Mar 27 18:36:56 2015 +0100

    Autodetect Encoder and normal Chronopic on menu change 70%

 glade/chronojump.glade |   69 ++++++++++++++++++++++++++++++++++++++++++++
 src/chronopic.cs       |   74 +++++++++++++++++++++++++++++++++---------------
 src/gui/chronojump.cs  |   18 ++++++++----
 3 files changed, 132 insertions(+), 29 deletions(-)
---
diff --git a/glade/chronojump.glade b/glade/chronojump.glade
index 7eacb46..97d4e4d 100644
--- a/glade/chronojump.glade
+++ b/glade/chronojump.glade
@@ -6606,6 +6606,9 @@ Second Chronopic to platforms.</property>
                                                         <child>
                                                           <placeholder/>
                                                         </child>
+                                                        <child>
+                                                          <placeholder/>
+                                                        </child>
                                                       </widget>
                                                       <packing>
                                                         <property name="expand">True</property>
@@ -7462,6 +7465,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             </child>
                                                             <child>
@@ -8570,6 +8576,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">2</property>
@@ -9319,6 +9328,9 @@ Second Chronopic to platforms.</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="position">4</property>
@@ -13390,6 +13402,18 @@ on current Chronojump version.</property>
                                             <property name="expand">False</property>
                                             <property name="fill">False</property>
                                             <property name="pack_type">end</property>
+                                            <property name="position">1</property>
+                                          </packing>
+                                        </child>
+                                        <child>
+                                          <widget class="GtkLabel" id="label_chronopic_encoder_detected">
+                                            <property name="visible">True</property>
+                                            <property name="can_focus">False</property>
+                                          </widget>
+                                          <packing>
+                                            <property name="expand">False</property>
+                                            <property name="fill">False</property>
+                                            <property name="pack_type">end</property>
                                             <property name="position">2</property>
                                           </packing>
                                         </child>
@@ -19800,6 +19824,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -20457,6 +20484,9 @@ by you</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -21606,6 +21636,9 @@ by you</property>
               <placeholder/>
             </child>
             <child>
+              <placeholder/>
+            </child>
+            <child>
               <widget class="GtkButton" id="button_video_url">
                 <property name="visible">True</property>
                 <property name="can_focus">True</property>
@@ -21812,6 +21845,9 @@ by you</property>
             <child>
               <placeholder/>
             </child>
+            <child>
+              <placeholder/>
+            </child>
           </widget>
           <packing>
             <property name="expand">True</property>
@@ -31693,6 +31729,24 @@ options</property>
                             <child>
                               <placeholder/>
                             </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
+                            <child>
+                              <placeholder/>
+                            </child>
                           </widget>
                         </child>
                       </widget>
@@ -33730,6 +33784,9 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                                 <child>
                                   <placeholder/>
                                 </child>
+                                <child>
+                                  <placeholder/>
+                                </child>
                               </widget>
                             </child>
                           </widget>
@@ -34425,6 +34482,9 @@ To differentiate between male and female, use the values 1/0, or m/f, or M/F on
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                     </child>
                   </widget>
@@ -37086,6 +37146,9 @@ show elevation as:</property>
                         <child>
                           <placeholder/>
                         </child>
+                        <child>
+                          <placeholder/>
+                        </child>
                       </widget>
                       <packing>
                         <property name="left_attach">2</property>
@@ -37407,6 +37470,9 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
@@ -37743,6 +37809,9 @@ show elevation as:</property>
                     <child>
                       <placeholder/>
                     </child>
+                    <child>
+                      <placeholder/>
+                    </child>
                   </widget>
                 </child>
               </widget>
diff --git a/src/chronopic.cs b/src/chronopic.cs
index 5e61744..79fe595 100644
--- a/src/chronopic.cs
+++ b/src/chronopic.cs
@@ -408,7 +408,7 @@ public abstract class ChronopicAuto
        private string str;
        public string CharToSend = "";
        public bool IsEncoder = false;
-       public bool Found;
+       public ChronopicAutoDetect.ChronopicType Found;
 
        private bool make(SerialPort sp) 
        {
@@ -528,7 +528,7 @@ public class ChronopicAutoCheck : ChronopicAuto
 {
        protected internal override string Communicate() 
        {
-               Found = false;
+               Found = ChronopicAutoDetect.ChronopicType.UNDETECTED;
 
                sp.Write("J");
                IsChronopicAuto = ( (char) sp.ReadByte() == 'J');
@@ -539,7 +539,7 @@ public class ChronopicAutoCheck : ChronopicAuto
                        sp.ReadByte();          //.
                        int minor = (char) sp.ReadByte() - '0'; 
 
-                       Found = true;
+                       Found = ChronopicAutoDetect.ChronopicType.NORMAL;
                        return "Yes! v" + major.ToString() + "." + minor.ToString();
                }
 
@@ -553,10 +553,10 @@ public class ChronopicAutoCheckEncoder : ChronopicAuto
        {
                LogB.Information("Communicate start ...");
                
-               Found = false;
+               Found = ChronopicAutoDetect.ChronopicType.UNDETECTED;
        
                char myByte;
-               for(int i = 0; i < 30; i ++) 
+               for(int i = 0; i < 20; i ++) //try 20 times (usually works 3-5 try)
                {
                        LogB.Debug("writting ...");
        
@@ -573,7 +573,7 @@ public class ChronopicAutoCheckEncoder : ChronopicAuto
                        if(myByte == 'J') {
                                LogB.Information("Encoder found!");
 
-                               Found = true;
+                               Found = ChronopicAutoDetect.ChronopicType.ENCODER;
                                return "1";
                        }
                }
@@ -623,7 +623,9 @@ public class ChronopicStartReactionTime : ChronopicAuto
 
 public class ChronopicAutoDetect
 {
-       public enum ChronopicType { NORMAL, ENCODER }
+       public enum ChronopicType { UNDETECTED, NORMAL, ENCODER }
+       private ChronopicType searched;
+
        public string Detected; // portname if detected, if not will be ""
 
        public ChronopicAutoDetect(ChronopicType type)
@@ -642,19 +644,17 @@ public class ChronopicAutoDetect
                 * they will not work after trying to be recognised as an encoder, until reset or disconnect 
cable
                 *
                 */
-               ChronopicAuto ca;
-               if(type == ChronopicType.ENCODER) {
-                       ca = new ChronopicAutoCheckEncoder();
-                       ca.IsEncoder = true;    //for the bauds.
-               } else {
-                       ca = new ChronopicAutoCheck();
-                       ca.IsEncoder = false;    //for the bauds.
-               }
+               this.searched = type;
 
-               autoDetect(ca);
+               //no matter if we are searching for 4MHz or 20MHz (encoder)
+               //first see if 4MHz is connected
+               ChronopicAuto caNormal = new ChronopicAutoCheck();
+               caNormal.IsEncoder = false;    //for the bauds.
+
+               autoDetect(caNormal);
        }
 
-       private void autoDetect(ChronopicAuto ca) 
+       private void autoDetect(ChronopicAuto caNormal)
        {
                LogB.Information("starting port detection");
 
@@ -669,13 +669,41 @@ public class ChronopicAutoDetect
                foreach(string port in usbSerial) 
                {
                        SerialPort sp = new SerialPort(port);
-                       LogB.Information("reading port:", port);
                        
-                       string readed = ca.Read(sp);
-
-                       if(ca.Found) {
-                               Detected = port;
-                               return;
+                       LogB.Information("searching normal Chronopic at port: ", port);
+                       string readed = caNormal.Read(sp);
+
+                       if(caNormal.Found == ChronopicType.NORMAL) //We found a normal Chronopic
+                       {
+                               if(searched == ChronopicType.NORMAL) //normal Chronopic is what we are 
searching
+                               {
+                                       Detected = port;
+                                       return;
+                               } else {
+                                       /*
+                                        * else: 
+                                        * means that we are searching for an encoder chronopic and found a 
normal
+                                        * so don't try to search for an encoder on that port, because 115200 
bauds will saturate it
+                                        */
+                                       LogB.Information("our goal is to search encoder but found normal 
Chronopic at port: ", port);
+                               }
+                       } else if(searched == ChronopicType.ENCODER) 
+                       {
+                               /*
+                                * we are searching an encoder
+                                * if we arrived here, we know is not a normal chronopic
+                                * then wecan search safely for an encoder here
+                                */
+                               ChronopicAuto caEncoder = new ChronopicAutoCheckEncoder();
+                               caEncoder.IsEncoder = true;    //for the bauds.
+                       
+                               LogB.Information("searching encoder Chronopic at port: ", port);
+                               readed = caEncoder.Read(sp);
+                               if(caEncoder.Found == ChronopicType.ENCODER) 
+                               {
+                                       Detected = port;
+                                       return;
+                               }
                        }
                }
                Detected = "";
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index c15103b..8e8f5ab 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -212,6 +212,7 @@ public partial class ChronoJumpWindow
        //[Widget] Gtk.Image image_connected_chronopics;
        [Widget] Gtk.Viewport viewport_chronopic_encoder;
        [Widget] Gtk.Label label_chronopic_encoder;
+       [Widget] Gtk.Label label_chronopic_encoder_detected;
        
        [Widget] Gtk.HBox hbox_video_capture;
        [Widget] Gtk.Label label_video_feedback;
@@ -2885,15 +2886,20 @@ public partial class ChronoJumpWindow
                if(m == menuitem_modes.POWER) {
                        LogB.Information("Detecting encoder... ");
                        cad = new ChronopicAutoDetect(ChronopicAutoDetect.ChronopicType.ENCODER);
+       
+                       if(cad.Detected != "") {
+                               LogB.Information("Detected at port: " + cad.Detected);
+                               label_chronopic_encoder_detected.Text = "Detected: " + cad.Detected;
+                       }
+                       else {
+                               LogB.Information("Not detected.");
+                               label_chronopic_encoder_detected.Text = "";
+                       }
                } else {
                        LogB.Information("Detecting normal Chronopic... ");
-                       cad = new ChronopicAutoDetect(ChronopicAutoDetect.ChronopicType.NORMAL);
+                       //cad = new ChronopicAutoDetect(ChronopicAutoDetect.ChronopicType.NORMAL);
+                       LogB.Warning("Disabled until full chronopic connection is done on 4MHz Chronopics");
                }
-                       
-               if(cad.Detected != "")
-                       LogB.Information("Detected at port: " + cad.Detected);
-               else
-                       LogB.Information("Not detected.");
        }
 
        //change debounce time automatically on change menuitem mode (if multitest firmware)


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