[chronojump] Threshold (debounce time) on SQL and GUI working



commit 4d2c89d14c10ea4854c5d2147266a1370ffde6a7
Author: Xavier de Blas <xaviblas gmail com>
Date:   Wed Dec 21 20:05:08 2016 +0100

    Threshold (debounce time) on SQL and GUI working

 chronojump.csproj         |    4 +-
 glade/app1.glade          |   45 +++++++-----------
 src/Makefile.am           |    1 +
 src/chronopic2016.cs      |   71 ++++++++--------------------
 src/constants.cs          |   23 ---------
 src/gui/chronojump.cs     |   77 ++++++++++++++----------------
 src/gui/guiTests.cs       |    4 +-
 src/preferences.cs        |    4 ++
 src/sqlite/main.cs        |   12 ++++-
 src/sqlite/preferences.cs |   10 ++++
 src/threshold.cs          |  115 +++++++++++++++++++++++++++++++++++++++++++++
 11 files changed, 219 insertions(+), 147 deletions(-)
---
diff --git a/chronojump.csproj b/chronojump.csproj
index 9b394e4..a2d60c6 100644
--- a/chronojump.csproj
+++ b/chronojump.csproj
@@ -1,4 +1,3 @@
-<?xml version="1.0" encoding="utf-8"?>
 <Project DefaultTargets="Build" ToolsVersion="4.0" 
xmlns="http://schemas.microsoft.com/developer/msbuild/2003";>
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -1005,6 +1004,7 @@
     <Compile Include="src\buildInfo.cs" />
     <Compile Include="src\restTime.cs" />
     <Compile Include="src\gui\encoderOverview.cs" />
+    <Compile Include="src\threshold.cs" />
   </ItemGroup>
   <ItemGroup>
     <Folder Include="src\" />
@@ -2051,4 +2051,4 @@
       </Properties>
     </MonoDevelop>
   </ProjectExtensions>
-</Project>
+</Project>
\ No newline at end of file
diff --git a/glade/app1.glade b/glade/app1.glade
index 885b27e..2475fec 100644
--- a/glade/app1.glade
+++ b/glade/app1.glade
@@ -1564,12 +1564,12 @@
                                   </packing>
                                 </child>
                                 <child>
-                                  <widget class="GtkButton" id="button_carles">
-                                    <property name="label">Button
-Carles</property>
+                                  <widget class="GtkButton" id="button_crash">
+                                    <property name="label">CRASH_ME
+(debug purposes)</property>
                                     <property name="can_focus">True</property>
                                     <property name="receives_default">True</property>
-                                    <signal name="clicked" handler="on_button_carles_clicked" swapped="no"/>
+                                    <signal name="clicked" handler="on_debug_crash_activate" swapped="no"/>
                                   </widget>
                                   <packing>
                                     <property name="expand">False</property>
@@ -1578,12 +1578,12 @@ Carles</property>
                                   </packing>
                                 </child>
                                 <child>
-                                  <widget class="GtkButton" id="button_crash">
-                                    <property name="label">CRASH_ME
-(debug purposes)</property>
+                                  <widget class="GtkButton" id="button_carles">
+                                    <property name="label">Button
+Carles</property>
                                     <property name="can_focus">True</property>
                                     <property name="receives_default">True</property>
-                                    <signal name="clicked" handler="on_debug_crash_activate" swapped="no"/>
+                                    <signal name="clicked" handler="on_button_carles_clicked" swapped="no"/>
                                   </widget>
                                   <packing>
                                     <property name="expand">False</property>
@@ -4208,20 +4208,6 @@ Carles</property>
                                                             <property name="can_focus">True</property>
                                                             <property name="spacing">8</property>
                                                             <child>
-                                                            <widget class="GtkLabel" 
id="label_chronopics_multitest">
-                                                            <property name="visible">True</property>
-                                                            <property name="sensitive">False</property>
-                                                            <property name="can_focus">False</property>
-                                                            <property name="label">50</property>
-                                                            </widget>
-                                                            <packing>
-                                                            <property name="expand">False</property>
-                                                            <property name="fill">False</property>
-                                                            <property name="padding">6</property>
-                                                            <property name="position">0</property>
-                                                            </packing>
-                                                            </child>
-                                                            <child>
                                                             <widget class="GtkButton" 
id="button_activate_chronopics">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">True</property>
@@ -4267,7 +4253,7 @@ Carles</property>
                                                             <packing>
                                                             <property name="expand">False</property>
                                                             <property name="fill">False</property>
-                                                            <property name="position">1</property>
+                                                            <property name="position">0</property>
                                                             </packing>
                                                             </child>
                                                             <child>
@@ -4287,7 +4273,7 @@ Carles</property>
                                                             <widget class="GtkLabel" id="label254">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
-                                                            <property name="label" 
translatable="yes">Sensibility</property>
+                                                            <property name="label" 
translatable="yes">Threshold</property>
                                                             </widget>
                                                             </child>
                                                             <child>
@@ -4311,7 +4297,7 @@ Carles</property>
                                                             </packing>
                                                             </child>
                                                             <child>
-                                                            <widget class="GtkHScale" 
id="hscale_chronopic_debounce">
+                                                            <widget class="GtkHScale" id="hscale_threshold">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">True</property>
                                                             <property name="adjustment">5 1 10 1 1 
0</property>
@@ -4319,7 +4305,7 @@ Carles</property>
                                                             <property name="fill_level">0</property>
                                                             <property name="digits">0</property>
                                                             <property name="draw_value">False</property>
-                                                            <signal name="value_changed" 
handler="on_hscale_chronopic_debounce_value_changed" swapped="no"/>
+                                                            <signal name="value_changed" 
handler="on_hscale_threshold_value_changed" swapped="no"/>
                                                             </widget>
                                                             <packing>
                                                             <property name="top_attach">1</property>
@@ -4327,7 +4313,7 @@ Carles</property>
                                                             </packing>
                                                             </child>
                                                             <child>
-                                                            <widget class="GtkLabel" 
id="label_chronopic_debounce">
+                                                            <widget class="GtkLabel" id="label_threshold">
                                                             <property name="visible">True</property>
                                                             <property name="can_focus">False</property>
                                                             <property name="label">50</property>
@@ -4350,7 +4336,7 @@ Carles</property>
                                                             <packing>
                                                             <property name="expand">False</property>
                                                             <property name="fill">False</property>
-                                                            <property name="position">2</property>
+                                                            <property name="position">1</property>
                                                             </packing>
                                                             </child>
                                                             </widget>
@@ -18631,6 +18617,9 @@ Concentric</property>
                                                             <child>
                                                             <placeholder/>
                                                             </child>
+                                                            <child>
+                                                            <placeholder/>
+                                                            </child>
                                                             </widget>
                                                             <packing>
                                                             <property name="expand">False</property>
diff --git a/src/Makefile.am b/src/Makefile.am
index 6469b0f..4538d14 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -156,6 +156,7 @@ SOURCES = \
        exportSession.cs\
        preferences.cs\
        restTime.cs\
+       threshold.cs\
        treeViewEvent.cs\
        treeViewPerson.cs\
        treeViewJump.cs\
diff --git a/src/chronopic2016.cs b/src/chronopic2016.cs
index 0547a61..0cdd787 100644
--- a/src/chronopic2016.cs
+++ b/src/chronopic2016.cs
@@ -227,28 +227,12 @@ public class Chronopic2016
 
        // ----- change multitest firmware START ----->
 
-       private bool previousMultitestFirmwareDefined = false;
-       private Constants.Menuitem_modes previousMultitestFirmware;
-
        //change debounce time automatically on change menuitem mode (if multitest firmware)
-       //return values:
-       //-1 error
-       //0 don't need to change
-       //10 or 50 the change value
-       public int ChangeMultitestFirmwareMaybe(Constants.Menuitem_modes m)
+       public bool ChangeMultitestFirmwarePre(int thresholdValue)
        {
-
                LogB.Information("ChangeMultitestFirmwareMaybe (A)");
 
-               //---- 1 if don't need to change, return
-               if(previousMultitestFirmwareDefined &&
-                               ! Constants.Menuitem_mode_multitest_should_change(previousMultitestFirmware, 
m))
-               {
-                       LogB.Information("don't need to change multitest firmware");
-                       return 0;
-               }
-
-
+               //---- 1
                //bool ok = cp.Read_platform(out platformState);
                //seems better to have a new platformState:
                Chronopic.Plataforma ps;
@@ -257,10 +241,9 @@ public class Chronopic2016
                        LogB.Information("Chronopic has been disconnected");
                        //createChronopicWindow(true, "");
                        //chronopicWin.Connected = false;
-                       return -1;
+                       return false;
                }
 
-
                /*
                 * method 1. Unused
                 try {
@@ -273,7 +256,7 @@ public class Chronopic2016
                return;
                }*/
 
-               //---- 4 try to communicate with multitest firmware (return if cannot connect)
+               //---- 2 try to communicate with multitest firmware (return if cannot connect)
 
                LogB.Information("ChangeMultitestFirmwareMaybe (B)");
                bool isChronopicAuto = false;
@@ -282,34 +265,24 @@ public class Chronopic2016
                        LogB.Debug("version: " + result);
                } catch {
                        LogB.Information("Could not read from Chronopic with method 2");
-                       return -1;
+                       return false;
                }
 
-               //---- 5 change 10 <-> 50 ms
-               int returnValue = -1;
-
+               //---- 3 change debounce time
                LogB.Information("ChangeMultitestFirmwareMaybe (C)");
-               if(isChronopicAuto) {
-                       int debounceChange = 50;
-                       if(m == Constants.Menuitem_modes.RUNSSIMPLE || m == 
Constants.Menuitem_modes.RUNSINTERVALLIC)
-                               debounceChange = 10;
-
-                       int msChanged = changeMultitestFirmwareDo(debounceChange);
-                       if(msChanged == 50)
-                               returnValue = 50;
-                       else if(msChanged == 10)
-                               returnValue = 10;
+               if(isChronopicAuto)
+               {
+                       bool changedOk = changeMultitestFirmwareDo(thresholdValue);
+                       if(! changedOk)
+                               return false;
                }
 
-               previousMultitestFirmwareDefined = true;
-               previousMultitestFirmware = m;
-
                LogB.Information("ChangeMultitestFirmwareMaybe (D)");
 
-               return returnValue;
+               return true;
        }
 
-       private int changeMultitestFirmwareDo (int debounceChange)
+       private bool changeMultitestFirmwareDo (int debounceChange)
        {
                LogB.Information("ChangeMultitestFirmwareDo");
                try {
@@ -323,36 +296,32 @@ public class Chronopic2016
                        do {
                                //read if ok
                                ca = new ChronopicAutoCheckDebounce();
-                               ms = ca.Read(sp);
+                               ms = ca.Read(sp); //ms wil be eg. "50 ms"
                                LogB.Information("ChronopicAutoCheckDebounce: " + ms);
 
                                if(ms.Length == 0)
                                        LogB.Error("multitest firmware. ms is null");
                                else if(ms[0] == '-') //is negative
                                        LogB.Error("multitest firmware. ms = " + ms);
-                               else if(debounceChange == 50 && ms == "50 ms")
-                                       success = true;
-                               else if(debounceChange == 10 && ms == "10 ms")
+                               else if(debounceChange.ToString() + " ms" == ms)
                                        success = true;
 
                                tryNum --;
                        } while (! success && tryNum > 0);
 
-                       LogB.Debug("multitest firmware. ms = " + ms);
+                       LogB.Information("multitest firmware CHANGED to ms = " + ms);
 
-                       if(ms == "50 ms")
-                               return 50;
-                       else if(ms == "10 ms")
-                               return 10;
+                       if(success)
+                               return true;
                } catch {
                        LogB.Error("Could not change debounce");
                }
 
-               return -1;
+               return false;
        }
 
        //public method to access from guiTests.cs
-       public int TestsChangeMultitestFirmwareDo (int debounceChange)
+       public bool TestsChangeMultitestFirmwareDo (int debounceChange)
        {
                return changeMultitestFirmwareDo(debounceChange);
        }
diff --git a/src/constants.cs b/src/constants.cs
index 3a07e70..1dcd788 100644
--- a/src/constants.cs
+++ b/src/constants.cs
@@ -708,29 +708,6 @@ public class Constants
        }
 
        public enum Menuitem_modes { JUMPSSIMPLE, JUMPSREACTIVE, RUNSSIMPLE, RUNSINTERVALLIC, 
POWERGRAVITATORY, POWERINERTIAL, OTHER }
-       public static bool Menuitem_mode_IsContacts (Menuitem_modes m) 
-       {
-               if(m == Menuitem_modes.POWERGRAVITATORY || m == Menuitem_modes.POWERINERTIAL)
-                       return false;
-               return true;
-       }
-
-       //unused on power
-       private static int menuitem_mode_multitest_debounce (Menuitem_modes m) 
-       {
-               if(m == Menuitem_modes.RUNSSIMPLE || m == Menuitem_modes.RUNSINTERVALLIC)
-                       return 10;
-               
-               return 50;
-       }
-       //know if multitest firmware has to be changed, eg jumps(50) <-> runs(10)
-       public static bool Menuitem_mode_multitest_should_change (Menuitem_modes mPre, Menuitem_modes m) 
-       {
-               if(menuitem_mode_multitest_debounce(mPre) != menuitem_mode_multitest_debounce(m))
-                       return true;
-
-               return false;
-       }
 
        public static string All = "All";
        public static string None = "None";
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 8cadb75..9045a4b 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -263,9 +263,8 @@ public partial class ChronoJumpWindow
        [Widget] Gtk.Box vbox_execute_test;
        [Widget] Gtk.Button button_execute_test;
        [Widget] Gtk.Viewport viewport_chronopics;
-       [Widget] Gtk.Label label_chronopics_multitest;
-       [Widget] Gtk.Label label_chronopic_debounce;
-       [Widget] Gtk.HScale hscale_chronopic_debounce;
+       [Widget] Gtk.Label label_threshold;
+       [Widget] Gtk.HScale hscale_threshold;
        //[Widget] Gtk.Label label_chronopic_encoder;
        //[Widget] Gtk.Image image_chronopic_encoder_no;
        //[Widget] Gtk.Image image_chronopic_encoder_yes;
@@ -473,6 +472,7 @@ public partial class ChronoJumpWindow
 
        ChronopicRegister chronopicRegister;
        Chronopic2016 cp2016;
+       private Threshold threshold;
 
        RestTime restTime;
        //to control method that is updating restTimes on treeview_persons
@@ -636,7 +636,8 @@ public partial class ChronoJumpWindow
                ls.Test();
                LogB.Information(string.Format("coef = {0} {1} {2}", ls.Coef[0], ls.Coef[1], ls.Coef[2]));
 
-               //this is constructed only one time
+               //these are constructed only one time
+               threshold = new Threshold();
                cp2016 = new Chronopic2016();
 
                restTime = new RestTime();
@@ -3000,6 +3001,7 @@ public partial class ChronoJumpWindow
                main_menu.Visible = false;
        }       
        
+       private Constants.Menuitem_modes last_menuitem_mode; //store it to decide not change threshold when 
change from jumps to jumpsRj
        private void select_menuitem_mode_toggled(Constants.Menuitem_modes m) 
        {
                menuitem_mode_selected_jumps_simple.Visible = false;
@@ -3157,29 +3159,27 @@ public partial class ChronoJumpWindow
                //it's not visible at startup
                main_menu.Visible = true;
 
-
-               /*
-               //if wizard has been used mark Chronopic as connected or disconnected depending if port exists
-               if(Constants.Menuitem_mode_IsContacts(m) && wizardPortContacts != "")
-                       chronopicWin.Connected =
-                               Util.FoundInStringArray(ChronopicPorts.GetPorts(), wizardPortContacts);
-               else if(! Constants.Menuitem_mode_IsContacts(m) && wizardPortEncoder != "")
-                       chronopicWin.Connected =
-                               Util.FoundInStringArray(ChronopicPorts.GetPorts(), wizardPortEncoder);
-                               */
-
-
-               //change multitest firmware or autoDetectChronopic
-               //if(Constants.Menuitem_mode_IsContacts(m))
-               //{
-                       //if(chronopicWin.Connected)
-                               //change_multitest_firmware(m);
-
-                       //else
-                       //      autoDetectChronopic(m); //on contacts will perform change_multitest_firmware 
at the end
-               //}
-               //else if(wizardPortEncoder == "")
-               //      autoDetectChronopic(m);
+               if(m != Constants.Menuitem_modes.POWERGRAVITATORY && m != 
Constants.Menuitem_modes.POWERINERTIAL)
+               {
+                       //don't change threshold if changing from jumpssimple to jumpsreactive ...
+                       if(last_menuitem_mode == null ||
+                                       ( m == Constants.Menuitem_modes.JUMPSSIMPLE &&
+                                         last_menuitem_mode != Constants.Menuitem_modes.JUMPSREACTIVE ) ||
+                                       ( m == Constants.Menuitem_modes.JUMPSREACTIVE &&
+                                         last_menuitem_mode != Constants.Menuitem_modes.JUMPSSIMPLE ) ||
+                                       ( m == Constants.Menuitem_modes.RUNSSIMPLE &&
+                                         last_menuitem_mode != Constants.Menuitem_modes.RUNSINTERVALLIC ) ||
+                                       ( m == Constants.Menuitem_modes.RUNSINTERVALLIC &&
+                                         last_menuitem_mode != Constants.Menuitem_modes.RUNSSIMPLE ) ||
+                                       m == Constants.Menuitem_modes.OTHER )
+                       {
+                               if(threshold.SelectTresholdForThisMode(m))
+                               {
+                                       hscale_threshold.Value = threshold.SetHScaleValue();
+                                       last_menuitem_mode = m;
+                               }
+                       }
+               }
 
                chronopicRegisterUpdate(false);
 
@@ -3826,25 +3826,22 @@ public partial class ChronoJumpWindow
 
        //hscale does not manage correctly the +10 increments.
        //we solve it with a label
-       private void on_hscale_chronopic_debounce_value_changed(object o, EventArgs arg)
+       private void on_hscale_threshold_value_changed(object o, EventArgs arg)
        {
-               label_chronopic_debounce.Text = (10 * 
Convert.ToInt32(hscale_chronopic_debounce.Value)).ToString();
+               threshold.UpdateFromGUI(10 * Convert.ToInt32(hscale_threshold.Value));
+               label_threshold.Text = threshold.GetLabel();
        }
 
        private void changeMultitestFirmwareIfNeeded()
        {
                //change multitest stuff
-               int changed = cp2016.ChangeMultitestFirmwareMaybe(getMenuItemMode());
-
-               //TODO: this is debug info. Remove this for 1.6.3
-               if(changed == -1)
-                       label_chronopics_multitest.Text = "??";
-               else if(changed == 50)
-                       label_chronopics_multitest.Text = "50";
-                               //"[" + Catalog.GetString("Jumps") + "]";
-               else if(changed == 10)
-                       label_chronopics_multitest.Text = "10";
-                               //"[" + Catalog.GetString("Runs") + "]";
+               threshold.UpdateAtDatabaseIfNeeded(getMenuItemMode());
+               if(threshold.ShouldUpdateChronopicFirmware())
+               {
+                       bool ok = cp2016.ChangeMultitestFirmwarePre(threshold.GetT);
+                       if(ok)
+                               threshold.ChronopicFirmwareUpdated();
+               }
 
                button_activate_chronopics.Show();
        }
diff --git a/src/gui/guiTests.cs b/src/gui/guiTests.cs
index 132cfbb..65ca207 100644
--- a/src/gui/guiTests.cs
+++ b/src/gui/guiTests.cs
@@ -414,8 +414,8 @@ public partial class ChronoJumpWindow
        {
                LogB.TestStart("chronojumpWindowTestsContactsMultitestDo " + ms.ToString());
 
-               int msRecorded = cp2016.TestsChangeMultitestFirmwareDo (ms);
-               if(msRecorded == ms)
+               bool recordedOk = cp2016.TestsChangeMultitestFirmwareDo (ms);
+               if(recordedOk)
                        testsSuccededCount ++;
                else
                        testsFailedCount ++;
diff --git a/src/preferences.cs b/src/preferences.cs
index 21761f1..dc3cabc 100644
--- a/src/preferences.cs
+++ b/src/preferences.cs
@@ -44,6 +44,10 @@ public class Preferences
        public int runDoubleContactsMS; //milliseconds
        public Constants.DoubleContact runIDoubleContactsMode; //default AVERAGE
        public int runIDoubleContactsMS; //milliseconds
+
+       public int thresholdJumps;
+       public int thresholdRuns;
+       public int thresholdOther;
        
        //encoder capture
        public int encoderCaptureTime;
diff --git a/src/sqlite/main.cs b/src/sqlite/main.cs
index 472ace8..66b93b1 100644
--- a/src/sqlite/main.cs
+++ b/src/sqlite/main.cs
@@ -123,7 +123,7 @@ class Sqlite
        /*
         * Important, change this if there's any update to database
         */
-       static string lastChronojumpDatabaseVersion = "1.33";
+       static string lastChronojumpDatabaseVersion = "1.34";
 
        public Sqlite() {
        }
@@ -1993,6 +1993,15 @@ class Sqlite
 
                                currentVersion = updateVersion("1.33");
                        }
+                       if(currentVersion == "1.33") {
+                               LogB.SQL("Added thresholdJumps, thresholdRuns, thresholdOther to 
preferences");
+
+                               SqlitePreferences.Insert ("thresholdJumps", "50");
+                               SqlitePreferences.Insert ("thresholdRuns", "10");
+                               SqlitePreferences.Insert ("thresholdOther", "50");
+
+                               currentVersion = updateVersion("1.34");
+                       }
 
 
 
@@ -2159,6 +2168,7 @@ class Sqlite
                SqliteChronopicRegister.createTableChronopicRegister();
 
                //changes [from - to - desc]
+               //1.33 - 1.34 Converted DB to 1.34 Added thresholdJumps, thresholdRuns, thresholdOther to 
preferences
                //1.32 - 1.33 Converted DB to 1.33 Added chronopicRegister table
                //1.31 - 1.32 Converted DB to 1.32 encoderCaptureOptionsWin -> preferences
                //1.30 - 1.31 Converted DB to 1.31 Insert encoderCaptureCheckFullyExtended and ...Value at 
preferences
diff --git a/src/sqlite/preferences.cs b/src/sqlite/preferences.cs
index 5f94b1f..562e161 100644
--- a/src/sqlite/preferences.cs
+++ b/src/sqlite/preferences.cs
@@ -77,6 +77,10 @@ class SqlitePreferences : Sqlite
                                                Constants.DoubleContact.AVERAGE.ToString(), dbcmdTr); 
                                Insert ("runIDoubleContactsMS", "300", dbcmdTr);
 
+                               Insert ("thresholdJumps", "50", dbcmdTr);
+                               Insert ("thresholdRuns", "10", dbcmdTr);
+                               Insert ("thresholdOther", "50", dbcmdTr);
+
                                Random rnd = new Random();
                                string machineID = rnd.Next().ToString();
                                Insert ("machineID", machineID, dbcmdTr);
@@ -282,6 +286,12 @@ class SqlitePreferences : Sqlite
                                        Enum.Parse(typeof(Constants.DoubleContact), reader[1].ToString()); 
                        else if(reader[0].ToString() == "runIDoubleContactsMS")
                                preferences.runIDoubleContactsMS = Convert.ToInt32(reader[1].ToString());
+                       else if(reader[0].ToString() == "thresholdJumps")
+                               preferences.thresholdJumps = Convert.ToInt32(reader[1].ToString());
+                       else if(reader[0].ToString() == "thresholdRuns")
+                               preferences.thresholdRuns = Convert.ToInt32(reader[1].ToString());
+                       else if(reader[0].ToString() == "thresholdOther")
+                               preferences.thresholdOther = Convert.ToInt32(reader[1].ToString());
 
                        else if(reader[0].ToString() == "machineID")
                                preferences.machineID = reader[1].ToString();
diff --git a/src/threshold.cs b/src/threshold.cs
new file mode 100644
index 0000000..8504c59
--- /dev/null
+++ b/src/threshold.cs
@@ -0,0 +1,115 @@
+/*
+ * This file is part of ChronoJump
+ *
+ * ChronoJump is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or   
+ * (at your option) any later version.
+ *    
+ * ChronoJump is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * Copyright (C) 2016   Xavier de Blas <xaviblas gmail com> 
+ */
+
+/*
+ * hscale goes from 1 to 10
+ * threshold and label from 10 to 100
+ * this is because there's a problem with hscale from 10 to 100 with increments of 10
+ */
+using System;
+
+public class Threshold
+{
+       private int t;
+       private int t_previous_on_this_mode; //on execute test, to decide if threshold have to be changed on 
SQL
+       private int t_stored_on_chronopic; //to know if has to be changed on chronopic or not
+
+       //only constructed one time
+       public Threshold()
+       {
+               t = 50;
+               t_previous_on_this_mode = t;
+               t_stored_on_chronopic = t;
+       }
+
+       //called when menuitem mode changes
+       public bool SelectTresholdForThisMode(Constants.Menuitem_modes m)
+       {
+               //declare it with default value to solve potential SQL problems
+               string newThreshold = "5";
+
+               //check current mode. Power modes doesn't use threshold
+               newThreshold = SqlitePreferences.Select(nameAtSQL(m), false);
+
+               if(Util.IsNumber(newThreshold, false))
+               {
+                       t = Convert.ToInt32(newThreshold);
+                       t_previous_on_this_mode = t;
+                       return true;
+               }
+               return false;
+       }
+
+       //called when threshold changes by user using the GUI   
+       public void UpdateFromGUI(int newThreshold)
+       {
+               t = newThreshold;
+               /*
+                * t_previous_on_this_mode && t_stored_on_chronopic
+                * are not updated now to don't bother SQL and Chronopic
+                * they are updated on test execution (if needed)
+                */
+       }
+
+       //called on test execution
+       public void UpdateAtDatabaseIfNeeded(Constants.Menuitem_modes m)
+       {
+               if(t != t_previous_on_this_mode)
+               {
+                       SqlitePreferences.Update(nameAtSQL(m), t.ToString(), false);
+                       t_previous_on_this_mode = t;
+               }
+       }
+
+       public bool ShouldUpdateChronopicFirmware()
+       {
+               return t != t_stored_on_chronopic;
+       }
+       public void ChronopicFirmwareUpdated()
+       {
+               t_stored_on_chronopic = t;
+       }
+
+       public string GetLabel()
+       {
+               return t.ToString();
+       }
+
+       public int SetHScaleValue()
+       {
+               return Convert.ToInt32(t / 10);
+       }
+
+       private string nameAtSQL(Constants.Menuitem_modes m)
+       {
+               if(m == Constants.Menuitem_modes.JUMPSSIMPLE || m == Constants.Menuitem_modes.JUMPSREACTIVE)
+                       return "thresholdJumps";
+               else if(m == Constants.Menuitem_modes.RUNSSIMPLE || m == 
Constants.Menuitem_modes.RUNSINTERVALLIC)
+                       return "thresholdRuns";
+               else // (m == Constants.Menuitem_modes.OTHER)
+                       return "thresholdOther";
+       }
+
+       //T is reserved word, so use GetT
+       public int GetT
+       {
+               get { return t; }
+       }       
+}


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