From 369f1934d3ad1b57c628203de4d50b2802cb44d0 Mon Sep 17 00:00:00 2001 From: Jens Christian True Date: Mon, 2 May 2022 16:41:16 +0200 Subject: [PATCH] Start of gentle refactoring. --- Tidstagning/Entry.cs | 18 ++++++++++++++++-- Tidstagning/MainUI.cs | 13 +++++-------- Tidstagning/Relay.cs | 19 ++++++++++++++++--- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/Tidstagning/Entry.cs b/Tidstagning/Entry.cs index b4615be..42ad4fa 100644 --- a/Tidstagning/Entry.cs +++ b/Tidstagning/Entry.cs @@ -4,11 +4,24 @@ namespace Tidstagning { class Entry { - + enum Score + { + -, + DNC, + DNS, + OCS, + DNF, + RET, + DSQ + } + //Competitors name public string Name { get; set; } + //Competitor identifier (Sailnumber/Bownumber or other) public string SailNumber { get; set; } + //DateTime the user completed the course public DateTime Completed_Time { get; set; } - + //Retirement score. + public Score Code {get; set; } public Entry(string name, string number) { @@ -24,6 +37,7 @@ namespace Tidstagning public void DNF() { + Code = DNF; this.Completed_Time = DateTime.Now; } } diff --git a/Tidstagning/MainUI.cs b/Tidstagning/MainUI.cs index 94fa9b2..b2538c0 100644 --- a/Tidstagning/MainUI.cs +++ b/Tidstagning/MainUI.cs @@ -12,7 +12,7 @@ namespace Tidstagning }; ResultList? liste; - Relay? horn; + Relay horn = new Relay(); Procedure startprocedure = new Procedure(); public MainUI(bool ShowConfig = false) @@ -94,8 +94,7 @@ namespace Tidstagning grid.CurrentCell = null; entries.RemoveAt(e.RowIndex); - if(horn is not null) - horn.Sound((uint)numericSignalLength.Value); + horn.Sound((uint)numericSignalLength.Value); } grid.Refresh(); } @@ -116,8 +115,7 @@ namespace Tidstagning if (checkStartProcedure.Checked) { liste.Write("Automatisk Start Procedure er aktiv."); - if(horn is not null) - startprocedure.setObjects(horn, liste); + startprocedure.setObjects(horn, liste); } else { @@ -147,8 +145,7 @@ namespace Tidstagning private void btnTest_Click(object sender, EventArgs e) { - if(horn is not null) - horn.Sound((uint)numericSignalLength.Value); + horn.Sound((uint)Properties.Settings.Default.SignalLength); } private void Clock_Tick(object sender, EventArgs e) @@ -164,7 +161,7 @@ namespace Tidstagning { if (comboComport is not null && comboComport.Items.Count != 0 && comboComport.SelectedItem is not null) { - horn = new Relay(comboComport.SelectedItem.ToString()); + horn.Open(comboComport.SelectedItem.ToString()); Properties.Settings.Default.ComPort = comboComport.SelectedItem.ToString(); Properties.Settings.Default.Save(); } diff --git a/Tidstagning/Relay.cs b/Tidstagning/Relay.cs index a127454..a0ac7e3 100644 --- a/Tidstagning/Relay.cs +++ b/Tidstagning/Relay.cs @@ -8,7 +8,7 @@ namespace Tidstagning { class Relay { - SerialPort com; + SerialPort com = new SerialPort(); struct SignalSpec { @@ -17,9 +17,16 @@ namespace Tidstagning }; Queue signals = new Queue(); bool processing_queue = false; - public Relay(string ComPort) + + public Relay() { - com = new SerialPort(); + } + + public Open(string ComPort) + { + if(SerialPort.IsOpen()) + SerialPort.Close(); + com.PortName = ComPort; com.BaudRate = 9600; try @@ -51,6 +58,12 @@ namespace Tidstagning */ public void Sound(uint time) { + if(!com.IsOpen) + { + Debug.WriteLine("ComPort not open"); + return; + } + Debug.WriteLine("Requesting horn for: " + time.ToString() + "ms"); SignalSpec on_signal; on_signal.duration = time;