From bb4d376e874d7aec8b878fdaa229b212a6f06fd3 Mon Sep 17 00:00:00 2001 From: tzim Date: Thu, 9 May 2013 19:10:23 +0000 Subject: [PATCH] Ajout driver boitier V1 --- DMX-2.0/Conduite.cs | 10 +++ DMX-2.0/DMX-2.0.csproj | 2 + DMX-2.0/DriverBoitierV1.cs | 139 +++++++++++++++++++++++++++++ DMX-2.0/DriverDMX.cs | 33 +++++++ DMX-2.0/Main.cs | 3 + DMX-2.0/MainWindow.cs | 7 ++ DMX-2.0/UniversDMX.cs | 11 ++- DMX-2.0/gtk-gui/DMX2.MainWindow.cs | 6 +- DMX-2.0/gtk-gui/gui.stetic | 7 ++ 9 files changed, 213 insertions(+), 5 deletions(-) create mode 100644 DMX-2.0/DriverBoitierV1.cs create mode 100644 DMX-2.0/DriverDMX.cs diff --git a/DMX-2.0/Conduite.cs b/DMX-2.0/Conduite.cs index 2dc7863..a3c7bb6 100644 --- a/DMX-2.0/Conduite.cs +++ b/DMX-2.0/Conduite.cs @@ -21,6 +21,8 @@ namespace DMX2 List circuits = new List(); List univers = new List(); + List drivers = new List(); + public Conduite() { @@ -78,6 +80,12 @@ namespace DMX2 } } + public List Drivers { + get { + return drivers; + } + } + int IComparer.Compare (Circuit x, Circuit y) { return Conduite.Courante.circuits.IndexOf(x) - @@ -151,6 +159,8 @@ namespace DMX2 disposed=true; if(timer!=null) timer.Dispose(); + foreach(var driver in Drivers) + driver.Dispose(); timer=null; } diff --git a/DMX-2.0/DMX-2.0.csproj b/DMX-2.0/DMX-2.0.csproj index 2ef7277..3ab001c 100644 --- a/DMX-2.0/DMX-2.0.csproj +++ b/DMX-2.0/DMX-2.0.csproj @@ -104,6 +104,8 @@ + + \ No newline at end of file diff --git a/DMX-2.0/DriverBoitierV1.cs b/DMX-2.0/DriverBoitierV1.cs new file mode 100644 index 0000000..aaa3a9c --- /dev/null +++ b/DMX-2.0/DriverBoitierV1.cs @@ -0,0 +1,139 @@ +using System; +using System.Threading; +using System.IO.Ports; + +namespace DMX2 +{ + public class DriverBoitierV1 : DriverDMX + { + // tampons Entrée/Sortie + byte[] inputbuffer = new byte[1]; + byte[] outputbuffer = new byte[260]; + + //Thread de boucle + Thread loopthread=null; + volatile bool running=true; + + UniversDMX patch=null; + + string portname = "/dev/ttyUSB0"; + SerialPort serial = null; + + public DriverBoitierV1 () + { + patch = Conduite.Courante.Patches[0]; + Start(); + outputbuffer[0]=27; + outputbuffer[1]=68; + outputbuffer[4]=255; + } + + + void Start () + { + OpenPort(); + if (loopthread == null) { + loopthread = new Thread(new ThreadStart(MainLoop)); + loopthread.Start(); + } + } + + void OpenPort () + { + if (serial != null) { + serial.Close(); + } + serial = new SerialPort(portname, 460800,Parity.None,8,StopBits.One); + serial.DtrEnable = false; + serial.ReadTimeout = 15; + serial.WriteTimeout = 200; + serial.Open(); + } + + bool CheckPortStatus () + { + if(serial.IsOpen) return true; + + OpenPort(); + + return false; + } + + + void MainLoop () + { + DateTime prochainEnvoi= DateTime.Now; + TimeSpan sleeptime; + while (running) { + lock(Conduite.Courante) + { + patch.CalculUnivers(outputbuffer,5,255); + } + + if(!CheckPortStatus()) + { + Thread.Sleep(1000); + continue; + } + + sleeptime = prochainEnvoi - DateTime.Now; + if(sleeptime.TotalMilliseconds>1) + Thread.Sleep(sleeptime); + + prochainEnvoi = DateTime.Now.AddMilliseconds(22); + Envoi(); + Reception(); + + } + } + + void Envoi () + { + try { + + if(!serial.IsOpen) return; + serial.Write(outputbuffer,0,outputbuffer.Length); + + } catch (TimeoutException ex) { + serial.Close(); + } + } + + void Reception () + { + try { + + if(!serial.IsOpen) return; + + serial.Read(inputbuffer,0,inputbuffer.Length); + Console.WriteLine(inputbuffer[0]); + + } catch (TimeoutException ex) { + serial.Close(); + } + } + + + #region implemented abstract members of DMX2.DriverDMX + public override Gtk.Window GetDialog () + { + return null; + } + #endregion + + public override void Dispose () + { + disposed = true; + running = false; + loopthread.Join(); + loopthread=null; + + //TODO : Close Port + if(serial != null) + serial.Close(); + + } + + } +} + diff --git a/DMX-2.0/DriverDMX.cs b/DMX-2.0/DriverDMX.cs new file mode 100644 index 0000000..dd5cde2 --- /dev/null +++ b/DMX-2.0/DriverDMX.cs @@ -0,0 +1,33 @@ +using System; +using System.Threading; + +namespace DMX2 +{ + public abstract class DriverDMX: IDisposable + { + + public DriverDMX () + { + + } + + public abstract Gtk.Window GetDialog(); + + protected bool disposed = false; + + #region IDisposable implementation + + public virtual void Dispose() + { + disposed = true; + } + + void IDisposable.Dispose () + { + if(!disposed) + Dispose(); + } + #endregion + } +} + diff --git a/DMX-2.0/Main.cs b/DMX-2.0/Main.cs index 1084a95..43ca51c 100644 --- a/DMX-2.0/Main.cs +++ b/DMX-2.0/Main.cs @@ -14,6 +14,9 @@ namespace DMX2 win.Show (); Application.Run (); + if (Conduite.Courante != null) { + Conduite.Courante.Dispose(); + } } } } \ No newline at end of file diff --git a/DMX-2.0/MainWindow.cs b/DMX-2.0/MainWindow.cs index 1e5e64f..2e3ec8a 100644 --- a/DMX-2.0/MainWindow.cs +++ b/DMX-2.0/MainWindow.cs @@ -187,8 +187,15 @@ namespace DMX2 foreach (Sequenceur s in Conduite.Courante.Sequenceurs) seqUiVbox.Add(s.GetUI()); + } + protected void OnConnectActionActivated (object sender, EventArgs e) + { + + Conduite.Courante.Drivers.Add( new DriverBoitierV1()); + } + } } \ No newline at end of file diff --git a/DMX-2.0/UniversDMX.cs b/DMX-2.0/UniversDMX.cs index 80bc61a..28ae752 100644 --- a/DMX-2.0/UniversDMX.cs +++ b/DMX-2.0/UniversDMX.cs @@ -59,14 +59,14 @@ namespace DMX2 { } - public void CalculUnivers(byte[] valeurs) + public void CalculUnivers(byte[] valeurs, int offset, int count) { Dimmer g; Debug.Assert(valeurs.Length == _dimmers.Length); - for(int i = 0 ; i<512; i++) + for(int i = 0 ; i"); + this.UIManager.AddUiFromString (""); this.toolbar7 = ((global::Gtk.Toolbar)(this.UIManager.GetWidget ("/toolbar7"))); this.toolbar7.Name = "toolbar7"; this.toolbar7.ShowArrow = false; @@ -389,6 +392,7 @@ namespace DMX2 this.fullscreenAction1.Activated += new global::System.EventHandler (this.OnFullscreenAction1Activated); this.showAllAction.Activated += new global::System.EventHandler (this.OnShowAllActionActivated); this.universAction.Activated += new global::System.EventHandler (this.OnUniversActionActivated); + this.connectAction.Activated += new global::System.EventHandler (this.OnConnectActionActivated); this.masterScale.ValueChanged += new global::System.EventHandler (this.OnMasterScaleValueChanged); } } diff --git a/DMX-2.0/gtk-gui/gui.stetic b/DMX-2.0/gtk-gui/gui.stetic index 536068b..6fadc57 100644 --- a/DMX-2.0/gtk-gui/gui.stetic +++ b/DMX-2.0/gtk-gui/gui.stetic @@ -159,6 +159,12 @@ gtk-execute + + Action + + gtk-connect + + MainWindow @@ -424,6 +430,7 @@ +