[chronojump] Autodetect Encoder and normal Chronopic on menu change. WIP
- From: Xavier de Blas <xaviblas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] Autodetect Encoder and normal Chronopic on menu change. WIP
- Date: Thu, 26 Mar 2015 20:50:58 +0000 (UTC)
commit 12f9d41a02d73086a566d553c9f633ee07969995
Author: Xavier de Blas <xaviblas gmail com>
Date: Thu Mar 26 21:50:03 2015 +0100
Autodetect Encoder and normal Chronopic on menu change. WIP
src/chronopic.cs | 167 ++++++++++++++++++++++++++++++++++++++++++++++++-
src/gui/chronojump.cs | 21 ++++++-
2 files changed, 186 insertions(+), 2 deletions(-)
---
diff --git a/src/chronopic.cs b/src/chronopic.cs
index ca45ea3..5e61744 100644
--- a/src/chronopic.cs
+++ b/src/chronopic.cs
@@ -407,6 +407,8 @@ public abstract class ChronopicAuto
protected internal abstract string Communicate();
private string str;
public string CharToSend = "";
+ public bool IsEncoder = false;
+ public bool Found;
private bool make(SerialPort sp)
{
@@ -420,10 +422,17 @@ public abstract class ChronopicAuto
sp.Close(); //close to ensure no bytes are comming
sp.Open();
+
+ if(IsEncoder)
+ setEncoderBauds();
str = "";
return true;
}
+ private void close(SerialPort sp) {
+ sp.Close();
+ }
+
//'template method'
public string Read(SerialPort sp)
@@ -431,14 +440,24 @@ public abstract class ChronopicAuto
if ( ! make(sp) )
return "Error sp == null";
+ //bool needToFlush = false;
try {
str = Communicate();
} catch {
//this.error=ErrorType.Timeout;
LogB.Warning("Error or Timeout. This is not Chronopic-Automatic-Firmware");
str = "Error / not Multitest firmware";
+
+ //needToFlush = true;
}
+ /*
+ if(needToFlush)
+ flush();
+ */
+
+ close(sp);
+
return str;
}
@@ -450,34 +469,119 @@ public abstract class ChronopicAuto
sendNum = num;
+ //bool needToFlush = false;
try {
str = Communicate();
} catch {
//this.error=ErrorType.Timeout;
LogB.Warning("Error or Timeout. This is not Chronopic-Automatic-Firmware");
str = "Error / not Multitest firmware";
+
+ //needToFlush = true;
}
+
+ /*
+ if(needToFlush)
+ flush();
+ */
+
+ close(sp);
return str;
}
+
+ private void setEncoderBauds()
+ {
+ sp.BaudRate = 115200; //encoder, 20MHz
+ LogB.Information("sp.BaudRate = 115200 bauds");
+ }
+
+ /*
+ protected void flush()
+ {
+ LogB.Information("Flushing");
+
+ //-- Esperar un tiempo y vaciar buffer
+ Thread.Sleep(500); //ErrorTimeout);
+
+ byte[] buffer = new byte[256];
+ sp.Read(buffer,0,256); //flush
+
+ bool success = false;
+ try {
+ do{
+ sp.Read(buffer,0,256);
+ success = true;
+ LogB.Debug(" spReaded ");
+ } while(! success);
+ } catch {
+ LogB.Information("Cannot flush");
+ return;
+ }
+
+ LogB.Information("Flushed");
+ }
+ */
}
public class ChronopicAutoCheck : ChronopicAuto
{
protected internal override string Communicate()
{
+ Found = false;
+
sp.Write("J");
IsChronopicAuto = ( (char) sp.ReadByte() == 'J');
- if (IsChronopicAuto) {
+ if (IsChronopicAuto)
+ {
sp.Write("V");
int major = (char) sp.ReadByte() - '0';
sp.ReadByte(); //.
int minor = (char) sp.ReadByte() - '0';
+
+ Found = true;
return "Yes! v" + major.ToString() + "." + minor.ToString();
}
+
return "Please update it\nwith Chronopic-firmwarecord";
}
}
+//only for encoder
+public class ChronopicAutoCheckEncoder : ChronopicAuto
+{
+ protected internal override string Communicate()
+ {
+ LogB.Information("Communicate start ...");
+
+ Found = false;
+
+ char myByte;
+ for(int i = 0; i < 30; i ++)
+ {
+ LogB.Debug("writting ...");
+
+ sp.Write("J");
+
+ LogB.Debug("reading ...");
+
+ myByte = (char) sp.ReadByte();
+
+ LogB.Debug("readed");
+ if(myByte != null && myByte.ToString() != "")
+ LogB.Information(myByte.ToString());
+
+ if(myByte == 'J') {
+ LogB.Information("Encoder found!");
+
+ Found = true;
+ return "1";
+ }
+ }
+
+ return "0";
+ }
+}
+
public class ChronopicAutoCheckDebounce : ChronopicAuto
{
@@ -516,3 +620,64 @@ public class ChronopicStartReactionTime : ChronopicAuto
return "SUCCESS";
}
}
+
+public class ChronopicAutoDetect
+{
+ public enum ChronopicType { NORMAL, ENCODER }
+ public string Detected; // portname if detected, if not will be ""
+
+ public ChronopicAutoDetect(ChronopicType type)
+ {
+ /*
+ * Try to detect a normal 4MHz Chronopic on a 20MHz encoder fails
+ * but encoder can be used normally
+ * In the other hand, try to detect an encoder on a 4MHz Chronopic fails
+ * but encoder cannot be used until 'reset' or disconnect cable (and can be problems with
Chronojump GUI)
+ *
+ * So the solution is:
+ * if we are searching encoder, on every port first check if 4MHz connection can be
stablished, if it's Found, then normal Chronopic is found
+ * if is not Fount, then search for the encoder.
+ *
+ * The only problem is in normal Chronopics with old firmware (without the 'J' read/write
+ * 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.
+ }
+
+ autoDetect(ca);
+ }
+
+ private void autoDetect(ChronopicAuto ca)
+ {
+ LogB.Information("starting port detection");
+
+ string [] usbSerial;
+ if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.LINUX)
+ usbSerial = Directory.GetFiles("/dev/", "ttyUSB*");
+ else if(UtilAll.GetOSEnum() == UtilAll.OperatingSystems.MACOSX)
+ usbSerial = Directory.GetFiles("/dev/", "tty.usbserial*");
+ else // WINDOWS
+ usbSerial = SerialPort.GetPortNames();
+
+ 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;
+ }
+ }
+ Detected = "";
+ }
+}
diff --git a/src/gui/chronojump.cs b/src/gui/chronojump.cs
index 0a037de..c15103b 100644
--- a/src/gui/chronojump.cs
+++ b/src/gui/chronojump.cs
@@ -2873,9 +2873,28 @@ public partial class ChronoJumpWindow
//it's not visible at startup
session_menuitem.Visible = true;
menuitem_mode.Visible = true;
-
+
+ autoDetectChronopic(m);
+
change_multitest_firmware(m);
}
+
+ private void autoDetectChronopic(menuitem_modes m)
+ {
+ ChronopicAutoDetect cad;
+ if(m == menuitem_modes.POWER) {
+ LogB.Information("Detecting encoder... ");
+ cad = new ChronopicAutoDetect(ChronopicAutoDetect.ChronopicType.ENCODER);
+ } else {
+ LogB.Information("Detecting normal Chronopic... ");
+ cad = new ChronopicAutoDetect(ChronopicAutoDetect.ChronopicType.NORMAL);
+ }
+
+ 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)
private void change_multitest_firmware(menuitem_modes m)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]