diff --git a/src/DCSFPCommon b/src/DCSFPCommon index 6bcc136af..4f52368f9 160000 --- a/src/DCSFPCommon +++ b/src/DCSFPCommon @@ -1 +1 @@ -Subproject commit 6bcc136afda8f67129d3071f5febf86caf51d18f +Subproject commit 4f52368f99bb55bc5a747b27a47b03aee50fd633 diff --git a/src/DCSFlightpanels.sln.DotSettings b/src/DCSFlightpanels.sln.DotSettings index e40191991..e77f63f77 100644 --- a/src/DCSFlightpanels.sln.DotSettings +++ b/src/DCSFlightpanels.sln.DotSettings @@ -10,6 +10,7 @@ ARN AV BIOS + BIP BIPS BLUE BNA @@ -25,6 +26,7 @@ DCSBIOSU DCSFP DED + DRO EUFD FA FM @@ -49,10 +51,12 @@ LCD LCDPZ LE + LED LF MB MK MT + NADIR NOUSE NS OK @@ -60,6 +64,7 @@ OS OSCOMMANDS PURPLE + PZ RED REF SA @@ -108,7 +113,9 @@ XPDR True True + True True + True True True @@ -159,6 +166,7 @@ True True True + True True True True diff --git a/src/NonVisuals/BindingClasses/DCSBIOSBindings/DCSBIOSActionBindingBase.cs b/src/NonVisuals/BindingClasses/DCSBIOSBindings/DCSBIOSActionBindingBase.cs index 8aeeb98c1..fe5893cfb 100644 --- a/src/NonVisuals/BindingClasses/DCSBIOSBindings/DCSBIOSActionBindingBase.cs +++ b/src/NonVisuals/BindingClasses/DCSBIOSBindings/DCSBIOSActionBindingBase.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Text; using System.Threading; +using System.Threading.Tasks; using ClassLibraryCommon; using DCS_BIOS.Serialized; using Newtonsoft.Json; @@ -25,8 +26,9 @@ public abstract class DCSBIOSActionBindingBase : IDisposable internal static Logger Logger = LogManager.GetCurrentClassLogger(); private bool _whenOnTurnedOn = true; private string _description; - [NonSerialized] private Thread _sendDCSBIOSCommandsThread; private volatile List _dcsbiosInputs; + private Task _sendingCommandsTask; + [JsonIgnore] public bool HasSequence => _dcsbiosInputs.Count > 1; internal abstract void ImportSettings(string settings); public abstract string ExportSettings(); @@ -61,14 +63,7 @@ public void Dispose() public bool IsRunning() { - if (_sendDCSBIOSCommandsThread != null && (_sendDCSBIOSCommandsThread.ThreadState == ThreadState.Running || - _sendDCSBIOSCommandsThread.ThreadState == ThreadState.WaitSleepJoin || - _sendDCSBIOSCommandsThread.ThreadState == ThreadState.Unstarted)) - { - return true; - } - - return false; + return _sendingCommandsTask != null && _sendingCommandsTask.Status == TaskStatus.Running; } @@ -83,17 +78,8 @@ public bool IsRunning() * * 3) Command is repeatable, for single commands only. Just keep executing the command, use thread. */ - public void SendDCSBIOSCommands(CancellationToken cancellationToken) + public async Task SendDCSBIOSCommandsAsync(CancellationToken cancellationToken) { - var repeatable = false; - - - if (this is ActionTypeDCSBIOS) - { - //Ugly, only Streamdeck has IsRepeatable - repeatable = ((ActionTypeDCSBIOS)this).IsRepeatable(); - } - if (DCSBIOSInputs.Count == 0) { return; @@ -101,7 +87,7 @@ public void SendDCSBIOSCommands(CancellationToken cancellationToken) if (_isSequenced && _sequenceIndex <= DCSBIOSInputs.Count - 1) { - DCSBIOSInputs[_sequenceIndex].SelectedDCSBIOSInterface.SendCommand(); + await DCSBIOSInputs[_sequenceIndex].SelectedDCSBIOSInterface.SendCommand(); _sequenceIndex++; if (_sequenceIndex >= DCSBIOSInputs.Count) @@ -116,12 +102,11 @@ public void SendDCSBIOSCommands(CancellationToken cancellationToken) { Thread.Sleep(200); } - - _sendDCSBIOSCommandsThread = new Thread(() => SendDCSBIOSCommandsThread(DCSBIOSInputs, cancellationToken)); - _sendDCSBIOSCommandsThread.Start(); + + _sendingCommandsTask = Task.Run(() => SendDCSBIOSCommandsAsync(DCSBIOSInputs, cancellationToken)); } - private void SendDCSBIOSCommandsThread(List dcsbiosInputs, CancellationToken cancellationToken) + private async Task SendDCSBIOSCommandsAsync(List dcsbiosInputs, CancellationToken cancellationToken) { try { @@ -129,7 +114,7 @@ private void SendDCSBIOSCommandsThread(List dcsbiosInputs, Cancell { Thread.Sleep(dcsbiosInput.SelectedDCSBIOSInterface.Delay); - dcsbiosInput.SelectedDCSBIOSInterface.SendCommand(); + await dcsbiosInput.SelectedDCSBIOSInterface.SendCommand(); if (_shutdownCommandsThread || cancellationToken.IsCancellationRequested) { @@ -137,8 +122,6 @@ private void SendDCSBIOSCommandsThread(List dcsbiosInputs, Cancell } } } - catch (ThreadAbortException) - { } catch (Exception ex) { Logger.Error(ex); diff --git a/src/NonVisuals/CockpitMaster/Panels/CDU737PanelBase.cs b/src/NonVisuals/CockpitMaster/Panels/CDU737PanelBase.cs index b9e2cd7b8..a3d3d5a20 100644 --- a/src/NonVisuals/CockpitMaster/Panels/CDU737PanelBase.cs +++ b/src/NonVisuals/CockpitMaster/Panels/CDU737PanelBase.cs @@ -1,4 +1,6 @@ -namespace NonVisuals.CockpitMaster.Panels +using System.Threading.Tasks; + +namespace NonVisuals.CockpitMaster.Panels { using System; using System.Collections.Generic; @@ -210,9 +212,8 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { - } public Dictionary ConvertTable { @@ -435,7 +436,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } - private void OnReport(HidReport report) + private async Task OnReport(HidReport report) { if (TypeOfPanel == GamingPanelEnum.CDU737 && report.Data.Length == 64) { @@ -444,7 +445,7 @@ private void OnReport(HidReport report) HashSet hashSet = GetHashSetOfChangedKnobs(OldCDUPanelValues, NewCDUPanelValues); if (hashSet.Count > 0) { - GamingPanelKnobChanged(!FirstReportHasBeenRead, hashSet); + await GamingPanelKnobChangedAsync(!FirstReportHasBeenRead, hashSet); AppEventHandler.SwitchesChanged(this, HIDSkeletonBase.HIDInstance, TypeOfPanel, hashSet); } diff --git a/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelA10C.cs b/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelA10C.cs index c1bd47f47..14d0e56cd 100644 --- a/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelA10C.cs +++ b/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelA10C.cs @@ -5,6 +5,7 @@ using DCS_BIOS.Interfaces; using NonVisuals.CockpitMaster.Switches; using System.Collections.Generic; +using System.Threading.Tasks; using NonVisuals.HID; using DCS_BIOS.Serialized; using DCS_BIOS.ControlLocator; @@ -89,7 +90,7 @@ protected override void Dispose(bool disposing) // Call base class implementation. base.Dispose(disposing); } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -100,7 +101,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -124,7 +125,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable> 8; - KeyboardBrightness= eufdBright >>8; + KeyboardBrightness = eufdBright >> 8; refreshLedsAndBrightness(); } // AH - 64D / PLT_MASTER_WARNING_L (shouldUpdate, newValue) = ShouldHandleDCSBiosData(e, _PLT_MASTER_WARNING_L); - if(shouldUpdate) + if (shouldUpdate) { if (newValue == 1) { @@ -195,7 +196,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { SetLine(0, string.Format("{0,24}", incomingData)); } - + } if (e.Address.Equals(_PLT_EUFD_LINE1.Address)) @@ -203,9 +204,9 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) incomingData = e.StringData.Substring(38, 17); if (HandleStringData(1, e, incomingData, ref linesChanged)) { - SetLine(1, string.Format("{0,24}", incomingData )); + SetLine(1, string.Format("{0,24}", incomingData)); } - + } if (e.Address.Equals(_PLT_EUFD_LINE2.Address)) @@ -254,9 +255,9 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) incomingData = e.StringData.Substring(0, 18); if (HandleStringData(7, e, incomingData, ref linesChanged)) { - SetLine(7, incomingData ); + SetLine(7, incomingData); } - + } if (e.Address.Equals(_PLT_EUFD_LINE9.Address)) { diff --git a/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelF14.cs b/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelF14.cs index 16124e685..5f50470f0 100644 --- a/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelF14.cs +++ b/src/NonVisuals/CockpitMaster/Preprogrammed/CDU737PanelF14.cs @@ -5,6 +5,7 @@ using DCS_BIOS.Interfaces; using NonVisuals.CockpitMaster.Switches; using System.Collections.Generic; +using System.Threading.Tasks; using NonVisuals.HID; using DCS_BIOS.Serialized; using DCS_BIOS.ControlLocator; @@ -149,7 +150,7 @@ protected override void Dispose(bool disposing) // Call base class implementation. base.Dispose(disposing); } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -160,7 +161,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -132,7 +133,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -71,7 +72,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (isFirstReport) { @@ -74,7 +75,7 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable /// - public bool Click(string dcsBIOSCommand) + public async Task ClickAsync(string dcsBIOSCommand) { _clicksDetected++; if (_clicksDetected <= ClicksToSkip) @@ -50,7 +51,7 @@ public bool Click(string dcsBIOSCommand) if (!string.IsNullOrEmpty(dcsBIOSCommand)) { - DCSBIOS.Send(dcsBIOSCommand); + await DCSBIOS.SendAsync(dcsBIOSCommand); } return false; diff --git a/src/NonVisuals/Interfaces/IStreamDeckButtonAction.cs b/src/NonVisuals/Interfaces/IStreamDeckButtonAction.cs index 322cbc58d..9ab84f6c5 100644 --- a/src/NonVisuals/Interfaces/IStreamDeckButtonAction.cs +++ b/src/NonVisuals/Interfaces/IStreamDeckButtonAction.cs @@ -1,4 +1,6 @@ -namespace NonVisuals.Interfaces +using System.Threading.Tasks; + +namespace NonVisuals.Interfaces { using System.Threading; using NonVisuals.Panels.StreamDeck.Panels; @@ -20,7 +22,7 @@ public interface IStreamDeckButtonAction string ActionDescription { get; } - void Execute(CancellationToken threadCancellationToken, bool executeOnce = false); + Task ExecuteAsync(CancellationToken threadCancellationToken, bool executeOnce = false); bool IsRunning(); diff --git a/src/NonVisuals/KeyEmulation/KeyPress.cs b/src/NonVisuals/KeyEmulation/KeyPress.cs index 9f8ad8579..13126e920 100644 --- a/src/NonVisuals/KeyEmulation/KeyPress.cs +++ b/src/NonVisuals/KeyEmulation/KeyPress.cs @@ -163,7 +163,6 @@ private void ExecuteKeyPresses(SortedList sortedList, Cancel if (Common.APIModeUsed == APIModeEnum.KeybdEvent) { KeyBdEvenAPI.Press(keyPressInfo.LengthOfBreak, keyPressInfo.VirtualKeyCodes.ToArray(), keyPressInfo.LengthOfKeyPress, _cancellationTokenSource.Token,outerCancellationToken, SLEEP_VALUE); - // Common.DebugP("KeyBdEventAPI result code -----------------------------------> " + Marshal.GetLastWin32Error()); } else { diff --git a/src/NonVisuals/Panels/GamingPanel.cs b/src/NonVisuals/Panels/GamingPanel.cs index 60e94766f..7b968b818 100644 --- a/src/NonVisuals/Panels/GamingPanel.cs +++ b/src/NonVisuals/Panels/GamingPanel.cs @@ -1,4 +1,6 @@ -namespace NonVisuals.Panels +using System.Threading.Tasks; + +namespace NonVisuals.Panels { using System; using System.Collections.Generic; @@ -24,7 +26,7 @@ public abstract class GamingPanel : IProfileHandlerListener, IDcsBiosDataListene public abstract void SavePanelSettings(object sender, ProfileHandlerEventArgs e); public abstract void SavePanelSettingsJSON(object sender, ProfileHandlerEventArgs e); public abstract void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e); - protected abstract void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet); + protected abstract Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet); protected abstract void StartListeningForHidPanelChanges(); private readonly DCSBIOSOutput _updateCounterDCSBIOSOutput; diff --git a/src/NonVisuals/Panels/Saitek/Panels/BacklitPanelBIP.cs b/src/NonVisuals/Panels/Saitek/Panels/BacklitPanelBIP.cs index ade9f4bf2..54bca4f9c 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/BacklitPanelBIP.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/BacklitPanelBIP.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Panels.Saitek.Panels { @@ -213,7 +214,7 @@ internal void CheckDcsDataForColorChangeHook(uint address, uint data) } } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { // Nothing needs to be done here } diff --git a/src/NonVisuals/Panels/Saitek/Panels/FarmingSidePanel.cs b/src/NonVisuals/Panels/Saitek/Panels/FarmingSidePanel.cs index a6d65fbf1..2c729b90f 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/FarmingSidePanel.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/FarmingSidePanel.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.DCSBIOSBindings; using NonVisuals.BindingClasses.Key; using NonVisuals.BindingClasses.OSCommand; @@ -232,7 +233,7 @@ public List OSCommandList set => _operatingSystemCommandBindings = value; } - private void FarmingSidePanelSwitchChanged(bool isFirstReport, IEnumerable hashSet) + private async Task FarmingSidePanelSwitchChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (!ForwardPanelEvent) { @@ -308,7 +309,7 @@ private void FarmingSidePanelSwitchChanged(bool isFirstReport, IEnumerable 0 && dcsBiosBinding.FarmingPanelKey == farmingPanelKey.FarmingPanelMKKey && dcsBiosBinding.WhenTurnedOn == farmingPanelKey.IsOn) { - dcsBiosBinding.SendDCSBIOSCommands(new CancellationToken()); + await dcsBiosBinding.SendDCSBIOSCommandsAsync(new CancellationToken()); break; } } @@ -599,9 +600,9 @@ public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff p } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { - FarmingSidePanelSwitchChanged(isFirstReport, hashSet); + await FarmingSidePanelSwitchChangedAsync(isFirstReport, hashSet); } private void CreateKeys() diff --git a/src/NonVisuals/Panels/Saitek/Panels/MultiPanelPZ70.cs b/src/NonVisuals/Panels/Saitek/Panels/MultiPanelPZ70.cs index 8de851ba1..c4153e364 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/MultiPanelPZ70.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/MultiPanelPZ70.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.DCSBIOSBindings; using NonVisuals.BindingClasses.Key; using NonVisuals.BindingClasses.OSCommand; @@ -372,11 +373,11 @@ private void ShowIdentifyingValue() } } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { // Set _selectedMode and LCD button statuses // and performs the actual actions for key presses - PZ70SwitchChanged(isFirstReport, hashSet); + await PZ70SwitchChangedAsync(isFirstReport, hashSet); } public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) @@ -697,7 +698,7 @@ public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff p } } - private void PZ70SwitchChanged(bool isFirstReport, IEnumerable hashSet) + private async Task PZ70SwitchChangedAsync(bool isFirstReport, IEnumerable hashSet) { foreach (var o in hashSet) { @@ -887,7 +888,7 @@ private void PZ70SwitchChanged(bool isFirstReport, IEnumerable hashSet) if (dcsBiosBinding.DialPosition == _pz70DialPosition && dcsBiosBinding.DCSBIOSInputs.Count > 0 && dcsBiosBinding.MultiPanelPZ70Knob == multiPanelKnob.MultiPanelPZ70Knob && dcsBiosBinding.WhenTurnedOn == multiPanelKnob.IsOn) { - dcsBiosBinding.SendDCSBIOSCommands(new CancellationToken()); + await dcsBiosBinding.SendDCSBIOSCommandsAsync(new CancellationToken()); break; } } @@ -1370,7 +1371,6 @@ private void SendLEDData(byte[] array) { try { - // Common.DebugP("HIDWriteDevice writing feature data " + TypeOfSaitekPanel); HIDSkeletonBase.HIDWriteDevice?.WriteFeatureData(array); } catch (Exception ex) diff --git a/src/NonVisuals/Panels/Saitek/Panels/SaitekPanel.cs b/src/NonVisuals/Panels/Saitek/Panels/SaitekPanel.cs index 842eee1d2..9475ef14f 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/SaitekPanel.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/SaitekPanel.cs @@ -100,7 +100,7 @@ private void OnReport(HidReport report) var hashSet = GetHashSetOfChangedKnobs(_oldSaitekPanelValueTPM, _newSaitekPanelValueTPM); if (hashSet.Count > 0) { - GamingPanelKnobChanged(!FirstReportHasBeenRead, hashSet); + GamingPanelKnobChangedAsync(!FirstReportHasBeenRead, hashSet); AppEventHandler.SwitchesChanged(this, HIDSkeletonBase.HIDInstance, TypeOfPanel, hashSet); } @@ -113,7 +113,7 @@ private void OnReport(HidReport report) var hashSet = GetHashSetOfChangedKnobs(_oldSaitekPanelValue, _newSaitekPanelValue); if (hashSet.Count > 0) { - GamingPanelKnobChanged(!FirstReportHasBeenRead, hashSet); + GamingPanelKnobChangedAsync(!FirstReportHasBeenRead, hashSet); AppEventHandler.SwitchesChanged(this, HIDSkeletonBase.HIDInstance, TypeOfPanel, hashSet); } @@ -126,7 +126,7 @@ private void OnReport(HidReport report) var hashSet = GetHashSetOfChangedKnobs(_oldPanelValueFarmingPanel, _newPanelValueFarmingPanel); if (hashSet.Count > 0) { - GamingPanelKnobChanged(!FirstReportHasBeenRead, hashSet); + GamingPanelKnobChangedAsync(!FirstReportHasBeenRead, hashSet); AppEventHandler.SwitchesChanged(this, HIDSkeletonBase.HIDInstance, TypeOfPanel, hashSet); } diff --git a/src/NonVisuals/Panels/Saitek/Panels/SwitchPanelPZ55.cs b/src/NonVisuals/Panels/Saitek/Panels/SwitchPanelPZ55.cs index edeb90105..3b5c17b8e 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/SwitchPanelPZ55.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/SwitchPanelPZ55.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.DCSBIOSBindings; using NonVisuals.BindingClasses.Key; using NonVisuals.BindingClasses.OSCommand; @@ -400,7 +401,7 @@ private void SetLandingGearLedsManually(PanelLEDColor panelLEDColor) } } - private void PZ55SwitchChanged(bool isFirstReport, IEnumerable hashSet) + private async Task PZ55SwitchChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (!ForwardPanelEvent) { @@ -499,7 +500,7 @@ private void PZ55SwitchChanged(bool isFirstReport, IEnumerable hashSet) { if (dcsBiosBinding.DCSBIOSInputs.Count > 0 && dcsBiosBinding.SwitchPanelPZ55Key == switchPanelKey.SwitchPanelPZ55Key && dcsBiosBinding.WhenTurnedOn == switchPanelKey.IsOn) { - dcsBiosBinding.SendDCSBIOSCommands(new CancellationToken()); + await dcsBiosBinding.SendDCSBIOSCommandsAsync(new CancellationToken()); break; } } @@ -861,9 +862,9 @@ public void SetLedDcsBiosOutput(SwitchPanelPZ55LEDPosition switchPanelPZ55LEDPos } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { - PZ55SwitchChanged(isFirstReport, hashSet); + await PZ55SwitchChangedAsync(isFirstReport, hashSet); } public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) @@ -922,19 +923,12 @@ private void SetLandingGearLED(SwitchPanelPZ55LEDs switchPanelPZ55LEDs) { try { - if (HIDSkeletonBase.HIDWriteDevice != null) - { - var array = new[] { (byte)0x0, (byte)switchPanelPZ55LEDs }; + if (HIDSkeletonBase.HIDWriteDevice == null) return; - // Common.DebugP("HIDWriteDevice writing feature data " + TypeOfSaitekPanel); - HIDSkeletonBase.HIDWriteDevice.WriteFeatureData(new byte[] { 0, 0 }); - HIDSkeletonBase.HIDWriteDevice.WriteFeatureData(array); - } + var array = new[] { (byte)0x0, (byte)switchPanelPZ55LEDs }; - // if (IsAttached) - // { - // Common.DebugP("Write ending"); - // } + HIDSkeletonBase.HIDWriteDevice.WriteFeatureData(new byte[] { 0, 0 }); + HIDSkeletonBase.HIDWriteDevice.WriteFeatureData(array); } catch (Exception ex) { diff --git a/src/NonVisuals/Panels/Saitek/Panels/TPMPanel.cs b/src/NonVisuals/Panels/Saitek/Panels/TPMPanel.cs index fdf038e41..1a7633bc7 100644 --- a/src/NonVisuals/Panels/Saitek/Panels/TPMPanel.cs +++ b/src/NonVisuals/Panels/Saitek/Panels/TPMPanel.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.DCSBIOSBindings; using NonVisuals.BindingClasses.Key; using NonVisuals.BindingClasses.OSCommand; @@ -222,7 +223,7 @@ public HashSet BipLinkHashSet set => _bipLinks = value; } - private void TPMSwitchChanged(bool isFirstReport, IEnumerable hashSet) + private async Task TPMSwitchChangedAsync(bool isFirstReport, IEnumerable hashSet) { if (!ForwardPanelEvent) { @@ -297,7 +298,7 @@ private void TPMSwitchChanged(bool isFirstReport, IEnumerable hashSet) { if (dcsBiosBinding.DCSBIOSInputs.Count > 0 && dcsBiosBinding.TPMSwitch == tpmPanelSwitch.TPMSwitch && dcsBiosBinding.WhenTurnedOn == tpmPanelSwitch.IsOn) { - dcsBiosBinding.SendDCSBIOSCommands(new CancellationToken()); + await dcsBiosBinding.SendDCSBIOSCommandsAsync(new CancellationToken()); break; } } @@ -605,9 +606,9 @@ public void Clear(TPMPanelSwitches tpmPanelSwitch) SetIsDirty(); } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { - TPMSwitchChanged(isFirstReport, hashSet); + await TPMSwitchChangedAsync(isFirstReport, hashSet); } private void CreateSwitchKeys() diff --git a/src/NonVisuals/Panels/StreamDeck/ActionTypeDCSBIOS.cs b/src/NonVisuals/Panels/StreamDeck/ActionTypeDCSBIOS.cs index 3dcf6866e..78d356e60 100644 --- a/src/NonVisuals/Panels/StreamDeck/ActionTypeDCSBIOS.cs +++ b/src/NonVisuals/Panels/StreamDeck/ActionTypeDCSBIOS.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.DCSBIOSBindings; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.DCSBIOSBindings; namespace NonVisuals.Panels.StreamDeck { @@ -77,10 +78,10 @@ public int GetHash() } } - public void Execute(CancellationToken threadCancellationToken, bool executeOnce = false) + public async Task ExecuteAsync(CancellationToken threadCancellationToken, bool executeOnce = false) { Common.PlaySoundFile(SoundFile, Volume); - SendDCSBIOSCommands(threadCancellationToken); + await SendDCSBIOSCommandsAsync(threadCancellationToken); } internal override void ImportSettings(string settings) { } diff --git a/src/NonVisuals/Panels/StreamDeck/ActionTypeKey.cs b/src/NonVisuals/Panels/StreamDeck/ActionTypeKey.cs index 585083ffb..78cc851d6 100644 --- a/src/NonVisuals/Panels/StreamDeck/ActionTypeKey.cs +++ b/src/NonVisuals/Panels/StreamDeck/ActionTypeKey.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.Key; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.Key; namespace NonVisuals.Panels.StreamDeck { @@ -91,7 +92,7 @@ public bool IsRunning() * Commands that must be cancelled by the release of the button are : * 1) Repeatable single command. */ - public void Execute(CancellationToken threadCancellationToken, bool executeOnce = false) + public Task ExecuteAsync(CancellationToken threadCancellationToken, bool executeOnce = false) { Common.PlaySoundFile(SoundFile, Volume); @@ -124,6 +125,8 @@ public void Execute(CancellationToken threadCancellationToken, bool executeOnce true, OSKeyPress.KeyPressSequence); } + + return Task.CompletedTask; } private void SendKeysCommandsThread(CancellationToken threadCancellationToken) diff --git a/src/NonVisuals/Panels/StreamDeck/ActionTypeLayer.cs b/src/NonVisuals/Panels/StreamDeck/ActionTypeLayer.cs index 44183c662..de48a55e8 100644 --- a/src/NonVisuals/Panels/StreamDeck/ActionTypeLayer.cs +++ b/src/NonVisuals/Panels/StreamDeck/ActionTypeLayer.cs @@ -1,4 +1,6 @@ -namespace NonVisuals.Panels.StreamDeck +using System.Threading.Tasks; + +namespace NonVisuals.Panels.StreamDeck { using System; using System.IO; @@ -108,12 +110,13 @@ public bool IsRunning() } - public void Execute(CancellationToken threadCancellationToken, bool executeOnce = false) + public Task ExecuteAsync(CancellationToken threadCancellationToken, bool executeOnce = false) { _isRunning = true; Common.PlaySoundFile(SoundFile, Volume); Navigate(threadCancellationToken); _isRunning = false; + return Task.CompletedTask; } diff --git a/src/NonVisuals/Panels/StreamDeck/ActionTypeOS.cs b/src/NonVisuals/Panels/StreamDeck/ActionTypeOS.cs index fa06fe2a7..8f85ba0c6 100644 --- a/src/NonVisuals/Panels/StreamDeck/ActionTypeOS.cs +++ b/src/NonVisuals/Panels/StreamDeck/ActionTypeOS.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.OSCommand; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.OSCommand; namespace NonVisuals.Panels.StreamDeck { @@ -70,10 +71,11 @@ public bool IsRunning() } - public void Execute(CancellationToken threadCancellationToken, bool executeOnce = false) + public Task ExecuteAsync(CancellationToken threadCancellationToken, bool executeOnce = false) { Common.PlaySoundFile(SoundFile, Volume); OSCommandObject.ExecuteCommand(threadCancellationToken); + return Task.CompletedTask; } internal override void ImportSettings(string settings) { } diff --git a/src/NonVisuals/Panels/StreamDeck/Panels/StreamDeckPanel.cs b/src/NonVisuals/Panels/StreamDeck/Panels/StreamDeckPanel.cs index bb0d13bfb..6dd7a9413 100644 --- a/src/NonVisuals/Panels/StreamDeck/Panels/StreamDeckPanel.cs +++ b/src/NonVisuals/Panels/StreamDeck/Panels/StreamDeckPanel.cs @@ -1,4 +1,5 @@ -using NonVisuals.Images; +using System.Threading.Tasks; +using NonVisuals.Images; namespace NonVisuals.Panels.StreamDeck.Panels { @@ -401,11 +402,12 @@ public override void ClearSettings(bool setIsDirty = false) } } - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { // Set _selectedMode and LCD button statuses // and performs the actual actions for key presses // ADD METHOD ? + return Task.CompletedTask; } public DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) diff --git a/src/NonVisuals/Panels/StreamDeck/StreamDeckButton.cs b/src/NonVisuals/Panels/StreamDeck/StreamDeckButton.cs index db1f9bc87..f26b9cc9b 100644 --- a/src/NonVisuals/Panels/StreamDeck/StreamDeckButton.cs +++ b/src/NonVisuals/Panels/StreamDeck/StreamDeckButton.cs @@ -128,11 +128,11 @@ public void DoPress() _cancellationTokenSource = new CancellationTokenSource(); if (ActionForPress.IsRepeatable() && !ActionForPress.HasSequence) { - ActionForPress.Execute(_cancellationTokenSource.Token); + ActionForPress.ExecuteAsync(_cancellationTokenSource.Token); } else { - ActionForPress.Execute(CancellationToken.None); + ActionForPress.ExecuteAsync(CancellationToken.None); } } @@ -176,7 +176,7 @@ public void DoRelease() _cancellationTokenSource?.Cancel(); } - ActionForRelease.Execute(CancellationToken.None); + ActionForRelease.ExecuteAsync(CancellationToken.None); } public bool CheckIfWouldOverwrite(StreamDeckButton newStreamDeckButton) diff --git a/src/NonVisuals/Panels/StreamDeck/StreamDeckPushRotary.cs b/src/NonVisuals/Panels/StreamDeck/StreamDeckPushRotary.cs index c9b06edf9..fa3f20c16 100644 --- a/src/NonVisuals/Panels/StreamDeck/StreamDeckPushRotary.cs +++ b/src/NonVisuals/Panels/StreamDeck/StreamDeckPushRotary.cs @@ -119,16 +119,16 @@ public void DoPress(bool executeOnce) _cancellationTokenSource = new CancellationTokenSource(); if (ActionForPress.IsRepeatable() && !ActionForPress.HasSequence) { - ActionForPress.Execute(_cancellationTokenSource.Token); + ActionForPress.ExecuteAsync(_cancellationTokenSource.Token); } else { - ActionForPress.Execute(CancellationToken.None); + ActionForPress.ExecuteAsync(CancellationToken.None); } } else { - ActionForPress.Execute(new CancellationToken(), true); + ActionForPress.ExecuteAsync(new CancellationToken(), true); } } @@ -172,7 +172,7 @@ public void DoRelease() _cancellationTokenSource?.Cancel(); } - ActionForRelease.Execute(CancellationToken.None); + ActionForRelease.ExecuteAsync(CancellationToken.None); } public bool CheckIfWouldOverwrite(StreamDeckPushRotary newStreamDeckPushRotary) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69A10C.cs b/src/NonVisuals/Radios/RadioPanelPZ69A10C.cs index f4bb33bc7..b50beb036 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69A10C.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69A10C.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -67,8 +68,8 @@ private enum CurrentA10RadioMode private const string VHF_AM_FREQ_2DIAL_COMMAND = "VHFAM_FREQ2 "; private const string VHF_AM_FREQ_3DIAL_COMMAND = "VHFAM_FREQ3 "; private const string VHF_AM_FREQ_4DIAL_COMMAND = "VHFAM_FREQ4 "; - private Thread _vhfAmSyncThread; - private long _vhfAmThreadNowSynching; + private Task _vhfAmSyncTask; + private CancellationTokenSource _vhfAmSyncTaskTokenSource = new(); private long _vhfAmDial1WaitingForFeedback; private long _vhfAmDial2WaitingForFeedback; private long _vhfAmDial3WaitingForFeedback; @@ -117,8 +118,8 @@ private enum CurrentA10RadioMode private const string UHF_FREQ_3DIAL_COMMAND = "UHF_1MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_4DIAL_COMMAND = "UHF_POINT1MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_5DIAL_COMMAND = "UHF_POINT25_SEL "; // "00" "25" "50" "75" - private Thread _uhfSyncThread; - private long _uhfThreadNowSynching; + private Task _uhfSyncTask; + private CancellationTokenSource _uhfSyncTaskTokenSource = new(); private long _uhfDial1WaitingForFeedback; private long _uhfDial2WaitingForFeedback; private long _uhfDial3WaitingForFeedback; @@ -164,8 +165,8 @@ private enum CurrentA10RadioMode private const string VHF_FM_FREQ_2DIAL_COMMAND = "VHFFM_FREQ2 "; private const string VHF_FM_FREQ_3DIAL_COMMAND = "VHFFM_FREQ3 "; private const string VHF_FM_FREQ_4DIAL_COMMAND = "VHFFM_FREQ4 "; - private Thread _vhfFmSyncThread; - private long _vhfFmThreadNowSynching; + private Task _vhfFmSyncTask; + private CancellationTokenSource _vhfFmSyncTaskTokenSource = new(); private long _vhfFmDial1WaitingForFeedback; private long _vhfFmDial2WaitingForFeedback; private long _vhfFmDial3WaitingForFeedback; @@ -202,8 +203,8 @@ private enum CurrentA10RadioMode private volatile uint _ilsCockpitFreq2DialPos = 1; private const string ILS_FREQ1_DIAL_COMMAND = "ILS_MHZ "; private const string ILS_FREQ2_DIAL_COMMAND = "ILS_KHZ "; - private Thread _ilsSyncThread; - private long _ilsThreadNowSynching; + private Task _ilsSyncTask; + private CancellationTokenSource _ilsSyncTaskTokenSource = new(); private long _ilsDial1WaitingForFeedback; private long _ilsDial2WaitingForFeedback; @@ -228,8 +229,8 @@ private enum CurrentA10RadioMode private const string TACAN_FREQ1_DIAL_COMMAND = "TACAN_10 "; private const string TACAN_FREQ2_DIAL_COMMAND = "TACAN_1 "; private const string TACAN_FREQ3_DIAL_COMMAND = "TACAN_XY "; - private Thread _tacanSyncThread; - private long _tacanThreadNowSynching; + private Task _tacanSyncTask; + private CancellationTokenSource _tacanSyncTaskTokenSource = new(); private long _tacanDial1WaitingForFeedback; private long _tacanDial2WaitingForFeedback; private long _tacanDial3WaitingForFeedback; @@ -239,7 +240,7 @@ private enum CurrentA10RadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69A10C(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -250,11 +251,11 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownUHFThread = true; - _shutdownVHFAMThread = true; - _shutdownILSThread = true; - _shutdownTACANThread = true; - _shutdownVHFFMThread = true; + _uhfSyncTaskTokenSource.Cancel(); + _vhfAmSyncTaskTokenSource.Cancel(); + _ilsSyncTaskTokenSource.Cancel(); + _tacanSyncTaskTokenSource.Cancel(); + _vhfFmSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachDataListener(this); BIOSEventHandler.DetachStringListener(this); } @@ -269,7 +270,7 @@ protected override void Dispose(bool disposing) public override void InitPanel() { CreateRadioKnobs(); - + // VHF AM _vhfAmDcsbiosOutputFreqDial1 = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("VHFAM_FREQ1"); @@ -548,10 +549,8 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { try { - // Common.DebugP("RadioPanelPZ69A10C Received DCSBIOS stringData : ->" + e.StringData + "<-"); if (string.IsNullOrWhiteSpace(e.StringData)) { - // Common.DebugP("Received DCSBIOS stringData : " + e.StringData); return; } @@ -747,7 +746,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10C knob) private void SendVhfAmToDCSBIOS() { - if (VhfAmNowSyncing()) + if (_vhfAmSyncTask?.Status == TaskStatus.Running) { return; } @@ -807,182 +806,172 @@ private void SendVhfAmToDCSBIOS() _ => 0 }; - // #1 - _shutdownVHFAMThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFAMThread = false; - _vhfAmSyncThread = new Thread(() => VhfAmSynchThreadMethod(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4)); - _vhfAmSyncThread.Start(); + _vhfAmSyncTaskTokenSource = new CancellationTokenSource(); + _vhfAmSyncTask = Task.Run(() => SyncVhfAmAsync(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4, _vhfAmSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVHFAMThread; - private void VhfAmSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int desiredPositionDial4) + private async Task SyncVhfAmAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int desiredPositionDial4, CancellationToken cancellationToken) { try - { - try - { /* + { /* * A-10C AN/ARC-186(V) VHF AM Radio 1 * * Large dial 116-151 [step of 1] * Small dial 0.00-0.95 [step of 0.05] */ - Interlocked.Exchange(ref _vhfAmThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - do + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + do + { + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfAmDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vhfAmDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vhfAmDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vhfAmDial4WaitingForFeedback); // Let's do an ugly reset + } - string str; - if (Interlocked.Read(ref _vhfAmDial1WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfAmDial1WaitingForFeedback) == 0) + { + if (_vhfAmCockpitFreq1DialPos != desiredPositionDial1) { + string command; + dial1OkTime = DateTime.Now.Ticks; lock (_lockVhfAmDialsObject1) { - - if (_vhfAmCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - str = VHF_AM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfAmCockpitFreq1DialPos); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfAmDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + command = VHF_AM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfAmCockpitFreq1DialPos); } + + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfAmDial1WaitingForFeedback, 1); } - else - { - dial1OkTime = DateTime.Now.Ticks; - } - if (Interlocked.Read(ref _vhfAmDial2WaitingForFeedback) == 0) + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfAmDial2WaitingForFeedback) == 0) + { + if (_vhfAmCockpitFreq2DialPos != desiredPositionDial2) { + string command; + dial2OkTime = DateTime.Now.Ticks; lock (_lockVhfAmDialsObject2) { - if (_vhfAmCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; - str = VHF_AM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfAmCockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfAmDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + command = VHF_AM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfAmCockpitFreq2DialPos); } + + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfAmDial2WaitingForFeedback, 1); } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (Interlocked.Read(ref _vhfAmDial3WaitingForFeedback) == 0) + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfAmDial3WaitingForFeedback) == 0) + { + if (_vhfAmCockpitFreq3DialPos != desiredPositionDial3) { + string command; + dial3OkTime = DateTime.Now.Ticks; lock (_lockVhfAmDialsObject3) { - if (_vhfAmCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; - str = VHF_AM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfAmCockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _vhfAmDial3WaitingForFeedback, 1); - } - - Reset(ref dial3Timeout); + command = VHF_AM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfAmCockpitFreq3DialPos); } - } - else - { - dial3OkTime = DateTime.Now.Ticks; + + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _vhfAmDial3WaitingForFeedback, 1); } - if (Interlocked.Read(ref _vhfAmDial4WaitingForFeedback) == 0) - { - lock (_lockVhfAmDialsObject4) - { - if (_vhfAmCockpitFreq4DialPos < desiredPositionDial4) - { - dial4OkTime = DateTime.Now.Ticks; - str = VHF_AM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfAmDial4WaitingForFeedback, 1); - } - else if (_vhfAmCockpitFreq4DialPos > desiredPositionDial4) - { - dial4OkTime = DateTime.Now.Ticks; - str = VHF_AM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfAmDial4WaitingForFeedback, 1); - } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - Reset(ref dial4Timeout); - } - } - else + if (Interlocked.Read(ref _vhfAmDial4WaitingForFeedback) == 0) + { + string command; + + if (_vhfAmCockpitFreq4DialPos < desiredPositionDial4) { dial4OkTime = DateTime.Now.Ticks; + command = VHF_AM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _vhfAmDial4WaitingForFeedback, 1); } - - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + else if (_vhfAmCockpitFreq4DialPos > desiredPositionDial4) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + dial4OkTime = DateTime.Now.Ticks; + command = VHF_AM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _vhfAmDial4WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial4Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVHFAMThread); - SwapCockpitStandbyFrequencyVhfAm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); + } + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyVhfAm(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfAmThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -990,7 +979,7 @@ private void VhfAmSynchThreadMethod(int desiredPositionDial1, int desiredPositio private void SendUhfToDCSBIOS() { - if (UhfNowSyncing()) + if (_uhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -1133,252 +1122,251 @@ private void SendUhfToDCSBIOS() // Small dial 0.00-0.95 [step of 0.05] // #1 - _shutdownUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownUHFThread = false; - _uhfSyncThread = new Thread(() => UhfSynchThreadMethod(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5)); - _uhfSyncThread.Start(); + _uhfSyncTaskTokenSource = new CancellationTokenSource(); + _uhfSyncTask = Task.Run(() => SyncUhfAsync(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5, _uhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownUHFThread; - private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5) + private async Task SyncUhfAsync(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial5Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + long dial5OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + var dial5SendCount = 0; + do { - Interlocked.Exchange(ref _uhfThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial5Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - long dial5OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - var dial5SendCount = 0; - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial5Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial5Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Let's do an ugly reset + } + + // Frequency selector 1 + // "2" "3" "A"/"-1" + // Pos 0 1 2 + if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - // Frequency selector 1 - // "2" "3" "A"/"-1" - // Pos 0 1 2 - if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + lock (_lockUhfDialsObject1) { - lock (_lockUhfDialsObject1) + if (_uhfCockpitFreq1DialPos != desiredPosition1) { - if (_uhfCockpitFreq1DialPos != desiredPosition1) - { - dial1OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq1DialPos < desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq1DialPos > desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } + dial1OkTime = DateTime.Now.Ticks; + } - Reset(ref dial1Timeout); + if (_uhfCockpitFreq1DialPos < desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } + else if (_uhfCockpitFreq1DialPos > desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + lock (_lockUhfDialsObject2) { - lock (_lockUhfDialsObject2) + if (_uhfCockpitFreq2DialPos != desiredPosition2) { - if (_uhfCockpitFreq2DialPos != desiredPosition2) - { - dial2OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq2DialPos < desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq2DialPos > desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } + dial2OkTime = DateTime.Now.Ticks; + } - Reset(ref dial2Timeout); + if (_uhfCockpitFreq2DialPos < desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq2DialPos > desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + lock (_lockUhfDialsObject3) { - lock (_lockUhfDialsObject3) + if (_uhfCockpitFreq3DialPos != desiredPosition3) { - if (_uhfCockpitFreq3DialPos != desiredPosition3) - { - dial3OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq3DialPos < desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq3DialPos > desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + } - Reset(ref dial3Timeout); + if (_uhfCockpitFreq3DialPos < desiredPosition3) + { + const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq3DialPos > desiredPosition3) + { + command = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + lock (_lockUhfDialsObject4) { - lock (_lockUhfDialsObject4) + if (_uhfCockpitFreq4DialPos != desiredPosition4) { - if (_uhfCockpitFreq4DialPos != desiredPosition4) - { - dial4OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq4DialPos < desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq4DialPos > desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } + dial4OkTime = DateTime.Now.Ticks; + } - Reset(ref dial4Timeout); + if (_uhfCockpitFreq4DialPos < desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq4DialPos > desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial4OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + lock (_lockUhfDialsObject5) { - lock (_lockUhfDialsObject5) + if (_uhfCockpitFreq5DialPos != desiredPosition5) { - if (_uhfCockpitFreq5DialPos != desiredPosition5) - { - dial5OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq5DialPos < desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq5DialPos > desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } + dial5OkTime = DateTime.Now.Ticks; + } - Reset(ref dial5Timeout); + if (_uhfCockpitFreq5DialPos < desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq5DialPos > desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial5OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial5SendCount++; + Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); } + Reset(ref dial5Timeout); + } + else + { + dial5OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)) && !_shutdownUHFThread); - SwapCockpitStandbyFrequencyUhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)); + SwapCockpitStandbyFrequencyUhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1386,7 +1374,7 @@ private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, in private void SendVhfFmToDCSBIOS() { - if (VhfFmNowSyncing()) + if (_vhfFmSyncTask?.Status == TaskStatus.Running) { return; } @@ -1444,179 +1432,184 @@ private void SendVhfFmToDCSBIOS() desiredPositionDial4 = 0; } - _shutdownVHFFMThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFFMThread = false; - _vhfFmSyncThread = new Thread(() => VhfFmSynchThreadMethod(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4)); - _vhfFmSyncThread.Start(); + + _vhfFmSyncTaskTokenSource = new CancellationTokenSource(); + _vhfFmSyncTask = Task.Run(() => SyncVhfFmAsync(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4, _vhfFmSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVHFFMThread; - private void VhfFmSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int frequencyDial4) + private async Task SyncVhfFmAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int frequencyDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + + do { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Let's do an ugly reset + } + + if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject1) { - lock (_lockVhfFmDialsObject1) + if (_vhfFmCockpitFreq1DialPos != desiredPositionDial1) { - if (_vhfFmCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfFmCockpitFreq1DialPos); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfFmCockpitFreq1DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfFmDialsObject2) { - // Common.DebugP("b"); - lock (_lockVhfFmDialsObject2) + if (_vhfFmCockpitFreq2DialPos != desiredPositionDial2) { - if (_vhfFmCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfFmCockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfFmCockpitFreq2DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject3) { - lock (_lockVhfFmDialsObject3) + if (_vhfFmCockpitFreq3DialPos != desiredPositionDial3) { - if (_vhfFmCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfFmCockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfFmCockpitFreq3DialPos); } - - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfFmDialsObject4) { - lock (_lockVhfFmDialsObject4) + // "00" "25" "50" "75", only "00" and "50" used. + // Pos 0 1 2 3 + if (_vhfFmCockpitFreq4DialPos < frequencyDial4) { - // "00" "25" "50" "75", only "00" and "50" used. - // Pos 0 1 2 3 - if (_vhfFmCockpitFreq4DialPos < frequencyDial4) - { - dial4OkTime = DateTime.Now.Ticks; - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq4DialPos > frequencyDial4) - { - dial4OkTime = DateTime.Now.Ticks; - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq4DialPos > frequencyDial4) + { + dial4OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial4OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVHFFMThread); - SwapCockpitStandbyFrequencyVhfFm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyVhfFm(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1624,7 +1617,7 @@ private void VhfFmSynchThreadMethod(int desiredPositionDial1, int desiredPositio private void SendILSToDCSBIOS() { - if (IlsNowSyncing()) + if (_ilsSyncTask?.Status == TaskStatus.Running) { return; } @@ -1649,125 +1642,116 @@ private void SendILSToDCSBIOS() var freqDial1 = GetILSDialPosForFrequency(1, int.Parse(frequencyAsString.Substring(0, 3))); var freqDial2 = GetILSDialPosForFrequency(2, int.Parse(frequencyAsString.Substring(4, 2))); - // #1 - _shutdownILSThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownILSThread = false; - _ilsSyncThread = new Thread(() => ILSSynchThreadMethod(freqDial1, freqDial2)); - _ilsSyncThread.Start(); + + _ilsSyncTaskTokenSource = new CancellationTokenSource(); + _ilsSyncTask = Task.Run(() => SyncILSAsync(freqDial1, freqDial2, _ilsSyncTaskTokenSource.Token)); } - private volatile bool _shutdownILSThread; - private void ILSSynchThreadMethod(int position1, int position2) + private async Task SyncILSAsync(int position1, int position2, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + + do { - Interlocked.Exchange(ref _ilsThreadNowSynching, 1); - - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _ilsDial1WaitingForFeedback); // Let's do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _ilsDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _ilsDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _ilsDial2WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _ilsDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _ilsDial1WaitingForFeedback) == 0) + lock (_lockIlsDialsObject1) { - lock (_lockIlsDialsObject1) + if (_ilsCockpitFreq1DialPos < position1) { - if (_ilsCockpitFreq1DialPos < position1) - { - dial1OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); - } - else if (_ilsCockpitFreq1DialPos > position1) - { - dial1OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_ilsCockpitFreq1DialPos > position1) + { + dial1OkTime = DateTime.Now.Ticks; + command = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _ilsDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _ilsDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockIlsDialsObject2) { - lock (_lockIlsDialsObject2) + if (_ilsCockpitFreq2DialPos < position2) { - - if (_ilsCockpitFreq2DialPos < position2) - { - dial2OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); - } - else if (_ilsCockpitFreq2DialPos > position2) - { - dial2OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_ilsCockpitFreq2DialPos > position2) + { + dial2OkTime = DateTime.Now.Ticks; + command = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial2OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial2Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownILSThread); - SwapCockpitStandbyFrequencyIls(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + Thread.Sleep(5000); + } + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); + SwapCockpitStandbyFrequencyIls(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _ilsThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1775,7 +1759,7 @@ private void ILSSynchThreadMethod(int position1, int position2) private void SendTacanToDCSBIOS() { - if (TacanNowSyncing()) + if (_tacanSyncTask?.Status == TaskStatus.Running) { return; } @@ -1796,146 +1780,145 @@ private void SendTacanToDCSBIOS() // #1 = 12 (position = value) // #2 = 0 (position = value) // #3 = 1 (position = value) - _shutdownTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownTACANThread = false; - _tacanSyncThread = new Thread(() => TacanSynchThreadMethod(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby)); - _tacanSyncThread.Start(); + _tacanSyncTaskTokenSource = new CancellationTokenSource(); + _tacanSyncTask = Task.Run(() => SyncTacanAsync(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby, _tacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownTACANThread; - private void TacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + private async Task SyncTacanAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + + do { - Interlocked.Exchange(ref _tacanThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Let's do an ugly reset + } + + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Let's do an ugly reset + } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Let's do an ugly reset + } - do + if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) { + var command = string.Empty; - if (IsTimedOut(ref dial1Timeout)) + lock (_lockTacanDialsObject1) { - ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Lets do an ugly reset + if (_tacanCockpitFreq1DialPos != desiredPositionDial1) + { + dial1OkTime = DateTime.Now.Ticks; + command = TACAN_FREQ1_DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); + } } - if (IsTimedOut(ref dial2Timeout)) + if (!string.IsNullOrEmpty(command)) { - ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Lets do an ugly reset + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) + lock (_lockTacanDialsObject2) { - - lock (_lockTacanDialsObject1) + if (_tacanCockpitFreq2DialPos != desiredPositionDial2) { - if (_tacanCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ1_DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); - } + dial2OkTime = DateTime.Now.Ticks; - Reset(ref dial1Timeout); + command = TACAN_FREQ2_DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else - { - dial1OkTime = DateTime.Now.Ticks; - } - if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + if (!string.IsNullOrEmpty(command)) { - // Common.DebugP("b"); - lock (_lockTacanDialsObject2) - { - if (_tacanCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); + } - var str = TACAN_FREQ2_DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); - } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - Reset(ref dial2Timeout); - } - } - else - { - dial2OkTime = DateTime.Now.Ticks; - } + if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) + lock (_lockTacanDialsObject3) { - - lock (_lockTacanDialsObject3) + if (_tacanCockpitFreq3DialPos != desiredPositionDial3) { - if (_tacanCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; + dial3OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ3_DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); - } + command = TACAN_FREQ3_DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } - - Reset(ref dial3Timeout); - } - else - { - dial3OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS - + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownTACANThread); - SwapCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _tacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -2392,7 +2375,7 @@ private string GetUhfFrequencyAsString() return frequencyAsString; } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -2417,14 +2400,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfAmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_MODE_INCREASE); } } else { if (VhfAmPresetSelected() && _vhfAmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_PRESET_INCREASE); } else if (_vhfAmBigFrequencyStandby.Equals(151.00)) { @@ -2446,14 +2429,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -2476,14 +2459,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_INCREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_INCREASE); } else if (_vhfFmBigFrequencyStandby.Equals(76)) { @@ -2546,14 +2529,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfAmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_MODE_DECREASE); } } else { if (VhfAmPresetSelected() && _vhfAmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_PRESET_DECREASE); } else if (_vhfAmBigFrequencyStandby.Equals(116.00)) { @@ -2575,14 +2558,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225.00)) { @@ -2604,14 +2587,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_DECREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_DECREASE); } else if (_vhfFmBigFrequencyStandby.Equals(30)) { @@ -2674,7 +2657,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfAmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_FREQ_MODE_INCREASE); } } else @@ -2692,7 +2675,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -2710,7 +2693,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_INCREASE); } } else @@ -2785,7 +2768,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfAmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_FREQ_MODE_DECREASE); } } else @@ -2803,7 +2786,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -2821,7 +2804,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_DECREASE); } } else @@ -2896,14 +2879,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfAmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_MODE_INCREASE); } } else { if (VhfAmPresetSelected() && _vhfAmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_PRESET_INCREASE); } else if (!_lowerButtonPressed && _vhfAmBigFrequencyStandby.Equals(151.00)) { @@ -2925,7 +2908,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else @@ -2933,7 +2916,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { // 225-399 - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -2955,14 +2938,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_INCREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_INCREASE); } else if (_vhfFmBigFrequencyStandby.Equals(76)) { @@ -3025,14 +3008,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfAmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_MODE_DECREASE); } } else { if (VhfAmPresetSelected() && _vhfAmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_PRESET_DECREASE); } else if (_vhfAmBigFrequencyStandby.Equals(116.00)) { @@ -3054,14 +3037,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225.00)) { @@ -3083,14 +3066,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_DECREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_DECREASE); } else if (_vhfFmBigFrequencyStandby.Equals(30)) { @@ -3153,7 +3136,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfAmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_AM_FREQ_MODE_INCREASE); } } else @@ -3171,7 +3154,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -3189,7 +3172,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_INCREASE); } } else @@ -3264,7 +3247,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfAmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_AM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_AM_FREQ_MODE_DECREASE); } } else @@ -3282,7 +3265,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -3300,7 +3283,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_DECREASE); } } else @@ -3528,7 +3511,7 @@ private void CheckFrequenciesForValidity() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { lock (LockLCDUpdateObject) { @@ -3706,7 +3689,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_upperButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10C.UPPER_FREQ_SWITCH); } @@ -3724,7 +3707,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_lowerButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10C.LOWER_FREQ_SWITCH); } @@ -3742,10 +3725,10 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - AdjustFrequency(hashSet); } + await AdjustFrequencyAsync(hashSet); } - + public override void ClearSettings(bool setIsDirty = false) { // ignore @@ -3755,7 +3738,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobA10C.GetRadioPanelKnobs(); @@ -4262,31 +4245,6 @@ private bool UhfPresetSelected() return _uhfCockpitFreqMode == 1; } - private bool VhfAmNowSyncing() - { - return Interlocked.Read(ref _vhfAmThreadNowSynching) > 0; - } - - private bool UhfNowSyncing() - { - return Interlocked.Read(ref _uhfThreadNowSynching) > 0; - } - - private bool VhfFmNowSyncing() - { - return Interlocked.Read(ref _vhfFmThreadNowSynching) > 0; - } - - private bool IlsNowSyncing() - { - return Interlocked.Read(ref _ilsThreadNowSynching) > 0; - } - - private bool TacanNowSyncing() - { - return Interlocked.Read(ref _tacanThreadNowSynching) > 0; - } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69A10CII.cs b/src/NonVisuals/Radios/RadioPanelPZ69A10CII.cs index 5f47d384b..8fdb90870 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69A10CII.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69A10CII.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -71,8 +72,8 @@ private enum CurrentA10RadioMode private const string ARC210_VHF_FREQ_3DIAL_COMMAND = "ARC210_1MHZ_SEL "; private const string ARC210_VHF_FREQ_4DIAL_COMMAND = "ARC210_100KHZ_SEL "; private const string ARC210_VHF_FREQ_5DIAL_COMMAND = "ARC210_25KHZ_SEL "; - private Thread _arc210VhfSyncThread; - private long _arc210VhfThreadNowSynching; + private Task _arc210VhfSyncTask; + private CancellationTokenSource _arc210VhfSyncTaskTokenSource = new(); private long _arc210VhfDial1WaitingForFeedback; private long _arc210VhfDial2WaitingForFeedback; private long _arc210VhfDial3WaitingForFeedback; @@ -125,8 +126,8 @@ private enum CurrentA10RadioMode private const string UHF_FREQ_3DIAL_COMMAND = "UHF_1MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_4DIAL_COMMAND = "UHF_POINT1MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_5DIAL_COMMAND = "UHF_POINT25_SEL "; // "00" "25" "50" "75" - private Thread _uhfSyncThread; - private long _uhfThreadNowSynching; + private Task _uhfSyncTask; + private CancellationTokenSource _uhfSyncTaskTokenSource = new(); private long _uhfDial1WaitingForFeedback; private long _uhfDial2WaitingForFeedback; private long _uhfDial3WaitingForFeedback; @@ -172,8 +173,8 @@ private enum CurrentA10RadioMode private const string VHF_FM_FREQ_2DIAL_COMMAND = "VHFFM_FREQ2 "; private const string VHF_FM_FREQ_3DIAL_COMMAND = "VHFFM_FREQ3 "; private const string VHF_FM_FREQ_4DIAL_COMMAND = "VHFFM_FREQ4 "; - private Thread _vhfFmSyncThread; - private long _vhfFmThreadNowSynching; + private Task _vhfFmSyncTask; + private CancellationTokenSource _vhfFmSyncTaskTokenSource = new(); private long _vhfFmDial1WaitingForFeedback; private long _vhfFmDial2WaitingForFeedback; private long _vhfFmDial3WaitingForFeedback; @@ -210,7 +211,8 @@ private enum CurrentA10RadioMode private volatile uint _ilsCockpitFreq2DialPos = 1; private const string ILS_FREQ1_DIAL_COMMAND = "ILS_MHZ "; private const string ILS_FREQ2_DIAL_COMMAND = "ILS_KHZ "; - private Thread _ilsSyncThread; + private Task _ilsSyncTask; + private CancellationTokenSource _ilsSyncTaskTokenSource = new(); private long _ilsThreadNowSynching; private long _ilsDial1WaitingForFeedback; private long _ilsDial2WaitingForFeedback; @@ -236,8 +238,8 @@ private enum CurrentA10RadioMode private const string TACAN_FREQ1_DIAL_COMMAND = "TACAN_10 "; private const string TACAN_FREQ2_DIAL_COMMAND = "TACAN_1 "; private const string TACAN_FREQ3_DIAL_COMMAND = "TACAN_XY "; - private Thread _tacanSyncThread; - private long _tacanThreadNowSynching; + private Task _tacanSyncTask; + private CancellationTokenSource _tacanSyncTaskTokenSource = new(); private long _tacanDial1WaitingForFeedback; private long _tacanDial2WaitingForFeedback; private long _tacanDial3WaitingForFeedback; @@ -247,7 +249,7 @@ private enum CurrentA10RadioMode private long _doUpdatePanelLCD = 1; public RadioPanelPZ69A10CII(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -258,11 +260,11 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownUHFThread = true; - _shutdownARC210VHFThread = true; - _shutdownILSThread = true; - _shutdownTACANThread = true; - _shutdownVHFFMThread = true; + _uhfSyncTaskTokenSource.Cancel(); + _arc210VhfSyncTaskTokenSource.Cancel(); + _ilsSyncTaskTokenSource.Cancel(); + _tacanSyncTaskTokenSource.Cancel(); + _vhfFmSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachDataListener(this); BIOSEventHandler.DetachStringListener(this); } @@ -566,7 +568,6 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { try { - // Common.DebugP("RadioPanelPZ69A10C Received DCSBIOS stringData : ->" + e.StringData + "<-"); if (string.IsNullOrWhiteSpace(e.StringData)) { return; @@ -776,14 +777,14 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10CII knob) private void SendARC210VhfToDCSBIOS() { - if (ARC210VhfNowSyncing()) + if (_arc210VhfSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyARC210Vhf(); var frequencyAsString = _arc210VhfBigFrequencyStandby + "." + _arc210VhfSmallFrequencyStandby.ToString(CultureInfo.InvariantCulture).PadLeft(3, '0'); - + /* * Dial 1 * " " 1 2 3 @@ -862,212 +863,220 @@ private void SendARC210VhfToDCSBIOS() }; // #1 - _shutdownARC210VHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownARC210VHFThread = false; - _arc210VhfSyncThread = new Thread(() => ARC210VhfSynchThreadMethod(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4, desiredPositionDial5)); - _arc210VhfSyncThread.Start(); + _arc210VhfSyncTaskTokenSource = new CancellationTokenSource(); + _arc210VhfSyncTask = Task.Run(() => SyncARC210VhfAsync(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4, desiredPositionDial5, _arc210VhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownARC210VHFThread; - private void ARC210VhfSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int desiredPositionDial4, int desiredPositionDial5) + private async Task SyncARC210VhfAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int desiredPositionDial4, int desiredPositionDial5, CancellationToken cancellationToken) { try - { - try - { /* - * A-10C II ARC-210 VHF - * - * Large dial 0-399 [step of 1] - * Small dial 0.000-0.975 [step of 0.05] - */ - - Interlocked.Exchange(ref _arc210VhfThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - var dial5Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - long dial5OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - var dial5SendCount = 0; - do + { /* + * A-10C II ARC-210 VHF + * + * Large dial 0-399 [step of 1] + * Small dial 0.000-0.975 [step of 0.05] + */ + + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + var dial5Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + long dial5OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + var dial5SendCount = 0; + do + { + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _arc210VhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _arc210VhfDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _arc210VhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _arc210VhfDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _arc210VhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _arc210VhfDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _arc210VhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _arc210VhfDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial5Timeout)) - { - ResetWaitingForFeedBack(ref _arc210VhfDial5WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial5Timeout)) + { + ResetWaitingForFeedBack(ref _arc210VhfDial5WaitingForFeedback); // Let's do an ugly reset + } + + if (Interlocked.Read(ref _arc210VhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - string str; - if (Interlocked.Read(ref _arc210VhfDial1WaitingForFeedback) == 0) + lock (_lockArc210VhfDialsObject1) { - lock (_lockArc210VhfDialsObject1) + if (_arc210VhfCockpitFreq1DialPos != desiredPositionDial1) { - - if (_arc210VhfCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_1DIAL_COMMAND + GetCommandDirectionForARC210VhfDial1(desiredPositionDial1, _arc210VhfCockpitFreq1DialPos); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _arc210VhfDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_1DIAL_COMMAND + GetCommandDirectionForARC210VhfDial1(desiredPositionDial1, _arc210VhfCockpitFreq1DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _arc210VhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _arc210VhfDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _arc210VhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockArc210VhfDialsObject2) { - lock (_lockArc210VhfDialsObject2) + if (_arc210VhfCockpitFreq2DialPos != desiredPositionDial2) { - if (_arc210VhfCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_2DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial2, _arc210VhfCockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _arc210VhfDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_2DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial2, _arc210VhfCockpitFreq2DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _arc210VhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _arc210VhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _arc210VhfDial3WaitingForFeedback) == 0) + lock (_lockArc210VhfDialsObject3) { - lock (_lockArc210VhfDialsObject3) + if (_arc210VhfCockpitFreq3DialPos != desiredPositionDial3) { - if (_arc210VhfCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_3DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial3, _arc210VhfCockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _arc210VhfDial3WaitingForFeedback, 1); - } - - Reset(ref dial3Timeout); + dial3OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_3DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial3, _arc210VhfCockpitFreq3DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _arc210VhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _arc210VhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _arc210VhfDial4WaitingForFeedback) == 0) + lock (_lockArc210VhfDialsObject4) { - lock (_lockArc210VhfDialsObject4) + if (_arc210VhfCockpitFreq4DialPos != desiredPositionDial4) { - if (_arc210VhfCockpitFreq4DialPos != desiredPositionDial4) - { - dial4OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_4DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial4, _arc210VhfCockpitFreq4DialPos); - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _arc210VhfDial4WaitingForFeedback, 1); - } - - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_4DIAL_COMMAND + GetCommandDirectionForARC210VhfDial23(desiredPositionDial4, _arc210VhfCockpitFreq4DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial4OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _arc210VhfDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _arc210VhfDial5WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _arc210VhfDial5WaitingForFeedback) == 0) + lock (_lockArc210VhfDialsObject5) { - lock (_lockArc210VhfDialsObject5) + if (_arc210VhfCockpitFreq5DialPos < desiredPositionDial5) { - if (_arc210VhfCockpitFreq5DialPos < desiredPositionDial5) - { - dial5OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _arc210VhfDial5WaitingForFeedback, 1); - } - else if (_arc210VhfCockpitFreq5DialPos > desiredPositionDial5) - { - dial5OkTime = DateTime.Now.Ticks; - str = ARC210_VHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _arc210VhfDial5WaitingForFeedback, 1); - } - - Reset(ref dial5Timeout); + dial5OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_arc210VhfCockpitFreq5DialPos > desiredPositionDial5) + { + dial5OkTime = DateTime.Now.Ticks; + command = ARC210_VHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial5OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 3 || dial2SendCount > 9 || dial3SendCount > 9 || dial4SendCount > 9 || dial5SendCount > 3) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - dial5SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial5SendCount++; + Interlocked.Exchange(ref _arc210VhfDial5WaitingForFeedback, 1); } + Reset(ref dial5Timeout); + } + else + { + dial5OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 3 || dial2SendCount > 9 || dial3SendCount > 9 || dial4SendCount > 9 || dial5SendCount > 3) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + dial5SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)) && !_shutdownARC210VHFThread); - SwapCockpitStandbyFrequencyARC210Vhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)); + + SwapCockpitStandbyFrequencyARC210Vhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _arc210VhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1075,7 +1084,7 @@ private void ARC210VhfSynchThreadMethod(int desiredPositionDial1, int desiredPos private void SendUhfToDCSBIOS() { - if (UhfNowSyncing()) + if (_uhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -1218,252 +1227,250 @@ private void SendUhfToDCSBIOS() // Small dial 0.00-0.95 [step of 0.05] // #1 - _shutdownUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownUHFThread = false; - _uhfSyncThread = new Thread(() => UhfSynchThreadMethod(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5)); - _uhfSyncThread.Start(); + _ilsSyncTaskTokenSource = new CancellationTokenSource(); + _ilsSyncTask = Task.Run(() => SyncUhfAsync(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5, _ilsSyncTaskTokenSource.Token)); } - private volatile bool _shutdownUHFThread; - private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5) + private async Task SyncUhfAsync(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial5Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + long dial5OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + var dial5SendCount = 0; + do { - Interlocked.Exchange(ref _uhfThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial5Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - long dial5OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - var dial5SendCount = 0; - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial5Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial5Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Let's do an ugly reset + } + + // Frequency selector 1 + // "2" "3" "A"/"-1" + // Pos 0 1 2 + if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - // Frequency selector 1 - // "2" "3" "A"/"-1" - // Pos 0 1 2 - if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + lock (_lockUhfDialsObject1) { - lock (_lockUhfDialsObject1) + if (_uhfCockpitFreq1DialPos != desiredPosition1) { - if (_uhfCockpitFreq1DialPos != desiredPosition1) - { - dial1OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq1DialPos < desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq1DialPos > desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } + dial1OkTime = DateTime.Now.Ticks; + } - Reset(ref dial1Timeout); + if (_uhfCockpitFreq1DialPos < desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq1DialPos > desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + lock (_lockUhfDialsObject2) { - lock (_lockUhfDialsObject2) + if (_uhfCockpitFreq2DialPos != desiredPosition2) { - if (_uhfCockpitFreq2DialPos != desiredPosition2) - { - dial2OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq2DialPos < desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq2DialPos > desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } + dial2OkTime = DateTime.Now.Ticks; + } - Reset(ref dial2Timeout); + if (_uhfCockpitFreq2DialPos < desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq2DialPos > desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDialsObject3) { - lock (_lockUhfDialsObject3) + if (_uhfCockpitFreq3DialPos != desiredPosition3) { - if (_uhfCockpitFreq3DialPos != desiredPosition3) - { - dial3OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq3DialPos < desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq3DialPos > desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + } - Reset(ref dial3Timeout); + if (_uhfCockpitFreq3DialPos < desiredPosition3) + { + command = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq3DialPos > desiredPosition3) + { + command = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDialsObject4) { - lock (_lockUhfDialsObject4) + if (_uhfCockpitFreq4DialPos != desiredPosition4) { - if (_uhfCockpitFreq4DialPos != desiredPosition4) - { - dial4OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq4DialPos < desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq4DialPos > desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } + dial4OkTime = DateTime.Now.Ticks; + } - Reset(ref dial4Timeout); + if (_uhfCockpitFreq4DialPos < desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq4DialPos > desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial4OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + lock (_lockUhfDialsObject5) { - lock (_lockUhfDialsObject5) + if (_uhfCockpitFreq5DialPos != desiredPosition5) { - if (_uhfCockpitFreq5DialPos != desiredPosition5) - { - dial5OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq5DialPos < desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq5DialPos > desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } + dial5OkTime = DateTime.Now.Ticks; + } - Reset(ref dial5Timeout); + if (_uhfCockpitFreq5DialPos < desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_uhfCockpitFreq5DialPos > desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial5OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial5SendCount++; + Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); } + Reset(ref dial5Timeout); + } + else + { + dial5OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)) && !_shutdownUHFThread); - SwapCockpitStandbyFrequencyUhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)); + SwapCockpitStandbyFrequencyUhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1471,7 +1478,7 @@ private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, in private void SendVhfFmToDCSBIOS() { - if (VhfFmNowSyncing()) + if (_vhfFmSyncTask?.Status == TaskStatus.Running) { return; } @@ -1529,179 +1536,183 @@ private void SendVhfFmToDCSBIOS() desiredPositionDial4 = 0; } - _shutdownVHFFMThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFFMThread = false; - _vhfFmSyncThread = new Thread(() => VhfFmSynchThreadMethod(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4)); - _vhfFmSyncThread.Start(); + _vhfFmSyncTaskTokenSource = new CancellationTokenSource(); + _vhfFmSyncTask = Task.Run(() => SyncVhfFmAsync(desiredPositionDial1, desiredPositionDial2, desiredPositionDial3, desiredPositionDial4, _vhfFmSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVHFFMThread; - private void VhfFmSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int frequencyDial4) + private async Task SyncVhfFmAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, int frequencyDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + + do { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Let's do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject1) { - lock (_lockVhfFmDialsObject1) + if (_vhfFmCockpitFreq1DialPos != desiredPositionDial1) { - if (_vhfFmCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfFmCockpitFreq1DialPos); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_1DIAL_COMMAND + GetCommandDirectionForVhfDial1(desiredPositionDial1, _vhfFmCockpitFreq1DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + + lock (_lockVhfFmDialsObject2) { - // Common.DebugP("b"); - lock (_lockVhfFmDialsObject2) + if (_vhfFmCockpitFreq2DialPos != desiredPositionDial2) { - if (_vhfFmCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfFmCockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_2DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial2, _vhfFmCockpitFreq2DialPos); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject3) { - lock (_lockVhfFmDialsObject3) + if (_vhfFmCockpitFreq3DialPos != desiredPositionDial3) { - if (_vhfFmCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; - var str = VHF_FM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfFmCockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_3DIAL_COMMAND + GetCommandDirectionForVhfDial23(desiredPositionDial3, _vhfFmCockpitFreq3DialPos); } - - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject4) { - lock (_lockVhfFmDialsObject4) + // "00" "25" "50" "75", only "00" and "50" used. + // Pos 0 1 2 3 + if (_vhfFmCockpitFreq4DialPos < frequencyDial4) { - // "00" "25" "50" "75", only "00" and "50" used. - // Pos 0 1 2 3 - if (_vhfFmCockpitFreq4DialPos < frequencyDial4) - { - dial4OkTime = DateTime.Now.Ticks; - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq4DialPos > frequencyDial4) - { - dial4OkTime = DateTime.Now.Ticks; - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq4DialPos > frequencyDial4) + { + dial4OkTime = DateTime.Now.Ticks; + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial4OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS - + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVHFFMThread); - SwapCockpitStandbyFrequencyVhfFm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyVhfFm(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1709,7 +1720,7 @@ private void VhfFmSynchThreadMethod(int desiredPositionDial1, int desiredPositio private void SendILSToDCSBIOS() { - if (IlsNowSyncing()) + if (_ilsSyncTask?.Status == TaskStatus.Running) { return; } @@ -1734,16 +1745,12 @@ private void SendILSToDCSBIOS() var freqDial1 = GetILSDialPosForFrequency(1, int.Parse(frequencyAsString.Substring(0, 3))); var freqDial2 = GetILSDialPosForFrequency(2, int.Parse(frequencyAsString.Substring(4, 2))); - // #1 - _shutdownILSThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownILSThread = false; - _ilsSyncThread = new Thread(() => ILSSynchThreadMethod(freqDial1, freqDial2)); - _ilsSyncThread.Start(); + + _ilsSyncTaskTokenSource = new CancellationTokenSource(); + _ilsSyncTask = Task.Run(() => SyncILSAsync(freqDial1, freqDial2, _ilsSyncTaskTokenSource.Token)); } - private volatile bool _shutdownILSThread; - private void ILSSynchThreadMethod(int position1, int position2) + private async Task SyncILSAsync(int position1, int position2, CancellationToken cancellationToken) { try { @@ -1762,37 +1769,40 @@ private void ILSSynchThreadMethod(int position1, int position2) { if (IsTimedOut(ref dial1Timeout)) { - ResetWaitingForFeedBack(ref _ilsDial1WaitingForFeedback); // Lets do an ugly reset + ResetWaitingForFeedBack(ref _ilsDial1WaitingForFeedback); // Let's do an ugly reset } if (IsTimedOut(ref dial2Timeout)) { - ResetWaitingForFeedBack(ref _ilsDial2WaitingForFeedback); // Lets do an ugly reset + ResetWaitingForFeedBack(ref _ilsDial2WaitingForFeedback); // Let's do an ugly reset } if (Interlocked.Read(ref _ilsDial1WaitingForFeedback) == 0) { + var command = string.Empty; + lock (_lockIlsDialsObject1) { if (_ilsCockpitFreq1DialPos < position1) { dial1OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); + command = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } else if (_ilsCockpitFreq1DialPos > position1) { dial1OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); + command = ILS_FREQ1_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - Reset(ref dial1Timeout); } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _ilsDial1WaitingForFeedback, 1); + } + Reset(ref dial1Timeout); } else { @@ -1801,28 +1811,30 @@ private void ILSSynchThreadMethod(int position1, int position2) if (Interlocked.Read(ref _ilsDial2WaitingForFeedback) == 0) { + var command = string.Empty; + lock (_lockIlsDialsObject2) { if (_ilsCockpitFreq2DialPos < position2) { dial2OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); + command = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } else if (_ilsCockpitFreq2DialPos > position2) { dial2OkTime = DateTime.Now.Ticks; - const string str = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); + command = ILS_FREQ2_DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } + } - Reset(ref dial2Timeout); + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _ilsDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); } else { @@ -1838,13 +1850,13 @@ private void ILSSynchThreadMethod(int position1, int position2) } Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownILSThread); + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); SwapCockpitStandbyFrequencyIls(); ShowFrequenciesOnPanel(); } - catch (ThreadAbortException) - { } catch (Exception ex) { Common.ShowErrorMessageBox(ex); @@ -1860,7 +1872,7 @@ private void ILSSynchThreadMethod(int position1, int position2) private void SendTacanToDCSBIOS() { - if (TacanNowSyncing()) + if (_tacanSyncTask?.Status == TaskStatus.Running) { return; } @@ -1881,146 +1893,146 @@ private void SendTacanToDCSBIOS() // #1 = 12 (position = value) // #2 = 0 (position = value) // #3 = 1 (position = value) - _shutdownTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownTACANThread = false; - _tacanSyncThread = new Thread(() => TacanSynchThreadMethod(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby)); - _tacanSyncThread.Start(); + _tacanSyncTaskTokenSource = new CancellationTokenSource(); + _tacanSyncTask = Task.Run(() => TacanAsync(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby, _tacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownTACANThread; - private void TacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + private async Task TacanAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + + do { - Interlocked.Exchange(ref _tacanThreadNowSynching, 1); - - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Let's do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { + ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Lets do an ugly reset - } - if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) + lock (_lockTacanDialsObject1) { - - lock (_lockTacanDialsObject1) + if (_tacanCockpitFreq1DialPos != desiredPositionDial1) { - if (_tacanCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ1_DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = TACAN_FREQ1_DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } + } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + lock (_lockTacanDialsObject2) { - // Common.DebugP("b"); - lock (_lockTacanDialsObject2) + if (_tacanCockpitFreq2DialPos != desiredPositionDial2) { - if (_tacanCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ2_DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); - } - - Reset(ref dial2Timeout); + command = TACAN_FREQ2_DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) - { + if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - lock (_lockTacanDialsObject3) + + lock (_lockTacanDialsObject3) + { + if (_tacanCockpitFreq3DialPos != desiredPositionDial3) { - if (_tacanCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; + dial3OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ3_DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); - } + command = TACAN_FREQ3_DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } - - Reset(ref dial3Timeout); - } - else - { - dial3OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS - } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownTACANThread); - SwapCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _tacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -2048,7 +2060,7 @@ private void ShowFrequenciesOnPanel() { case CurrentA10RadioMode.ARC210_VHF: { - if (!ARC210TurnedOn() ||( !ARC210VhfPresetSelected() && !ARC210VhfManualSelected())) + if (!ARC210TurnedOn() || (!ARC210VhfPresetSelected() && !ARC210VhfManualSelected())) { SetPZ69DisplayBlank(ref bytes, PZ69LCDPosition.UPPER_ACTIVE_LEFT); SetPZ69DisplayBlank(ref bytes, PZ69LCDPosition.UPPER_STBY_RIGHT); @@ -2444,7 +2456,7 @@ private string GetARC210VhfFrequencyAsString() frequencyAsString += freq; } - + return frequencyAsString; } @@ -2499,7 +2511,7 @@ private string GetUhfFrequencyAsString() return frequencyAsString; } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -2522,7 +2534,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_RADIO_MODE_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_RADIO_MODE_DIAL_INCREASE); } else if (ARC210VhfManualSelected()) { @@ -2544,14 +2556,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -2574,14 +2586,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_INCREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_INCREASE); } else if (_vhfFmBigFrequencyStandby.Equals(76)) { @@ -2642,7 +2654,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_RADIO_MODE_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_RADIO_MODE_DIAL_DECREASE); } else if (ARC210VhfManualSelected()) { @@ -2665,14 +2677,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225.00)) { @@ -2694,14 +2706,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_DECREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_DECREASE); } else if (_vhfFmBigFrequencyStandby.Equals(30)) { @@ -2762,11 +2774,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_MODE_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_MODE_DIAL_INCREASE); } else if (ARC210VhfPresetSelected() && _arc210VhfChannelDialClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(ARC210_VHF_CHANNEL_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_CHANNEL_DIAL_INCREASE); } else if (ARC210VhfManualSelected()) { @@ -2783,7 +2795,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -2801,7 +2813,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_INCREASE); } } else @@ -2874,11 +2886,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_MODE_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_MODE_DIAL_DECREASE); } else if (ARC210VhfPresetSelected() && _arc210VhfChannelDialClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(ARC210_VHF_CHANNEL_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_CHANNEL_DIAL_DECREASE); } else if (ARC210VhfManualSelected()) { @@ -2894,7 +2906,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -2912,7 +2924,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_DECREASE); } } else @@ -2985,7 +2997,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_RADIO_MODE_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_RADIO_MODE_DIAL_INCREASE); } else if (ARC210VhfManualSelected()) { @@ -3007,7 +3019,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else @@ -3015,7 +3027,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { // 225-399 - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -3037,14 +3049,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_INCREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_INCREASE); } else if (_vhfFmBigFrequencyStandby.Equals(76)) { @@ -3105,7 +3117,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_RADIO_MODE_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_RADIO_MODE_DIAL_DECREASE); } else if (ARC210VhfManualSelected()) { @@ -3128,14 +3140,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225.00)) { @@ -3157,14 +3169,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_MODE_DECREASE); } } else { if (VhfFmPresetSelected() && _vhfFmChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_PRESET_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_PRESET_DECREASE); } else if (_vhfFmBigFrequencyStandby.Equals(30)) { @@ -3225,11 +3237,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_MODE_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_MODE_DIAL_INCREASE); } else if (ARC210VhfPresetSelected() && _arc210VhfChannelDialClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(ARC210_VHF_CHANNEL_DIAL_INCREASE); + await DCSBIOS.SendAsync(ARC210_VHF_CHANNEL_DIAL_INCREASE); } else if (ARC210VhfManualSelected()) { @@ -3246,7 +3258,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -3264,7 +3276,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_INCREASE); } } else @@ -3337,11 +3349,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(ARC210_VHF_MODE_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_MODE_DIAL_DECREASE); } else if (ARC210VhfPresetSelected() && _arc210VhfChannelDialClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(ARC210_VHF_CHANNEL_DIAL_DECREASE); + await DCSBIOS.SendAsync(ARC210_VHF_CHANNEL_DIAL_DECREASE); } else if (ARC210VhfManualSelected()) { @@ -3357,7 +3369,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -3375,7 +3387,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vhfFmFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VHF_FM_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VHF_FM_FREQ_MODE_DECREASE); } } else @@ -3591,7 +3603,7 @@ private void CheckFrequenciesForValidity() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { lock (LockLCDUpdateObject) { @@ -3769,7 +3781,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_upperButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10CII.UPPER_FREQ_SWITCH); } @@ -3787,7 +3799,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_lowerButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsA10CII.LOWER_FREQ_SWITCH); } @@ -3804,11 +3816,11 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_A10C, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } } - - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } - + public override void ClearSettings(bool setIsDirty = false) { // ignore @@ -3818,7 +3830,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobA10CII.GetRadioPanelKnobs(); @@ -4331,31 +4343,6 @@ private bool UhfPresetSelected() return _uhfCockpitFreqMode == 1; } - private bool ARC210VhfNowSyncing() - { - return Interlocked.Read(ref _arc210VhfThreadNowSynching) > 0; - } - - private bool UhfNowSyncing() - { - return Interlocked.Read(ref _uhfThreadNowSynching) > 0; - } - - private bool VhfFmNowSyncing() - { - return Interlocked.Read(ref _vhfFmThreadNowSynching) > 0; - } - - private bool IlsNowSyncing() - { - return Interlocked.Read(ref _ilsThreadNowSynching) > 0; - } - - private bool TacanNowSyncing() - { - return Interlocked.Read(ref _tacanThreadNowSynching) > 0; - } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69AH64D.cs b/src/NonVisuals/Radios/RadioPanelPZ69AH64D.cs index ec667b454..cc2b0637b 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69AH64D.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69AH64D.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.Helpers; namespace NonVisuals.Radios @@ -93,7 +94,7 @@ private enum CurrentAH64DRadioMode public RadioPanelPZ69AH64D(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -205,7 +206,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) //ShowFrequenciesOnPanel(); } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAH64D knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsAH64D.UPPER_FREQ_SWITCH || knob == RadioPanelPZ69KnobsAH64D.LOWER_FREQ_SWITCH)) { @@ -229,27 +230,27 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D knob) { case CurrentAH64DRadioMode.VHF: { - SendVHFToDCSBIOS(); + await SendVHFToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.FM1: { - SendFM1ToDCSBIOS(); + await SendFM1ToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.FM2: { - SendFM2ToDCSBIOS(); + await SendFM2ToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.HF: { - SendHFToDCSBIOS(); + await SendHFToDCSBIOSAsync(); break; } } @@ -263,27 +264,27 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D knob) case CurrentAH64DRadioMode.VHF: { - SendVHFToDCSBIOS(); + await SendVHFToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.FM1: { - SendFM1ToDCSBIOS(); + await SendFM1ToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.FM2: { - SendFM2ToDCSBIOS(); + await SendFM2ToDCSBIOSAsync(); break; } case CurrentAH64DRadioMode.HF: { - SendHFToDCSBIOS(); + await SendHFToDCSBIOSAsync(); break; } } @@ -292,12 +293,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D knob) } } - private void SendVHFToDCSBIOS() + private async Task SendVHFToDCSBIOSAsync() { try { var newStandbyFrequency = _vhfCockpitFrequency; - DCSBIOS.Send($"{VHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.VHF)}\n"); + await DCSBIOS.SendAsync($"{VHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.VHF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _vhfBigFrequencyStandby = uint.Parse(array[0]); _vhfSmallFrequencyStandby = uint.Parse(array[1]); @@ -309,12 +310,12 @@ private void SendVHFToDCSBIOS() } } - private void SendUHFToDCSBIOS() + private async Task SendUHFToDCSBIOSAsync() { try { var newStandbyFrequency = _uhfCockpitFrequency; - DCSBIOS.Send($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.UHF)}\n"); + await DCSBIOS.SendAsync($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.UHF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _uhfBigFrequencyStandby = uint.Parse(array[0]); _uhfSmallFrequencyStandby = uint.Parse(array[1]); @@ -326,12 +327,12 @@ private void SendUHFToDCSBIOS() } } - private void SendFM1ToDCSBIOS() + private async Task SendFM1ToDCSBIOSAsync() { try { var newStandbyFrequency = _fm1CockpitFrequency; - DCSBIOS.Send($"{FM1_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.FM1)}\n"); + await DCSBIOS.SendAsync($"{FM1_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.FM1)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _fm1BigFrequencyStandby = uint.Parse(array[0]); _fm1SmallFrequencyStandby = uint.Parse(array[1]); @@ -343,12 +344,12 @@ private void SendFM1ToDCSBIOS() } } - private void SendFM2ToDCSBIOS() + private async Task SendFM2ToDCSBIOSAsync() { try { var newStandbyFrequency = _fm2CockpitFrequency; - DCSBIOS.Send($"{FM2_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.FM2)}\n"); + await DCSBIOS.SendAsync($"{FM2_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.FM2)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _fm2BigFrequencyStandby = uint.Parse(array[0]); _fm2SmallFrequencyStandby = uint.Parse(array[1]); @@ -360,12 +361,12 @@ private void SendFM2ToDCSBIOS() } } - private void SendHFToDCSBIOS() + private async Task SendHFToDCSBIOSAsync() { try { var newStandbyFrequency = _hfCockpitFrequency; - DCSBIOS.Send($"{HF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.HF)}\n"); + await DCSBIOS.SendAsync($"{HF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentAH64DRadioMode.HF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _hfBigFrequencyStandby = uint.Parse(array[0]); _hfSmallFrequencyStandby = uint.Parse(array[1]); @@ -1106,146 +1107,145 @@ private void AdjustFrequency(IEnumerable hashSet) ShowFrequenciesOnPanel(); } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobAH64D)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobAH64D)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsAH64D.UPPER_VHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsAH64D.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.VHF; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.VHF; } - case RadioPanelPZ69KnobsAH64D.UPPER_UHF: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.UHF; } - case RadioPanelPZ69KnobsAH64D.UPPER_FM1: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_FM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.FM1; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.FM1; } - case RadioPanelPZ69KnobsAH64D.UPPER_FM2: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_FM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.FM2; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.FM2; } - case RadioPanelPZ69KnobsAH64D.UPPER_HF: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_HF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.HF; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.HF; } - case RadioPanelPZ69KnobsAH64D.LOWER_VHF: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.VHF; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.VHF; } - case RadioPanelPZ69KnobsAH64D.LOWER_UHF: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.UHF; } - case RadioPanelPZ69KnobsAH64D.LOWER_FM1: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_FM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.FM1; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.FM1; } - case RadioPanelPZ69KnobsAH64D.LOWER_FM2: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_FM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.FM2; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.FM2; } - case RadioPanelPZ69KnobsAH64D.LOWER_HF: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_HF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.HF; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.HF; } - case RadioPanelPZ69KnobsAH64D.UPPER_FREQ_SWITCH: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D.UPPER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAH64D.UPPER_FREQ_SWITCH); } - case RadioPanelPZ69KnobsAH64D.LOWER_FREQ_SWITCH: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAH64D.LOWER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAH64D.LOWER_FREQ_SWITCH); } - case RadioPanelPZ69KnobsAH64D.UPPER_NO_USE3: - case RadioPanelPZ69KnobsAH64D.UPPER_NO_USE4: + break; + } + case RadioPanelPZ69KnobsAH64D.UPPER_NO_USE3: + case RadioPanelPZ69KnobsAH64D.UPPER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAH64DRadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentAH64DRadioMode.NO_USE; } - case RadioPanelPZ69KnobsAH64D.LOWER_NO_USE3: - case RadioPanelPZ69KnobsAH64D.LOWER_NO_USE4: + break; + } + case RadioPanelPZ69KnobsAH64D.LOWER_NO_USE3: + case RadioPanelPZ69KnobsAH64D.LOWER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAH64DRadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentAH64DRadioMode.NO_USE; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + AdjustFrequency(hashSet); } public override void ClearSettings(bool setIsDirty = false) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69AJS37.cs b/src/NonVisuals/Radios/RadioPanelPZ69AJS37.cs index ea23bcfcf..ef55c8c41 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69AJS37.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69AJS37.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -90,7 +91,7 @@ private enum FR22DialSideSelected private long _doUpdatePanelLCD; public RadioPanelPZ69AJS37(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -122,7 +123,7 @@ public override void InitPanel() BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } - + public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) { try @@ -178,7 +179,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37 knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAJS37 knob) { try { @@ -214,7 +215,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37 knob) case CurrentAJS37RadioMode.TILS: { - DCSBIOS.Send(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); + await DCSBIOS.SendAsync(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); break; } @@ -242,7 +243,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37 knob) case CurrentAJS37RadioMode.TILS: { - DCSBIOS.Send(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); + await DCSBIOS.SendAsync(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); break; } @@ -262,154 +263,153 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37 knob) } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) + var radioPanelKnob = (RadioPanelKnobAJS37)radioPanelKnobObject; + + switch (radioPanelKnob.RadioPanelPZ69Knob) { - var radioPanelKnob = (RadioPanelKnobAJS37)radioPanelKnobObject; + case RadioPanelPZ69KnobsAJS37.UPPER_FR22: + { + if (radioPanelKnob.IsOn) + { + SetUpperRadioMode(CurrentAJS37RadioMode.FR22); + } + break; + } - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsAJS37.UPPER_FR22: + case RadioPanelPZ69KnobsAJS37.UPPER_FR24: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentAJS37RadioMode.FR22); - } - break; + SetUpperRadioMode(CurrentAJS37RadioMode.FR24); } + break; + } - case RadioPanelPZ69KnobsAJS37.UPPER_FR24: + case RadioPanelPZ69KnobsAJS37.UPPER_TILS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentAJS37RadioMode.FR24); - } - break; + SetUpperRadioMode(CurrentAJS37RadioMode.TILS); } + break; + } - case RadioPanelPZ69KnobsAJS37.UPPER_TILS: + case RadioPanelPZ69KnobsAJS37.LOWER_FR22: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentAJS37RadioMode.TILS); - } - break; + SetLowerRadioMode(CurrentAJS37RadioMode.FR22); } + break; + } - case RadioPanelPZ69KnobsAJS37.LOWER_FR22: + case RadioPanelPZ69KnobsAJS37.LOWER_FR24: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentAJS37RadioMode.FR22); - } - break; + SetLowerRadioMode(CurrentAJS37RadioMode.FR24); } + break; + } - case RadioPanelPZ69KnobsAJS37.LOWER_FR24: + case RadioPanelPZ69KnobsAJS37.LOWER_TILS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentAJS37RadioMode.FR24); - } - break; + SetLowerRadioMode(CurrentAJS37RadioMode.TILS); } + break; + } + + case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE0: + case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE1: + case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE2: + case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE3: + case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE0: + case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE1: + case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE2: + case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE3: + { + break; + } - case RadioPanelPZ69KnobsAJS37.LOWER_TILS: + case RadioPanelPZ69KnobsAJS37.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsAJS37.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsAJS37.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsAJS37.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsAJS37.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsAJS37.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsAJS37.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsAJS37.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } + + case RadioPanelPZ69KnobsAJS37.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentAJS37RadioMode.FR22) { if (radioPanelKnob.IsOn) { - SetLowerRadioMode(CurrentAJS37RadioMode.TILS); + _fr22DialSideSelected = _fr22DialSideSelected == FR22DialSideSelected.Left ? FR22DialSideSelected.Right : FR22DialSideSelected.Left; } - break; } + else if (_currentUpperRadioMode == CurrentAJS37RadioMode.FR24 && radioPanelKnob.IsOn) + { - case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE0: - case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE1: - case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE2: - case RadioPanelPZ69KnobsAJS37.UPPER_NO_USE3: - case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE0: - case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE1: - case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE2: - case RadioPanelPZ69KnobsAJS37.LOWER_NO_USE3: + } + else if (_currentUpperRadioMode == CurrentAJS37RadioMode.TILS && radioPanelKnob.IsOn) { - break; + await DCSBIOS.SendAsync(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); } - - case RadioPanelPZ69KnobsAJS37.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsAJS37.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsAJS37.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsAJS37.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsAJS37.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsAJS37.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsAJS37.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsAJS37.LOWER_SMALL_FREQ_WHEEL_DEC: + else if (radioPanelKnob.IsOn) { - // Ignore - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAJS37.UPPER_FREQ_SWITCH); } + break; + } - case RadioPanelPZ69KnobsAJS37.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsAJS37.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentAJS37RadioMode.FR22) { - if (_currentUpperRadioMode == CurrentAJS37RadioMode.FR22) - { - if (radioPanelKnob.IsOn) - { - _fr22DialSideSelected = _fr22DialSideSelected == FR22DialSideSelected.Left ? FR22DialSideSelected.Right : FR22DialSideSelected.Left; - } - } - else if (_currentUpperRadioMode == CurrentAJS37RadioMode.FR24 && radioPanelKnob.IsOn) - { - - } - else if (_currentUpperRadioMode == CurrentAJS37RadioMode.TILS && radioPanelKnob.IsOn) - { - DCSBIOS.Send(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); - } - else if (radioPanelKnob.IsOn) + if (radioPanelKnob.IsOn) { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37.UPPER_FREQ_SWITCH); + _fr22DialSideSelected = _fr22DialSideSelected == FR22DialSideSelected.Left ? FR22DialSideSelected.Right : FR22DialSideSelected.Left; } - break; } - - case RadioPanelPZ69KnobsAJS37.LOWER_FREQ_SWITCH: + else if (_currentLowerRadioMode == CurrentAJS37RadioMode.FR24 && radioPanelKnob.IsOn) { - if (_currentLowerRadioMode == CurrentAJS37RadioMode.FR22) - { - if (radioPanelKnob.IsOn) - { - _fr22DialSideSelected = _fr22DialSideSelected == FR22DialSideSelected.Left ? FR22DialSideSelected.Right : FR22DialSideSelected.Left; - } - } - else if (_currentLowerRadioMode == CurrentAJS37RadioMode.FR24 && radioPanelKnob.IsOn) - { - } - else if (_currentLowerRadioMode == CurrentAJS37RadioMode.TILS && radioPanelKnob.IsOn) - { - DCSBIOS.Send(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); - } - else if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsAJS37.LOWER_FREQ_SWITCH); - } - break; } - } + else if (_currentLowerRadioMode == CurrentAJS37RadioMode.TILS && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(TILS_CHANNEL_LAYER_DIAL_COMMAND_TOGGLE); + } + else if (radioPanelKnob.IsOn) + { + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsAJS37.LOWER_FREQ_SWITCH); + } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AJS37, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AJS37, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -417,7 +417,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -439,7 +439,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_INCREASE_COMMAND : FR22_RIGHT_BIG_DIAL_INCREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_INCREASE_COMMAND : FR22_RIGHT_BIG_DIAL_INCREASE_COMMAND); break; } @@ -450,7 +450,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _tilsChannelDialSkipper.Click(TILS_CHANNEL_DIAL_COMMAND_INC); + await _tilsChannelDialSkipper.ClickAsync(TILS_CHANNEL_DIAL_COMMAND_INC); break; } @@ -468,7 +468,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_DECREASE_COMMAND : FR22_RIGHT_BIG_DIAL_DECREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_DECREASE_COMMAND : FR22_RIGHT_BIG_DIAL_DECREASE_COMMAND); break; } @@ -479,7 +479,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _tilsChannelDialSkipper.Click(TILS_CHANNEL_DIAL_COMMAND_DEC); + await _tilsChannelDialSkipper.ClickAsync(TILS_CHANNEL_DIAL_COMMAND_DEC); break; } @@ -497,7 +497,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_INCREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_INCREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_INCREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_INCREASE_COMMAND); break; } @@ -508,7 +508,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _masterModeSelectorDialSkipper.Click(MASTER_MODE_SELECTOR_COMMAND_INC); + await _masterModeSelectorDialSkipper.ClickAsync(MASTER_MODE_SELECTOR_COMMAND_INC); break; } @@ -526,7 +526,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_DECREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_DECREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_DECREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_DECREASE_COMMAND); break; } @@ -537,7 +537,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _masterModeSelectorDialSkipper.Click(MASTER_MODE_SELECTOR_COMMAND_DEC); + await _masterModeSelectorDialSkipper.ClickAsync(MASTER_MODE_SELECTOR_COMMAND_DEC); break; } @@ -555,7 +555,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_INCREASE_COMMAND : FR22_RIGHT_BIG_DIAL_INCREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_INCREASE_COMMAND : FR22_RIGHT_BIG_DIAL_INCREASE_COMMAND); break; } @@ -566,7 +566,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _tilsChannelDialSkipper.Click(TILS_CHANNEL_DIAL_COMMAND_INC); + await _tilsChannelDialSkipper.ClickAsync(TILS_CHANNEL_DIAL_COMMAND_INC); break; } @@ -584,7 +584,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_DECREASE_COMMAND : FR22_RIGHT_BIG_DIAL_DECREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_BIG_DIAL_DECREASE_COMMAND : FR22_RIGHT_BIG_DIAL_DECREASE_COMMAND); break; } @@ -595,7 +595,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _tilsChannelDialSkipper.Click(TILS_CHANNEL_DIAL_COMMAND_DEC); + await _tilsChannelDialSkipper.ClickAsync(TILS_CHANNEL_DIAL_COMMAND_DEC); break; } @@ -613,7 +613,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_INCREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_INCREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_INCREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_INCREASE_COMMAND); break; } @@ -624,7 +624,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _masterModeSelectorDialSkipper.Click(MASTER_MODE_SELECTOR_COMMAND_INC); + await _masterModeSelectorDialSkipper.ClickAsync(MASTER_MODE_SELECTOR_COMMAND_INC); break; } @@ -642,7 +642,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAJS37RadioMode.FR22: { - DCSBIOS.Send(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_DECREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_DECREASE_COMMAND); + await DCSBIOS.SendAsync(_fr22DialSideSelected == FR22DialSideSelected.Left ? FR22_LEFT_SMALL_DIAL_DECREASE_COMMAND : FR22_RIGHT_SMALL_DIAL_DECREASE_COMMAND); break; } @@ -653,7 +653,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentAJS37RadioMode.TILS: { - _masterModeSelectorDialSkipper.Click(MASTER_MODE_SELECTOR_COMMAND_DEC); + await _masterModeSelectorDialSkipper.ClickAsync(MASTER_MODE_SELECTOR_COMMAND_DEC); break; } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69AV8BNA.cs b/src/NonVisuals/Radios/RadioPanelPZ69AV8BNA.cs index af33fa645..f430d8aa0 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69AV8BNA.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69AV8BNA.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -70,7 +71,7 @@ private enum CurrentAV8BNARadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69AV8BNA(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -152,7 +153,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelKnobAV8BNA knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelKnobAV8BNA knob) { if (IgnoreSwitchButtonOnce() && (knob.RadioPanelPZ69Knob == RadioPanelPZ69KnobsAV8BNA.UPPER_FREQ_SWITCH || knob.RadioPanelPZ69Knob == RadioPanelPZ69KnobsAV8BNA.LOWER_FREQ_SWITCH)) { @@ -176,14 +177,14 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobAV8BNA knob) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(knob.IsOn ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); + await DCSBIOS.SendAsync(knob.IsOn ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); ShowFrequenciesOnPanel(); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(knob.IsOn ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); + await DCSBIOS.SendAsync(knob.IsOn ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); ShowFrequenciesOnPanel(); break; } @@ -197,14 +198,14 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobAV8BNA knob) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(knob.IsOn ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); + await DCSBIOS.SendAsync(knob.IsOn ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); ShowFrequenciesOnPanel(); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(knob.IsOn ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); + await DCSBIOS.SendAsync(knob.IsOn ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); ShowFrequenciesOnPanel(); break; } @@ -303,7 +304,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -326,11 +327,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm1ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); } else { - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); } break; } @@ -340,11 +341,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm2ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); } else { - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); } break; } @@ -361,11 +362,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm1ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); } else { - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); } break; } @@ -375,11 +376,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm2ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); } else { - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); } break; } @@ -393,13 +394,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_INC); + await DCSBIOS.SendAsync(COMM1_VOL_INC); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_INC); + await DCSBIOS.SendAsync(COMM2_VOL_INC); break; } } @@ -412,13 +413,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_DEC); + await DCSBIOS.SendAsync(COMM1_VOL_DEC); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_DEC); + await DCSBIOS.SendAsync(COMM2_VOL_DEC); break; } } @@ -434,11 +435,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm1ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); } else { - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); } break; } @@ -448,11 +449,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm2ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); } else { - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); } break; } @@ -469,11 +470,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm1ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); } else { - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); } break; } @@ -483,11 +484,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_comm2ChannelClickSpeedDetector.ClickAndCheck()) { // No need for turbo - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); } else { - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); } break; } @@ -501,13 +502,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_INC); + await DCSBIOS.SendAsync(COMM1_VOL_INC); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_INC); + await DCSBIOS.SendAsync(COMM2_VOL_INC); break; } } @@ -520,13 +521,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentAV8BNARadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_DEC); + await DCSBIOS.SendAsync(COMM1_VOL_DEC); break; } case CurrentAV8BNARadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_DEC); + await DCSBIOS.SendAsync(COMM2_VOL_DEC); break; } } @@ -541,139 +542,137 @@ private void AdjustFrequency(IEnumerable hashSet) } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobAV8BNA)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobAV8BNA)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsAV8BNA.UPPER_COMM1: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsAV8BNA.UPPER_COMM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAV8BNARadioMode.COMM1; - } - break; + _currentUpperRadioMode = CurrentAV8BNARadioMode.COMM1; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_COMM2: + case RadioPanelPZ69KnobsAV8BNA.UPPER_COMM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAV8BNARadioMode.COMM2; - } - break; + _currentUpperRadioMode = CurrentAV8BNARadioMode.COMM2; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_NAV1: - case RadioPanelPZ69KnobsAV8BNA.UPPER_NAV2: - case RadioPanelPZ69KnobsAV8BNA.UPPER_ADF: - case RadioPanelPZ69KnobsAV8BNA.UPPER_DME: - case RadioPanelPZ69KnobsAV8BNA.UPPER_XPDR: + case RadioPanelPZ69KnobsAV8BNA.UPPER_NAV1: + case RadioPanelPZ69KnobsAV8BNA.UPPER_NAV2: + case RadioPanelPZ69KnobsAV8BNA.UPPER_ADF: + case RadioPanelPZ69KnobsAV8BNA.UPPER_DME: + case RadioPanelPZ69KnobsAV8BNA.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentAV8BNARadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentAV8BNARadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_COMM1: + case RadioPanelPZ69KnobsAV8BNA.LOWER_COMM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAV8BNARadioMode.COMM1; - } - break; + _currentLowerRadioMode = CurrentAV8BNARadioMode.COMM1; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_COMM2: + case RadioPanelPZ69KnobsAV8BNA.LOWER_COMM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAV8BNARadioMode.COMM2; - } - break; + _currentLowerRadioMode = CurrentAV8BNARadioMode.COMM2; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_NAV1: - case RadioPanelPZ69KnobsAV8BNA.LOWER_NAV2: - case RadioPanelPZ69KnobsAV8BNA.LOWER_ADF: - case RadioPanelPZ69KnobsAV8BNA.LOWER_DME: - case RadioPanelPZ69KnobsAV8BNA.LOWER_XPDR: + case RadioPanelPZ69KnobsAV8BNA.LOWER_NAV1: + case RadioPanelPZ69KnobsAV8BNA.LOWER_NAV2: + case RadioPanelPZ69KnobsAV8BNA.LOWER_ADF: + case RadioPanelPZ69KnobsAV8BNA.LOWER_DME: + case RadioPanelPZ69KnobsAV8BNA.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentAV8BNARadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentAV8BNARadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.UPPER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.UPPER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.UPPER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.UPPER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.LOWER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.LOWER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.LOWER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsAV8BNA.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsAV8BNA.UPPER_FREQ_SWITCH: - { - SendFrequencyToDCSBIOS(radioPanelKnob); - break; - } + case RadioPanelPZ69KnobsAV8BNA.UPPER_FREQ_SWITCH: + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob); + break; + } - case RadioPanelPZ69KnobsAV8BNA.LOWER_FREQ_SWITCH: - { - SendFrequencyToDCSBIOS(radioPanelKnob); - break; - } - } + case RadioPanelPZ69KnobsAV8BNA.LOWER_FREQ_SWITCH: + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob); + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AV8BNA, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AV8BNA, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } public override void ClearSettings(bool setIsDirty = false) { } @@ -682,7 +681,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobAV8BNA.GetRadioPanelKnobs(); diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Base.cs b/src/NonVisuals/Radios/RadioPanelPZ69Base.cs index 72d764799..00175da38 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Base.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Base.cs @@ -1,4 +1,5 @@ -using NonVisuals.Interfaces; +using System.Threading.Tasks; +using NonVisuals.Interfaces; namespace NonVisuals.Radios { @@ -77,9 +78,9 @@ protected override void Dispose(bool disposing) base.Dispose(disposing); } - protected abstract void PZ69KnobChanged(IEnumerable hashSet); + protected abstract Task PZ69KnobChangedAsync(IEnumerable hashSet); - protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task GamingPanelKnobChangedAsync(bool isFirstReport, IEnumerable hashSet) { /* * Here we want to remove ACT/STBY info. This way we get what radio mode is chosen. @@ -103,11 +104,11 @@ protected override void GamingPanelKnobChanged(bool isFirstReport, IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobBf109)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobBf109)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsBf109.UPPER_FUG16ZY: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsBf109.UPPER_FUG16ZY: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentBf109RadioMode.FUG16ZY); - } - break; + SetUpperRadioMode(CurrentBf109RadioMode.FUG16ZY); } + break; + } - case RadioPanelPZ69KnobsBf109.UPPER_IFF: + case RadioPanelPZ69KnobsBf109.UPPER_IFF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentBf109RadioMode.IFF); - } - break; + SetUpperRadioMode(CurrentBf109RadioMode.IFF); } + break; + } - case RadioPanelPZ69KnobsBf109.UPPER_HOMING: + case RadioPanelPZ69KnobsBf109.UPPER_HOMING: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentBf109RadioMode.HOMING); - } - break; + SetUpperRadioMode(CurrentBf109RadioMode.HOMING); } + break; + } - case RadioPanelPZ69KnobsBf109.UPPER_NO_USE1: - case RadioPanelPZ69KnobsBf109.UPPER_NO_USE2: - case RadioPanelPZ69KnobsBf109.UPPER_NO_USE3: - case RadioPanelPZ69KnobsBf109.UPPER_NO_USE4: + case RadioPanelPZ69KnobsBf109.UPPER_NO_USE1: + case RadioPanelPZ69KnobsBf109.UPPER_NO_USE2: + case RadioPanelPZ69KnobsBf109.UPPER_NO_USE3: + case RadioPanelPZ69KnobsBf109.UPPER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentBf109RadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentBf109RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsBf109.LOWER_FUG16ZY: + case RadioPanelPZ69KnobsBf109.LOWER_FUG16ZY: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentBf109RadioMode.FUG16ZY); - } - break; + SetLowerRadioMode(CurrentBf109RadioMode.FUG16ZY); } + break; + } - case RadioPanelPZ69KnobsBf109.LOWER_IFF: + case RadioPanelPZ69KnobsBf109.LOWER_IFF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentBf109RadioMode.IFF); - } - break; + SetLowerRadioMode(CurrentBf109RadioMode.IFF); } + break; + } - case RadioPanelPZ69KnobsBf109.LOWER_HOMING: + case RadioPanelPZ69KnobsBf109.LOWER_HOMING: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentBf109RadioMode.HOMING); - } - break; + SetLowerRadioMode(CurrentBf109RadioMode.HOMING); } + break; + } - case RadioPanelPZ69KnobsBf109.LOWER_NO_USE1: - case RadioPanelPZ69KnobsBf109.LOWER_NO_USE2: - case RadioPanelPZ69KnobsBf109.LOWER_NO_USE3: - case RadioPanelPZ69KnobsBf109.LOWER_NO_USE4: + case RadioPanelPZ69KnobsBf109.LOWER_NO_USE1: + case RadioPanelPZ69KnobsBf109.LOWER_NO_USE2: + case RadioPanelPZ69KnobsBf109.LOWER_NO_USE3: + case RadioPanelPZ69KnobsBf109.LOWER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentBf109RadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentBf109RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsBf109.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsBf109.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsBf109.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsBf109.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsBf109.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsBf109.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsBf109.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsBf109.LOWER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsBf109.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsBf109.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsBf109.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsBf109.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsBf109.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsBf109.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsBf109.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsBf109.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } + + case RadioPanelPZ69KnobsBf109.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentBf109RadioMode.IFF) { - // Ignore - break; + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); } - case RadioPanelPZ69KnobsBf109.UPPER_FREQ_SWITCH: + if (_currentUpperRadioMode == CurrentBf109RadioMode.HOMING) { - if (_currentLowerRadioMode == CurrentBf109RadioMode.IFF) + if (radioPanelKnob.IsOn) { - DCSBIOS.Send(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); - } + string command; - if (_currentUpperRadioMode == CurrentBf109RadioMode.HOMING) - { - if (radioPanelKnob.IsOn) + lock (_lockHomingDialObject1) { - lock (_lockHomingDialObject1) - { - DCSBIOS.Send(_homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC); - } + command = _homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC; } + + await DCSBIOS.SendAsync(command); } - break; } + break; + } - case RadioPanelPZ69KnobsBf109.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsBf109.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentBf109RadioMode.IFF) { - if (_currentLowerRadioMode == CurrentBf109RadioMode.IFF) - { - DCSBIOS.Send(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); - } + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); + } - if (_currentLowerRadioMode == CurrentBf109RadioMode.HOMING) + if (_currentLowerRadioMode == CurrentBf109RadioMode.HOMING) + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) + string command; + + lock (_lockHomingDialObject1) { - lock (_lockHomingDialObject1) - { - DCSBIOS.Send(_homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC); - } + command = _homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC; } + + await DCSBIOS.SendAsync(command); } - break; } - } - - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_BF109, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + break; + } } - AdjustFrequency(hashSet); + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_BF109, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); + } } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -393,7 +400,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -416,13 +423,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentBf109RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_INC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_INC); break; } case CurrentBf109RadioMode.IFF: { - _fug25aIFFDialSkipper.Click(FUG25AIFFCommandInc); + await _fug25aIFFDialSkipper.ClickAsync(FUG25AIFFCommandInc); break; } @@ -446,13 +453,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentBf109RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_DEC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_DEC); break; } case CurrentBf109RadioMode.IFF: { - _fug25aIFFDialSkipper.Click(FUG25AIFFCommandDec); + await _fug25aIFFDialSkipper.ClickAsync(FUG25AIFFCommandDec); break; } @@ -483,13 +490,13 @@ private void AdjustFrequency(IEnumerable hashSet) // Change faster changeFaster = true; } - DCSBIOS.Send(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_INC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_INC); + await DCSBIOS.SendAsync(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_INC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_INC); break; } case CurrentBf109RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_INC); break; } @@ -520,13 +527,13 @@ private void AdjustFrequency(IEnumerable hashSet) // Change faster changeFaster = true; } - DCSBIOS.Send(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_DEC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_DEC); + await DCSBIOS.SendAsync(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_DEC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_DEC); break; } case CurrentBf109RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_DEC); break; } @@ -550,13 +557,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentBf109RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_INC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_INC); break; } case CurrentBf109RadioMode.IFF: { - _fug25aIFFDialSkipper.Click(FUG25AIFFCommandInc); + await _fug25aIFFDialSkipper.ClickAsync(FUG25AIFFCommandInc); break; } @@ -580,13 +587,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentBf109RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_DEC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_DEC); break; } case CurrentBf109RadioMode.IFF: { - _fug25aIFFDialSkipper.Click(FUG25AIFFCommandDec); + await _fug25aIFFDialSkipper.ClickAsync(FUG25AIFFCommandDec); break; } @@ -617,13 +624,13 @@ private void AdjustFrequency(IEnumerable hashSet) // Change faster changeFaster = true; } - DCSBIOS.Send(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_INC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_INC); + await DCSBIOS.SendAsync(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_INC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_INC); break; } case CurrentBf109RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_INC); break; } @@ -654,13 +661,13 @@ private void AdjustFrequency(IEnumerable hashSet) // Change faster changeFaster = true; } - DCSBIOS.Send(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_DEC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_DEC); + await DCSBIOS.SendAsync(changeFaster ? FUG16_ZY_FINE_TUNE_COMMAND_DEC_MORE : FUG16_ZY_FINE_TUNE_COMMAND_DEC); break; } case CurrentBf109RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_DEC); break; } @@ -831,7 +838,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - + public override void ClearSettings(bool setIsDirty = false) { } public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69C101.cs b/src/NonVisuals/Radios/RadioPanelPZ69C101.cs index 887b235cd..845158c11 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69C101.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69C101.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -64,7 +65,7 @@ private enum CurrentC101RadioMode public RadioPanelPZ69C101(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -95,12 +96,12 @@ public override void InitPanel() // UHF _uhfRadioControl = DCSBIOSControlLocator.GetStringDCSBIOSOutput("VUHF_RADIO"); - + BIOSEventHandler.AttachDataListener(this); BIOSEventHandler.AttachStringListener(this); StartListeningForHidPanelChanges(); } - + public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) { UpdateCounter(e.Address, e.Data); @@ -145,7 +146,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelKnobsC101 knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelKnobsC101 knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelKnobsC101.UPPER_FREQ_SWITCH || knob == RadioPanelKnobsC101.LOWER_FREQ_SWITCH)) { @@ -169,12 +170,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsC101 knob) { case CurrentC101RadioMode.VHF: { - SendVHFToDCSBIOS(); + await SendVHFToDCSBIOSAsync(); break; } case CurrentC101RadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } } @@ -188,12 +189,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsC101 knob) case CurrentC101RadioMode.VHF: { - SendVHFToDCSBIOS(); + await SendVHFToDCSBIOSAsync(); break; } case CurrentC101RadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } } @@ -202,12 +203,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsC101 knob) } } - private void SendVHFToDCSBIOS() + private async Task SendVHFToDCSBIOSAsync() { try { var newStandbyFrequency = _vhfCockpitFrequency; - DCSBIOS.Send($"{VHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentC101RadioMode.VHF)}\n"); + await DCSBIOS.SendAsync($"{VHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentC101RadioMode.VHF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _vhfBigFrequencyStandby = uint.Parse(array[0]); _vhfSmallFrequencyStandby = uint.Parse(array[1]); @@ -219,12 +220,12 @@ private void SendVHFToDCSBIOS() } } - private void SendUHFToDCSBIOS() + private async Task SendUHFToDCSBIOSAsync() { try { var newStandbyFrequency = _uhfCockpitFrequency; - DCSBIOS.Send($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentC101RadioMode.UHF)}\n"); + await DCSBIOS.SendAsync($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentC101RadioMode.UHF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _uhfBigFrequencyStandby = uint.Parse(array[0]); _uhfSmallFrequencyStandby = uint.Parse(array[1]); @@ -598,102 +599,101 @@ private void AdjustFrequency(IEnumerable hashSet) ShowFrequenciesOnPanel(); } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobC101)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobC101)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelKnobsC101.UPPER_VHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelKnobsC101.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentC101RadioMode.VHF; - } - break; + _currentUpperRadioMode = CurrentC101RadioMode.VHF; } - case RadioPanelKnobsC101.UPPER_UHF: + break; + } + case RadioPanelKnobsC101.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentC101RadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentC101RadioMode.UHF; } - case RadioPanelKnobsC101.UPPER_NAV1: - case RadioPanelKnobsC101.UPPER_NAV2: - case RadioPanelKnobsC101.UPPER_ADF: - case RadioPanelKnobsC101.UPPER_DME: - case RadioPanelKnobsC101.UPPER_XPDR: + break; + } + case RadioPanelKnobsC101.UPPER_NAV1: + case RadioPanelKnobsC101.UPPER_NAV2: + case RadioPanelKnobsC101.UPPER_ADF: + case RadioPanelKnobsC101.UPPER_DME: + case RadioPanelKnobsC101.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentC101RadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentC101RadioMode.NO_USE; } - case RadioPanelKnobsC101.LOWER_VHF: + break; + } + case RadioPanelKnobsC101.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentC101RadioMode.VHF; - } - break; + _currentLowerRadioMode = CurrentC101RadioMode.VHF; } - case RadioPanelKnobsC101.LOWER_UHF: + break; + } + case RadioPanelKnobsC101.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentC101RadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentC101RadioMode.UHF; } - case RadioPanelKnobsC101.LOWER_NAV1: - case RadioPanelKnobsC101.LOWER_NAV2: - case RadioPanelKnobsC101.LOWER_ADF: - case RadioPanelKnobsC101.LOWER_DME: - case RadioPanelKnobsC101.LOWER_XPDR: + break; + } + case RadioPanelKnobsC101.LOWER_NAV1: + case RadioPanelKnobsC101.LOWER_NAV2: + case RadioPanelKnobsC101.LOWER_ADF: + case RadioPanelKnobsC101.LOWER_DME: + case RadioPanelKnobsC101.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentC101RadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentC101RadioMode.NO_USE; } - case RadioPanelKnobsC101.UPPER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsC101.UPPER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelKnobsC101.UPPER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsC101.UPPER_FREQ_SWITCH); } - case RadioPanelKnobsC101.LOWER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsC101.LOWER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelKnobsC101.LOWER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsC101.LOWER_FREQ_SWITCH); } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } + AdjustFrequency(hashSet); } } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Emulator.cs b/src/NonVisuals/Radios/RadioPanelPZ69Emulator.cs index 137d5a6fc..3dc0107fb 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Emulator.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Emulator.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.Key; using NonVisuals.BindingClasses.OSCommand; using NonVisuals.KeyEmulation; @@ -199,7 +200,7 @@ public List OSCommandHashSet public HashSet DisplayValueHashSet => _displayValues; - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { if (ForwardPanelEvent) { diff --git a/src/NonVisuals/Radios/RadioPanelPZ69F14B.cs b/src/NonVisuals/Radios/RadioPanelPZ69F14B.cs index c6e145e08..347af75a0 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69F14B.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69F14B.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -101,8 +102,8 @@ private enum CurrentF14RadioMode private volatile uint _uhfCockpitMode; // OFF = 0 private readonly ClickSpeedDetector _uhfModeClickSpeedDetector = new(8); private byte _skipUhfSmallFreqChange; - private long _uhfThreadNowSynching; - private Thread _uhfSyncThread; + private Task _uhfSyncTask; + private CancellationTokenSource _uhfSyncTaskTokenSource = new(); private long _uhfDial1WaitingForFeedback; private long _uhfDial2WaitingForFeedback; private long _uhfDial3WaitingForFeedback; @@ -169,8 +170,8 @@ This is a RIO radio. private volatile uint _vuhfCockpitMode; // OFF = 0 private readonly ClickSpeedDetector _vuhfModeClickSpeedDetector = new(8); private byte _skipVuhfSmallFreqChange; - private long _vuhfThreadNowSynching; - private Thread _vuhfSyncThread; + private Task _vuhfSyncTask; + private CancellationTokenSource _vuhfSyncTaskTokenSource = new(); private long _vuhfDial1WaitingForFeedback; private long _vuhfDial2WaitingForFeedback; private long _vuhfDial3WaitingForFeedback; @@ -198,8 +199,8 @@ This is a RIO radio. private const string PILOT_TACAN_TENS_DIAL_COMMAND = "PLT_TACAN_DIAL_TENS "; private const string PILOT_TACAN_ONES_DIAL_COMMAND = "PLT_TACAN_DIAL_ONES "; private const string PILOT_TACAN_XY_DIAL_COMMAND = "PLT_TACAN_CHANNEL "; // X = 0 | Y = 1 - private Thread _pilotTacanSyncThread; - private long _pilotTacanThreadNowSynching; + private Task _pilotTacanSyncTask; + private CancellationTokenSource _pilotTacanSyncTaskTokenSource = new(); private long _pilotTacanTensWaitingForFeedback; private long _pilotTacanOnesWaitingForFeedback; private long _pilotTacanXYWaitingForFeedback; @@ -226,8 +227,8 @@ This is a RIO radio. private const string RIO_TACAN_TENS_DIAL_COMMAND = "RIO_TACAN_DIAL_TENS "; private const string RIO_TACAN_ONES_DIAL_COMMAND = "RIO_TACAN_DIAL_ONES "; private const string RIO_TACAN_XY_DIAL_COMMAND = "RIO_TACAN_CHANNEL "; // X = 0 | Y = 1 - private Thread _rioTacanSyncThread; - private long _rioTacanThreadNowSynching; + private Task _rioTacanSyncTask; + private CancellationTokenSource _rioTacanSyncTaskTokenSource = new(); private long _rioTacanTensWaitingForFeedback; private long _rioTacanOnesWaitingForFeedback; private long _rioTacanXYWaitingForFeedback; @@ -256,8 +257,8 @@ This is a RIO radio. private const string RIO_LINK4_ONES_DIAL_COMMAND = "RIO_DATALINK_FREQ_100 "; private const string RIO_LINK4_POWER_COMMAND_INC = "RIO_DATALINK_PW INC\n"; private const string RIO_LINK4_POWER_COMMAND_DEC = "RIO_DATALINK_PW DEC\n"; - private Thread _rioLink4SyncThread; - private long _rioLink4ThreadNowSynching; + private Task _rioLink4SyncTask; + private CancellationTokenSource _rioLink4SyncTaskTokenSource = new(); private long _rioLinkHundredsWaitingForFeedback; private long _rioLinkTensWaitingForFeedback; private long _rioLinkOnesWaitingForFeedback; @@ -269,7 +270,7 @@ This is a RIO radio. private long _doUpdatePanelLCD; public RadioPanelPZ69F14B(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -280,11 +281,12 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownUHFThread = true; - _shutdownPilotTACANThread = true; - _shutdownRIOLink4Thread = true; - _shutdownRIOTACANThread = true; - _shutdownVUHFThread = true; + _uhfSyncTaskTokenSource.Cancel(); + _pilotTacanSyncTaskTokenSource.Cancel(); + _rioTacanSyncTaskTokenSource.Cancel(); + _rioLink4SyncTaskTokenSource.Cancel(); + _vuhfSyncTaskTokenSource.Cancel(); + BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -333,9 +335,9 @@ public override void InitPanel() _rioLink4DcsbiosOutputTensDial = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RIO_DATALINK_FREQ_1"); _rioLink4DcsbiosOutputOnesDial = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RIO_DATALINK_FREQ_100"); _rioLink4DcsbiosOutputPowerSwitch = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RIO_DATALINK_PW"); - + BIOSEventHandler.AttachStringListener(this); - BIOSEventHandler.AttachDataListener(this); + BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } @@ -720,7 +722,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF14B knob) private void SendUHFToDCSBIOS() { - if (UHFNowSyncing()) + if (_uhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -752,170 +754,187 @@ private void SendUHFToDCSBIOS() desiredDial4Value = int.Parse(frequencyAsString.Substring(5, 2)); } - _shutdownUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownUHFThread = false; - _uhfSyncThread = new Thread(() => UHFSynchThreadMethod(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value)); - _uhfSyncThread.Start(); + _uhfSyncTaskTokenSource = new(); + _uhfSyncTask = Task.Run(() => UHFAsync(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value, _uhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownUHFThread; - private void UHFSynchThreadMethod(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4) + + private async Task UHFAsync(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + do { - Interlocked.Exchange(ref _uhfThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + lock (_lockUhfDialBigFreqObject) { - lock (_lockUhfDialBigFreqObject) + if (_uhfCockpitDial1Frequency != desiredValueDial1) { - if (_uhfCockpitDial1Frequency != desiredValueDial1) - { - dial1OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_uhfCockpitDial1Frequency < desiredValueDial1 ? UHF_1ST_DIAL_INCREASE : UHF_1ST_DIAL_DECREASE); - DCSBIOS.Send(UHF_1ST_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = _uhfCockpitDial1Frequency < desiredValueDial1 ? UHF_1ST_DIAL_INCREASE : UHF_1ST_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(UHF_1ST_DIAL_NEUTRAL); + dial1SendCount++; + Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDialBigFreqObject) { - lock (_lockUhfDialBigFreqObject) + if (_uhfCockpitDial2Frequency != desiredValueDial2) { - if (_uhfCockpitDial2Frequency != desiredValueDial2) - { - dial2OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(UHF_2ND_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial2, _uhfCockpitDial2Frequency)); - DCSBIOS.Send(UHF_2ND_DIAL_NEUTRAL); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = UHF_2ND_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial2, _uhfCockpitDial2Frequency); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(UHF_2ND_DIAL_NEUTRAL); + dial2SendCount++; + Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDial3FreqObject) { - lock (_lockUhfDial3FreqObject) + if (_uhfCockpitDial3Frequency != desiredValueDial3) { - if (_uhfCockpitDial3Frequency != desiredValueDial3) - { - dial3OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(UHF_3RD_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial3, _uhfCockpitDial3Frequency)); - DCSBIOS.Send(UHF_3RD_DIAL_NEUTRAL); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = UHF_3RD_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial3, _uhfCockpitDial3Frequency); } - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(UHF_3RD_DIAL_NEUTRAL); + dial3SendCount++; + Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + lock (_lockUhfDial4FreqObject) { - lock (_lockUhfDial4FreqObject) + if (_uhfCockpitDial4Frequency != desiredValueDial4) { - if (_uhfCockpitDial4Frequency != desiredValueDial4) - { - dial4OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_uhfCockpitDial4Frequency < desiredValueDial4 ? UHF_4TH_DIAL_INCREASE : UHF_4TH_DIAL_DECREASE); - DCSBIOS.Send(UHF_4TH_DIAL_NEUTRAL); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = _uhfCockpitDial4Frequency < desiredValueDial4 ? UHF_4TH_DIAL_INCREASE : UHF_4TH_DIAL_DECREASE; } } - else - { - dial4OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(UHF_4TH_DIAL_NEUTRAL); + dial4SendCount++; + Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS - + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownUHFThread); - SwapCockpitStandbyFrequencyUhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; + } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyUhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendVUHFToDCSBIOS() { - if (VUHFNowSyncing()) + if (_vuhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -947,168 +966,185 @@ private void SendVUHFToDCSBIOS() desiredDial4Value = int.Parse(frequencyAsString.Substring(5, 2)); } - _shutdownVUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVUHFThread = false; - _vuhfSyncThread = new Thread(() => VUHFSynchThreadMethod(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value)); - _vuhfSyncThread.Start(); + _vuhfSyncTaskTokenSource = new(); + _vuhfSyncTask = Task.Run(() => SyncVUHFAsync(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value, _vuhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVUHFThread; - private void VUHFSynchThreadMethod(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4) + + private async Task SyncVUHFAsync(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + do { - Interlocked.Exchange(ref _vuhfThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _vuhfDial1WaitingForFeedback); // Lets do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vuhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vuhfDial2WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vuhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vuhfDial3WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vuhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vuhfDial4WaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vuhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _vuhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhfDial1WaitingForFeedback) == 0) + lock (_lockVuhfBigFreqObject) { - lock (_lockVuhfBigFreqObject) + if (_vuhfCockpitDial1Frequency != desiredValueDial1) { - if (_vuhfCockpitDial1Frequency != desiredValueDial1) - { - dial1OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhfCockpitDial1Frequency < desiredValueDial1 ? VUHF_1ST_DIAL_INCREASE : VUHF_1ST_DIAL_DECREASE); - DCSBIOS.Send(VUHF_1ST_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhfDial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = _vuhfCockpitDial1Frequency < desiredValueDial1 ? VUHF_1ST_DIAL_INCREASE : VUHF_1ST_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF_1ST_DIAL_NEUTRAL); + dial1SendCount++; + Interlocked.Exchange(ref _vuhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhfDial2WaitingForFeedback) == 0) + lock (_lockVuhfBigFreqObject) { - lock (_lockVuhfBigFreqObject) + if (_vuhfCockpitDial2Frequency != desiredValueDial2) { - if (_vuhfCockpitDial2Frequency != desiredValueDial2) - { - dial2OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(VUHF_2ND_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial2, _vuhfCockpitDial2Frequency)); - DCSBIOS.Send(VUHF_2ND_DIAL_NEUTRAL); - dial2SendCount++; - Interlocked.Exchange(ref _vuhfDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = VUHF_2ND_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial2, _vuhfCockpitDial2Frequency); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF_2ND_DIAL_NEUTRAL); + dial2SendCount++; + Interlocked.Exchange(ref _vuhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vuhfDial3WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vuhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVuhfDial3FreqObject) { - lock (_lockVuhfDial3FreqObject) + if (_vuhfCockpitDial3Frequency != desiredValueDial3) { - if (_vuhfCockpitDial3Frequency != desiredValueDial3) - { - dial3OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(VUHF_3RD_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial3, _vuhfCockpitDial3Frequency)); - DCSBIOS.Send(VUHF_3RD_DIAL_NEUTRAL); - dial3SendCount++; - Interlocked.Exchange(ref _vuhfDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = VUHF_3RD_DIAL_COMMAND + GetCommandDirection10Dial(desiredValueDial3, _vuhfCockpitDial3Frequency); } - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF_3RD_DIAL_NEUTRAL); + dial3SendCount++; + Interlocked.Exchange(ref _vuhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhfDial4WaitingForFeedback) == 0) + lock (_lockVuhfDial4FreqObject) { - lock (_lockVuhfDial4FreqObject) + if (_vuhfCockpitDial4Frequency != desiredValueDial4) { - if (_vuhfCockpitDial4Frequency != desiredValueDial4) - { - dial4OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhfCockpitDial4Frequency < desiredValueDial4 ? VUHF_4TH_DIAL_INCREASE : VUHF_4TH_DIAL_DECREASE); - DCSBIOS.Send(VUHF_4TH_DIAL_NEUTRAL); - dial4SendCount++; - Interlocked.Exchange(ref _vuhfDial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = _vuhfCockpitDial4Frequency < desiredValueDial4 ? VUHF_4TH_DIAL_INCREASE : VUHF_4TH_DIAL_DECREASE; } } - else - { - dial4OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF_4TH_DIAL_NEUTRAL); + dial4SendCount++; + Interlocked.Exchange(ref _vuhfDial4WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial4Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVUHFThread); - SwapCockpitStandbyFrequencyVuhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyVuhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vuhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendLink4ToDCSBIOS() { - if (Link4NowSyncing()) + if (_rioLink4SyncTask?.Status == TaskStatus.Running) { return; } @@ -1116,140 +1152,147 @@ private void SendLink4ToDCSBIOS() SaveLink4Frequency(); var dial2 = int.Parse(_rioLink4TensAndOnesFrequencyStandby.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0').Substring(0, 1)); var dial3 = int.Parse(_rioLink4TensAndOnesFrequencyStandby.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0').Substring(1, 1)); - _shutdownRIOLink4Thread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownRIOLink4Thread = false; - _rioLink4SyncThread = new Thread(() => RioDatalink4SynchThreadMethod(_rioLink4HundredsFrequencyStandby, dial2, dial3)); - _rioLink4SyncThread.Start(); + + _rioLink4SyncTaskTokenSource = new(); + _rioLink4SyncTask = Task.Run(() => SyncRioDatalink4Async(_rioLink4HundredsFrequencyStandby, dial2, dial3, _rioLink4SyncTaskTokenSource.Token)); } - private volatile bool _shutdownRIOLink4Thread; - private void RioDatalink4SynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + + private async Task SyncRioDatalink4Async(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + do { - Interlocked.Exchange(ref _rioLink4ThreadNowSynching, 1); - - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _rioLinkHundredsWaitingForFeedback); // Lets do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _rioLinkHundredsWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _rioLinkTensWaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _rioLinkTensWaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _rioLinkOnesWaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _rioLinkOnesWaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _rioLinkHundredsWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _rioLinkHundredsWaitingForFeedback) == 0) + lock (_lockRioLink4HundredsDial) { - lock (_lockRioLink4HundredsDial) + if (_rioLink4HundredsCockpitFrequency != desiredPositionDial1) { - if (_rioLink4HundredsCockpitFrequency != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = RIO_LINK4_HUNDREDS_DIAL_COMMAND + (_rioLink4HundredsCockpitFrequency < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _rioLinkHundredsWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = RIO_LINK4_HUNDREDS_DIAL_COMMAND + (_rioLink4HundredsCockpitFrequency < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _rioLinkHundredsWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _rioLinkTensWaitingForFeedback) == 0) + if (Interlocked.Read(ref _rioLinkTensWaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockRioLink4TensDial) { - lock (_lockRioLink4TensDial) + if (_rioLink4TensCockpitFrequency != desiredPositionDial2) { - if (_rioLink4TensCockpitFrequency != desiredPositionDial2) - { - dial1OkTime = DateTime.Now.Ticks; - var str = RIO_LINK4_TENS_DIAL_COMMAND + (_rioLink4TensCockpitFrequency < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _rioLinkTensWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = RIO_LINK4_TENS_DIAL_COMMAND + (_rioLink4TensCockpitFrequency < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _rioLinkTensWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _rioLinkOnesWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _rioLinkOnesWaitingForFeedback) == 0) + lock (_lockRioLink4OnesDial) { - lock (_lockRioLink4OnesDial) + if (_rioLink4OnesCockpitFrequency != desiredPositionDial3) { - if (_rioLink4OnesCockpitFrequency != desiredPositionDial3) - { - dial1OkTime = DateTime.Now.Ticks; - var str = RIO_LINK4_ONES_DIAL_COMMAND + (_rioLink4OnesCockpitFrequency < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _rioLinkOnesWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = RIO_LINK4_ONES_DIAL_COMMAND + (_rioLink4OnesCockpitFrequency < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else - { - dial3OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _rioLinkOnesWaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial1Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownRIOLink4Thread); - SwapCockpitStandbyFrequencyDataLink4(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapCockpitStandbyFrequencyDataLink4(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _rioLink4ThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendPilotTacanToDCSBIOS() { - if (PilotTacanNowSyncing()) + if (_pilotTacanSyncTask?.Status == TaskStatus.Running) { return; } @@ -1270,136 +1313,141 @@ private void SendPilotTacanToDCSBIOS() // #1 = 12 (position = value) // #2 = 0 (position = value) // #3 = 1 (position = value) - _shutdownPilotTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownPilotTACANThread = false; - _pilotTacanSyncThread = new Thread(() => PilotTacanSynchThreadMethod(_pilotTacanTensFrequencyStandby, _pilotTacanOnesFrequencyStandby, _pilotTacanXYStandby)); - _pilotTacanSyncThread.Start(); + _pilotTacanSyncTaskTokenSource = new(); + _pilotTacanSyncTask = Task.Run(() => SyncPilotTacanAsync(_pilotTacanTensFrequencyStandby, _pilotTacanOnesFrequencyStandby, _pilotTacanXYStandby, _pilotTacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownPilotTACANThread; - private void PilotTacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + + private async Task SyncPilotTacanAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + do { - Interlocked.Exchange(ref _pilotTacanThreadNowSynching, 1); - - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _pilotTacanTensWaitingForFeedback); // Lets do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _pilotTacanTensWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _pilotTacanOnesWaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _pilotTacanOnesWaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _pilotTacanXYWaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _pilotTacanXYWaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _pilotTacanTensWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _pilotTacanTensWaitingForFeedback) == 0) + lock (_lockPilotTacanTensDialObject) { - lock (_lockPilotTacanTensDialObject) + if (_pilotTacanCockpitTensDialPos != desiredPositionDial1) { - if (_pilotTacanCockpitTensDialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = PILOT_TACAN_TENS_DIAL_COMMAND + (_pilotTacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _pilotTacanTensWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = PILOT_TACAN_TENS_DIAL_COMMAND + (_pilotTacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _pilotTacanTensWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _pilotTacanOnesWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _pilotTacanOnesWaitingForFeedback) == 0) + lock (_lockPilotTacanOnesObject) { - // Common.DebugP("b"); - lock (_lockPilotTacanOnesObject) + if (_pilotTacanCockpitOnesDialPos != desiredPositionDial2) { - if (_pilotTacanCockpitOnesDialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - var str = PILOT_TACAN_ONES_DIAL_COMMAND + (_pilotTacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _pilotTacanOnesWaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = PILOT_TACAN_ONES_DIAL_COMMAND + (_pilotTacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _pilotTacanOnesWaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _pilotTacanXYWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _pilotTacanXYWaitingForFeedback) == 0) + lock (_lockPilotTacanXYDialObject) { - lock (_lockPilotTacanXYDialObject) + if (_pilotTacanCockpitXYDialPos != desiredPositionDial3) { - if (_pilotTacanCockpitXYDialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; + dial3OkTime = DateTime.Now.Ticks; - var str = PILOT_TACAN_XY_DIAL_COMMAND + (_pilotTacanCockpitXYDialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _pilotTacanXYWaitingForFeedback, 1); - } + command = PILOT_TACAN_XY_DIAL_COMMAND + (_pilotTacanCockpitXYDialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } - Reset(ref dial3Timeout); - } - else - { - dial3OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _pilotTacanXYWaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial3Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownPilotTACANThread); - SwapPilotCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapPilotCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _pilotTacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1407,143 +1455,147 @@ private void PilotTacanSynchThreadMethod(int desiredPositionDial1, int desiredPo private void SendRioTacanToDCSBIOS() { - if (RioTacanNowSyncing()) + if (_rioTacanSyncTask?.Status == TaskStatus.Running) { return; } SaveRioCockpitFrequencyTacan(); - _shutdownRIOTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownRIOTACANThread = false; - _rioTacanSyncThread = new Thread(() => RioTacanSynchThreadMethod(_rioTacanTensFrequencyStandby, _rioTacanOnesFrequencyStandby, _rioTacanXYStandby)); - _rioTacanSyncThread.Start(); + _rioTacanSyncTaskTokenSource = new(); + _rioTacanSyncTask = Task.Run(() => SyncRioTacanAsync(_rioTacanTensFrequencyStandby, _rioTacanOnesFrequencyStandby, _rioTacanXYStandby, _rioTacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownRIOTACANThread; - private void RioTacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + + private async Task SyncRioTacanAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + do { - Interlocked.Exchange(ref _rioTacanThreadNowSynching, 1); + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _rioTacanTensWaitingForFeedback); // Lets do an ugly reset + } - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _rioTacanOnesWaitingForFeedback); // Lets do an ugly reset + } - do + if (IsTimedOut(ref dial3Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _rioTacanTensWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _rioTacanXYWaitingForFeedback); // Lets do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _rioTacanOnesWaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _rioTacanTensWaitingForFeedback) == 0) + { + var command = string.Empty; - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _rioTacanXYWaitingForFeedback); // Lets do an ugly reset - } - if (Interlocked.Read(ref _rioTacanTensWaitingForFeedback) == 0) + lock (_lockRioTacanTensDialObject) { - - lock (_lockRioTacanTensDialObject) + if (_rioTacanCockpitTensDialPos != desiredPositionDial1) { - if (_rioTacanCockpitTensDialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = RIO_TACAN_TENS_DIAL_COMMAND + (_rioTacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _rioTacanTensWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = RIO_TACAN_TENS_DIAL_COMMAND + (_rioTacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _rioTacanTensWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _rioTacanOnesWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _rioTacanOnesWaitingForFeedback) == 0) + lock (_lockRioTacanOnesObject) { - // Common.DebugP("b"); - lock (_lockRioTacanOnesObject) + if (_rioTacanCockpitOnesDialPos != desiredPositionDial2) { - if (_rioTacanCockpitOnesDialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - var str = RIO_TACAN_ONES_DIAL_COMMAND + (_rioTacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _rioTacanOnesWaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = RIO_TACAN_ONES_DIAL_COMMAND + (_rioTacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _rioTacanOnesWaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _rioTacanXYWaitingForFeedback) == 0) + if (Interlocked.Read(ref _rioTacanXYWaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockRioTacanXYDialObject) { - lock (_lockRioTacanXYDialObject) + if (_rioTacanCockpitXYDialPos != desiredPositionDial3) { - if (_rioTacanCockpitXYDialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; - - var str = RIO_TACAN_XY_DIAL_COMMAND + (_rioTacanCockpitXYDialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _rioTacanXYWaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = RIO_TACAN_XY_DIAL_COMMAND + (_rioTacanCockpitXYDialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } - Reset(ref dial3Timeout); - } - else - { - dial3OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _rioTacanXYWaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial3Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownRIOTACANThread); - SwapRioCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapRioCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _rioTacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } @@ -1910,7 +1962,7 @@ private string GetVUHFCockpitFrequencyAsString() return frequencyAsString; } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -1935,7 +1987,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_MODE_INCREASE); } } else @@ -1944,7 +1996,7 @@ private void AdjustFrequency(IEnumerable hashSet) { if (_uhfCockpitPresetChannel < 20) { - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } } else if (_uhfBigFrequencyStandby.Equals(399)) @@ -1966,7 +2018,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_MODE_INCREASE); + await DCSBIOS.SendAsync(VUHF_MODE_INCREASE); } } else @@ -1975,7 +2027,7 @@ private void AdjustFrequency(IEnumerable hashSet) { if (_vuhfCockpitPresetChannel < 30) { - DCSBIOS.Send(VUHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(VUHF_PRESET_INCREASE); } } else @@ -2024,7 +2076,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(RIO_LINK4_POWER_COMMAND_INC); + await DCSBIOS.SendAsync(RIO_LINK4_POWER_COMMAND_INC); } else { @@ -2053,14 +2105,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_MODE_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225)) { @@ -2081,14 +2133,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_MODE_DECREASE); + await DCSBIOS.SendAsync(VUHF_MODE_DECREASE); } } else { if (VuhfPresetSelected() && _vuhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(VUHF_PRESET_DECREASE); } else { @@ -2136,7 +2188,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(RIO_LINK4_POWER_COMMAND_DEC); + await DCSBIOS.SendAsync(RIO_LINK4_POWER_COMMAND_DEC); } else { @@ -2165,7 +2217,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -2182,7 +2234,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VUHF_FREQ_MODE_INCREASE); } } else @@ -2247,7 +2299,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -2264,7 +2316,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VUHF_FREQ_MODE_DECREASE); } } else @@ -2329,7 +2381,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_MODE_INCREASE); } } else @@ -2338,7 +2390,7 @@ private void AdjustFrequency(IEnumerable hashSet) { if (_uhfCockpitPresetChannel < 20) { - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } } else if (!_lowerButtonPressed && _uhfBigFrequencyStandby.Equals(399)) @@ -2360,7 +2412,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vuhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_MODE_INCREASE); + await DCSBIOS.SendAsync(VUHF_MODE_INCREASE); } } else @@ -2369,7 +2421,7 @@ private void AdjustFrequency(IEnumerable hashSet) { if (_vuhfCockpitPresetChannel < 30) { - DCSBIOS.Send(VUHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(VUHF_PRESET_INCREASE); } } else @@ -2418,7 +2470,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(RIO_LINK4_POWER_COMMAND_INC); + await DCSBIOS.SendAsync(RIO_LINK4_POWER_COMMAND_INC); } else { @@ -2447,14 +2499,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_MODE_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(225)) { @@ -2476,14 +2528,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_vuhfModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_MODE_DECREASE); + await DCSBIOS.SendAsync(VUHF_MODE_DECREASE); } } else { if (VuhfPresetSelected() && _vuhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(VUHF_PRESET_DECREASE); } else { @@ -2531,7 +2583,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(RIO_LINK4_POWER_COMMAND_DEC); + await DCSBIOS.SendAsync(RIO_LINK4_POWER_COMMAND_DEC); } else { @@ -2560,7 +2612,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -2577,7 +2629,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(VUHF_FREQ_MODE_INCREASE); } } else @@ -2642,7 +2694,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -2659,7 +2711,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_vuhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(VUHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(VUHF_FREQ_MODE_DECREASE); } } else @@ -2909,209 +2961,178 @@ private void CheckFrequenciesForValidity() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobF14B)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobF14B)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsF14B.UPPER_UHF: - { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.UHF; - } - break; - } - - case RadioPanelPZ69KnobsF14B.UPPER_VUHF: - { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.VUHF; - } - break; - } - - case RadioPanelPZ69KnobsF14B.UPPER_PLT_TACAN: - { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.PLT_TACAN; - } - break; - } - - case RadioPanelPZ69KnobsF14B.UPPER_RIO_TACAN: - { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.RIO_TACAN; - } - break; - } - - case RadioPanelPZ69KnobsF14B.UPPER_LINK4: - { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.LINK4; - } - break; - } - - case RadioPanelPZ69KnobsF14B.UPPER_ADF: - case RadioPanelPZ69KnobsF14B.UPPER_XPDR: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsF14B.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF14RadioMode.NO_USE; - } - break; - } - - case RadioPanelPZ69KnobsF14B.LOWER_UHF: - { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentF14RadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_VUHF: + case RadioPanelPZ69KnobsF14B.UPPER_VUHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.VUHF; - } - break; - } - - case RadioPanelPZ69KnobsF14B.LOWER_PLT_TACAN: - { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.PLT_TACAN; - } - break; + _currentUpperRadioMode = CurrentF14RadioMode.VUHF; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_RIO_TACAN: + case RadioPanelPZ69KnobsF14B.UPPER_PLT_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.RIO_TACAN; - } - break; + _currentUpperRadioMode = CurrentF14RadioMode.PLT_TACAN; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_LINK4: + case RadioPanelPZ69KnobsF14B.UPPER_RIO_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.LINK4; - } - break; + _currentUpperRadioMode = CurrentF14RadioMode.RIO_TACAN; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_ADF: - case RadioPanelPZ69KnobsF14B.LOWER_XPDR: + case RadioPanelPZ69KnobsF14B.UPPER_LINK4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF14RadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentF14RadioMode.LINK4; } + break; + } - case RadioPanelPZ69KnobsF14B.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.UPPER_ADF: + case RadioPanelPZ69KnobsF14B.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - break; + _currentUpperRadioMode = CurrentF14RadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsF14B.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsF14B.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.LOWER_VUHF: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.VUHF; } + break; + } - case RadioPanelPZ69KnobsF14B.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.LOWER_PLT_TACAN: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.PLT_TACAN; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.LOWER_RIO_TACAN: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.RIO_TACAN; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.LOWER_LINK4: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.LINK4; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.LOWER_ADF: + case RadioPanelPZ69KnobsF14B.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - break; + _currentLowerRadioMode = CurrentF14RadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsF14B.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF14B.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF14B.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsF14B.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF14B.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF14B.UPPER_FREQ_SWITCH); - } - - _upperButtonPressedAndDialRotated = false; + // Do not synch if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF14B.UPPER_FREQ_SWITCH); } - break; + + _upperButtonPressedAndDialRotated = false; } + break; + } - case RadioPanelPZ69KnobsF14B.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF14B.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF14B.LOWER_FREQ_SWITCH); - } - - _lowerButtonPressedAndDialRotated = false; + // Do not synch if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF14B.LOWER_FREQ_SWITCH); } - break; + + _lowerButtonPressedAndDialRotated = false; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F14B, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F14B, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + await AdjustFrequencyAsync(hashSet); } public override void ClearSettings(bool setIsDirty = false) { } @@ -3120,7 +3141,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { @@ -3285,21 +3306,6 @@ private bool VuhfPresetSelected() return _vuhfCockpitFreqMode == 3; } - private bool PilotTacanNowSyncing() - { - return Interlocked.Read(ref _pilotTacanThreadNowSynching) > 0; - } - - private bool Link4NowSyncing() - { - return Interlocked.Read(ref _rioLink4ThreadNowSynching) > 0; - } - - private bool RioTacanNowSyncing() - { - return Interlocked.Read(ref _rioTacanThreadNowSynching) > 0; - } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } @@ -3324,16 +3330,6 @@ public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnO { } - private bool UHFNowSyncing() - { - return Interlocked.Read(ref _uhfThreadNowSynching) > 0; - } - - private bool VUHFNowSyncing() - { - return Interlocked.Read(ref _vuhfThreadNowSynching) > 0; - } - private static string GetCommandDirection10Dial(int desiredDialPosition, uint actualDialPosition) { var counterUp = 0; diff --git a/src/NonVisuals/Radios/RadioPanelPZ69F15E.cs b/src/NonVisuals/Radios/RadioPanelPZ69F15E.cs index bf530b4b0..a3ce867c0 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69F15E.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69F15E.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using System.Threading.Tasks; using NonVisuals.BindingClasses.BIP; using NonVisuals.Radios.RadioControls; using NonVisuals.Radios.RadioSettings; @@ -153,7 +154,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelKnobsF15E knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelKnobsF15E knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelKnobsF15E.UPPER_FREQ_SWITCH || knob == RadioPanelKnobsF15E.LOWER_FREQ_SWITCH)) { @@ -177,13 +178,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsF15E knob) { case CurrentF15ERadioMode.ARC210: { - DCSBIOS.Send(_arc210Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc210Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } case CurrentF15ERadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } } @@ -197,13 +198,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsF15E knob) case CurrentF15ERadioMode.ARC210: { - DCSBIOS.Send(_arc210Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc210Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } case CurrentF15ERadioMode.UHF: { - SendUHFToDCSBIOS(); + await SendUHFToDCSBIOSAsync(); break; } } @@ -212,12 +213,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsF15E knob) } } - private void SendUHFToDCSBIOS() + private async Task SendUHFToDCSBIOSAsync() { try { var newStandbyFrequency = _uhfCockpitFrequency; - DCSBIOS.Send($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentF15ERadioMode.UHF)}\n"); + await DCSBIOS.SendAsync($"{UHF_RADIO_COMMAND} {GetStandbyFrequencyString(CurrentF15ERadioMode.UHF)}\n"); var array = newStandbyFrequency.Split('.', StringSplitOptions.RemoveEmptyEntries); _uhfBigFrequencyStandby = uint.Parse(array[0]); _uhfSmallFrequencyStandby = uint.Parse(array[1]); @@ -582,154 +583,152 @@ private void AdjustFrequency(IEnumerable hashSet) ShowFrequenciesOnPanel(); } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobF15E)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobF15E)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelKnobsF15E.UPPER_ARC210: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelKnobsF15E.UPPER_ARC210: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF15ERadioMode.ARC210; - } - break; + _currentUpperRadioMode = CurrentF15ERadioMode.ARC210; } - case RadioPanelKnobsF15E.UPPER_UHF: + break; + } + case RadioPanelKnobsF15E.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF15ERadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentF15ERadioMode.UHF; } - case RadioPanelKnobsF15E.UPPER_NAV1: - case RadioPanelKnobsF15E.UPPER_NAV2: - case RadioPanelKnobsF15E.UPPER_ADF: - case RadioPanelKnobsF15E.UPPER_DME: - case RadioPanelKnobsF15E.UPPER_XPDR: + break; + } + case RadioPanelKnobsF15E.UPPER_NAV1: + case RadioPanelKnobsF15E.UPPER_NAV2: + case RadioPanelKnobsF15E.UPPER_ADF: + case RadioPanelKnobsF15E.UPPER_DME: + case RadioPanelKnobsF15E.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF15ERadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentF15ERadioMode.NO_USE; } - case RadioPanelKnobsF15E.LOWER_ARC210: + break; + } + case RadioPanelKnobsF15E.LOWER_ARC210: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF15ERadioMode.ARC210; - } - break; + _currentLowerRadioMode = CurrentF15ERadioMode.ARC210; } - case RadioPanelKnobsF15E.LOWER_UHF: + break; + } + case RadioPanelKnobsF15E.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF15ERadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentF15ERadioMode.UHF; } - case RadioPanelKnobsF15E.LOWER_NAV1: - case RadioPanelKnobsF15E.LOWER_NAV2: - case RadioPanelKnobsF15E.LOWER_ADF: - case RadioPanelKnobsF15E.LOWER_DME: - case RadioPanelKnobsF15E.LOWER_XPDR: + break; + } + case RadioPanelKnobsF15E.LOWER_NAV1: + case RadioPanelKnobsF15E.LOWER_NAV2: + case RadioPanelKnobsF15E.LOWER_ADF: + case RadioPanelKnobsF15E.LOWER_DME: + case RadioPanelKnobsF15E.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF15ERadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentF15ERadioMode.NO_USE; } - case RadioPanelKnobsF15E.UPPER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsF15E.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Sync when user releases button - SendFrequencyToDCSBIOS(RadioPanelKnobsF15E.UPPER_FREQ_SWITCH); - } - else + // Do not sync if user has pressed the button to configure the radio + // Sync when user releases button + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsF15E.UPPER_FREQ_SWITCH); + } + else + { + /* We must say that the user now has stopped rotating */ + switch (_currentUpperRadioMode) { - /* We must say that the user now has stopped rotating */ - switch (_currentUpperRadioMode) - { - case CurrentF15ERadioMode.ARC210: - { - _arc210Radio.SwitchFrequencyBand(); - Interlocked.Increment(ref _doUpdatePanelLCD); - break; - } - case CurrentF15ERadioMode.UHF: - { - break; - } - } + case CurrentF15ERadioMode.ARC210: + { + _arc210Radio.SwitchFrequencyBand(); + Interlocked.Increment(ref _doUpdatePanelLCD); + break; + } + case CurrentF15ERadioMode.UHF: + { + break; + } } - - _upperButtonPressedAndDialRotated = false; } - break; + + _upperButtonPressedAndDialRotated = false; } - case RadioPanelKnobsF15E.LOWER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsF15E.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Sync when user releases button - SendFrequencyToDCSBIOS(RadioPanelKnobsF15E.LOWER_FREQ_SWITCH); - } - else + // Do not sync if user has pressed the button to configure the radio + // Sync when user releases button + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsF15E.LOWER_FREQ_SWITCH); + } + else + { + /* We must say that the user now has stopped rotating */ + switch (_currentLowerRadioMode) { - /* We must say that the user now has stopped rotating */ - switch (_currentLowerRadioMode) - { - case CurrentF15ERadioMode.ARC210: + case CurrentF15ERadioMode.ARC210: { _arc210Radio.SwitchFrequencyBand(); Interlocked.Increment(ref _doUpdatePanelLCD); break; } - case CurrentF15ERadioMode.UHF: + case CurrentF15ERadioMode.UHF: { break; } - } } - - _lowerButtonPressedAndDialRotated = false; } - break; + + _lowerButtonPressedAndDialRotated = false; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + AdjustFrequency(hashSet); } public override void ClearSettings(bool setIsDirty = false) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69F16C.cs b/src/NonVisuals/Radios/RadioPanelPZ69F16C.cs index 26cd0f04d..be04dc737 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69F16C.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69F16C.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -55,7 +56,7 @@ private enum CurrentF16CRadioMode private DCSBIOSOutput _arc222RadioDCSBIOSControl; public RadioPanelPZ69F16C(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -151,108 +152,107 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) Common.ShowErrorMessageBox(ex, "DCSBIOSStringReceived()"); } } - - protected override void PZ69KnobChanged(IEnumerable hashSet) + + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobF16C)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobF16C)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsF16C.UPPER_UHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsF16C.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF16CRadioMode.ARC164_UHF); - } - break; + SetUpperRadioMode(CurrentF16CRadioMode.ARC164_UHF); } + break; + } - case RadioPanelPZ69KnobsF16C.UPPER_VHF: + case RadioPanelPZ69KnobsF16C.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF16CRadioMode.ARC222_VHF); - } - break; + SetUpperRadioMode(CurrentF16CRadioMode.ARC222_VHF); } + break; + } - case RadioPanelPZ69KnobsF16C.UPPER_NO_USE0: - case RadioPanelPZ69KnobsF16C.UPPER_NO_USE1: - case RadioPanelPZ69KnobsF16C.UPPER_NO_USE2: - case RadioPanelPZ69KnobsF16C.UPPER_NO_USE3: - case RadioPanelPZ69KnobsF16C.UPPER_NO_USE4: + case RadioPanelPZ69KnobsF16C.UPPER_NO_USE0: + case RadioPanelPZ69KnobsF16C.UPPER_NO_USE1: + case RadioPanelPZ69KnobsF16C.UPPER_NO_USE2: + case RadioPanelPZ69KnobsF16C.UPPER_NO_USE3: + case RadioPanelPZ69KnobsF16C.UPPER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF16CRadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentF16CRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsF16C.LOWER_UHF: + case RadioPanelPZ69KnobsF16C.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF16CRadioMode.ARC164_UHF); - } - break; + SetLowerRadioMode(CurrentF16CRadioMode.ARC164_UHF); } + break; + } - case RadioPanelPZ69KnobsF16C.LOWER_VHF: + case RadioPanelPZ69KnobsF16C.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF16CRadioMode.ARC222_VHF); - } - break; + SetLowerRadioMode(CurrentF16CRadioMode.ARC222_VHF); } + break; + } - case RadioPanelPZ69KnobsF16C.LOWER_NO_USE0: - case RadioPanelPZ69KnobsF16C.LOWER_NO_USE1: - case RadioPanelPZ69KnobsF16C.LOWER_NO_USE2: - case RadioPanelPZ69KnobsF16C.LOWER_NO_USE3: - case RadioPanelPZ69KnobsF16C.LOWER_NO_USE4: + case RadioPanelPZ69KnobsF16C.LOWER_NO_USE0: + case RadioPanelPZ69KnobsF16C.LOWER_NO_USE1: + case RadioPanelPZ69KnobsF16C.LOWER_NO_USE2: + case RadioPanelPZ69KnobsF16C.LOWER_NO_USE3: + case RadioPanelPZ69KnobsF16C.LOWER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF16CRadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentF16CRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH: + { + if (!radioPanelKnob.IsOn) { - if (!radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH); } + break; + } - case RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH: + { + if (!radioPanelKnob.IsOn) { - if (!radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH); } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F16C, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F16C, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + AdjustFrequency(hashSet); } catch (Exception ex) { @@ -260,9 +260,9 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF16C knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF16C knob) { - if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH || knob == RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH)) + if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsF16C.UPPER_FREQ_SWITCH || knob == RadioPanelPZ69KnobsF16C.LOWER_FREQ_SWITCH)) { // Don't do anything on the very first button press as the panel sends ALL // switches when it is manipulated the first time @@ -284,13 +284,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF16C knob) { case CurrentF16CRadioMode.ARC164_UHF: { - DCSBIOS.Send(_arc164Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc164Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } case CurrentF16CRadioMode.ARC222_VHF: { - DCSBIOS.Send(_arc222Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc222Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } @@ -304,13 +304,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF16C knob) { case CurrentF16CRadioMode.ARC164_UHF: { - DCSBIOS.Send(_arc164Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc164Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } case CurrentF16CRadioMode.ARC222_VHF: { - DCSBIOS.Send(_arc222Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_arc222Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); break; } @@ -598,7 +598,7 @@ private void ShowFrequenciesOnPanel() } Interlocked.Decrement(ref _doUpdatePanelLCD); } - + public override void ClearSettings(bool setIsDirty = false) { } public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) @@ -634,7 +634,7 @@ private void SetLowerRadioMode(CurrentF16CRadioMode currentF16CRadioMode) Logger.Error(ex); } } - + public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69F5E.cs b/src/NonVisuals/Radios/RadioPanelPZ69F5E.cs index 3bea06651..3aa931912 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69F5E.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69F5E.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -75,8 +76,8 @@ private enum CurrentF5ERadioMode private const string UHF_FREQ_3DIAL_COMMAND = "UHF_1MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_4DIAL_COMMAND = "UHF_01MHZ_SEL "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ_5DIAL_COMMAND = "UHF_0025MHZ_SEL "; // "00" "25" "50" "75" - private Thread _uhfSyncThread; - private long _uhfThreadNowSynching; + private Task _uhfSyncTask; + private CancellationTokenSource _uhfSyncTaskTokenSource = new(); private long _uhfDial1WaitingForFeedback; private long _uhfDial2WaitingForFeedback; private long _uhfDial3WaitingForFeedback; @@ -121,8 +122,8 @@ private enum CurrentF5ERadioMode private const string TACAN_FREQ_1DIAL_COMMAND = "TACAN_10 "; private const string TACAN_FREQ_2DIAL_COMMAND = "TACAN_1 "; private const string TACAN_FREQ_3DIAL_COMMAND = "TACAN_XY "; - private Thread _tacanSyncThread; - private long _tacanThreadNowSynching; + private Task _tacanSyncTask; + private CancellationTokenSource _tacanSyncTaskTokenSource = new(); private long _tacanDial1WaitingForFeedback; private long _tacanDial2WaitingForFeedback; private long _tacanDial3WaitingForFeedback; @@ -132,7 +133,7 @@ private enum CurrentF5ERadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69F5E(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -143,8 +144,8 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownUHFThread = true; - _shutdownTACANThread = true; + _uhfSyncTaskTokenSource.Cancel(); + _tacanSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -172,7 +173,7 @@ public override void InitPanel() // TACAN _tacanDcsbiosOutputFreqChannel = DCSBIOSControlLocator.GetStringDCSBIOSOutput("TACAN_CHANNEL"); - + BIOSEventHandler.AttachStringListener(this); BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); @@ -272,14 +273,12 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { try { - // Common.DebugP("RadioPanelPZ69F5E Received DCSBIOS stringData : ->" + e.StringData + "<-"); if (string.IsNullOrWhiteSpace(e.StringData)) { - // Common.DebugP("Received DCSBIOS stringData : " + e.StringData); return; } - if (_tacanDcsbiosOutputFreqChannel.StringValueHasChanged(e.Address,e.StringData)) + if (_tacanDcsbiosOutputFreqChannel.StringValueHasChanged(e.Address, e.StringData)) { try { @@ -423,7 +422,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF5E knob) private void SendUhfToDCSBIOS() { - if (UhfNowSyncing()) + if (_uhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -467,252 +466,250 @@ private void SendUhfToDCSBIOS() _ => 0 // Safeguard in case it is in a invalid position }; - _shutdownUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownUHFThread = false; - _uhfSyncThread = new Thread(() => UhfSynchThreadMethod(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5)); - _uhfSyncThread.Start(); + _uhfSyncTaskTokenSource = new(); + _uhfSyncTask = Task.Run(() => SyncUhfAsync(freqDial1, freqDial2, freqDial3, freqDial4, freqDial5, _uhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownUHFThread; - private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5) + private async Task SyncUhfAsync(int desiredPosition1, int desiredPosition2, int desiredPosition3, int desiredPosition4, int desiredPosition5, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial5Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + long dial5OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + var dial5SendCount = 0; + do { - Interlocked.Exchange(ref _uhfThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial5Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - long dial5OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - var dial5SendCount = 0; - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial5Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial5Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial5WaitingForFeedback); // Let's do an ugly reset + } + + // Frequency selector 1 + // "2" "3" "A"/"-1" + // Pos 0 1 2 + if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - // Frequency selector 1 - // "2" "3" "A"/"-1" - // Pos 0 1 2 - if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + lock (_lockUhfDialsObject1) { - lock (_lockUhfDialsObject1) + if (_uhfCockpitFreq1DialPos != desiredPosition1) { - if (_uhfCockpitFreq1DialPos != desiredPosition1) - { - dial1OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq1DialPos < desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq1DialPos > desiredPosition1) - { - const string str = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } + dial1OkTime = DateTime.Now.Ticks; + } - Reset(ref dial1Timeout); + if (_uhfCockpitFreq1DialPos < desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + else if (_uhfCockpitFreq1DialPos > desiredPosition1) + { + command = UHF_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDialsObject2) { - lock (_lockUhfDialsObject2) + if (_uhfCockpitFreq2DialPos != desiredPosition2) { - if (_uhfCockpitFreq2DialPos != desiredPosition2) - { - dial2OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq2DialPos < desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq2DialPos > desiredPosition2) - { - const string str = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } + dial2OkTime = DateTime.Now.Ticks; + } - Reset(ref dial2Timeout); + if (_uhfCockpitFreq2DialPos < desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + else if (_uhfCockpitFreq2DialPos > desiredPosition2) + { + command = UHF_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + lock (_lockUhfDialsObject3) { - lock (_lockUhfDialsObject3) + if (_uhfCockpitFreq3DialPos != desiredPosition3) { - if (_uhfCockpitFreq3DialPos != desiredPosition3) - { - dial3OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq3DialPos < desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq3DialPos > desiredPosition3) - { - const string str = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + } - Reset(ref dial3Timeout); + if (_uhfCockpitFreq3DialPos < desiredPosition3) + { + command = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + else if (_uhfCockpitFreq3DialPos > desiredPosition3) + { + command = UHF_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial4WaitingForFeedback) == 0) + lock (_lockUhfDialsObject4) { - lock (_lockUhfDialsObject4) + if (_uhfCockpitFreq4DialPos != desiredPosition4) { - if (_uhfCockpitFreq4DialPos != desiredPosition4) - { - dial4OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq4DialPos < desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq4DialPos > desiredPosition4) - { - const string str = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); - } + dial4OkTime = DateTime.Now.Ticks; + } - Reset(ref dial4Timeout); + if (_uhfCockpitFreq4DialPos < desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + else if (_uhfCockpitFreq4DialPos > desiredPosition4) + { + command = UHF_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial4OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _uhfDial4WaitingForFeedback, 1); } + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + if (Interlocked.Read(ref _uhfDial5WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockUhfDialsObject5) { - lock (_lockUhfDialsObject5) + if (_uhfCockpitFreq5DialPos != desiredPosition5) { - if (_uhfCockpitFreq5DialPos != desiredPosition5) - { - dial5OkTime = DateTime.Now.Ticks; - } - - if (_uhfCockpitFreq5DialPos < desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } - else if (_uhfCockpitFreq5DialPos > desiredPosition5) - { - const string str = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial5SendCount++; - Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); - } + dial5OkTime = DateTime.Now.Ticks; + } - Reset(ref dial5Timeout); + if (_uhfCockpitFreq5DialPos < desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; + } + else if (_uhfCockpitFreq5DialPos > desiredPosition5) + { + command = UHF_FREQ_5DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } - } - else - { - dial5OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial5SendCount++; + Interlocked.Exchange(ref _uhfDial5WaitingForFeedback, 1); } + Reset(ref dial5Timeout); + } + else + { + dial5OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 3 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 10 || dial5SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)) && !_shutdownUHFThread); - SwapCockpitStandbyFrequencyUhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (_uhfSyncTaskTokenSource.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime) || IsTooShort(dial5OkTime)); + SwapCockpitStandbyFrequencyUhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -720,7 +717,7 @@ private void UhfSynchThreadMethod(int desiredPosition1, int desiredPosition2, in private void SendTacanToDCSBIOS() { - if (TacanNowSyncing()) + if (_tacanSyncTask?.Status == TaskStatus.Running) { return; } @@ -741,145 +738,144 @@ private void SendTacanToDCSBIOS() // #1 = 12 (position = value) // #2 = 0 (position = value) // #3 = 1 (position = value) - _shutdownTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownTACANThread = false; - _tacanSyncThread = new Thread(() => TacanSynchThreadMethod(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby)); - _tacanSyncThread.Start(); + _tacanSyncTaskTokenSource = new(); + _tacanSyncTask = Task.Run(() => TacanAsync(_tacanBigFrequencyStandby, _tacanSmallFrequencyStandby, _tacanXYStandby, _tacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownTACANThread; - private void TacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3) + private async Task TacanAsync(int desiredPositionDial1, int desiredPositionDial2, int desiredPositionDial3, CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + do { - Interlocked.Exchange(ref _tacanThreadNowSynching, 1); - - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - - do + + if (IsTimedOut(ref dial1Timeout)) { + ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial1WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _tacanDial3WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanDial1WaitingForFeedback) == 0) + lock (_lockTacanDialsObject1) { - - lock (_lockTacanDialsObject1) + if (_tacanCockpitFreq1DialPos != desiredPositionDial1) { - if (_tacanCockpitFreq1DialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = TACAN_FREQ_1DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); - } - - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = TACAN_FREQ_1DIAL_COMMAND + (_tacanCockpitFreq1DialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _tacanDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanDial2WaitingForFeedback) == 0) + lock (_lockTacanDialsObject2) { - // Common.DebugP("b"); - lock (_lockTacanDialsObject2) + if (_tacanCockpitFreq2DialPos != desiredPositionDial2) { - if (_tacanCockpitFreq2DialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; - - var str = TACAN_FREQ_2DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); - } + dial2OkTime = DateTime.Now.Ticks; - Reset(ref dial2Timeout); + command = TACAN_FREQ_2DIAL_COMMAND + (_tacanCockpitFreq2DialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) + if (!string.IsNullOrEmpty(command)) { + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _tacanDial2WaitingForFeedback, 1); + } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - lock (_lockTacanDialsObject3) - { - if (_tacanCockpitFreq3DialPos != desiredPositionDial3) - { - dial3OkTime = DateTime.Now.Ticks; + if (Interlocked.Read(ref _tacanDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - var str = TACAN_FREQ_3DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); - } - } - Reset(ref dial3Timeout); - } - else + lock (_lockTacanDialsObject3) { - dial3OkTime = DateTime.Now.Ticks; + if (_tacanCockpitFreq3DialPos != desiredPositionDial3) + { + dial3OkTime = DateTime.Now.Ticks; + command = TACAN_FREQ_3DIAL_COMMAND + (_tacanCockpitFreq3DialPos < desiredPositionDial3 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); + } } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _tacanDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS - } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownTACANThread); - SwapCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + if (_tacanSyncTaskTokenSource.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + SwapCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (ThreadAbortException) + { } + catch (Exception ex) { - Interlocked.Exchange(ref _tacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1118,7 +1114,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -1143,14 +1139,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -1201,14 +1197,14 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(100.00)) { @@ -1258,7 +1254,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -1305,7 +1301,7 @@ private void AdjustFrequency(IEnumerable hashSet) _upperButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -1352,7 +1348,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_INCREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_INCREASE); } } else @@ -1360,7 +1356,7 @@ private void AdjustFrequency(IEnumerable hashSet) if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { // 225-399 - DCSBIOS.Send(UHF_PRESET_INCREASE); + await DCSBIOS.SendAsync(UHF_PRESET_INCREASE); } else if (_uhfBigFrequencyStandby.Equals(399.00)) { @@ -1410,14 +1406,14 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFunctionClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FUNCTION_DECREASE); + await DCSBIOS.SendAsync(UHF_FUNCTION_DECREASE); } } else { if (UhfPresetSelected() && _uhfChannelClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_PRESET_DECREASE); + await DCSBIOS.SendAsync(UHF_PRESET_DECREASE); } else if (_uhfBigFrequencyStandby.Equals(100.00)) { @@ -1467,7 +1463,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_INCREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_INCREASE); } } else @@ -1514,7 +1510,7 @@ private void AdjustFrequency(IEnumerable hashSet) _lowerButtonPressedAndDialRotated = true; if (_uhfFreqModeClickSpeedDetector.ClickAndCheck()) { - DCSBIOS.Send(UHF_FREQ_MODE_DECREASE); + await DCSBIOS.SendAsync(UHF_FREQ_MODE_DECREASE); } } else @@ -1721,171 +1717,168 @@ private void CheckFrequenciesForValidity() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobF5E)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobF5E)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsF5E.UPPER_UHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsF5E.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF5ERadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentF5ERadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_TACAN: + case RadioPanelPZ69KnobsF5E.UPPER_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF5ERadioMode.TACAN; - } - break; + _currentUpperRadioMode = CurrentF5ERadioMode.TACAN; } + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_NOUSE1: - case RadioPanelPZ69KnobsF5E.UPPER_NOUSE2: - case RadioPanelPZ69KnobsF5E.UPPER_NOUSE3: - case RadioPanelPZ69KnobsF5E.UPPER_NOUSE4: - case RadioPanelPZ69KnobsF5E.UPPER_NOUSE5: + case RadioPanelPZ69KnobsF5E.UPPER_NOUSE1: + case RadioPanelPZ69KnobsF5E.UPPER_NOUSE2: + case RadioPanelPZ69KnobsF5E.UPPER_NOUSE3: + case RadioPanelPZ69KnobsF5E.UPPER_NOUSE4: + case RadioPanelPZ69KnobsF5E.UPPER_NOUSE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentF5ERadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentF5ERadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_UHF: + case RadioPanelPZ69KnobsF5E.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF5ERadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentF5ERadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_TACAN: + case RadioPanelPZ69KnobsF5E.LOWER_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF5ERadioMode.TACAN; - } - break; + _currentLowerRadioMode = CurrentF5ERadioMode.TACAN; } + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_NOUSE1: - case RadioPanelPZ69KnobsF5E.LOWER_NOUSE2: - case RadioPanelPZ69KnobsF5E.LOWER_NOUSE3: - case RadioPanelPZ69KnobsF5E.LOWER_NOUSE4: - case RadioPanelPZ69KnobsF5E.LOWER_NOUSE5: + case RadioPanelPZ69KnobsF5E.LOWER_NOUSE1: + case RadioPanelPZ69KnobsF5E.LOWER_NOUSE2: + case RadioPanelPZ69KnobsF5E.LOWER_NOUSE3: + case RadioPanelPZ69KnobsF5E.LOWER_NOUSE4: + case RadioPanelPZ69KnobsF5E.LOWER_NOUSE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentF5ERadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentF5ERadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsF5E.UPPER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsF5E.UPPER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsF5E.UPPER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsF5E.UPPER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsF5E.LOWER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsF5E.LOWER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsF5E.LOWER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsF5E.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsF5E.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF5E.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF5E.UPPER_FREQ_SWITCH); - } - - _upperButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF5E.UPPER_FREQ_SWITCH); } - break; + + _upperButtonPressedAndDialRotated = false; } + break; + } - case RadioPanelPZ69KnobsF5E.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF5E.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF5E.LOWER_FREQ_SWITCH); - } - - _lowerButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF5E.LOWER_FREQ_SWITCH); } - break; - } - } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F5E, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + _lowerButtonPressedAndDialRotated = false; + } + break; + } } - AdjustFrequency(hashSet); + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F5E, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); + } } + + await AdjustFrequencyAsync(hashSet); } - + public override void ClearSettings(bool setIsDirty = false) { } public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobF5E.GetRadioPanelKnobs(); @@ -2054,16 +2047,6 @@ private bool UhfPresetSelected() return _uhfCockpitFreqMode == 1; } - private bool UhfNowSyncing() - { - return Interlocked.Read(ref _uhfThreadNowSynching) > 0; - } - - private bool TacanNowSyncing() - { - return Interlocked.Read(ref _tacanThreadNowSynching) > 0; - } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69F86F.cs b/src/NonVisuals/Radios/RadioPanelPZ69F86F.cs index 625315eb4..13c87c54a 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69F86F.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69F86F.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -108,7 +109,7 @@ private enum CurrentF86FRadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69F86F(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -148,7 +149,7 @@ public override void InitPanel() // ADF _apx6ModeDcsbiosOutputCockpit = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("APX6_MASTER"); - + BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } @@ -248,7 +249,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF86F knob) { try { @@ -294,7 +295,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F knob) case CurrentF86FRadioMode.ADF_APX6: { - DCSBIOS.Send(APX_6DIAL_STOP_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(APX_6DIAL_STOP_TOGGLE_COMMAND); break; } } @@ -327,7 +328,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F knob) case CurrentF86FRadioMode.ADF_APX6: { - DCSBIOS.Send(APX_6DIAL_STOP_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(APX_6DIAL_STOP_TOGGLE_COMMAND); break; } } @@ -341,175 +342,173 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F knob) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobF86F)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobF86F)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsF86F.UPPER_ARC27_PRESET: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsF86F.UPPER_ARC27_PRESET: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.ARC27_PRESET); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.ARC27_PRESET); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_ARC27_VOL: + case RadioPanelPZ69KnobsF86F.UPPER_ARC27_VOL: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.ARC27_VOL); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.ARC27_VOL); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_ARN6: + case RadioPanelPZ69KnobsF86F.UPPER_ARN6: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.ARN6); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.ARN6); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_ARN6_MODES: + case RadioPanelPZ69KnobsF86F.UPPER_ARN6_MODES: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.ARN6_MODES); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.ARN6_MODES); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_ADF_APX6: + case RadioPanelPZ69KnobsF86F.UPPER_ADF_APX6: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.ADF_APX6); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.ADF_APX6); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_NO_USE1: - case RadioPanelPZ69KnobsF86F.UPPER_NO_USE2: + case RadioPanelPZ69KnobsF86F.UPPER_NO_USE1: + case RadioPanelPZ69KnobsF86F.UPPER_NO_USE2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentF86FRadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentF86FRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_ARC27_PRESET: + case RadioPanelPZ69KnobsF86F.LOWER_ARC27_PRESET: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.ARC27_PRESET); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.ARC27_PRESET); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_ARC27_VOL: + case RadioPanelPZ69KnobsF86F.LOWER_ARC27_VOL: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.ARC27_VOL); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.ARC27_VOL); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_ARN6: + case RadioPanelPZ69KnobsF86F.LOWER_ARN6: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.ARN6); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.ARN6); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_ARN6_MODES: + case RadioPanelPZ69KnobsF86F.LOWER_ARN6_MODES: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.ARN6_MODES); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.ARN6_MODES); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_ADF_APX6: + case RadioPanelPZ69KnobsF86F.LOWER_ADF_APX6: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.ADF_APX6); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.ADF_APX6); } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_NO_USE1: - case RadioPanelPZ69KnobsF86F.LOWER_NO_USE2: + case RadioPanelPZ69KnobsF86F.LOWER_NO_USE1: + case RadioPanelPZ69KnobsF86F.LOWER_NO_USE2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentF86FRadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentF86FRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsF86F.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsF86F.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsF86F.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsF86F.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsF86F.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsF86F.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsF86F.LOWER_SMALL_FREQ_WHEEL_DEC: - { - // Ignore - break; - } + case RadioPanelPZ69KnobsF86F.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF86F.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF86F.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF86F.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF86F.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF86F.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsF86F.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsF86F.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } - case RadioPanelPZ69KnobsF86F.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF86F.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentF86FRadioMode.ADF_APX6) { - if (_currentUpperRadioMode == CurrentF86FRadioMode.ADF_APX6) + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F.UPPER_FREQ_SWITCH); - } + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF86F.UPPER_FREQ_SWITCH); } - break; } + break; + } - case RadioPanelPZ69KnobsF86F.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsF86F.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentF86FRadioMode.ADF_APX6) { - if (_currentLowerRadioMode == CurrentF86FRadioMode.ADF_APX6) + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsF86F.LOWER_FREQ_SWITCH); - } + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsF86F.LOWER_FREQ_SWITCH); } - break; } - } - - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F86F, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + break; + } } - AdjustFrequency(hashSet); + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_F86F, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); + } } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -517,7 +516,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -539,26 +538,26 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27PresetDialSkipper.Click(ARC27_PRESET_COMMAND_INC); + await _arc27PresetDialSkipper.ClickAsync(ARC27_PRESET_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6: { _bigFreqIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_bigFreqIncreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_INC : ARN6_FREQUENCY_COMMAND_INC); + await DCSBIOS.SendAsync(_bigFreqIncreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_INC : ARN6_FREQUENCY_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_MODE_COMMAND_INC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_MODE_COMMAND_INC); break; } case CurrentF86FRadioMode.ADF_APX6: { - _apx6ModeDialSkipper.Click(APX6_MODE_DIAL_COMMAND_INC); + await _apx6ModeDialSkipper.ClickAsync(APX6_MODE_DIAL_COMMAND_INC); break; } @@ -576,26 +575,26 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27PresetDialSkipper.Click(ARC27_PRESET_COMMAND_DEC); + await _arc27PresetDialSkipper.ClickAsync(ARC27_PRESET_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6: { _bigFreqDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_bigFreqDecreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_DEC : ARN6_FREQUENCY_COMMAND_DEC); + await DCSBIOS.SendAsync(_bigFreqDecreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_DEC : ARN6_FREQUENCY_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_MODE_COMMAND_DEC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_MODE_COMMAND_DEC); break; } case CurrentF86FRadioMode.ADF_APX6: { - _apx6ModeDialSkipper.Click(APX6_MODE_DIAL_COMMAND_DEC); + await _apx6ModeDialSkipper.ClickAsync(APX6_MODE_DIAL_COMMAND_DEC); break; } @@ -613,25 +612,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27ModeDialSkipper.Click(ARC27_MODE_COMMAND_INC); + await _arc27ModeDialSkipper.ClickAsync(ARC27_MODE_COMMAND_INC); break; } case CurrentF86FRadioMode.ARC27_VOL: { - DCSBIOS.Send(ARC27_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ARC27_VOLUME_KNOB_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6: { - _arn6BandDialSkipper.Click(ARN6_BAND_DIAL_COMMAND_INC); + await _arn6BandDialSkipper.ClickAsync(ARN6_BAND_DIAL_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_VOLUME_KNOB_COMMAND_INC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_VOLUME_KNOB_COMMAND_INC); break; } @@ -650,25 +649,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27ModeDialSkipper.Click(ARC27_MODE_COMMAND_DEC); + await _arc27ModeDialSkipper.ClickAsync(ARC27_MODE_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARC27_VOL: { - DCSBIOS.Send(ARC27_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC27_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6: { - _arn6BandDialSkipper.Click(ARN6_BAND_DIAL_COMMAND_DEC); + await _arn6BandDialSkipper.ClickAsync(ARN6_BAND_DIAL_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_VOLUME_KNOB_COMMAND_DEC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_VOLUME_KNOB_COMMAND_DEC); break; } @@ -687,26 +686,26 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27PresetDialSkipper.Click(ARC27_PRESET_COMMAND_INC); + await _arc27PresetDialSkipper.ClickAsync(ARC27_PRESET_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6: { _bigFreqIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_bigFreqIncreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_INC : ARN6_FREQUENCY_COMMAND_INC); + await DCSBIOS.SendAsync(_bigFreqIncreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_INC : ARN6_FREQUENCY_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_MODE_COMMAND_INC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_MODE_COMMAND_INC); break; } case CurrentF86FRadioMode.ADF_APX6: { - _apx6ModeDialSkipper.Click(APX6_MODE_DIAL_COMMAND_INC); + await _apx6ModeDialSkipper.ClickAsync(APX6_MODE_DIAL_COMMAND_INC); break; } @@ -724,26 +723,26 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27PresetDialSkipper.Click(ARC27_PRESET_COMMAND_DEC); + await _arc27PresetDialSkipper.ClickAsync(ARC27_PRESET_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6: { _bigFreqDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_bigFreqDecreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_DEC : ARN6_FREQUENCY_COMMAND_DEC); + await DCSBIOS.SendAsync(_bigFreqDecreaseChangeMonitor.ClickThresholdReached() ? ARN6_FREQUENCY_COMMAND_MORE_DEC : ARN6_FREQUENCY_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_MODE_COMMAND_DEC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_MODE_COMMAND_DEC); break; } case CurrentF86FRadioMode.ADF_APX6: { - _apx6ModeDialSkipper.Click(APX6_MODE_DIAL_COMMAND_DEC); + await _apx6ModeDialSkipper.ClickAsync(APX6_MODE_DIAL_COMMAND_DEC); break; } @@ -761,25 +760,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27ModeDialSkipper.Click(ARC27_MODE_COMMAND_INC); + await _arc27ModeDialSkipper.ClickAsync(ARC27_MODE_COMMAND_INC); break; } case CurrentF86FRadioMode.ARC27_VOL: { - DCSBIOS.Send(ARC27_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ARC27_VOLUME_KNOB_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6: { - _arn6BandDialSkipper.Click(ARN6_BAND_DIAL_COMMAND_INC); + await _arn6BandDialSkipper.ClickAsync(ARN6_BAND_DIAL_COMMAND_INC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_VOLUME_KNOB_COMMAND_INC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_VOLUME_KNOB_COMMAND_INC); break; } @@ -798,25 +797,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentF86FRadioMode.ARC27_PRESET: { - _arc27ModeDialSkipper.Click(ARC27_MODE_COMMAND_DEC); + await _arc27ModeDialSkipper.ClickAsync(ARC27_MODE_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARC27_VOL: { - DCSBIOS.Send(ARC27_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC27_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6: { - _arn6BandDialSkipper.Click(ARN6_BAND_DIAL_COMMAND_DEC); + await _arn6BandDialSkipper.ClickAsync(ARN6_BAND_DIAL_COMMAND_DEC); break; } case CurrentF86FRadioMode.ARN6_MODES: { - _arn6ModeDialSkipper.Click(ARN6_VOLUME_KNOB_COMMAND_DEC); + await _arn6ModeDialSkipper.ClickAsync(ARN6_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1063,7 +1062,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - + public override void ClearSettings(bool setIsDirty = false) { } public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPanelLEDPosition saitekPanelLEDPosition, PanelLEDColor panelLEDColor, DCSBIOSOutput dcsBiosOutput) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69FA18C.cs b/src/NonVisuals/Radios/RadioPanelPZ69FA18C.cs index 9ceae3ee6..6ba3a7dba 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69FA18C.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69FA18C.cs @@ -2,6 +2,7 @@ // added by Capt Zeen // +using System.Threading.Tasks; using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios @@ -85,7 +86,8 @@ private enum CurrentFA18CRadioMode private DCSBIOSOutput _ilsDcsbiosOutputChannel; private volatile uint _ilsCockpitChannel = 1; private const string ILS_CHANNEL_COMMAND = "COM_ILS_CHANNEL_SW "; - private Thread _ilsSyncThread; + private Task _ilsSyncTask; + private CancellationTokenSource _ilsSyncTaskTokenSource = new(); private long _ilsThreadNowSynching; private long _ilsDialWaitingForFeedback; private readonly object _lockShowFrequenciesOnPanelObject = new(); @@ -93,7 +95,7 @@ private enum CurrentFA18CRadioMode public RadioPanelPZ69FA18C(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -104,7 +106,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownILSThread = true; + _ilsSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachDataListener(this); } @@ -133,7 +135,7 @@ public override void InitPanel() BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } - + public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) { // ------------------------- Get the data from DCSbios @@ -205,11 +207,6 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { try { - // Common.DebugP("RadioPanelPZ69FA18C Received DCSBIOS stringData : ->" + e.StringData + "<-"); - if (string.IsNullOrWhiteSpace(e.StringData)) - { - // Common.DebugP("Received DCSBIOS stringData : " + e.StringData); - } } catch (Exception ex) { @@ -306,95 +303,89 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsFA18C knob) private void SendILSToDCSBIOS() { - if (IlsNowSyncing()) + if (_ilsSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyIls(); - _shutdownILSThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownILSThread = false; - _ilsSyncThread = new Thread(() => ILSSynchThreadMethod(_ilsChannelStandby)); - _ilsSyncThread.Start(); + _ilsSyncTaskTokenSource = new(); + _ilsSyncTask = Task.Run(() => SyncILSAsync(_ilsChannelStandby, _ilsSyncTaskTokenSource.Token)); } - private volatile bool _shutdownILSThread; - private void ILSSynchThreadMethod(uint standbyPosition) + private async Task SyncILSAsync(uint standbyPosition, CancellationToken cancellationToken) { try { - try - { - Interlocked.Exchange(ref _ilsThreadNowSynching, 1); + Interlocked.Exchange(ref _ilsThreadNowSynching, 1); - long dialTimeout = DateTime.Now.Ticks; - long dialOkTime = 0; - int dialSendCount = 0; + long dialTimeout = DateTime.Now.Ticks; + long dialOkTime = 0; + int dialSendCount = 0; - do + do + { + if (IsTimedOut(ref dialTimeout)) { - if (IsTimedOut(ref dialTimeout)) - { - ResetWaitingForFeedBack(ref _ilsDialWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _ilsDialWaitingForFeedback); // Let's do an ugly reset + } + + if (Interlocked.Read(ref _ilsDialWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _ilsDialWaitingForFeedback) == 0) + lock (_lockIlsDialsObject) { - lock (_lockIlsDialsObject) + if (_ilsCockpitChannel < standbyPosition) { - if (_ilsCockpitChannel < standbyPosition) - { - dialOkTime = DateTime.Now.Ticks; - const string str = ILS_CHANNEL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dialSendCount++; - Interlocked.Exchange(ref _ilsDialWaitingForFeedback, 1); - } - else if (_ilsCockpitChannel > standbyPosition) - { - dialOkTime = DateTime.Now.Ticks; - const string str = ILS_CHANNEL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dialSendCount++; - Interlocked.Exchange(ref _ilsDialWaitingForFeedback, 1); - } - - Reset(ref dialTimeout); + dialOkTime = DateTime.Now.Ticks; + command = ILS_CHANNEL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_ilsCockpitChannel > standbyPosition) + { + dialOkTime = DateTime.Now.Ticks; + command = ILS_CHANNEL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dialOkTime = DateTime.Now.Ticks; } - if (dialSendCount > 12) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dialSendCount = 0; - - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dialSendCount++; + Interlocked.Exchange(ref _ilsDialWaitingForFeedback, 1); } + Reset(ref dialTimeout); + } + else + { + dialOkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dialSendCount > 12) + { + // "Race" condition detected? + dialSendCount = 0; + + Thread.Sleep(5000); } - while (IsTooShort(dialOkTime) && !_shutdownILSThread); - SwapCockpitStandbyFrequencyIls(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; + } + while (IsTooShort(dialOkTime)); + + SwapCockpitStandbyFrequencyIls(); + ShowFrequenciesOnPanel(); + } + catch (ThreadAbortException) + { } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _ilsThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -604,7 +595,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -624,13 +615,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); break; } @@ -666,13 +657,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); break; } @@ -708,13 +699,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_INC); + await DCSBIOS.SendAsync(COMM1_VOL_INC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_INC); + await DCSBIOS.SendAsync(COMM2_VOL_INC); break; } @@ -742,13 +733,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_DEC); + await DCSBIOS.SendAsync(COMM1_VOL_DEC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_DEC); + await DCSBIOS.SendAsync(COMM2_VOL_DEC); break; } @@ -776,13 +767,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_INC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_CHANNEL_INC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_INC); break; } @@ -818,13 +809,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM1_CHANNEL_DEC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_CHANNEL_DEC); + await DCSBIOS.SendAsync(COMM2_CHANNEL_DEC); break; } @@ -860,13 +851,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_INC); + await DCSBIOS.SendAsync(COMM1_VOL_INC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_INC); + await DCSBIOS.SendAsync(COMM2_VOL_INC); break; } @@ -894,13 +885,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentFA18CRadioMode.COMM1: { - DCSBIOS.Send(COMM1_VOL_DEC); + await DCSBIOS.SendAsync(COMM1_VOL_DEC); break; } case CurrentFA18CRadioMode.COMM2: { - DCSBIOS.Send(COMM2_VOL_DEC); + await DCSBIOS.SendAsync(COMM2_VOL_DEC); break; } @@ -963,237 +954,235 @@ private void CheckFrequenciesForValidity() // 00X/Y - 129X/Y } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobFA18C)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobFA18C)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsFA18C.UPPER_COMM1: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsFA18C.UPPER_COMM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentFA18CRadioMode.COMM1; - } - break; + _currentUpperRadioMode = CurrentFA18CRadioMode.COMM1; } + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_COMM2: + case RadioPanelPZ69KnobsFA18C.UPPER_COMM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentFA18CRadioMode.COMM2; - } - break; + _currentUpperRadioMode = CurrentFA18CRadioMode.COMM2; } + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_VHFFM: + case RadioPanelPZ69KnobsFA18C.UPPER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentFA18CRadioMode.VHFFM; - } - break; + _currentUpperRadioMode = CurrentFA18CRadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_ILS: + case RadioPanelPZ69KnobsFA18C.UPPER_ILS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentFA18CRadioMode.ILS; - } - break; + _currentUpperRadioMode = CurrentFA18CRadioMode.ILS; } + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_TACAN: + case RadioPanelPZ69KnobsFA18C.UPPER_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentFA18CRadioMode.TACAN; - } - break; + _currentUpperRadioMode = CurrentFA18CRadioMode.TACAN; } + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_DME: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_DME: + { + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_XPDR: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_XPDR: + { + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_COMM1: + case RadioPanelPZ69KnobsFA18C.LOWER_COMM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentFA18CRadioMode.COMM1; - } - break; + _currentLowerRadioMode = CurrentFA18CRadioMode.COMM1; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_COMM2: + case RadioPanelPZ69KnobsFA18C.LOWER_COMM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentFA18CRadioMode.COMM2; - } - break; + _currentLowerRadioMode = CurrentFA18CRadioMode.COMM2; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_VHFFM: + case RadioPanelPZ69KnobsFA18C.LOWER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentFA18CRadioMode.VHFFM; - } - break; + _currentLowerRadioMode = CurrentFA18CRadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_ILS: + case RadioPanelPZ69KnobsFA18C.LOWER_ILS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentFA18CRadioMode.ILS; - } - break; + _currentLowerRadioMode = CurrentFA18CRadioMode.ILS; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_TACAN: + case RadioPanelPZ69KnobsFA18C.LOWER_TACAN: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentFA18CRadioMode.TACAN; - } - break; + _currentLowerRadioMode = CurrentFA18CRadioMode.TACAN; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_DME: - { - break; - } + case RadioPanelPZ69KnobsFA18C.LOWER_DME: + { + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_XPDR: - { - break; - } + case RadioPanelPZ69KnobsFA18C.LOWER_XPDR: + { + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.UPPER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.UPPER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.LOWER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsFA18C.LOWER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsFA18C.LOWER_SMALL_FREQ_WHEEL_INC: + { + break; + } + + case RadioPanelPZ69KnobsFA18C.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } + + case RadioPanelPZ69KnobsFA18C.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + + if (_currentUpperRadioMode == CurrentFA18CRadioMode.COMM1) { - break; + await DCSBIOS.SendAsync(_upperButtonPressed ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); } - case RadioPanelPZ69KnobsFA18C.LOWER_SMALL_FREQ_WHEEL_DEC: + if (_currentUpperRadioMode == CurrentFA18CRadioMode.COMM2) { - break; + await DCSBIOS.SendAsync(_upperButtonPressed ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); } - case RadioPanelPZ69KnobsFA18C.UPPER_FREQ_SWITCH: + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - - if (_currentUpperRadioMode == CurrentFA18CRadioMode.COMM1) - { - DCSBIOS.Send(_upperButtonPressed ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); - } - - if (_currentUpperRadioMode == CurrentFA18CRadioMode.COMM2) + if (!_upperButtonPressedAndDialRotated) { - DCSBIOS.Send(_upperButtonPressed ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsFA18C.UPPER_FREQ_SWITCH); } - if (!radioPanelKnob.IsOn) - { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsFA18C.UPPER_FREQ_SWITCH); - } - - _upperButtonPressedAndDialRotated = false; - } - break; + _upperButtonPressedAndDialRotated = false; } + break; + } - case RadioPanelPZ69KnobsFA18C.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsFA18C.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + + if (_currentLowerRadioMode == CurrentFA18CRadioMode.COMM1) { - _lowerButtonPressed = radioPanelKnob.IsOn; + await DCSBIOS.SendAsync(_lowerButtonPressed ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); + } - if (_currentLowerRadioMode == CurrentFA18CRadioMode.COMM1) - { - DCSBIOS.Send(_lowerButtonPressed ? COMM1_PULL_PRESS : COMM1_PULL_RELEASE); - } + if (_currentLowerRadioMode == CurrentFA18CRadioMode.COMM2) + { + await DCSBIOS.SendAsync(_lowerButtonPressed ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); + } - if (_currentLowerRadioMode == CurrentFA18CRadioMode.COMM2) + if (!radioPanelKnob.IsOn) + { + if (!_lowerButtonPressedAndDialRotated) { - DCSBIOS.Send(_lowerButtonPressed ? COMM2_PULL_PRESS : COMM2_PULL_RELEASE); + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsFA18C.LOWER_FREQ_SWITCH); } - if (!radioPanelKnob.IsOn) - { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsFA18C.LOWER_FREQ_SWITCH); - } - - _lowerButtonPressedAndDialRotated = false; - } - break; + _lowerButtonPressedAndDialRotated = false; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_FA18C, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_FA18C, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } public override void ClearSettings(bool setIsDirty = false) @@ -1204,7 +1193,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobFA18C.GetRadioPanelKnobs(); @@ -1223,11 +1212,6 @@ private void SwapCockpitStandbyFrequencyIls() _ilsChannelStandby = _ilsSavedCockpitChannel; } - private bool IlsNowSyncing() - { - return Interlocked.Read(ref _ilsThreadNowSynching) > 0; - } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Fw190.cs b/src/NonVisuals/Radios/RadioPanelPZ69Fw190.cs index 441762727..42dfe4235 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Fw190.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Fw190.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.Helpers; namespace NonVisuals.Radios @@ -115,7 +116,7 @@ private enum CurrentFw190RadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69Fw190(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -149,7 +150,7 @@ public override void InitPanel() // NAV1 _homingDcsbiosOutputPresetDial = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("FT_ZF_SWITCH"); - + BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } @@ -229,158 +230,164 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobFw190)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobFw190)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsFw190.UPPER_FUG16ZY: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsFw190.UPPER_FUG16ZY: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentFw190RadioMode.FUG16ZY); - } - break; + SetUpperRadioMode(CurrentFw190RadioMode.FUG16ZY); } + break; + } - case RadioPanelPZ69KnobsFw190.UPPER_IFF: + case RadioPanelPZ69KnobsFw190.UPPER_IFF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentFw190RadioMode.IFF); - } - break; + SetUpperRadioMode(CurrentFw190RadioMode.IFF); } + break; + } - case RadioPanelPZ69KnobsFw190.UPPER_HOMING: + case RadioPanelPZ69KnobsFw190.UPPER_HOMING: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentFw190RadioMode.HOMING); - } - break; + SetUpperRadioMode(CurrentFw190RadioMode.HOMING); } + break; + } - case RadioPanelPZ69KnobsFw190.UPPER_NO_USE1: - case RadioPanelPZ69KnobsFw190.UPPER_NO_USE2: - case RadioPanelPZ69KnobsFw190.UPPER_NO_USE3: - case RadioPanelPZ69KnobsFw190.UPPER_NO_USE4: + case RadioPanelPZ69KnobsFw190.UPPER_NO_USE1: + case RadioPanelPZ69KnobsFw190.UPPER_NO_USE2: + case RadioPanelPZ69KnobsFw190.UPPER_NO_USE3: + case RadioPanelPZ69KnobsFw190.UPPER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentFw190RadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentFw190RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsFw190.LOWER_FUG16ZY: + case RadioPanelPZ69KnobsFw190.LOWER_FUG16ZY: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentFw190RadioMode.FUG16ZY); - } - break; + SetLowerRadioMode(CurrentFw190RadioMode.FUG16ZY); } + break; + } - case RadioPanelPZ69KnobsFw190.LOWER_IFF: + case RadioPanelPZ69KnobsFw190.LOWER_IFF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentFw190RadioMode.IFF); - } - break; + SetLowerRadioMode(CurrentFw190RadioMode.IFF); } + break; + } - case RadioPanelPZ69KnobsFw190.LOWER_HOMING: + case RadioPanelPZ69KnobsFw190.LOWER_HOMING: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentFw190RadioMode.HOMING); - } - break; + SetLowerRadioMode(CurrentFw190RadioMode.HOMING); } + break; + } - case RadioPanelPZ69KnobsFw190.LOWER_NO_USE1: - case RadioPanelPZ69KnobsFw190.LOWER_NO_USE2: - case RadioPanelPZ69KnobsFw190.LOWER_NO_USE3: - case RadioPanelPZ69KnobsFw190.LOWER_NO_USE4: + case RadioPanelPZ69KnobsFw190.LOWER_NO_USE1: + case RadioPanelPZ69KnobsFw190.LOWER_NO_USE2: + case RadioPanelPZ69KnobsFw190.LOWER_NO_USE3: + case RadioPanelPZ69KnobsFw190.LOWER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentFw190RadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentFw190RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsFw190.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsFw190.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsFw190.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsFw190.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsFw190.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsFw190.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsFw190.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsFw190.LOWER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsFw190.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsFw190.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsFw190.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsFw190.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsFw190.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsFw190.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsFw190.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsFw190.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } + + case RadioPanelPZ69KnobsFw190.UPPER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentFw190RadioMode.IFF) { - // Ignore - break; + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); } - case RadioPanelPZ69KnobsFw190.UPPER_FREQ_SWITCH: + if (_currentUpperRadioMode == CurrentFw190RadioMode.HOMING) { - if (_currentLowerRadioMode == CurrentFw190RadioMode.IFF) + if (radioPanelKnob.IsOn) { - DCSBIOS.Send(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); - } + string command; - if (_currentUpperRadioMode == CurrentFw190RadioMode.HOMING) - { - if (radioPanelKnob.IsOn) + lock (_lockHomingDialObject1) { - lock (_lockHomingDialObject1) - { - DCSBIOS.Send(_homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC); - } + command = _homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC; } + + await DCSBIOS.SendAsync(command); } - break; } + break; + } - case RadioPanelPZ69KnobsFw190.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsFw190.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentFw190RadioMode.IFF) { - if (_currentLowerRadioMode == CurrentFw190RadioMode.IFF) - { - DCSBIOS.Send(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); - } + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? FU_G25_A_TEST_COMMAND_INC : FU_G25_A_TEST_COMMAND_DEC); + } - if (_currentLowerRadioMode == CurrentFw190RadioMode.HOMING) + if (_currentLowerRadioMode == CurrentFw190RadioMode.HOMING) + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) + string command; + + lock (_lockHomingDialObject1) { - lock (_lockHomingDialObject1) - { - DCSBIOS.Send(_homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC); - } + command = _homingCockpitDialPos == 1 ? HOMING_COMMAND_DEC : HOMING_COMMAND_INC; } + + await DCSBIOS.SendAsync(command); } - break; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_FW190, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_FW190, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); + await AdjustFrequencyAsync(hashSet); } } catch (Exception ex) @@ -389,7 +396,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -412,13 +419,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_INC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_INC); break; } case CurrentFw190RadioMode.IFF: { - _fug25AIFFDialSkipper.Click(FUG25_AIFF_COMMAND_INC); + await _fug25AIFFDialSkipper.ClickAsync(FUG25_AIFF_COMMAND_INC); break; } @@ -442,13 +449,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_DEC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_DEC); break; } case CurrentFw190RadioMode.IFF: { - _fug25AIFFDialSkipper.Click(FUG25_AIFF_COMMAND_DEC); + await _fug25AIFFDialSkipper.ClickAsync(FUG25_AIFF_COMMAND_DEC); break; } @@ -472,13 +479,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Fine tuning - DCSBIOS.Send(FUG16_ZY_FINE_TUNE_COMMAND_INC); + await DCSBIOS.SendAsync(FUG16_ZY_FINE_TUNE_COMMAND_INC); break; } case CurrentFw190RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_INC); break; } @@ -502,13 +509,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Fine tuning - DCSBIOS.Send(FUG16_ZY_FINE_TUNE_COMMAND_DEC); + await DCSBIOS.SendAsync(FUG16_ZY_FINE_TUNE_COMMAND_DEC); break; } case CurrentFw190RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_DEC); break; } @@ -532,13 +539,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_INC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_INC); break; } case CurrentFw190RadioMode.IFF: { - _fug25AIFFDialSkipper.Click(FUG25_AIFF_COMMAND_INC); + await _fug25AIFFDialSkipper.ClickAsync(FUG25_AIFF_COMMAND_INC); break; } @@ -562,13 +569,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Presets - _fug16ZyPresetDialSkipper.Click(FUG16_ZY_PRESET_COMMAND_DEC); + await _fug16ZyPresetDialSkipper.ClickAsync(FUG16_ZY_PRESET_COMMAND_DEC); break; } case CurrentFw190RadioMode.IFF: { - _fug25AIFFDialSkipper.Click(FUG25_AIFF_COMMAND_DEC); + await _fug25AIFFDialSkipper.ClickAsync(FUG25_AIFF_COMMAND_DEC); break; } @@ -592,13 +599,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Fine tuning - DCSBIOS.Send(FUG16_ZY_FINE_TUNE_COMMAND_INC); + await DCSBIOS.SendAsync(FUG16_ZY_FINE_TUNE_COMMAND_INC); break; } case CurrentFw190RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_INC); break; } @@ -622,13 +629,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentFw190RadioMode.FUG16ZY: { // Fine tuning - DCSBIOS.Send(FUG16_ZY_FINE_TUNE_COMMAND_DEC); + await DCSBIOS.SendAsync(FUG16_ZY_FINE_TUNE_COMMAND_DEC); break; } case CurrentFw190RadioMode.IFF: { - DCSBIOS.Send(RADIO_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_KNOB_COMMAND_DEC); break; } @@ -679,11 +686,11 @@ private void ShowFrequenciesOnPanel() case CurrentFw190RadioMode.FUG16ZY: { // 1-4 - string modeDialPostionAsString; + string modeDialPositionAsString; string fineTunePositionAsString; lock (_lockFug16ZyPresetDialObject1) { - modeDialPostionAsString = (_fug16ZyPresetCockpitDialPos + 1).ToString(); + modeDialPositionAsString = (_fug16ZyPresetCockpitDialPos + 1).ToString(); } lock (_lockFug16ZyFineTuneDialObject1) @@ -692,7 +699,7 @@ private void ShowFrequenciesOnPanel() fineTunePositionAsString = (_fug16ZyFineTuneCockpitDialPos / 10).ToString(); } - SetPZ69DisplayBytesUnsignedInteger(ref bytes, Convert.ToUInt32(modeDialPostionAsString), PZ69LCDPosition.UPPER_ACTIVE_LEFT); + SetPZ69DisplayBytesUnsignedInteger(ref bytes, Convert.ToUInt32(modeDialPositionAsString), PZ69LCDPosition.UPPER_ACTIVE_LEFT); SetPZ69DisplayBytesUnsignedInteger(ref bytes, Convert.ToUInt32(fineTunePositionAsString), PZ69LCDPosition.UPPER_STBY_RIGHT); break; } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Generic.cs b/src/NonVisuals/Radios/RadioPanelPZ69Generic.cs index 71550f59b..2f4b635a6 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Generic.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Generic.cs @@ -1,4 +1,5 @@ using System.Globalization; +using System.Threading.Tasks; using NonVisuals.BindingClasses.BIP; using NonVisuals.BindingClasses.DCSBIOSBindings; using NonVisuals.BindingClasses.Key; @@ -303,7 +304,7 @@ public List OSCommandHashSet public HashSet DisplayValueHashSet => _displayValues; - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { if (ForwardPanelEvent) { @@ -457,7 +458,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (dcsBiosBinding.DCSBIOSInputs.Count > 0 && dcsBiosBinding.RadioPanelPZ69Knob == radioPanelKey.RadioPanelPZ69Knob && dcsBiosBinding.WhenTurnedOn == radioPanelKey.IsOn) { - dcsBiosBinding.SendDCSBIOSCommands(new CancellationToken()); + await dcsBiosBinding.SendDCSBIOSCommandsAsync(new CancellationToken()); break; } } @@ -487,23 +488,22 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (displayValue.RadioPanelPZ69Knob == radioPanelKey.RadioPanelPZ69Knob) { - double parsedValue = double.Parse(displayValue.Value, NumberFormatInfoFullDisplay); + var parsedValue = double.Parse(displayValue.Value, NumberFormatInfoFullDisplay); - if (displayValue.RadioPanelDisplay == RadioPanelPZ69Display.UpperActive) + switch (displayValue.RadioPanelDisplay) { - _upperActive = parsedValue; - } - else if (displayValue.RadioPanelDisplay == RadioPanelPZ69Display.UpperStandby) - { - _upperStandby = parsedValue; - } - else if (displayValue.RadioPanelDisplay == RadioPanelPZ69Display.LowerActive) - { - _lowerActive = parsedValue; - } - else if (displayValue.RadioPanelDisplay == RadioPanelPZ69Display.LowerStandby) - { - _lowerStandby = parsedValue; + case RadioPanelPZ69Display.UpperActive: + _upperActive = parsedValue; + break; + case RadioPanelPZ69Display.UpperStandby: + _upperStandby = parsedValue; + break; + case RadioPanelPZ69Display.LowerActive: + _lowerActive = parsedValue; + break; + case RadioPanelPZ69Display.LowerStandby: + _lowerStandby = parsedValue; + break; } } } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69JF17.cs b/src/NonVisuals/Radios/RadioPanelPZ69JF17.cs index 678bdb7ef..b924f760f 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69JF17.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69JF17.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.Radios.RadioSettings; namespace NonVisuals.Radios @@ -150,7 +151,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelKnobsJF17 knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelKnobsJF17 knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelKnobsJF17.UPPER_FREQ_SWITCH || knob == RadioPanelKnobsJF17.LOWER_FREQ_SWITCH)) { @@ -174,12 +175,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsJF17 knob) { case CurrentJF17RadioMode.COM1: { - SendCOM1ToDCSBIOS(); + await SendCOM1ToDCSBIOSAsync(); break; } case CurrentJF17RadioMode.COM2: { - SendCOM2ToDCSBIOS(); + await SendCOM2ToDCSBIOSAsync(); break; } } @@ -193,12 +194,12 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsJF17 knob) case CurrentJF17RadioMode.COM1: { - SendCOM1ToDCSBIOS(); + await SendCOM1ToDCSBIOSAsync(); break; } case CurrentJF17RadioMode.COM2: { - SendCOM2ToDCSBIOS(); + await SendCOM2ToDCSBIOSAsync(); break; } } @@ -207,11 +208,11 @@ private void SendFrequencyToDCSBIOS(RadioPanelKnobsJF17 knob) } } - private void SendCOM1ToDCSBIOS() + private async Task SendCOM1ToDCSBIOSAsync() { try { - DCSBIOS.Send(_com1Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_com1Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); } catch (Exception ex) @@ -220,11 +221,11 @@ private void SendCOM1ToDCSBIOS() } } - private void SendCOM2ToDCSBIOS() + private async Task SendCOM2ToDCSBIOSAsync() { try { - DCSBIOS.Send(_com2Radio.GetDCSBIOSCommand()); + await DCSBIOS.SendAsync(_com2Radio.GetDCSBIOSCommand()); Interlocked.Increment(ref _doUpdatePanelLCD); } catch (Exception ex) @@ -567,160 +568,158 @@ private void AdjustFrequency(IEnumerable hashSet) ShowFrequenciesOnPanel(); } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobJF17)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobJF17)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelKnobsJF17.UPPER_COM1: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelKnobsJF17.UPPER_COM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentJF17RadioMode.COM1; - } - break; + _currentUpperRadioMode = CurrentJF17RadioMode.COM1; } - case RadioPanelKnobsJF17.UPPER_COM2: + break; + } + case RadioPanelKnobsJF17.UPPER_COM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentJF17RadioMode.COM2; - } - break; + _currentUpperRadioMode = CurrentJF17RadioMode.COM2; } - case RadioPanelKnobsJF17.UPPER_NAV1: - case RadioPanelKnobsJF17.UPPER_NAV2: - case RadioPanelKnobsJF17.UPPER_ADF: - case RadioPanelKnobsJF17.UPPER_DME: - case RadioPanelKnobsJF17.UPPER_XPDR: + break; + } + case RadioPanelKnobsJF17.UPPER_NAV1: + case RadioPanelKnobsJF17.UPPER_NAV2: + case RadioPanelKnobsJF17.UPPER_ADF: + case RadioPanelKnobsJF17.UPPER_DME: + case RadioPanelKnobsJF17.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentJF17RadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentJF17RadioMode.NO_USE; } - case RadioPanelKnobsJF17.LOWER_COM1: + break; + } + case RadioPanelKnobsJF17.LOWER_COM1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentJF17RadioMode.COM1; - } - break; + _currentLowerRadioMode = CurrentJF17RadioMode.COM1; } - case RadioPanelKnobsJF17.LOWER_COM2: + break; + } + case RadioPanelKnobsJF17.LOWER_COM2: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentJF17RadioMode.COM2; - } - break; + _currentLowerRadioMode = CurrentJF17RadioMode.COM2; } - case RadioPanelKnobsJF17.LOWER_NAV1: - case RadioPanelKnobsJF17.LOWER_NAV2: - case RadioPanelKnobsJF17.LOWER_ADF: - case RadioPanelKnobsJF17.LOWER_DME: - case RadioPanelKnobsJF17.LOWER_XPDR: + break; + } + case RadioPanelKnobsJF17.LOWER_NAV1: + case RadioPanelKnobsJF17.LOWER_NAV2: + case RadioPanelKnobsJF17.LOWER_ADF: + case RadioPanelKnobsJF17.LOWER_DME: + case RadioPanelKnobsJF17.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentJF17RadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentJF17RadioMode.NO_USE; } - case RadioPanelKnobsJF17.UPPER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsJF17.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Sync when user releases button - SendFrequencyToDCSBIOS(RadioPanelKnobsJF17.UPPER_FREQ_SWITCH); - } - else + // Do not sync if user has pressed the button to configure the radio + // Sync when user releases button + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsJF17.UPPER_FREQ_SWITCH); + } + else + { + /* We must say that the user now has stopped rotating */ + switch (_currentUpperRadioMode) { - /* We must say that the user now has stopped rotating */ - switch (_currentUpperRadioMode) - { - case CurrentJF17RadioMode.COM1: - { - - _com1Radio.SwitchFrequencyBand(); - Interlocked.Increment(ref _doUpdatePanelLCD); - break; - } - case CurrentJF17RadioMode.COM2: - { - - _com2Radio.SwitchFrequencyBand(); - Interlocked.Increment(ref _doUpdatePanelLCD); - break; - } - } - } + case CurrentJF17RadioMode.COM1: + { + + _com1Radio.SwitchFrequencyBand(); + Interlocked.Increment(ref _doUpdatePanelLCD); + break; + } + case CurrentJF17RadioMode.COM2: + { - _upperButtonPressedAndDialRotated = false; + _com2Radio.SwitchFrequencyBand(); + Interlocked.Increment(ref _doUpdatePanelLCD); + break; + } + } } - break; + + _upperButtonPressedAndDialRotated = false; } - case RadioPanelKnobsJF17.LOWER_FREQ_SWITCH: + break; + } + case RadioPanelKnobsJF17.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Sync when user releases button - SendFrequencyToDCSBIOS(RadioPanelKnobsJF17.LOWER_FREQ_SWITCH); - } - else + // Do not sync if user has pressed the button to configure the radio + // Sync when user releases button + await SendFrequencyToDCSBIOSAsync(RadioPanelKnobsJF17.LOWER_FREQ_SWITCH); + } + else + { + /* We must say that the user now has stopped rotating */ + switch (_currentLowerRadioMode) { - /* We must say that the user now has stopped rotating */ - switch (_currentLowerRadioMode) - { - case CurrentJF17RadioMode.COM1: - { - - _com1Radio.SwitchFrequencyBand(); - break; - } - case CurrentJF17RadioMode.COM2: - { - - _com2Radio.SwitchFrequencyBand(); - break; - } - } - } + case CurrentJF17RadioMode.COM1: + { - _lowerButtonPressedAndDialRotated = false; + _com1Radio.SwitchFrequencyBand(); + break; + } + case CurrentJF17RadioMode.COM2: + { + + _com2Radio.SwitchFrequencyBand(); + break; + } + } } - break; + + _lowerButtonPressedAndDialRotated = false; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_AH64D, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + AdjustFrequency(hashSet); } public override void ClearSettings(bool setIsDirty = false) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Ka50.cs b/src/NonVisuals/Radios/RadioPanelPZ69Ka50.cs index 95d0631b5..429690706 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Ka50.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Ka50.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -89,8 +90,8 @@ private enum CurrentKa50RadioMode private const string R800_L1_FREQ_2DIAL_COMMAND = "R800_FREQ2 "; private const string R800_L1_FREQ_3DIAL_COMMAND = "R800_FREQ3 "; private const string R800_L1_FREQ_4DIAL_COMMAND = "R800_FREQ4 "; - private Thread _r800L1SyncThread; - private long _r800L1ThreadNowSynching; + private Task _r800L1SyncTask; + private CancellationTokenSource _r800L1SyncTokenSource = new(); private long _r800L1Dial1WaitingForFeedback; private long _r800L1Dial2WaitingForFeedback; private long _r800L1Dial3WaitingForFeedback; @@ -166,7 +167,7 @@ private enum CurrentKa50RadioMode public RadioPanelPZ69Ka50(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -177,7 +178,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownR800L1Thread = true; + _r800L1SyncTokenSource.Cancel(); BIOSEventHandler.DetachDataListener(this); } @@ -209,7 +210,7 @@ public override void InitPanel() _datalinkMasterModeDcsbiosOutput = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("DLNK_MASTER_MODE"); _datalinkSelfIdDcsbiosOutput = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("DLNK_SELF_ID"); _datalinkPowerOnOffDcsbiosOutput = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("PVI_POWER"); - + BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } @@ -342,7 +343,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(bool isOn, RadioPanelPZ69KnobsKa50 knob) + private async Task SendFrequencyToDCSBIOSAsync(bool isOn, RadioPanelPZ69KnobsKa50 knob) { try { @@ -382,7 +383,7 @@ private void SendFrequencyToDCSBIOS(bool isOn, RadioPanelPZ69KnobsKa50 knob) case CurrentKa50RadioMode.ABRIS: { - DCSBIOS.Send(isOn ? ABRIS_RIGHT_DIAL_PUSH_TOGGLE_ON_COMMAND : ABRIS_RIGHT_DIAL_PUSH_TOGGLE_OFF_COMMAND); + await DCSBIOS.SendAsync(isOn ? ABRIS_RIGHT_DIAL_PUSH_TOGGLE_ON_COMMAND : ABRIS_RIGHT_DIAL_PUSH_TOGGLE_OFF_COMMAND); break; } @@ -414,7 +415,7 @@ private void SendFrequencyToDCSBIOS(bool isOn, RadioPanelPZ69KnobsKa50 knob) case CurrentKa50RadioMode.ABRIS: { - DCSBIOS.Send(isOn ? ABRIS_RIGHT_DIAL_PUSH_TOGGLE_ON_COMMAND : ABRIS_RIGHT_DIAL_PUSH_TOGGLE_OFF_COMMAND); + await DCSBIOS.SendAsync(isOn ? ABRIS_RIGHT_DIAL_PUSH_TOGGLE_ON_COMMAND : ABRIS_RIGHT_DIAL_PUSH_TOGGLE_OFF_COMMAND); break; } @@ -437,18 +438,15 @@ private void SendR800L1ToDCSBIOS() { try { - if (R800L1NowSyncing()) + if (_r800L1SyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyR800L1(); - _shutdownR800L1Thread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownR800L1Thread = false; - _r800L1SyncThread = new Thread(() => R800L1SynchThreadMethod()); - _r800L1SyncThread.Start(); + _r800L1SyncTokenSource = new(); + _r800L1SyncTask = Task.Run(() => SyncR800L1Async(_r800L1SyncTokenSource.Token)); } catch (Exception ex) { @@ -456,219 +454,223 @@ private void SendR800L1ToDCSBIOS() } } - private volatile bool _shutdownR800L1Thread; - private void R800L1SynchThreadMethod() + private async Task SyncR800L1Async(CancellationToken cancellationToken) { try { - try + /* + * Ka-50 R-800L1 VHF 2 + */ + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + var frequencyAsString = _r800L1BigFrequencyStandby + "." + _r800L1SmallFrequencyStandby.ToString().PadLeft(2, '0'); + frequencyAsString = frequencyAsString.PadRight(6, '0'); + + // Frequency selector 1 R800_FREQ1 + // "10" "11" "12" "13" "14" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" + // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 + + // Frequency selector 2 R800_FREQ2 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 3 R800_FREQ3 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 4 R800_FREQ4 + // "00" "25" "50" "75", only "00" and "50" used. + // Pos 0 1 2 3 + + // Reason for this is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + + // 151.95 + // #1 = 15 (position = value - 3) + // #2 = 1 (position = value) + // #3 = 9 (position = value) + // #4 = 5 + var desiredPositionDial1X = Array.IndexOf(_r800L1Freq1DialValues, int.Parse(frequencyAsString.Substring(0, 2))); + var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); + var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(4, 1)); + var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(5, 1)); + + do { - try + if (IsTimedOut(ref dial1Timeout)) { - /* - * Ka-50 R-800L1 VHF 2 - */ - Interlocked.Exchange(ref _r800L1ThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - var frequencyAsString = _r800L1BigFrequencyStandby + "." + _r800L1SmallFrequencyStandby.ToString().PadLeft(2, '0'); - frequencyAsString = frequencyAsString.PadRight(6, '0'); - - // Frequency selector 1 R800_FREQ1 - // "10" "11" "12" "13" "14" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" - // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - - // Frequency selector 2 R800_FREQ2 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 3 R800_FREQ3 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 4 R800_FREQ4 - // "00" "25" "50" "75", only "00" and "50" used. - // Pos 0 1 2 3 + ResetWaitingForFeedBack(ref _r800L1Dial1WaitingForFeedback); // Let's do an ugly reset + } - // Reason for this is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - - // 151.95 - // #1 = 15 (position = value - 3) - // #2 = 1 (position = value) - // #3 = 9 (position = value) - // #4 = 5 - var desiredPositionDial1X = Array.IndexOf(_r800L1Freq1DialValues, int.Parse(frequencyAsString.Substring(0, 2))); - var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); - var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(4, 1)); - var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(5, 1)); - - do - { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _r800L1Dial1WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _r800L1Dial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _r800L1Dial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _r800L1Dial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _r800L1Dial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _r800L1Dial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _r800L1Dial4WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _r800L1Dial1WaitingForFeedback) == 0) + { + var command = string.Empty; - string str; - if (Interlocked.Read(ref _r800L1Dial1WaitingForFeedback) == 0) - { - lock (_lockR800L1DialsObject1) - { - if (_r800L1CockpitFreq1DialPos != desiredPositionDial1X) - { - dial1OkTime = DateTime.Now.Ticks; - str = R800_L1_FREQ_1DIAL_COMMAND + GetCommandDirectionForR800L1Dial1(desiredPositionDial1X, _r800L1CockpitFreq1DialPos); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _r800L1Dial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); - } - } - else + lock (_lockR800L1DialsObject1) + { + if (_r800L1CockpitFreq1DialPos != desiredPositionDial1X) { dial1OkTime = DateTime.Now.Ticks; + command = R800_L1_FREQ_1DIAL_COMMAND + GetCommandDirectionForR800L1Dial1(desiredPositionDial1X, _r800L1CockpitFreq1DialPos); } + } - if (Interlocked.Read(ref _r800L1Dial2WaitingForFeedback) == 0) - { - lock (_lockR800L1DialsObject2) - { - if (_r800L1CockpitFreq2DialPos != desiredPositionDial2X) - { - dial2OkTime = DateTime.Now.Ticks; - str = R800_L1_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _r800L1CockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _r800L1Dial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _r800L1Dial1WaitingForFeedback, 1); + + } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r800L1Dial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockR800L1DialsObject2) + { + if (_r800L1CockpitFreq2DialPos != desiredPositionDial2X) { dial2OkTime = DateTime.Now.Ticks; + command = R800_L1_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _r800L1CockpitFreq2DialPos); } + } - if (Interlocked.Read(ref _r800L1Dial3WaitingForFeedback) == 0) - { - lock (_lockR800L1DialsObject3) - { - if (_r800L1CockpitFreq3DialPos != desiredPositionDial3X) - { - dial3OkTime = DateTime.Now.Ticks; - str = R800_L1_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _r800L1CockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _r800L1Dial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _r800L1Dial2WaitingForFeedback, 1); + } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r800L1Dial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockR800L1DialsObject3) + { + if (_r800L1CockpitFreq3DialPos != desiredPositionDial3X) { dial3OkTime = DateTime.Now.Ticks; + command = R800_L1_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _r800L1CockpitFreq3DialPos); } + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _r800L1Dial3WaitingForFeedback, 1); + } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r800L1Dial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _r800L1Dial4WaitingForFeedback) == 0) - { #pragma warning disable CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive). - var desiredPositionDial4 = desiredPositionDial4X switch - { - 0 => 0, - 2 => 0, - 5 => 2, - 7 => 2 - }; + var desiredPositionDial4 = desiredPositionDial4X switch + { + 0 => 0, + 2 => 0, + 5 => 2, + 7 => 2 + }; #pragma warning restore CS8509 // The switch expression does not handle all possible values of its input type (it is not exhaustive). - // "00" "25" "50" "75", only "00" and "50" used. - // Pos 0 1 2 3 - lock (_lockR800L1DialsObject4) - { - if (_r800L1CockpitFreq4DialPos < desiredPositionDial4) - { - dial4OkTime = DateTime.Now.Ticks; - str = R800_L1_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _r800L1Dial4WaitingForFeedback, 1); - } - else if (_r800L1CockpitFreq4DialPos > desiredPositionDial4) - { - dial4OkTime = DateTime.Now.Ticks; - str = R800_L1_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _r800L1Dial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); - } - } - else + // "00" "25" "50" "75", only "00" and "50" used. + // Pos 0 1 2 3 + lock (_lockR800L1DialsObject4) + { + if (_r800L1CockpitFreq4DialPos < desiredPositionDial4) { dial4OkTime = DateTime.Now.Ticks; + command = R800_L1_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } - - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + else if (_r800L1CockpitFreq4DialPos > desiredPositionDial4) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + dial4OkTime = DateTime.Now.Ticks; + command = R800_L1_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownR800L1Thread); - SwapCockpitStandbyFrequencyR800L1(); - ShowFrequenciesOnPanel(); + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _r800L1Dial4WaitingForFeedback, 1); + } + Reset(ref dial4Timeout); } - catch (ThreadAbortException) + else { + dial4OkTime = DateTime.Now.Ticks; } - catch (Exception ex) + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) { - Common.ShowErrorMessageBox(ex); + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } - finally - { - Interlocked.Exchange(ref _r800L1ThreadNowSynching, 0); - } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + + SwapCockpitStandbyFrequencyR800L1(); + ShowFrequenciesOnPanel(); } catch (Exception ex) { - Logger.Error(ex); + Common.ShowErrorMessageBox(ex); } // Refresh panel once this debacle is finished @@ -688,238 +690,245 @@ private void SwapCockpitStandbyFrequencyR800L1() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobKa50)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobKa50)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsKa50.UPPER_VHF1_R828: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsKa50.UPPER_VHF1_R828: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.VHF1_R828); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.VHF1_R828); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_VHF2_R800L1: + case RadioPanelPZ69KnobsKa50.UPPER_VHF2_R800L1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.VHF2_R800L1); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.VHF2_R800L1); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_ADF_ARK22: + case RadioPanelPZ69KnobsKa50.UPPER_ADF_ARK22: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.ADF_ARK22); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.ADF_ARK22); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_VHF1_R828: + case RadioPanelPZ69KnobsKa50.LOWER_VHF1_R828: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.VHF1_R828); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.VHF1_R828); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_VHF2_R800L1: + case RadioPanelPZ69KnobsKa50.LOWER_VHF2_R800L1: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.VHF2_R800L1); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.VHF2_R800L1); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_ADF_ARK22: + case RadioPanelPZ69KnobsKa50.LOWER_ADF_ARK22: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.ADF_ARK22); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.ADF_ARK22); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_ABRIS: + case RadioPanelPZ69KnobsKa50.UPPER_ABRIS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.ABRIS); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.ABRIS); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_DATALINK: + case RadioPanelPZ69KnobsKa50.UPPER_DATALINK: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.DATALINK); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.DATALINK); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_NO_USE3: - case RadioPanelPZ69KnobsKa50.UPPER_NO_USE4: + case RadioPanelPZ69KnobsKa50.UPPER_NO_USE3: + case RadioPanelPZ69KnobsKa50.UPPER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentKa50RadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentKa50RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_ABRIS: + case RadioPanelPZ69KnobsKa50.LOWER_ABRIS: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.ABRIS); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.ABRIS); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_DATALINK: + case RadioPanelPZ69KnobsKa50.LOWER_DATALINK: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.DATALINK); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.DATALINK); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_NO_USE3: - case RadioPanelPZ69KnobsKa50.LOWER_NO_USE4: + case RadioPanelPZ69KnobsKa50.LOWER_NO_USE3: + case RadioPanelPZ69KnobsKa50.LOWER_NO_USE4: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentKa50RadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentKa50RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsKa50.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsKa50.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsKa50.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsKa50.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsKa50.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsKa50.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsKa50.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsKa50.LOWER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsKa50.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsKa50.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsKa50.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsKa50.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsKa50.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsKa50.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsKa50.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsKa50.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } + + case RadioPanelPZ69KnobsKa50.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentKa50RadioMode.VHF1_R828) { - // Ignore - break; + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? VHF1_TUNER_BUTTON_PRESS : VHF1_TUNER_BUTTON_RELEASE); } - - case RadioPanelPZ69KnobsKa50.UPPER_FREQ_SWITCH: + else if (_currentUpperRadioMode == CurrentKa50RadioMode.ADF_ARK22 && radioPanelKnob.IsOn) { - if (_currentUpperRadioMode == CurrentKa50RadioMode.VHF1_R828) - { - DCSBIOS.Send(radioPanelKnob.IsOn ? VHF1_TUNER_BUTTON_PRESS : VHF1_TUNER_BUTTON_RELEASE); - } - else if (_currentUpperRadioMode == CurrentKa50RadioMode.ADF_ARK22 && radioPanelKnob.IsOn) + var command = string.Empty; + + lock (_lockADFModeDialObject) { - lock (_lockADFModeDialObject) + if (_adfModeSwitchDirectionUp && _adfModeCockpitPos == 2) { - if (_adfModeSwitchDirectionUp && _adfModeCockpitPos == 2) - { - _adfModeSwitchDirectionUp = false; - DCSBIOS.Send(ADF_MODE_DEC); - } - else if (!_adfModeSwitchDirectionUp && _adfModeCockpitPos == 0) - { - _adfModeSwitchDirectionUp = true; - DCSBIOS.Send(ADF_MODE_INC); - } - else if (_adfModeSwitchDirectionUp) - { - DCSBIOS.Send(ADF_MODE_INC); - } - else if (!_adfModeSwitchDirectionUp) - { - DCSBIOS.Send(ADF_MODE_DEC); - } + _adfModeSwitchDirectionUp = false; + command = ADF_MODE_DEC; + } + else if (!_adfModeSwitchDirectionUp && _adfModeCockpitPos == 0) + { + _adfModeSwitchDirectionUp = true; + command = ADF_MODE_INC; + } + else if (_adfModeSwitchDirectionUp) + { + command = ADF_MODE_INC; + } + else if (!_adfModeSwitchDirectionUp) + { + command = ADF_MODE_DEC; } } - else if (_currentUpperRadioMode == CurrentKa50RadioMode.DATALINK && radioPanelKnob.IsOn) - { - DCSBIOS.Send(DATALINK_POWER_ON_OFF_COMMAND_TOGGLE); - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsKa50.UPPER_FREQ_SWITCH); - } - break; + + await DCSBIOS.SendAsync(command); + } + else if (_currentUpperRadioMode == CurrentKa50RadioMode.DATALINK && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(DATALINK_POWER_ON_OFF_COMMAND_TOGGLE); + } + else + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsKa50.UPPER_FREQ_SWITCH); } + break; + } - case RadioPanelPZ69KnobsKa50.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsKa50.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentKa50RadioMode.VHF1_R828) { - if (_currentLowerRadioMode == CurrentKa50RadioMode.VHF1_R828) - { - DCSBIOS.Send(radioPanelKnob.IsOn ? VHF1_TUNER_BUTTON_PRESS : VHF1_TUNER_BUTTON_RELEASE); - } - else if (_currentLowerRadioMode == CurrentKa50RadioMode.ADF_ARK22 && radioPanelKnob.IsOn) + await DCSBIOS.SendAsync(radioPanelKnob.IsOn ? VHF1_TUNER_BUTTON_PRESS : VHF1_TUNER_BUTTON_RELEASE); + } + else if (_currentLowerRadioMode == CurrentKa50RadioMode.ADF_ARK22 && radioPanelKnob.IsOn) + { + var command = string.Empty; + + lock (_lockADFModeDialObject) { - lock (_lockADFModeDialObject) + if (_adfModeSwitchDirectionUp && _adfModeCockpitPos == 2) { - if (_adfModeSwitchDirectionUp && _adfModeCockpitPos == 2) - { - _adfModeSwitchDirectionUp = false; - DCSBIOS.Send(ADF_MODE_DEC); - } - else if (!_adfModeSwitchDirectionUp && _adfModeCockpitPos == 0) - { - _adfModeSwitchDirectionUp = true; - DCSBIOS.Send(ADF_MODE_INC); - } - else if (_adfModeSwitchDirectionUp) - { - DCSBIOS.Send(ADF_MODE_INC); - } - else if (!_adfModeSwitchDirectionUp) - { - DCSBIOS.Send(ADF_MODE_DEC); - } + _adfModeSwitchDirectionUp = false; + command = ADF_MODE_DEC; + } + else if (!_adfModeSwitchDirectionUp && _adfModeCockpitPos == 0) + { + _adfModeSwitchDirectionUp = true; + command = ADF_MODE_INC; + } + else if (_adfModeSwitchDirectionUp) + { + command = ADF_MODE_INC; + } + else if (!_adfModeSwitchDirectionUp) + { + command = ADF_MODE_DEC; } } - else if (_currentLowerRadioMode == CurrentKa50RadioMode.DATALINK && radioPanelKnob.IsOn) - { - DCSBIOS.Send(DATALINK_POWER_ON_OFF_COMMAND_TOGGLE); - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsKa50.LOWER_FREQ_SWITCH); - } - break; + + await DCSBIOS.SendAsync(command); } - } + else if (_currentLowerRadioMode == CurrentKa50RadioMode.DATALINK && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(DATALINK_POWER_ON_OFF_COMMAND_TOGGLE); + } + else + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsKa50.LOWER_FREQ_SWITCH); + } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_KA50, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_KA50, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -927,7 +936,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -949,7 +958,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - _vhf1PresetDialSkipper.Click(VHF1_PRESET_COMMAND_INC); + await _vhf1PresetDialSkipper.ClickAsync(VHF1_PRESET_COMMAND_INC); break; } @@ -976,19 +985,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisLeftDialIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisLeftDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_INC_MORE : ABRIS_LEFT_DIAL_COMMAND_INC); + await DCSBIOS.SendAsync(_abrisLeftDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_INC_MORE : ABRIS_LEFT_DIAL_COMMAND_INC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkMasterModeDialSkipper.Click(DATALINK_MASTER_MODE_COMMAND_INC); + await _datalinkMasterModeDialSkipper.ClickAsync(DATALINK_MASTER_MODE_COMMAND_INC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - _adfPresetDialSkipper.Click(ADF_PRESET_COMMAND_INC); + await _adfPresetDialSkipper.ClickAsync(ADF_PRESET_COMMAND_INC); break; } @@ -1006,7 +1015,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - _vhf1PresetDialSkipper.Click(VHF1_PRESET_COMMAND_DEC); + await _vhf1PresetDialSkipper.ClickAsync(VHF1_PRESET_COMMAND_DEC); break; } @@ -1033,19 +1042,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisLeftDialDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisLeftDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_DEC_MORE : ABRIS_LEFT_DIAL_COMMAND_DEC); + await DCSBIOS.SendAsync(_abrisLeftDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_DEC_MORE : ABRIS_LEFT_DIAL_COMMAND_DEC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkMasterModeDialSkipper.Click(DATALINK_MASTER_MODE_COMMAND_DEC); + await _datalinkMasterModeDialSkipper.ClickAsync(DATALINK_MASTER_MODE_COMMAND_DEC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - _adfPresetDialSkipper.Click(ADF_PRESET_COMMAND_DEC); + await _adfPresetDialSkipper.ClickAsync(ADF_PRESET_COMMAND_DEC); break; } @@ -1063,7 +1072,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); break; } @@ -1082,19 +1091,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisRightDialIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisRightDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_INC_MORE : ABRIS_RIGHT_DIAL_COMMAND_INC); + await DCSBIOS.SendAsync(_abrisRightDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_INC_MORE : ABRIS_RIGHT_DIAL_COMMAND_INC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkSelfIdDialSkipper.Click(DATALINK_SELF_ID_COMMAND_INC); + await _datalinkSelfIdDialSkipper.ClickAsync(DATALINK_SELF_ID_COMMAND_INC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - DCSBIOS.Send(ADF_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_VOLUME_KNOB_COMMAND_INC); break; } @@ -1112,7 +1121,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1131,19 +1140,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisRightDialDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisRightDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_DEC_MORE : ABRIS_RIGHT_DIAL_COMMAND_DEC); + await DCSBIOS.SendAsync(_abrisRightDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_DEC_MORE : ABRIS_RIGHT_DIAL_COMMAND_DEC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkSelfIdDialSkipper.Click(DATALINK_SELF_ID_COMMAND_DEC); + await _datalinkSelfIdDialSkipper.ClickAsync(DATALINK_SELF_ID_COMMAND_DEC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - DCSBIOS.Send(ADF_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1161,7 +1170,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - _vhf1PresetDialSkipper.Click(VHF1_PRESET_COMMAND_INC); + await _vhf1PresetDialSkipper.ClickAsync(VHF1_PRESET_COMMAND_INC); break; } @@ -1202,19 +1211,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisLeftDialIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisLeftDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_INC_MORE : ABRIS_LEFT_DIAL_COMMAND_INC); + await DCSBIOS.SendAsync(_abrisLeftDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_INC_MORE : ABRIS_LEFT_DIAL_COMMAND_INC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkMasterModeDialSkipper.Click(DATALINK_MASTER_MODE_COMMAND_INC); + await _datalinkMasterModeDialSkipper.ClickAsync(DATALINK_MASTER_MODE_COMMAND_INC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - _adfPresetDialSkipper.Click(ADF_PRESET_COMMAND_INC); + await _adfPresetDialSkipper.ClickAsync(ADF_PRESET_COMMAND_INC); break; } @@ -1232,7 +1241,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - _vhf1PresetDialSkipper.Click(VHF1_PRESET_COMMAND_DEC); + await _vhf1PresetDialSkipper.ClickAsync(VHF1_PRESET_COMMAND_DEC); break; } @@ -1273,19 +1282,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisLeftDialDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisLeftDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_DEC_MORE : ABRIS_LEFT_DIAL_COMMAND_DEC); + await DCSBIOS.SendAsync(_abrisLeftDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_LEFT_DIAL_COMMAND_DEC_MORE : ABRIS_LEFT_DIAL_COMMAND_DEC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkMasterModeDialSkipper.Click(DATALINK_MASTER_MODE_COMMAND_DEC); + await _datalinkMasterModeDialSkipper.ClickAsync(DATALINK_MASTER_MODE_COMMAND_DEC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - _adfPresetDialSkipper.Click(ADF_PRESET_COMMAND_DEC); + await _adfPresetDialSkipper.ClickAsync(ADF_PRESET_COMMAND_DEC); break; } @@ -1303,7 +1312,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); break; } @@ -1323,19 +1332,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisRightDialIncreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisRightDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_INC_MORE : ABRIS_RIGHT_DIAL_COMMAND_INC); + await DCSBIOS.SendAsync(_abrisRightDialIncreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_INC_MORE : ABRIS_RIGHT_DIAL_COMMAND_INC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkSelfIdDialSkipper.Click(DATALINK_SELF_ID_COMMAND_INC); + await _datalinkSelfIdDialSkipper.ClickAsync(DATALINK_SELF_ID_COMMAND_INC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - DCSBIOS.Send(ADF_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_VOLUME_KNOB_COMMAND_INC); break; } @@ -1353,7 +1362,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentKa50RadioMode.VHF1_R828: { - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1372,19 +1381,19 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentKa50RadioMode.ABRIS: { _abrisRightDialDecreaseChangeMonitor.Click(); - DCSBIOS.Send(_abrisRightDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_DEC_MORE : ABRIS_RIGHT_DIAL_COMMAND_DEC); + await DCSBIOS.SendAsync(_abrisRightDialDecreaseChangeMonitor.ClickThresholdReached() ? ABRIS_RIGHT_DIAL_COMMAND_DEC_MORE : ABRIS_RIGHT_DIAL_COMMAND_DEC); break; } case CurrentKa50RadioMode.DATALINK: { - _datalinkSelfIdDialSkipper.Click(DATALINK_SELF_ID_COMMAND_DEC); + await _datalinkSelfIdDialSkipper.ClickAsync(DATALINK_SELF_ID_COMMAND_DEC); break; } case CurrentKa50RadioMode.ADF_ARK22: { - DCSBIOS.Send(ADF_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1441,7 +1450,7 @@ private void CheckFrequenciesForValidity() Logger.Error(ex); } } - + private void ShowFrequenciesOnPanel() { try @@ -1715,7 +1724,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - + public override void ClearSettings(bool setIsDirty = false) { } @@ -1757,11 +1766,6 @@ private void SetLowerRadioMode(CurrentKa50RadioMode currentKa50RadioMode) } } - private bool R800L1NowSyncing() - { - return Interlocked.Read(ref _r800L1ThreadNowSynching) > 0; - } - private void SaveCockpitFrequencyR800L1() { try @@ -2256,7 +2260,7 @@ private static string GetR800L1DialFrequencyForPosition(uint position) return string.Empty; } - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff){ } + public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) { } public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) { } public override void AddOrUpdateDCSBIOSBinding(PanelSwitchOnOff panelSwitchOnOff, List dcsbiosInputs, string description, bool isSequenced) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69M2000C.cs b/src/NonVisuals/Radios/RadioPanelPZ69M2000C.cs index acb28d792..10412ab48 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69M2000C.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69M2000C.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -117,7 +118,7 @@ private enum CurrentM2000CRadioMode private bool _lowerFreqSwitchPressedDown; public RadioPanelPZ69M2000C(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -292,7 +293,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) { lock (_lockVoRialObject) { - _vorDecimalsCockpitDialPos = _vorDcsbiosOutputDialDecimals.LastUIntValue; ; + _vorDecimalsCockpitDialPos = _vorDcsbiosOutputDialDecimals.LastUIntValue; Interlocked.Increment(ref _doUpdatePanelLCD); } } @@ -337,7 +338,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { @@ -475,10 +476,10 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_M2000C, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } } - - AdjustFrequency(hashSet); - ShowFrequenciesOnPanel(); } + + await AdjustFrequencyAsync(hashSet); + ShowFrequenciesOnPanel(); } catch (Exception ex) { @@ -486,7 +487,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -509,25 +510,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.VUHF: { - _vhfPresetDialSkipper.Click(VHF_PRESET_COMMAND_INC); + await _vhfPresetDialSkipper.ClickAsync(VHF_PRESET_COMMAND_INC); break; } case CurrentM2000CRadioMode.UHF: { - _uhfPresetDialSkipper.Click(UHF_PRESET_COMMAND_INC); + await _uhfPresetDialSkipper.ClickAsync(UHF_PRESET_COMMAND_INC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_upperFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_INC : TACAN_TENS_COMMAND_INC); + await _tacanDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_INC : TACAN_TENS_COMMAND_INC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_upperFreqSwitchPressedDown ? VOR_POWER_COMMAND_INC : VOR_DECIMALS_COMMAND_INC); + await _vorDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? VOR_POWER_COMMAND_INC : VOR_DECIMALS_COMMAND_INC); break; } @@ -545,25 +546,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.VUHF: { - _vhfPresetDialSkipper.Click(VHF_PRESET_COMMAND_DEC); + await _vhfPresetDialSkipper.ClickAsync(VHF_PRESET_COMMAND_DEC); break; } case CurrentM2000CRadioMode.UHF: { - _uhfPresetDialSkipper.Click(UHF_PRESET_COMMAND_DEC); + await _uhfPresetDialSkipper.ClickAsync(UHF_PRESET_COMMAND_DEC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_upperFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_DEC : TACAN_TENS_COMMAND_DEC); + await _tacanDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_DEC : TACAN_TENS_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_upperFreqSwitchPressedDown ? VOR_POWER_COMMAND_DEC : VOR_DECIMALS_COMMAND_DEC); + await _vorDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? VOR_POWER_COMMAND_DEC : VOR_DECIMALS_COMMAND_DEC); break; } } @@ -576,25 +577,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.VUHF: { - DCSBIOS.Send(VHF_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(VHF_VOLUME_COMMAND_INC); break; } case CurrentM2000CRadioMode.UHF: { - DCSBIOS.Send(UHF_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(UHF_VOLUME_COMMAND_INC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_upperFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_INC : TACAN_ONES_COMMAND_INC); + await _tacanDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_INC : TACAN_ONES_COMMAND_INC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_upperFreqSwitchPressedDown ? VOR_TEST_COMMAND_INC : VOR_ONES_COMMAND_INC); + await _vorDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? VOR_TEST_COMMAND_INC : VOR_ONES_COMMAND_INC); break; } @@ -612,25 +613,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.UHF: { - DCSBIOS.Send(UHF_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(UHF_VOLUME_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VUHF: { - DCSBIOS.Send(VHF_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF_VOLUME_COMMAND_DEC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_upperFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_DEC : TACAN_ONES_COMMAND_DEC); + await _tacanDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_DEC : TACAN_ONES_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_upperFreqSwitchPressedDown ? VOR_TEST_COMMAND_DEC : VOR_ONES_COMMAND_DEC); + await _vorDialSkipper.ClickAsync(_upperFreqSwitchPressedDown ? VOR_TEST_COMMAND_DEC : VOR_ONES_COMMAND_DEC); break; } @@ -648,25 +649,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.VUHF: { - _vhfPresetDialSkipper.Click(VHF_PRESET_COMMAND_INC); + await _vhfPresetDialSkipper.ClickAsync(VHF_PRESET_COMMAND_INC); break; } case CurrentM2000CRadioMode.UHF: { - _uhfPresetDialSkipper.Click(UHF_PRESET_COMMAND_INC); + await _uhfPresetDialSkipper.ClickAsync(UHF_PRESET_COMMAND_INC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_lowerFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_INC : TACAN_TENS_COMMAND_INC); + await _tacanDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_INC : TACAN_TENS_COMMAND_INC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_lowerFreqSwitchPressedDown ? VOR_POWER_COMMAND_INC : VOR_DECIMALS_COMMAND_INC); + await _vorDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? VOR_POWER_COMMAND_INC : VOR_DECIMALS_COMMAND_INC); break; } @@ -684,25 +685,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.VUHF: { - _vhfPresetDialSkipper.Click(VHF_PRESET_COMMAND_DEC); + await _vhfPresetDialSkipper.ClickAsync(VHF_PRESET_COMMAND_DEC); break; } case CurrentM2000CRadioMode.UHF: { - _uhfPresetDialSkipper.Click(UHF_PRESET_COMMAND_DEC); + await _uhfPresetDialSkipper.ClickAsync(UHF_PRESET_COMMAND_DEC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_lowerFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_DEC : TACAN_TENS_COMMAND_DEC); + await _tacanDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? TACANXY_SELECT_COMMAND_DEC : TACAN_TENS_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_lowerFreqSwitchPressedDown ? VOR_POWER_COMMAND_DEC : VOR_DECIMALS_COMMAND_DEC); + await _vorDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? VOR_POWER_COMMAND_DEC : VOR_DECIMALS_COMMAND_DEC); break; } @@ -720,25 +721,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.UHF: { - DCSBIOS.Send(UHF_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(UHF_VOLUME_COMMAND_INC); break; } case CurrentM2000CRadioMode.VUHF: { - DCSBIOS.Send(VHF_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(VHF_VOLUME_COMMAND_INC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_lowerFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_INC : TACAN_ONES_COMMAND_INC); + await _tacanDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_INC : TACAN_ONES_COMMAND_INC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_lowerFreqSwitchPressedDown ? VOR_TEST_COMMAND_INC : VOR_ONES_COMMAND_INC); + await _vorDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? VOR_TEST_COMMAND_INC : VOR_ONES_COMMAND_INC); break; } @@ -756,25 +757,25 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentM2000CRadioMode.UHF: { - DCSBIOS.Send(UHF_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(UHF_VOLUME_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VUHF: { - DCSBIOS.Send(VHF_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF_VOLUME_COMMAND_DEC); break; } case CurrentM2000CRadioMode.TACAN: { - _tacanDialSkipper.Click(_lowerFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_DEC : TACAN_ONES_COMMAND_DEC); + await _tacanDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? TACAN_MODE_SELECT_COMMAND_DEC : TACAN_ONES_COMMAND_DEC); break; } case CurrentM2000CRadioMode.VOR: { - _vorDialSkipper.Click(_lowerFreqSwitchPressedDown ? VOR_TEST_COMMAND_DEC : VOR_ONES_COMMAND_DEC); + await _vorDialSkipper.ClickAsync(_lowerFreqSwitchPressedDown ? VOR_TEST_COMMAND_DEC : VOR_ONES_COMMAND_DEC); break; } @@ -1043,7 +1044,7 @@ private void SetLowerRadioMode(CurrentM2000CRadioMode currentM2000CRadioMode) Logger.Error(ex); } } - + public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) { } public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Mi24P.cs b/src/NonVisuals/Radios/RadioPanelPZ69Mi24P.cs index db952e9f2..bc828f6fa 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Mi24P.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Mi24P.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -78,8 +79,8 @@ private enum CurrentMi24PRadioMode private const string YADRO1_A_FREQ_2DIAL_COMMAND = "PLT_JADRO_100K "; private const string YADRO1_A_FREQ_3DIAL_COMMAND = "PLT_JADRO_10K "; private const string YADRO1_A_FREQ_4DIAL_COMMAND = "PLT_JADRO_1K "; - private Thread _yadro1ASyncThread; - private long _yadro1AThreadNowSynching; + private Task _yadro1ASyncTask; + private CancellationTokenSource _yadro1ASyncTokenSource = new(); private long _yadro1ADial1WaitingForFeedback; private long _yadro1ADial2WaitingForFeedback; private long _yadro1ADial3WaitingForFeedback; @@ -174,7 +175,7 @@ private enum CurrentMi24PRadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69Mi24P(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -185,7 +186,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownYaDRO1AThread = true; + _yadro1ASyncTokenSource.Cancel(); BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -433,7 +434,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi24P knob) + private async Task SendFrequencyToDCSBIOSAsync(bool knobIsOn, RadioPanelPZ69KnobsMi24P knob) { try { @@ -476,7 +477,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi24P knob { if (knobIsOn) { - DCSBIOS.Send(SPU8_ICS_SWITCH_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(SPU8_ICS_SWITCH_TOGGLE_COMMAND); } break; } @@ -508,7 +509,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi24P knob { if (knobIsOn) { - DCSBIOS.Send(SPU8_ICS_SWITCH_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(SPU8_ICS_SWITCH_TOGGLE_COMMAND); } break; } @@ -527,17 +528,15 @@ private void SendYaDRO1AToDCSBIOS() { try { - if (YaDRO1ANowSyncing()) + if (_yadro1ASyncTask?.Status == TaskStatus.Running) { return; } + SaveCockpitFrequencyYaDRO1A(); - _shutdownYaDRO1AThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownYaDRO1AThread = false; - _yadro1ASyncThread = new Thread(() => YaDRO1ASynchThreadMethod()); - _yadro1ASyncThread.Start(); + _yadro1ASyncTokenSource = new(); + _yadro1ASyncTask = Task.Run(() => SyncYaDRO1Async(_yadro1ASyncTokenSource.Token)); } catch (Exception ex) { @@ -545,179 +544,186 @@ private void SendYaDRO1AToDCSBIOS() } } - private volatile bool _shutdownYaDRO1AThread; - private void YaDRO1ASynchThreadMethod() + private async Task SyncYaDRO1Async(CancellationToken cancellationToken) { try { - try + // Mi-24P YaDRO-1A + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + + var frequencyAsString = _yadro1ABigFrequencyStandby.ToString().PadLeft(3, '0') + _yadro1ASmallFrequencyStandby.ToString().PadLeft(2, '0'); + frequencyAsString = frequencyAsString.PadRight(6, '0'); + //Frequency selector 1 YADRO1A_FREQ1 + // "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" + //Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + + //Frequency selector 2 YADRO1A_FREQ2 + //0 1 2 3 4 5 6 7 8 9 + + //Frequency selector 3 YADRO1A_FREQ3 + //0 1 2 3 4 5 6 7 8 9 + + //Frequency selector 4 YADRO1A_FREQ4 + //0 1 2 3 4 5 6 7 8 9 + + //Reason for this is to separate the standby frequency from the sync loop + //If not the sync would pick up any changes made by the user during the + //sync process + + //02000 + //17999 + //#1 = 17 (position = value) + //#2 = 9 (position = value) + //#3 = 9 (position = value) + //#4 = 9 (position = value) + var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); + var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); + var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(3, 1)); + var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(4, 1)); + + do { - try + if (IsTimedOut(ref dial1Timeout)) { - // Mi-24P YaDRO-1A - Interlocked.Exchange(ref _yadro1AThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - - var frequencyAsString = _yadro1ABigFrequencyStandby.ToString().PadLeft(3, '0') + _yadro1ASmallFrequencyStandby.ToString().PadLeft(2, '0'); - frequencyAsString = frequencyAsString.PadRight(6, '0'); - //Frequency selector 1 YADRO1A_FREQ1 - // "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" - //Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - - //Frequency selector 2 YADRO1A_FREQ2 - //0 1 2 3 4 5 6 7 8 9 - - //Frequency selector 3 YADRO1A_FREQ3 - //0 1 2 3 4 5 6 7 8 9 - - //Frequency selector 4 YADRO1A_FREQ4 - //0 1 2 3 4 5 6 7 8 9 - - //Reason for this is to separate the standby frequency from the sync loop - //If not the sync would pick up any changes made by the user during the - //sync process - - //02000 - //17999 - //#1 = 17 (position = value) - //#2 = 9 (position = value) - //#3 = 9 (position = value) - //#4 = 9 (position = value) - var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); - var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); - var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(3, 1)); - var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(4, 1)); - - do - { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _yadro1ADial1WaitingForFeedback); // Lets do an ugly reset - } - if (IsTimedOut(ref dial2Timeout)) - { - //Lets do an ugly reset - ResetWaitingForFeedBack(ref _yadro1ADial2WaitingForFeedback); // Lets do an ugly reset - } - if (IsTimedOut(ref dial3Timeout)) - { - //Lets do an ugly reset - ResetWaitingForFeedBack(ref _yadro1ADial3WaitingForFeedback); // Lets do an ugly reset - } - if (IsTimedOut(ref dial4Timeout)) - { - //Lets do an ugly reset - ResetWaitingForFeedBack(ref _yadro1ADial4WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _yadro1ADial1WaitingForFeedback); // Let's do an ugly reset + } + if (IsTimedOut(ref dial2Timeout)) + { + //Let's do an ugly reset + ResetWaitingForFeedBack(ref _yadro1ADial2WaitingForFeedback); // Let's do an ugly reset + } + if (IsTimedOut(ref dial3Timeout)) + { + //Let's do an ugly reset + ResetWaitingForFeedBack(ref _yadro1ADial3WaitingForFeedback); // Let's do an ugly reset + } + if (IsTimedOut(ref dial4Timeout)) + { + //Let's do an ugly reset + ResetWaitingForFeedBack(ref _yadro1ADial4WaitingForFeedback); // Let's do an ugly reset + } + + if (Interlocked.Read(ref _yadro1ADial1WaitingForFeedback) == 0) + { + var command = string.Empty; - string str; - if (Interlocked.Read(ref _yadro1ADial1WaitingForFeedback) == 0) + lock (_lockYadro1ADialsObject1) + { + if (_yadro1ACockpitFreq1DialPos != desiredPositionDial1X) { - lock (_lockYadro1ADialsObject1) + dial1OkTime = DateTime.Now.Ticks; + if (_yadro1ACockpitFreq1DialPos < desiredPositionDial1X) { - if (_yadro1ACockpitFreq1DialPos != desiredPositionDial1X) - { - dial1OkTime = DateTime.Now.Ticks; - if (_yadro1ACockpitFreq1DialPos < desiredPositionDial1X) - { - str = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - } - else - { - str = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - } - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + command = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } - } - else - { - dial1OkTime = DateTime.Now.Ticks; - } - if (Interlocked.Read(ref _yadro1ADial2WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject2) + else { - if (_yadro1ACockpitFreq2DialPos != desiredPositionDial2X) - { - dial2OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _yadro1ACockpitFreq2DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial1WaitingForFeedback, 1); + } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + if (Interlocked.Read(ref _yadro1ADial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject2) + { + if (_yadro1ACockpitFreq2DialPos != desiredPositionDial2X) { dial2OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _yadro1ACockpitFreq2DialPos); } - if (Interlocked.Read(ref _yadro1ADial3WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject3) - { - if (_yadro1ACockpitFreq3DialPos != desiredPositionDial3X) - { - dial3OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _yadro1ACockpitFreq3DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); - } - } - else + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial2WaitingForFeedback, 1); + } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + if (Interlocked.Read(ref _yadro1ADial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject3) + { + if (_yadro1ACockpitFreq3DialPos != desiredPositionDial3X) { dial3OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _yadro1ACockpitFreq3DialPos); } - if (Interlocked.Read(ref _yadro1ADial4WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject4) - { - if (_yadro1ACockpitFreq4DialPos != desiredPositionDial4X) - { - dial4OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_4DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial4X, _yadro1ACockpitFreq4DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); - } - } - else + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial3WaitingForFeedback, 1); + } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + if (Interlocked.Read(ref _yadro1ADial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject4) + { + if (_yadro1ACockpitFreq4DialPos != desiredPositionDial4X) { dial4OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_4DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial4X, _yadro1ACockpitFreq4DialPos); } - Thread.Sleep(SynchSleepTime); //Should be enough to get an update cycle from DCS-BIOS } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownYaDRO1AThread); - SwapCockpitStandbyFrequencyYaDRO1A(); - ShowFrequenciesOnPanel(); + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial4WaitingForFeedback, 1); + } + Reset(ref dial4Timeout); } - catch (ThreadAbortException) - { } - catch (Exception ex) + else { - Common.ShowErrorMessageBox(ex); + dial4OkTime = DateTime.Now.Ticks; } + Thread.Sleep(SynchSleepTime); //Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } - finally - { - Interlocked.Exchange(ref _yadro1AThreadNowSynching, 0); - } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + SwapCockpitStandbyFrequencyYaDRO1A(); + ShowFrequenciesOnPanel(); } catch (Exception ex) { - Logger.Error(ex); + Common.ShowErrorMessageBox(ex); } //Refresh panel once this debacle is finished Interlocked.Increment(ref _doUpdatePanelLCD); @@ -736,131 +742,130 @@ private void SwapCockpitStandbyFrequencyYaDRO1A() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) + var radioPanelKnob = (RadioPanelKnobMi24P)radioPanelKnobObject; + + switch (radioPanelKnob.RadioPanelPZ69Knob) { - var radioPanelKnob = (RadioPanelKnobMi24P)radioPanelKnobObject; + case RadioPanelPZ69KnobsMi24P.UPPER_R863_MANUAL: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_MANUAL); + break; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsMi24P.UPPER_R863_MANUAL: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_MANUAL); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_R863_PRESET: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_PRESET); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_R863_PRESET: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_PRESET); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_YADRO1A: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.YADRO1A); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_YADRO1A: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.YADRO1A); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_R828: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R828_PRESETS); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_R828: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R828_PRESETS); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_ADF_ARK15: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.ADF_ARK15_HIGH); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_ADF_ARK15: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.ADF_ARK15_HIGH); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_SPU8: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.SPU8); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_SPU8: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.SPU8); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_R863_MANUAL: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_MANUAL); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_R863_MANUAL: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_MANUAL); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_R863_PRESET: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_PRESET); + break; + case RadioPanelPZ69KnobsMi24P.LOWER_YADRO1A: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.YADRO1A); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_R863_PRESET: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R863_PRESET); - break; - case RadioPanelPZ69KnobsMi24P.LOWER_YADRO1A: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.YADRO1A); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_R828: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R828_PRESETS); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_R828: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.R828_PRESETS); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_ADF_ARK15: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.ADF_ARK15_HIGH); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_ADF_ARK15: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.ADF_ARK15_HIGH); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_SPU8: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.SPU8); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_SPU8: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.SPU8); - break; + case RadioPanelPZ69KnobsMi24P.UPPER_ARK_UD: + SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.DME_ARK15_LOW); + break; - case RadioPanelPZ69KnobsMi24P.UPPER_ARK_UD: - SetUpperRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.DME_ARK15_LOW); - break; + case RadioPanelPZ69KnobsMi24P.LOWER_ARK_UD: + SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.DME_ARK15_LOW); + break; - case RadioPanelPZ69KnobsMi24P.LOWER_ARK_UD: - SetLowerRadioMode(radioPanelKnob.IsOn, CurrentMi24PRadioMode.DME_ARK15_LOW); + case RadioPanelPZ69KnobsMi24P.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi24P.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi24P.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi24P.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi24P.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi24P.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi24P.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi24P.LOWER_SMALL_FREQ_WHEEL_DEC: + { + //Ignore break; - - case RadioPanelPZ69KnobsMi24P.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi24P.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi24P.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi24P.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi24P.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi24P.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi24P.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi24P.LOWER_SMALL_FREQ_WHEEL_DEC: + } + case RadioPanelPZ69KnobsMi24P.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentMi24PRadioMode.R863_PRESET) { - //Ignore - break; } - case RadioPanelPZ69KnobsMi24P.UPPER_FREQ_SWITCH: + else if (_currentUpperRadioMode == CurrentMi24PRadioMode.ADF_ARK15_HIGH && radioPanelKnob.IsOn) { - if (_currentUpperRadioMode == CurrentMi24PRadioMode.R863_PRESET) - { - } - else if (_currentUpperRadioMode == CurrentMi24PRadioMode.ADF_ARK15_HIGH && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); - } - else if (_currentUpperRadioMode == CurrentMi24PRadioMode.DME_ARK15_LOW && radioPanelKnob.IsOn) - { - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi24P.UPPER_FREQ_SWITCH); - } - break; + await DCSBIOS.SendAsync(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); } - case RadioPanelPZ69KnobsMi24P.LOWER_FREQ_SWITCH: + else if (_currentUpperRadioMode == CurrentMi24PRadioMode.DME_ARK15_LOW && radioPanelKnob.IsOn) { - if (_currentLowerRadioMode == CurrentMi24PRadioMode.R863_PRESET) - { - } - else if (_currentLowerRadioMode == CurrentMi24PRadioMode.ADF_ARK15_HIGH && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); - } - else if (_currentLowerRadioMode == CurrentMi24PRadioMode.DME_ARK15_LOW && radioPanelKnob.IsOn) - { - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi24P.LOWER_FREQ_SWITCH); - } - break; } - } + else + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi24P.UPPER_FREQ_SWITCH); + } + break; + } + case RadioPanelPZ69KnobsMi24P.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentMi24PRadioMode.R863_PRESET) + { + } + else if (_currentLowerRadioMode == CurrentMi24PRadioMode.ADF_ARK15_HIGH && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); + } + else if (_currentLowerRadioMode == CurrentMi24PRadioMode.DME_ARK15_LOW && radioPanelKnob.IsOn) + { + } + else + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi24P.LOWER_FREQ_SWITCH); + } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MI24P, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MI24P, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -934,7 +939,7 @@ private void YADRO1A_BigFrequencyDecrease() _yadro1ABigFrequencyStandby--; } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -962,7 +967,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - _r863PresetDialSkipper.Click(R863_PRESET_COMMAND_INC); + await _r863PresetDialSkipper.ClickAsync(R863_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -972,22 +977,22 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - _r828PresetDialSkipper.Click(R828_PRESET_COMMAND_INC); + await _r828PresetDialSkipper.ClickAsync(R828_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); + await _adfPresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: { - _dmePresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_INC : DME_BACKUP1_KHZ_PRESET_COMMAND_INC); + await _dmePresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_INC : DME_BACKUP1_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.SPU8: { - _spu8DialSkipper.Click(SPU8_COMMAND_INC); + await _spu8DialSkipper.ClickAsync(SPU8_COMMAND_INC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1007,7 +1012,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - _r863PresetDialSkipper.Click(R863_PRESET_COMMAND_DEC); + await _r863PresetDialSkipper.ClickAsync(R863_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1017,22 +1022,22 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - _r828PresetDialSkipper.Click(R828_PRESET_COMMAND_DEC); + await _r828PresetDialSkipper.ClickAsync(R828_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); + await _adfPresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: { - _dmePresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_DEC : DME_BACKUP1_KHZ_PRESET_COMMAND_DEC); + await _dmePresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_DEC : DME_BACKUP1_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.SPU8: { - _spu8DialSkipper.Click(SPU8_COMMAND_DEC); + await _spu8DialSkipper.ClickAsync(SPU8_COMMAND_DEC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1052,7 +1057,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1062,12 +1067,12 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial2Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); + await _adfPresetDial2Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: @@ -1076,7 +1081,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.SPU8: { - DCSBIOS.Send(SPU8_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(SPU8_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1096,7 +1101,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1106,12 +1111,12 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial2Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); + await _adfPresetDial2Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: @@ -1120,7 +1125,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.SPU8: { - DCSBIOS.Send(SPU8_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU8_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1140,7 +1145,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - _r863PresetDialSkipper.Click(R863_PRESET_COMMAND_INC); + await _r863PresetDialSkipper.ClickAsync(R863_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1150,22 +1155,22 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - _r828PresetDialSkipper.Click(R828_PRESET_COMMAND_INC); + await _r828PresetDialSkipper.ClickAsync(R828_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); + await _adfPresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: { - _dmePresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_INC : DME_BACKUP1_KHZ_PRESET_COMMAND_INC); + await _dmePresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_INC : DME_BACKUP1_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.SPU8: { - _spu8DialSkipper.Click(SPU8_COMMAND_INC); + await _spu8DialSkipper.ClickAsync(SPU8_COMMAND_INC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1185,7 +1190,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - _r863PresetDialSkipper.Click(R863_PRESET_COMMAND_DEC); + await _r863PresetDialSkipper.ClickAsync(R863_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1195,22 +1200,22 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - _r828PresetDialSkipper.Click(R828_PRESET_COMMAND_DEC); + await _r828PresetDialSkipper.ClickAsync(R828_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); + await _adfPresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: { - _dmePresetDial1Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_DEC : DME_BACKUP1_KHZ_PRESET_COMMAND_DEC); + await _dmePresetDial1Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? DME_MAIN1_KHZ_PRESET_COMMAND_DEC : DME_BACKUP1_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.SPU8: { - _spu8DialSkipper.Click(SPU8_COMMAND_DEC); + await _spu8DialSkipper.ClickAsync(SPU8_COMMAND_DEC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1230,7 +1235,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1240,12 +1245,12 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial2Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); + await _adfPresetDial2Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: @@ -1254,7 +1259,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.SPU8: { - DCSBIOS.Send(SPU8_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(SPU8_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1274,7 +1279,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.YADRO1A: @@ -1284,12 +1289,12 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.ADF_ARK15_HIGH: { - _adfPresetDial2Skipper.Click(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); + await _adfPresetDial2Skipper.ClickAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi24PRadioMode.DME_ARK15_LOW: @@ -1298,7 +1303,7 @@ private void AdjustFrequency(IEnumerable hashSet) } case CurrentMi24PRadioMode.SPU8: { - DCSBIOS.Send(SPU8_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU8_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi24PRadioMode.NO_USE: @@ -1692,11 +1697,6 @@ private void SetLowerRadioMode(bool radioKnobIsOn, CurrentMi24PRadioMode Current } } - private bool YaDRO1ANowSyncing() - { - return Interlocked.Read(ref _yadro1AThreadNowSynching) > 0; - } - private void SaveCockpitFrequencyYaDRO1A() { try diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Mi8.cs b/src/NonVisuals/Radios/RadioPanelPZ69Mi8.cs index b2dc2eae5..cf0f0f169 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Mi8.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Mi8.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -74,8 +75,8 @@ private enum CurrentMi8RadioMode private const string R863_MANUAL_FREQ_2DIAL_COMMAND = "R863_FREQ2 "; private const string R863_MANUAL_FREQ_3DIAL_COMMAND = "R863_FREQ3 "; private const string R863_MANUAL_FREQ_4DIAL_COMMAND = "R863_FREQ4 "; - private Thread _r863ManualSyncThread; - private long _r863ManualThreadNowSynching; + private Task _r863ManualSyncTask; + private CancellationTokenSource _r863ManualSyncTaskTokenSource = new(); private long _r863ManualDial1WaitingForFeedback; private long _r863ManualDial2WaitingForFeedback; private long _r863ManualDial3WaitingForFeedback; @@ -92,7 +93,7 @@ private enum CurrentMi8RadioMode private volatile uint _r863PresetCockpitDialPos = 1; private const string R863_PRESET_COMMAND_INC = "R863_CNL_SEL INC\n"; private const string R863_PRESET_COMMAND_DEC = "R863_CNL_SEL DEC\n"; - + private const string R863_PRESET_VOLUME_KNOB_COMMAND_INC = "R863_VOL +2500\n"; private const string R863_PRESET_VOLUME_KNOB_COMMAND_DEC = "R863_VOL -2500\n"; private readonly object _lockR863UnitSwitchObject = new(); @@ -125,13 +126,12 @@ private enum CurrentMi8RadioMode private const string YADRO1_A_FREQ_2DIAL_COMMAND = "YADRO1A_FREQ2 "; private const string YADRO1_A_FREQ_3DIAL_COMMAND = "YADRO1A_FREQ3 "; private const string YADRO1_A_FREQ_4DIAL_COMMAND = "YADRO1A_FREQ4 "; - private Thread _yadro1ASyncThread; - private long _yadro1AThreadNowSynching; + private Task _yadro1ASyncTask; + private CancellationTokenSource _yadro1ASyncTaskTokenSource = new(); private long _yadro1ADial1WaitingForFeedback; private long _yadro1ADial2WaitingForFeedback; private long _yadro1ADial3WaitingForFeedback; private long _yadro1ADial4WaitingForFeedback; - private volatile bool _shutdownYaDRO1AThread; /* * Mi-8 R-828 FM Radio PRESETS NAV2 @@ -204,7 +204,7 @@ private enum CurrentMi8RadioMode private volatile uint _arkUdModeCockpitDial1Pos; private const string ARKUD_MODE_COMMAND_INC = "ARCUD_MODE INC\n"; private const string ARKUD_MODE_COMMAND_DEC = "ARCUD_MODE DEC\n"; - + private readonly object _lockArkudVhfUhfModeDialObject = new(); private DCSBIOSOutput _arkUdVhfUhfModeDcsbiosOutputDial; private volatile uint _arkUdVhfUhfModeCockpitDial1Pos; @@ -232,7 +232,7 @@ Small dial volume control private long _doUpdatePanelLCD; public RadioPanelPZ69Mi8(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -243,8 +243,8 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownR863Thread = true; - _shutdownYaDRO1AThread = true; + _r863ManualSyncTaskTokenSource.Cancel(); + _yadro1ASyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -590,7 +590,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) + private async Task SendFrequencyToDCSBIOSAsync(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) { try { @@ -633,7 +633,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(knobIsOn ? R828_GAIN_CONTROL_COMMAND_ON : R828_GAIN_CONTROL_COMMAND_OFF); + await DCSBIOS.SendAsync(knobIsOn ? R828_GAIN_CONTROL_COMMAND_ON : R828_GAIN_CONTROL_COMMAND_OFF); break; } @@ -646,7 +646,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) { if (knobIsOn) { - DCSBIOS.Send(SPU7_ICS_SWITCH_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(SPU7_ICS_SWITCH_TOGGLE_COMMAND); } break; } @@ -677,7 +677,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(knobIsOn ? R828_GAIN_CONTROL_COMMAND_ON : R828_GAIN_CONTROL_COMMAND_OFF); + await DCSBIOS.SendAsync(knobIsOn ? R828_GAIN_CONTROL_COMMAND_ON : R828_GAIN_CONTROL_COMMAND_OFF); break; } @@ -690,7 +690,7 @@ private void SendFrequencyToDCSBIOS(bool knobIsOn, RadioPanelPZ69KnobsMi8 knob) { if (knobIsOn) { - DCSBIOS.Send(SPU7_ICS_SWITCH_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(SPU7_ICS_SWITCH_TOGGLE_COMMAND); } break; } @@ -709,18 +709,15 @@ private void SendR863ManualToDCSBIOS() { try { - if (R863ManualNowSyncing()) + if (_r863ManualSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyR863Manual(); - _shutdownR863Thread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownR863Thread = false; - _r863ManualSyncThread = new Thread(R863ManualSynchThreadMethod); - _r863ManualSyncThread.Start(); + _r863ManualSyncTaskTokenSource = new(); + _r863ManualSyncTask = Task.Run(() => SyncR863ManualAsync(_r863ManualSyncTaskTokenSource.Token)); } catch (Exception ex) { @@ -728,230 +725,236 @@ private void SendR863ManualToDCSBIOS() } } - private volatile bool _shutdownR863Thread; - private void R863ManualSynchThreadMethod() + private async Task SyncR863ManualAsync(CancellationToken cancellationToken) { try { - try + /* + * Mi-8 R-863 COM1 + */ + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + var frequencyAsString = _r863ManualBigFrequencyStandby + "." + _r863ManualSmallFrequencyStandby.ToString().PadLeft(3, '0'); + frequencyAsString = frequencyAsString.PadRight(7, '0'); + + // Frequency selector 1 R863_FREQ1 + // "10" "11" "12" "13" "14" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" + // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 + + // Frequency selector 2 R863_FREQ2 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 3 R863_FREQ3 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 4 R863_FREQ4 + // "00" "25" "50" "75", only "00" and "50" used. + // Pos 0 1 2 3 + + // Reason for this is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + + // 151.95 + // #1 = 15 (position = value - 3) + // #2 = 1 (position = value) + // #3 = 9 (position = value) + // #4 = 5 + var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); // Array.IndexOf(_r863ManualFreq1DialValues, int.Parse(xfrequencyAsString.Substring(0, 2))); + var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); + var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(4, 1)); + var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(5, 2)); + Debug.WriteLine("Frequency " + frequencyAsString); + Debug.WriteLine("Desired1 " + desiredPositionDial1X); + Debug.WriteLine("Desired2 " + desiredPositionDial2X); + Debug.WriteLine("Desired3 " + desiredPositionDial3X); + Debug.WriteLine("Desired4 " + desiredPositionDial4X); + + do { - try + if (IsTimedOut(ref dial1Timeout)) { - /* - * Mi-8 R-863 COM1 - */ - Interlocked.Exchange(ref _r863ManualThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - var frequencyAsString = _r863ManualBigFrequencyStandby + "." + _r863ManualSmallFrequencyStandby.ToString().PadLeft(3, '0'); - frequencyAsString = frequencyAsString.PadRight(7, '0'); - - // Frequency selector 1 R863_FREQ1 - // "10" "11" "12" "13" "14" "22" "23" "24" "25" "26" "27" "28" "29" "30" "31" "32" "33" "34" "35" "36" "37" "38" "39" - // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 - - // Frequency selector 2 R863_FREQ2 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 3 R863_FREQ3 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 4 R863_FREQ4 - // "00" "25" "50" "75", only "00" and "50" used. - // Pos 0 1 2 3 - - // Reason for this is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - - // 151.95 - // #1 = 15 (position = value - 3) - // #2 = 1 (position = value) - // #3 = 9 (position = value) - // #4 = 5 - var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); // Array.IndexOf(_r863ManualFreq1DialValues, int.Parse(xfrequencyAsString.Substring(0, 2))); - var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); - var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(4, 1)); - var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(5, 2)); - Debug.WriteLine("Frequency " + frequencyAsString); - Debug.WriteLine("Desired1 " + desiredPositionDial1X); - Debug.WriteLine("Desired2 " + desiredPositionDial2X); - Debug.WriteLine("Desired3 " + desiredPositionDial3X); - Debug.WriteLine("Desired4 " + desiredPositionDial4X); - - do - { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _r863ManualDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _r863ManualDial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _r863ManualDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _r863ManualDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _r863ManualDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _r863ManualDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _r863ManualDial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _r863ManualDial4WaitingForFeedback); // Let's do an ugly reset + } - string str; - if (Interlocked.Read(ref _r863ManualDial1WaitingForFeedback) == 0) - { - lock (_lockR863ManualDialsObject1) - { - if (_r863ManualCockpitFreq1DialPos != desiredPositionDial1X) - { - dial1OkTime = DateTime.Now.Ticks; - str = R863_MANUAL_FREQ_1DIAL_COMMAND - + GetCommandDirectionForR863ManualDial1(desiredPositionDial1X, _r863ManualCockpitFreq1DialPos);// + Decrease; // is this still a problem? 30.7.2018 Went into loop GetCommandDirectionForR863ManualDial1(desiredPositionDial1X, _r863ManualCockpitFreq1DialPos); - - /* - 25.7.2018 - 10 0.22999967634678 - 39 0.21999971568584 - - 10Mhz Rotary Knob (Mi-8MT/R863_FREQ1) - Changing the dial (in cockpit) 39 => 10 does not show in DCS-BIOS in the CTRL-Ref page. But going down from 10 => 39 is OK. - I have gotten the values above from DCS using the Lua console so I can see that they do actually change but there is something not working in DCS-BIOS - So only go down with it - */ - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _r863ManualDial1WaitingForFeedback, 1); - } + if (Interlocked.Read(ref _r863ManualDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - Reset(ref dial1Timeout); - } - } - else + lock (_lockR863ManualDialsObject1) + { + if (_r863ManualCockpitFreq1DialPos != desiredPositionDial1X) { dial1OkTime = DateTime.Now.Ticks; + command = R863_MANUAL_FREQ_1DIAL_COMMAND + + GetCommandDirectionForR863ManualDial1(desiredPositionDial1X, _r863ManualCockpitFreq1DialPos);// + Decrease; // is this still a problem? 30.7.2018 Went into loop GetCommandDirectionForR863ManualDial1(desiredPositionDial1X, _r863ManualCockpitFreq1DialPos); + + /* + 25.7.2018 + 10 0.22999967634678 + 39 0.21999971568584 + + 10Mhz Rotary Knob (Mi-8MT/R863_FREQ1) + Changing the dial (in cockpit) 39 => 10 does not show in DCS-BIOS in the CTRL-Ref page. But going down from 10 => 39 is OK. + I have gotten the values above from DCS using the Lua console so I can see that they do actually change but there is something not working in DCS-BIOS + So only go down with it + */ } - if (Interlocked.Read(ref _r863ManualDial2WaitingForFeedback) == 0) - { - lock (_lockR863ManualDialsObject2) - { - if (_r863ManualCockpitFreq2DialPos != desiredPositionDial2X) - { - dial2OkTime = DateTime.Now.Ticks; - str = R863_MANUAL_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _r863ManualCockpitFreq2DialPos); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _r863ManualDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); - } - } - else + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _r863ManualDial1WaitingForFeedback, 1); + } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r863ManualDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockR863ManualDialsObject2) + { + if (_r863ManualCockpitFreq2DialPos != desiredPositionDial2X) { dial2OkTime = DateTime.Now.Ticks; + command = R863_MANUAL_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _r863ManualCockpitFreq2DialPos); } + } - if (Interlocked.Read(ref _r863ManualDial3WaitingForFeedback) == 0) - { - lock (_lockR863ManualDialsObject3) - { - if (_r863ManualCockpitFreq3DialPos != desiredPositionDial3X) - { - dial3OkTime = DateTime.Now.Ticks; - str = R863_MANUAL_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _r863ManualCockpitFreq3DialPos); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _r863ManualDial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _r863ManualDial2WaitingForFeedback, 1); + } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r863ManualDial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockR863ManualDialsObject3) + { + if (_r863ManualCockpitFreq3DialPos != desiredPositionDial3X) { dial3OkTime = DateTime.Now.Ticks; + command = R863_MANUAL_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _r863ManualCockpitFreq3DialPos); } + } - if (Interlocked.Read(ref _r863ManualDial4WaitingForFeedback) == 0) - { - lock (_lockR863ManualDialsObject4) - { - if (_r863ManualCockpitFreq4DialPos < desiredPositionDial4X) - { - dial4OkTime = DateTime.Now.Ticks; - str = R863_MANUAL_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _r863ManualDial4WaitingForFeedback, 1); - } - else if (_r863ManualCockpitFreq4DialPos > desiredPositionDial4X) - { - dial4OkTime = DateTime.Now.Ticks; - str = R863_MANUAL_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _r863ManualDial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _r863ManualDial3WaitingForFeedback, 1); + } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _r863ManualDial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockR863ManualDialsObject4) + { + if (_r863ManualCockpitFreq4DialPos < desiredPositionDial4X) { dial4OkTime = DateTime.Now.Ticks; + command = R863_MANUAL_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; } - - // if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) - /* ATTN ! 12.05.2019 - * This radio is problematic, sometimes it goes bonkers, DCS-BIOS doesn't see the update from DCS - * and everything goes wrong after that. Been like this from start. - * Added big send counts here just to make sure it has time to find the correct position. - */ - if (dial1SendCount > 40 || dial2SendCount > 20 || dial3SendCount > 20 || dial4SendCount > 10) + else if (_r863ManualCockpitFreq4DialPos > desiredPositionDial4X) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + dial4OkTime = DateTime.Now.Ticks; + command = R863_MANUAL_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _r863ManualDial4WaitingForFeedback, 1); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownR863Thread); + Reset(ref dial4Timeout); } - catch (ThreadAbortException ex) + else { - Logger.Error(ex); + dial4OkTime = DateTime.Now.Ticks; } - catch (Exception ex) + + // if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + /* ATTN ! 12.05.2019 + * This radio is problematic, sometimes it goes bonkers, DCS-BIOS doesn't see the update from DCS + * and everything goes wrong after that. Been like this from start. + * Added big send counts here just to make sure it has time to find the correct position. + */ + if (dial1SendCount > 40 || dial2SendCount > 20 || dial3SendCount > 20 || dial4SendCount > 10) { - Common.ShowErrorMessageBox(ex); + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } - finally - { - Interlocked.Exchange(ref _r863ManualThreadNowSynching, 0); - } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); } - catch (Exception ex) + catch (ThreadAbortException ex) { Logger.Error(ex); } + catch (Exception ex) + { + Common.ShowErrorMessageBox(ex); + } // Refresh panel once this debacle is finished Interlocked.Increment(ref _doUpdatePanelLCD); @@ -976,18 +979,15 @@ private void SendYaDRO1AToDCSBIOS() { try { - if (YaDRO1ANowSyncing()) + if (_yadro1ASyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyYaDRO1A(); - _shutdownYaDRO1AThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownYaDRO1AThread = false; - _yadro1ASyncThread = new Thread(YaDRO1ASynchThreadMethod); - _yadro1ASyncThread.Start(); + _yadro1ASyncTaskTokenSource = new(); + _yadro1ASyncTask = Task.Run(() => SyncYaDRO1Async(_yadro1ASyncTaskTokenSource.Token)); } catch (Exception ex) { @@ -995,188 +995,194 @@ private void SendYaDRO1AToDCSBIOS() } } - private void YaDRO1ASynchThreadMethod() + private async Task SyncYaDRO1Async(CancellationToken cancellationToken) { try { - try + /* + * Mi-8 YaDRO-1A + */ + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + + var frequencyAsString = _yadro1ABigFrequencyStandby.ToString().PadLeft(3, '0') + _yadro1ASmallFrequencyStandby.ToString().PadLeft(2, '0'); + frequencyAsString = frequencyAsString.PadRight(6, '0'); + + // Frequency selector 1 YADRO1A_FREQ1 + // "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" + // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 + + // Frequency selector 2 YADRO1A_FREQ2 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 3 YADRO1A_FREQ3 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 4 YADRO1A_FREQ4 + // 0 1 2 3 4 5 6 7 8 9 + + // Reason for this is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + + // 02000 + // 17999 + // #1 = 17 (position = value) + // #2 = 9 (position = value) + // #3 = 9 (position = value) + // #4 = 9 (position = value) + var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); + var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); + var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(3, 1)); + var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(4, 1)); + + do { - try + if (IsTimedOut(ref dial1Timeout)) { - /* - * Mi-8 YaDRO-1A - */ - Interlocked.Exchange(ref _yadro1AThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - - var frequencyAsString = _yadro1ABigFrequencyStandby.ToString().PadLeft(3, '0') + _yadro1ASmallFrequencyStandby.ToString().PadLeft(2, '0'); - frequencyAsString = frequencyAsString.PadRight(6, '0'); - - // Frequency selector 1 YADRO1A_FREQ1 - // "02" "03" "04" "05" "06" "07" "08" "09" "10" "11" "12" "13" "14" "15" "16" "17" - // Pos 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 - - // Frequency selector 2 YADRO1A_FREQ2 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 3 YADRO1A_FREQ3 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 4 YADRO1A_FREQ4 - // 0 1 2 3 4 5 6 7 8 9 - - // Reason for this is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - - // 02000 - // 17999 - // #1 = 17 (position = value) - // #2 = 9 (position = value) - // #3 = 9 (position = value) - // #4 = 9 (position = value) - var desiredPositionDial1X = int.Parse(frequencyAsString.Substring(0, 2)); - var desiredPositionDial2X = int.Parse(frequencyAsString.Substring(2, 1)); - var desiredPositionDial3X = int.Parse(frequencyAsString.Substring(3, 1)); - var desiredPositionDial4X = int.Parse(frequencyAsString.Substring(4, 1)); - - do - { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _yadro1ADial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _yadro1ADial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _yadro1ADial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _yadro1ADial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _yadro1ADial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _yadro1ADial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _yadro1ADial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _yadro1ADial4WaitingForFeedback); // Let's do an ugly reset + } - string str; - if (Interlocked.Read(ref _yadro1ADial1WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject1) - { - if (_yadro1ACockpitFreq1DialPos != desiredPositionDial1X) - { - dial1OkTime = DateTime.Now.Ticks; - if (_yadro1ACockpitFreq1DialPos < desiredPositionDial1X) - { - str = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - } - else - { - str = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - } + if (Interlocked.Read(ref _yadro1ADial1WaitingForFeedback) == 0) + { + var command = string.Empty; - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); - } - } - else + lock (_lockYadro1ADialsObject1) + { + if (_yadro1ACockpitFreq1DialPos != desiredPositionDial1X) { dial1OkTime = DateTime.Now.Ticks; - } - - if (Interlocked.Read(ref _yadro1ADial2WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject2) + if (_yadro1ACockpitFreq1DialPos < desiredPositionDial1X) { - if (_yadro1ACockpitFreq2DialPos != desiredPositionDial2X) - { - dial2OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _yadro1ACockpitFreq2DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else + { + command = YADRO1_A_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial1WaitingForFeedback, 1); + } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _yadro1ADial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject2) + { + if (_yadro1ACockpitFreq2DialPos != desiredPositionDial2X) { dial2OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_2DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial2X, _yadro1ACockpitFreq2DialPos); } + } - if (Interlocked.Read(ref _yadro1ADial3WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject3) - { - if (_yadro1ACockpitFreq3DialPos != desiredPositionDial3X) - { - dial3OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _yadro1ACockpitFreq3DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial2WaitingForFeedback, 1); + } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _yadro1ADial3WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject3) + { + if (_yadro1ACockpitFreq3DialPos != desiredPositionDial3X) { dial3OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_3DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial3X, _yadro1ACockpitFreq3DialPos); } + } - if (Interlocked.Read(ref _yadro1ADial4WaitingForFeedback) == 0) - { - lock (_lockYadro1ADialsObject4) - { - if (_yadro1ACockpitFreq4DialPos != desiredPositionDial4X) - { - dial4OkTime = DateTime.Now.Ticks; - str = YADRO1_A_FREQ_4DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial4X, _yadro1ACockpitFreq4DialPos); - DCSBIOS.Send(str); - Interlocked.Exchange(ref _yadro1ADial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); - } - } - else + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial3WaitingForFeedback, 1); + } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _yadro1ADial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockYadro1ADialsObject4) + { + if (_yadro1ACockpitFreq4DialPos != desiredPositionDial4X) { dial4OkTime = DateTime.Now.Ticks; + command = YADRO1_A_FREQ_4DIAL_COMMAND + GetCommandDirectionFor0To9Dials(desiredPositionDial4X, _yadro1ACockpitFreq4DialPos); } - - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownYaDRO1AThread); - SwapCockpitStandbyFrequencyYaDRO1A(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + Interlocked.Exchange(ref _yadro1ADial4WaitingForFeedback, 1); + } + Reset(ref dial4Timeout); } - catch (Exception ex) + else { - Common.ShowErrorMessageBox(ex); + dial4OkTime = DateTime.Now.Ticks; } + + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } - finally - { - Interlocked.Exchange(ref _yadro1AThreadNowSynching, 0); - } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + + SwapCockpitStandbyFrequencyYaDRO1A(); + ShowFrequenciesOnPanel(); } catch (Exception ex) { - Logger.Error(ex); + Common.ShowErrorMessageBox(ex); } // Refresh panel once this debacle is finished @@ -1196,220 +1202,219 @@ private void SwapCockpitStandbyFrequencyYaDRO1A() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) + var radioPanelKnob = (RadioPanelKnobMi8)radioPanelKnobObject; + + switch (radioPanelKnob.RadioPanelPZ69Knob) { - var radioPanelKnob = (RadioPanelKnobMi8)radioPanelKnobObject; + case RadioPanelPZ69KnobsMi8.UPPER_R863_MANUAL: + { + if (radioPanelKnob.IsOn) + { + SetUpperRadioMode(CurrentMi8RadioMode.R863_MANUAL); + } + break; + } - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsMi8.UPPER_R863_MANUAL: + case RadioPanelPZ69KnobsMi8.UPPER_R863_PRESET: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.R863_MANUAL); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.R863_PRESET); } + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_R863_PRESET: + case RadioPanelPZ69KnobsMi8.UPPER_YADRO1A: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.R863_PRESET); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.YADRO1A); } + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_YADRO1A: + case RadioPanelPZ69KnobsMi8.UPPER_R828: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.YADRO1A); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.R828_PRESETS); } + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_R828: + case RadioPanelPZ69KnobsMi8.UPPER_ADF_ARK9: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.R828_PRESETS); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.ADF_ARK9); } + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_ADF_ARK9: + case RadioPanelPZ69KnobsMi8.UPPER_SPU7: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.ADF_ARK9); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.SPU7); } + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_SPU7: + case RadioPanelPZ69KnobsMi8.LOWER_R863_MANUAL: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMi8RadioMode.SPU7); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.R863_MANUAL); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_R863_MANUAL: + case RadioPanelPZ69KnobsMi8.LOWER_R863_PRESET: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.R863_MANUAL); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.R863_PRESET); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_R863_PRESET: + case RadioPanelPZ69KnobsMi8.LOWER_YADRO1A: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.R863_PRESET); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.YADRO1A); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_YADRO1A: + case RadioPanelPZ69KnobsMi8.LOWER_R828: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.YADRO1A); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.R828_PRESETS); + } + break; + } + + case RadioPanelPZ69KnobsMi8.LOWER_ADF_ARK9: + { + if (radioPanelKnob.IsOn) + { + SetLowerRadioMode(CurrentMi8RadioMode.ADF_ARK9); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_R828: + case RadioPanelPZ69KnobsMi8.LOWER_SPU7: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.R828_PRESETS); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.SPU7); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_ADF_ARK9: + case RadioPanelPZ69KnobsMi8.UPPER_ARK_UD: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.ADF_ARK9); - } - break; + SetUpperRadioMode(CurrentMi8RadioMode.ARK_UD); } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_SPU7: + case RadioPanelPZ69KnobsMi8.LOWER_ARK_UD: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMi8RadioMode.SPU7); - } - break; + SetLowerRadioMode(CurrentMi8RadioMode.ARK_UD); } + break; + } + + case RadioPanelPZ69KnobsMi8.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi8.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi8.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi8.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi8.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi8.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsMi8.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsMi8.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } - case RadioPanelPZ69KnobsMi8.UPPER_ARK_UD: + case RadioPanelPZ69KnobsMi8.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentMi8RadioMode.R863_PRESET) { if (radioPanelKnob.IsOn) { - SetUpperRadioMode(CurrentMi8RadioMode.ARK_UD); + await DCSBIOS.SendAsync(R863_UNIT_SWITCH_COMMAND_TOGGLE); } - break; } + else if (_currentUpperRadioMode == CurrentMi8RadioMode.ADF_ARK9 && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); + } + else if (_currentUpperRadioMode == CurrentMi8RadioMode.ARK_UD && radioPanelKnob.IsOn) + { + await DCSBIOS.SendAsync(ARKUD_VHF_UHF_MODE_COMMAND_TOGGLE); + } + else + { + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi8.UPPER_FREQ_SWITCH); + } + break; + } - case RadioPanelPZ69KnobsMi8.LOWER_ARK_UD: + case RadioPanelPZ69KnobsMi8.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentMi8RadioMode.R863_PRESET) { if (radioPanelKnob.IsOn) { - SetLowerRadioMode(CurrentMi8RadioMode.ARK_UD); + await DCSBIOS.SendAsync(R863_UNIT_SWITCH_COMMAND_TOGGLE); } - break; } - - case RadioPanelPZ69KnobsMi8.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi8.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi8.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi8.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi8.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi8.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsMi8.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsMi8.LOWER_SMALL_FREQ_WHEEL_DEC: + else if (_currentLowerRadioMode == CurrentMi8RadioMode.ADF_ARK9 && radioPanelKnob.IsOn) { - // Ignore - break; + await DCSBIOS.SendAsync(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); } - - case RadioPanelPZ69KnobsMi8.UPPER_FREQ_SWITCH: + else if (_currentLowerRadioMode == CurrentMi8RadioMode.ARK_UD && radioPanelKnob.IsOn) { - if (_currentUpperRadioMode == CurrentMi8RadioMode.R863_PRESET) - { - if (radioPanelKnob.IsOn) - { - DCSBIOS.Send(R863_UNIT_SWITCH_COMMAND_TOGGLE); - } - } - else if (_currentUpperRadioMode == CurrentMi8RadioMode.ADF_ARK9 && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); - } - else if (_currentUpperRadioMode == CurrentMi8RadioMode.ARK_UD && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ARKUD_VHF_UHF_MODE_COMMAND_TOGGLE); - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi8.UPPER_FREQ_SWITCH); - } - break; + await DCSBIOS.SendAsync(ARKUD_VHF_UHF_MODE_COMMAND_TOGGLE); } - - case RadioPanelPZ69KnobsMi8.LOWER_FREQ_SWITCH: + else { - if (_currentLowerRadioMode == CurrentMi8RadioMode.R863_PRESET) - { - if (radioPanelKnob.IsOn) - { - DCSBIOS.Send(R863_UNIT_SWITCH_COMMAND_TOGGLE); - } - } - else if (_currentLowerRadioMode == CurrentMi8RadioMode.ADF_ARK9 && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ADF_BACKUP_MAIN_SWITCH_TOGGLE_COMMAND); - } - else if (_currentLowerRadioMode == CurrentMi8RadioMode.ARK_UD && radioPanelKnob.IsOn) - { - DCSBIOS.Send(ARKUD_VHF_UHF_MODE_COMMAND_TOGGLE); - } - else - { - SendFrequencyToDCSBIOS(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi8.LOWER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(radioPanelKnob.IsOn, RadioPanelPZ69KnobsMi8.LOWER_FREQ_SWITCH); } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MI8, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MI8, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -1417,7 +1422,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -1471,7 +1476,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_COMMAND_INC); break; } @@ -1504,25 +1509,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(ARKUD_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_COMMAND_INC); + await DCSBIOS.SendAsync(SPU7_COMMAND_INC); break; } @@ -1572,7 +1577,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_COMMAND_DEC); break; } @@ -1605,25 +1610,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(ARKUD_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU7_COMMAND_DEC); break; } @@ -1654,7 +1659,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_INC); break; } @@ -1673,25 +1678,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_MODE_COMMAND_INC); + await DCSBIOS.SendAsync(ARKUD_MODE_COMMAND_INC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(SPU7_VOLUME_KNOB_COMMAND_INC); break; } @@ -1721,7 +1726,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1739,25 +1744,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_MODE_COMMAND_DEC); + await DCSBIOS.SendAsync(ARKUD_MODE_COMMAND_DEC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU7_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1807,7 +1812,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_COMMAND_INC); break; } @@ -1840,25 +1845,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_INC : ADF_BACKUP100_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(ARKUD_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_COMMAND_INC); + await DCSBIOS.SendAsync(SPU7_COMMAND_INC); break; } @@ -1908,7 +1913,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_COMMAND_DEC); break; } @@ -1941,25 +1946,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN100_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP100_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(ARKUD_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU7_COMMAND_DEC); break; } @@ -1990,7 +1995,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_INC); break; } @@ -2009,25 +2014,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_INC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_INC : ADF_BACKUP10_KHZ_PRESET_COMMAND_INC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_MODE_COMMAND_INC); + await DCSBIOS.SendAsync(ARKUD_MODE_COMMAND_INC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(SPU7_VOLUME_KNOB_COMMAND_INC); break; } @@ -2057,7 +2062,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R863_PRESET: { - DCSBIOS.Send(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R863_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } @@ -2075,25 +2080,25 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentMi8RadioMode.R828_PRESETS: { - DCSBIOS.Send(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(R828_PRESET_VOLUME_KNOB_COMMAND_DEC); break; } case CurrentMi8RadioMode.ADF_ARK9: { - DCSBIOS.Send(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(_adfBackupMainCockpitDial1Pos == 1 ? ADF_MAIN10_KHZ_PRESET_COMMAND_DEC : ADF_BACKUP10_KHZ_PRESET_COMMAND_DEC); break; } case CurrentMi8RadioMode.ARK_UD: { - DCSBIOS.Send(ARKUD_MODE_COMMAND_DEC); + await DCSBIOS.SendAsync(ARKUD_MODE_COMMAND_DEC); break; } case CurrentMi8RadioMode.SPU7: { - DCSBIOS.Send(SPU7_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(SPU7_VOLUME_KNOB_COMMAND_DEC); break; } @@ -2618,16 +2623,6 @@ private void SetLowerRadioMode(CurrentMi8RadioMode currentMi8RadioMode) } } - private bool R863ManualNowSyncing() - { - return Interlocked.Read(ref _r863ManualThreadNowSynching) > 0; - } - - private bool YaDRO1ANowSyncing() - { - return Interlocked.Read(ref _yadro1AThreadNowSynching) > 0; - } - private void SaveCockpitFrequencyR863Manual() { try diff --git a/src/NonVisuals/Radios/RadioPanelPZ69MiG21Bis.cs b/src/NonVisuals/Radios/RadioPanelPZ69MiG21Bis.cs index a5a0822c0..801dba30a 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69MiG21Bis.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69MiG21Bis.cs @@ -1,4 +1,5 @@ -using ClassLibraryCommon; +using System.Threading.Tasks; +using ClassLibraryCommon; using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios @@ -85,7 +86,7 @@ private enum CurrentMiG21BisRadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69MiG21Bis(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -196,7 +197,7 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) { } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsMiG21Bis knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsMiG21Bis knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsMiG21Bis.UpperFreqSwitch || knob == RadioPanelPZ69KnobsMiG21Bis.LowerFreqSwitch)) { @@ -220,19 +221,19 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsMiG21Bis knob) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_ON_OFF_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(RADIO_ON_OFF_TOGGLE_COMMAND); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(SELECT_RSBN_COMMAND); + await DCSBIOS.SendAsync(SELECT_RSBN_COMMAND); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(SELECT_ARC_COMMAND); + await DCSBIOS.SendAsync(SELECT_ARC_COMMAND); break; } } @@ -245,19 +246,19 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsMiG21Bis knob) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_ON_OFF_TOGGLE_COMMAND); + await DCSBIOS.SendAsync(RADIO_ON_OFF_TOGGLE_COMMAND); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(SELECT_RSBN_COMMAND); + await DCSBIOS.SendAsync(SELECT_RSBN_COMMAND); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(SELECT_ARC_COMMAND); + await DCSBIOS.SendAsync(SELECT_ARC_COMMAND); break; } } @@ -368,7 +369,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -388,19 +389,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_FREQ_SELECTOR_POSITION_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_FREQ_SELECTOR_POSITION_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_NAV_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(RSBN_NAV_CHANNEL_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_SECTOR_COMMAND_INC); + await DCSBIOS.SendAsync(ARC_SECTOR_COMMAND_INC); break; } } @@ -413,19 +414,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_FREQ_SELECTOR_POSITION_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_FREQ_SELECTOR_POSITION_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_NAV_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(RSBN_NAV_CHANNEL_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_SECTOR_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC_SECTOR_COMMAND_DEC); break; } } @@ -438,19 +439,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_ILS_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(RSBN_ILS_CHANNEL_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_PRESET_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(ARC_PRESET_CHANNEL_COMMAND_INC); break; } } @@ -463,19 +464,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_ILS_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(RSBN_ILS_CHANNEL_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_PRESET_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC_PRESET_CHANNEL_COMMAND_DEC); break; } } @@ -488,19 +489,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_FREQ_SELECTOR_POSITION_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_FREQ_SELECTOR_POSITION_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_NAV_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(RSBN_NAV_CHANNEL_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_SECTOR_COMMAND_INC); + await DCSBIOS.SendAsync(ARC_SECTOR_COMMAND_INC); break; } } @@ -513,20 +514,20 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_FREQ_SELECTOR_POSITION_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_FREQ_SELECTOR_POSITION_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_NAV_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(RSBN_NAV_CHANNEL_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_SECTOR_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC_SECTOR_COMMAND_DEC); break; } } @@ -539,19 +540,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_VOLUME_COMMAND_INC); + await DCSBIOS.SendAsync(RADIO_VOLUME_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_ILS_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(RSBN_ILS_CHANNEL_COMMAND_INC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_PRESET_CHANNEL_COMMAND_INC); + await DCSBIOS.SendAsync(ARC_PRESET_CHANNEL_COMMAND_INC); break; } } @@ -564,19 +565,19 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentMiG21BisRadioMode.Radio: { - DCSBIOS.Send(RADIO_VOLUME_COMMAND_DEC); + await DCSBIOS.SendAsync(RADIO_VOLUME_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.RSBN: { - DCSBIOS.Send(RSBN_ILS_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(RSBN_ILS_CHANNEL_COMMAND_DEC); break; } case CurrentMiG21BisRadioMode.ARC: { - DCSBIOS.Send(ARC_PRESET_CHANNEL_COMMAND_DEC); + await DCSBIOS.SendAsync(ARC_PRESET_CHANNEL_COMMAND_DEC); break; } } @@ -589,177 +590,175 @@ private void AdjustFrequency(IEnumerable hashSet) ShowFrequenciesOnPanel(); } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobMiG21Bis)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobMiG21Bis)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsMiG21Bis.UpperRadio: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsMiG21Bis.UpperRadio: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentMiG21BisRadioMode.Radio; - } - break; + _currentUpperRadioMode = CurrentMiG21BisRadioMode.Radio; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperRsbn: + case RadioPanelPZ69KnobsMiG21Bis.UpperRsbn: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentMiG21BisRadioMode.RSBN; - } - break; + _currentUpperRadioMode = CurrentMiG21BisRadioMode.RSBN; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperArc: + case RadioPanelPZ69KnobsMiG21Bis.UpperArc: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentMiG21BisRadioMode.ARC; - } - break; + _currentUpperRadioMode = CurrentMiG21BisRadioMode.ARC; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperCom2: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperCom2: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperNav2: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperNav2: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperDme: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperDme: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperXpdr: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperXpdr: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerRadio: + case RadioPanelPZ69KnobsMiG21Bis.LowerRadio: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentMiG21BisRadioMode.Radio; - } - break; + _currentLowerRadioMode = CurrentMiG21BisRadioMode.Radio; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerRsbn: + case RadioPanelPZ69KnobsMiG21Bis.LowerRsbn: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentMiG21BisRadioMode.RSBN; - } - break; + _currentLowerRadioMode = CurrentMiG21BisRadioMode.RSBN; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerArc: + case RadioPanelPZ69KnobsMiG21Bis.LowerArc: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentMiG21BisRadioMode.ARC; - } - break; + _currentLowerRadioMode = CurrentMiG21BisRadioMode.ARC; } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerCom2: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerCom2: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerNav2: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerNav2: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerDme: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerDme: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerXpdr: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerXpdr: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperLargeFreqWheelInc: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperLargeFreqWheelInc: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperLargeFreqWheelDec: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperLargeFreqWheelDec: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperSmallFreqWheelInc: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperSmallFreqWheelInc: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperSmallFreqWheelDec: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.UpperSmallFreqWheelDec: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerLargeFreqWheelInc: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerLargeFreqWheelInc: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerLargeFreqWheelDec: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerLargeFreqWheelDec: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerSmallFreqWheelInc: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerSmallFreqWheelInc: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerSmallFreqWheelDec: - { - break; - } + case RadioPanelPZ69KnobsMiG21Bis.LowerSmallFreqWheelDec: + { + break; + } - case RadioPanelPZ69KnobsMiG21Bis.UpperFreqSwitch: + case RadioPanelPZ69KnobsMiG21Bis.UpperFreqSwitch: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsMiG21Bis.UpperFreqSwitch); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsMiG21Bis.UpperFreqSwitch); } + break; + } - case RadioPanelPZ69KnobsMiG21Bis.LowerFreqSwitch: + case RadioPanelPZ69KnobsMiG21Bis.LowerFreqSwitch: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsMiG21Bis.LowerFreqSwitch); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsMiG21Bis.LowerFreqSwitch); } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MIG21BIS, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_MIG21BIS, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } public override void ClearSettings(bool setIsDirty = false) { } @@ -768,7 +767,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobMiG21Bis.GetRadioPanelKnobs(); diff --git a/src/NonVisuals/Radios/RadioPanelPZ69Mosquito.cs b/src/NonVisuals/Radios/RadioPanelPZ69Mosquito.cs index b1f850466..53dabfca1 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69Mosquito.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69Mosquito.cs @@ -1,27 +1,24 @@ -using ClassLibraryCommon; +using System.Threading.Tasks; +using ClassLibraryCommon; using NonVisuals.BindingClasses.BIP; +using System; +using System.Collections.Generic; +using System.Threading; + +using DCS_BIOS; +using DCS_BIOS.EventArgs; + +using MEF; +using NonVisuals.Plugin; +using NonVisuals.Radios.Knobs; +using NonVisuals.Panels.Saitek; +using NonVisuals.HID; +using NonVisuals.Helpers; +using DCS_BIOS.Serialized; +using DCS_BIOS.ControlLocator; namespace NonVisuals.Radios { - using System; - using System.Collections.Generic; - using System.Threading; - - using DCS_BIOS; - using DCS_BIOS.EventArgs; - - using MEF; - using Plugin; - using Knobs; - using Panels.Saitek; - using HID; - using Helpers; - using DCS_BIOS.Serialized; - using DCS_BIOS.ControlLocator; - - - - /// /// Pre-programmed radio panel for the Mosquito. /// @@ -63,7 +60,7 @@ private enum CurrentMosquitoRadioMode private long _doUpdatePanelLCD; public RadioPanelPZ69Mosquito(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -95,7 +92,7 @@ public override void InitPanel() _vhf1DcsbiosOutputPresetButton3 = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RADIO_C"); _vhf1DcsbiosOutputPresetButton4 = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RADIO_D"); _vhfRadioModeDcsbiosOutput = DCSBIOSControlLocator.GetUIntDCSBIOSOutput("RADIO_T_MODE"); - + BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } @@ -204,126 +201,125 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobMosquito)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobMosquito)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelKnobsMosquito.UPPER_VHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelKnobsMosquito.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMosquitoRadioMode.VHF); - } - break; + SetUpperRadioMode(CurrentMosquitoRadioMode.VHF); } + break; + } - case RadioPanelKnobsMosquito.UPPER_NO_USE0: - case RadioPanelKnobsMosquito.UPPER_NO_USE1: - case RadioPanelKnobsMosquito.UPPER_NO_USE2: - case RadioPanelKnobsMosquito.UPPER_NO_USE3: - case RadioPanelKnobsMosquito.UPPER_NO_USE4: - case RadioPanelKnobsMosquito.UPPER_NO_USE5: + case RadioPanelKnobsMosquito.UPPER_NO_USE0: + case RadioPanelKnobsMosquito.UPPER_NO_USE1: + case RadioPanelKnobsMosquito.UPPER_NO_USE2: + case RadioPanelKnobsMosquito.UPPER_NO_USE3: + case RadioPanelKnobsMosquito.UPPER_NO_USE4: + case RadioPanelKnobsMosquito.UPPER_NO_USE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentMosquitoRadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentMosquitoRadioMode.NO_USE); } + break; + } - case RadioPanelKnobsMosquito.LOWER_VHF: + case RadioPanelKnobsMosquito.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMosquitoRadioMode.VHF); - } - break; + SetLowerRadioMode(CurrentMosquitoRadioMode.VHF); } + break; + } - case RadioPanelKnobsMosquito.LOWER_NO_USE0: - case RadioPanelKnobsMosquito.LOWER_NO_USE1: - case RadioPanelKnobsMosquito.LOWER_NO_USE2: - case RadioPanelKnobsMosquito.LOWER_NO_USE3: - case RadioPanelKnobsMosquito.LOWER_NO_USE4: - case RadioPanelKnobsMosquito.LOWER_NO_USE5: + case RadioPanelKnobsMosquito.LOWER_NO_USE0: + case RadioPanelKnobsMosquito.LOWER_NO_USE1: + case RadioPanelKnobsMosquito.LOWER_NO_USE2: + case RadioPanelKnobsMosquito.LOWER_NO_USE3: + case RadioPanelKnobsMosquito.LOWER_NO_USE4: + case RadioPanelKnobsMosquito.LOWER_NO_USE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentMosquitoRadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentMosquitoRadioMode.NO_USE); } + break; + } - case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelKnobsMosquito.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelKnobsMosquito.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelKnobsMosquito.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelKnobsMosquito.LOWER_SMALL_FREQ_WHEEL_DEC: - { - // Ignore - break; - } + case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelKnobsMosquito.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelKnobsMosquito.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelKnobsMosquito.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelKnobsMosquito.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } - case RadioPanelKnobsMosquito.UPPER_FREQ_SWITCH: + case RadioPanelKnobsMosquito.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentMosquitoRadioMode.VHF) { - if (_currentUpperRadioMode == CurrentMosquitoRadioMode.VHF) + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF1_RADIO_LIGHT_SWITCH_COMMAND); - } - - _upperButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF1_RADIO_LIGHT_SWITCH_COMMAND); } + + _upperButtonPressedAndDialRotated = false; } - break; } + break; + } - case RadioPanelKnobsMosquito.LOWER_FREQ_SWITCH: + case RadioPanelKnobsMosquito.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentMosquitoRadioMode.VHF) { - if (_currentLowerRadioMode == CurrentMosquitoRadioMode.VHF) + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF1_RADIO_LIGHT_SWITCH_COMMAND); - } - - _lowerButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF1_RADIO_LIGHT_SWITCH_COMMAND); } + + _lowerButtonPressedAndDialRotated = false; } - break; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_Mosquito, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_Mosquito, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -331,7 +327,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -350,13 +346,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_INC: { - _vhfDialChangeSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfDialChangeSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case RadioPanelKnobsMosquito.UPPER_LARGE_FREQ_WHEEL_DEC: { - _vhfDialChangeSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfDialChangeSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } @@ -369,11 +365,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); } else if (!_vhfDialChangeSkipper.ShouldSkip()) { - SendIncVHFPresetCommand(); + await SendIncVHFPresetCommandAsync(); } break; } @@ -390,11 +386,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); } else if (!_vhfDialChangeSkipper.ShouldSkip()) { - SendDecVHFPresetCommand(); + await SendDecVHFPresetCommandAsync(); } break; } @@ -405,14 +401,14 @@ private void AdjustFrequency(IEnumerable hashSet) case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_INC: { // MODE - _vhfDialChangeSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfDialChangeSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case RadioPanelKnobsMosquito.LOWER_LARGE_FREQ_WHEEL_DEC: { // MODE - _vhfDialChangeSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfDialChangeSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } @@ -425,11 +421,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); } - else if (!_vhfDialChangeSkipper.ShouldSkip()) + else if (!_vhfDialChangeSkipper.ShouldSkip()) { - SendIncVHFPresetCommand(); + await SendIncVHFPresetCommandAsync(); } break; } @@ -446,11 +442,11 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); } - else if (!_vhfDialChangeSkipper.ShouldSkip()) + else if (!_vhfDialChangeSkipper.ShouldSkip()) { - SendDecVHFPresetCommand(); + await SendDecVHFPresetCommandAsync(); } break; } @@ -574,34 +570,37 @@ private string GetHFRadioModeStringCommand(bool moveUp) } } - private void SendIncVHFPresetCommand() + private async Task SendIncVHFPresetCommandAsync() { Interlocked.Increment(ref _doUpdatePanelLCD); + + var command = string.Empty; + lock (_lockVhf1DialObject1) { switch (_vhf1CockpitPresetActiveButton) { case 0: { - DCSBIOS.Send("RADIO_A 1\n"); + command = "RADIO_A 1\n"; break; } case 1: { - DCSBIOS.Send("RADIO_B 1\n"); + command = "RADIO_B 1\n"; break; } case 2: { - DCSBIOS.Send("RADIO_C 1\n"); + command = "RADIO_C 1\n"; break; } case 3: { - DCSBIOS.Send("RADIO_D 1\n"); + command = "RADIO_D 1\n"; break; } @@ -611,11 +610,19 @@ private void SendIncVHFPresetCommand() } } } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + } } - private void SendDecVHFPresetCommand() + private async Task SendDecVHFPresetCommandAsync() { Interlocked.Increment(ref _doUpdatePanelLCD); + + var command = string.Empty; + lock (_lockVhf1DialObject1) { switch (_vhf1CockpitPresetActiveButton) @@ -627,29 +634,34 @@ private void SendDecVHFPresetCommand() case 1: { - DCSBIOS.Send("RADIO_OFF 1\n"); + command = "RADIO_OFF 1\n"; break; } case 2: { - DCSBIOS.Send("RADIO_A 1\n"); + command = "RADIO_A 1\n"; break; } case 3: { - DCSBIOS.Send("RADIO_B 1\n"); + command = "RADIO_B 1\n"; break; } case 4: { - DCSBIOS.Send("RADIO_C 1\n"); + command = "RADIO_C 1\n"; break; } } } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + } } public override void ClearSettings(bool setIsDirty = false) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69P51D.cs b/src/NonVisuals/Radios/RadioPanelPZ69P51D.cs index 139d8e891..962273134 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69P51D.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69P51D.cs @@ -1,4 +1,5 @@ -using ClassLibraryCommon; +using System.Threading.Tasks; +using ClassLibraryCommon; using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios @@ -19,6 +20,7 @@ namespace NonVisuals.Radios using DCS_BIOS.Serialized; using DCS_BIOS.ControlLocator; using DCS_BIOS.misc; + using Newtonsoft.Json.Linq; @@ -84,8 +86,16 @@ private enum CurrentP51DRadioMode private readonly object _lockShowFrequenciesOnPanelObject = new(); private long _doUpdatePanelLCD; + private const string RADIO_CHANNEL_A_ON = "VHF_RADIO_CHAN_A 1\n"; + private const string RADIO_CHANNEL_B_ON = "VHF_RADIO_CHAN_B 1\n"; + private const string RADIO_CHANNEL_C_ON = "VHF_RADIO_CHAN_C 1\n"; + private const string RADIO_CHANNEL_D_ON = "VHF_RADIO_CHAN_D 1\n"; + + private const string RADIO_FREQ_SELECTOR = "DETROLA_FREQUENCY "; + private const string RADIO_VOLUME = "DETROLA_VOLUME "; + public RadioPanelPZ69P51D(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -264,138 +274,137 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobP51D)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobP51D)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsP51D.UPPER_VHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsP51D.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentP51DRadioMode.VHF); - } - break; + SetUpperRadioMode(CurrentP51DRadioMode.VHF); } - case RadioPanelPZ69KnobsP51D.UPPER_LF_RADIO: + break; + } + case RadioPanelPZ69KnobsP51D.UPPER_LF_RADIO: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentP51DRadioMode.DETROLA); - } - break; + SetUpperRadioMode(CurrentP51DRadioMode.DETROLA); } - case RadioPanelPZ69KnobsP51D.UPPER_NO_USE1: - case RadioPanelPZ69KnobsP51D.UPPER_NO_USE2: - case RadioPanelPZ69KnobsP51D.UPPER_NO_USE3: - case RadioPanelPZ69KnobsP51D.UPPER_NO_USE4: - case RadioPanelPZ69KnobsP51D.UPPER_NO_USE5: + break; + } + case RadioPanelPZ69KnobsP51D.UPPER_NO_USE1: + case RadioPanelPZ69KnobsP51D.UPPER_NO_USE2: + case RadioPanelPZ69KnobsP51D.UPPER_NO_USE3: + case RadioPanelPZ69KnobsP51D.UPPER_NO_USE4: + case RadioPanelPZ69KnobsP51D.UPPER_NO_USE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentP51DRadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentP51DRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsP51D.LOWER_VHF: + case RadioPanelPZ69KnobsP51D.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentP51DRadioMode.VHF); - } - break; + SetLowerRadioMode(CurrentP51DRadioMode.VHF); } - case RadioPanelPZ69KnobsP51D.LOWER_LF_RADIO: + break; + } + case RadioPanelPZ69KnobsP51D.LOWER_LF_RADIO: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentP51DRadioMode.DETROLA); - } - break; + SetLowerRadioMode(CurrentP51DRadioMode.DETROLA); } - case RadioPanelPZ69KnobsP51D.LOWER_NO_USE1: - case RadioPanelPZ69KnobsP51D.LOWER_NO_USE2: - case RadioPanelPZ69KnobsP51D.LOWER_NO_USE3: - case RadioPanelPZ69KnobsP51D.LOWER_NO_USE4: - case RadioPanelPZ69KnobsP51D.LOWER_NO_USE5: + break; + } + case RadioPanelPZ69KnobsP51D.LOWER_NO_USE1: + case RadioPanelPZ69KnobsP51D.LOWER_NO_USE2: + case RadioPanelPZ69KnobsP51D.LOWER_NO_USE3: + case RadioPanelPZ69KnobsP51D.LOWER_NO_USE4: + case RadioPanelPZ69KnobsP51D.LOWER_NO_USE5: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentP51DRadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentP51DRadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsP51D.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsP51D.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsP51D.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsP51D.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsP51D.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsP51D.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsP51D.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsP51D.LOWER_SMALL_FREQ_WHEEL_DEC: - { - // Ignore - break; - } + case RadioPanelPZ69KnobsP51D.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsP51D.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsP51D.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsP51D.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsP51D.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsP51D.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsP51D.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsP51D.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } - case RadioPanelPZ69KnobsP51D.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsP51D.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentP51DRadioMode.VHF) { - if (_currentUpperRadioMode == CurrentP51DRadioMode.VHF) + _upperButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _upperButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF_RADIO_LIGHT_SWITCH_COMMAND); - } - - _upperButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF_RADIO_LIGHT_SWITCH_COMMAND); } + + _upperButtonPressedAndDialRotated = false; } - break; } + break; + } - case RadioPanelPZ69KnobsP51D.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsP51D.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentP51DRadioMode.VHF) { - if (_currentLowerRadioMode == CurrentP51DRadioMode.VHF) + _lowerButtonPressed = radioPanelKnob.IsOn; + if (!radioPanelKnob.IsOn) { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF_RADIO_LIGHT_SWITCH_COMMAND); - } - - _lowerButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF_RADIO_LIGHT_SWITCH_COMMAND); } + + _lowerButtonPressedAndDialRotated = false; } - break; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_P51D, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_P51D, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -403,7 +412,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -426,12 +435,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP51DRadioMode.VHF: { - _vhfRadioDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfRadioDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentP51DRadioMode.DETROLA: { - SendLFVolumeCommand(true); + await SendLFVolumeCommandAsync(true); break; } } @@ -444,12 +453,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP51DRadioMode.VHF: { - _vhfRadioDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfRadioDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentP51DRadioMode.DETROLA: { - SendLFVolumeCommand(false); + await SendLFVolumeCommandAsync(false); break; } } @@ -465,17 +474,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); } else if (!_vhfRadioDialSkipper.ShouldSkip()) { - SendIncVHFPresetCommand(); + await SendIncVHFPresetCommandAsync(); } break; } case CurrentP51DRadioMode.DETROLA: { - SendLFFrequencyCommand(true); + await SendLFFrequencyCommandAsync(true); break; } } @@ -491,17 +500,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); } else if (!_vhfRadioDialSkipper.ShouldSkip()) { - SendDecVHFPresetCommand(); + await SendDecVHFPresetCommandAsync(); } break; } case CurrentP51DRadioMode.DETROLA: { - SendLFFrequencyCommand(false); + await SendLFFrequencyCommandAsync(false); break; } } @@ -514,12 +523,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP51DRadioMode.VHF: { - _vhfRadioDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfRadioDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentP51DRadioMode.DETROLA: { - SendLFVolumeCommand(true); + await SendLFVolumeCommandAsync(true); break; } } @@ -532,12 +541,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP51DRadioMode.VHF: { - _vhfRadioDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfRadioDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentP51DRadioMode.DETROLA: { - SendLFVolumeCommand(false); + await SendLFVolumeCommandAsync(false); break; } } @@ -553,17 +562,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_INC); } else if (!_vhfRadioDialSkipper.ShouldSkip()) { - SendIncVHFPresetCommand(); + await SendIncVHFPresetCommandAsync(); } break; } case CurrentP51DRadioMode.DETROLA: { - SendLFFrequencyCommand(true); + await SendLFFrequencyCommandAsync(true); break; } } @@ -579,17 +588,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF1_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF1_VOLUME_KNOB_COMMAND_DEC); } else if (!_vhfRadioDialSkipper.ShouldSkip()) { - SendDecVHFPresetCommand(); + await SendDecVHFPresetCommandAsync(); } break; } case CurrentP51DRadioMode.DETROLA: { - SendLFFrequencyCommand(false); + await SendLFFrequencyCommandAsync(false); break; } } @@ -716,34 +725,37 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void SendIncVHFPresetCommand() + private async Task SendIncVHFPresetCommandAsync() { Interlocked.Increment(ref _doUpdatePanelLCD); + + var command = string.Empty; + lock (_lockVhf1DialObject1) { switch (_vhf1CockpitPresetActiveButton) { case 0: { - DCSBIOS.Send("VHF_RADIO_CHAN_A 1\n"); + command = RADIO_CHANNEL_A_ON; break; } case 1: { - DCSBIOS.Send("VHF_RADIO_CHAN_B 1\n"); + command = RADIO_CHANNEL_B_ON; break; } case 2: { - DCSBIOS.Send("VHF_RADIO_CHAN_C 1\n"); + command = RADIO_CHANNEL_C_ON; break; } case 3: { - DCSBIOS.Send("VHF_RADIO_CHAN_D 1\n"); + command = RADIO_CHANNEL_D_ON; break; } @@ -753,17 +765,22 @@ private void SendIncVHFPresetCommand() } } } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + } } - private void SendLFFrequencyCommand(bool increase) + private async Task SendLFFrequencyCommandAsync(bool increase) { - DCSBIOS.Send(GetDetrolaFrequencyStringCommand(increase, _lfFrequencyDialChangeMonitor.ClickAndCheck() ? _lfFrequencyChangeValue * 10 : _lfFrequencyChangeValue)); + await DCSBIOS.SendAsync(GetDetrolaFrequencyStringCommand(increase, _lfFrequencyDialChangeMonitor.ClickAndCheck() ? _lfFrequencyChangeValue * 10 : _lfFrequencyChangeValue)); } - private void SendLFVolumeCommand(bool increase) + private async Task SendLFVolumeCommandAsync(bool increase) { - DCSBIOS.Send(GetDetrolaVolumeStringCommand(increase, _lfVolumeDialChangeMonitor.ClickAndCheck() ? _lfVolumeChangeValue * 10 : _lfVolumeChangeValue)); + await DCSBIOS.SendAsync(GetDetrolaVolumeStringCommand(increase, _lfVolumeDialChangeMonitor.ClickAndCheck() ? _lfVolumeChangeValue * 10 : _lfVolumeChangeValue)); } @@ -771,15 +788,12 @@ private string GetDetrolaFrequencyStringCommand(bool moveUp, uint changeValue) { lock (_lockLFRadioFrequencyDialObject1) { - uint newValue; if (moveUp) { - newValue = _lfRadioFrequencyDCSBIOSValue + changeValue > 0xFFFF ? 0xFFFF : _lfRadioFrequencyDCSBIOSValue + changeValue; - return $"DETROLA_FREQUENCY {newValue}\n"; + return RADIO_FREQ_SELECTOR + (changeValue > 0xFFFF ? 0xFFFF : _lfRadioFrequencyDCSBIOSValue + changeValue); } - newValue = _lfRadioFrequencyDCSBIOSValue < changeValue ? 0 : _lfRadioFrequencyDCSBIOSValue - changeValue; - return $"DETROLA_FREQUENCY {newValue}\n"; + return RADIO_FREQ_SELECTOR + (_lfRadioFrequencyDCSBIOSValue < changeValue ? 0 : _lfRadioFrequencyDCSBIOSValue - changeValue); } } @@ -787,15 +801,12 @@ private string GetDetrolaVolumeStringCommand(bool moveUp, uint changeValue) { lock (_lockLFRadioVolumeDialObject1) { - uint newValue; if (moveUp) { - newValue = _lfRadioVolumeDCSBIOSValue + changeValue > 0xFFFF ? 0xFFFF : _lfRadioVolumeDCSBIOSValue + changeValue; - return $"DETROLA_VOLUME {newValue}\n"; + return RADIO_VOLUME + (_lfRadioVolumeDCSBIOSValue + changeValue > 0xFFFF ? 0xFFFF : _lfRadioVolumeDCSBIOSValue + changeValue); } - newValue = _lfRadioVolumeDCSBIOSValue < changeValue ? 0 : _lfRadioVolumeDCSBIOSValue - changeValue; - return $"DETROLA_VOLUME {newValue}\n"; + return RADIO_VOLUME + (_lfRadioVolumeDCSBIOSValue < changeValue ? 0 : _lfRadioVolumeDCSBIOSValue - changeValue); } } @@ -832,9 +843,11 @@ private string GetHFRadioModeStringCommand(bool moveUp) } } - private void SendDecVHFPresetCommand() + private async Task SendDecVHFPresetCommandAsync() { Interlocked.Increment(ref _doUpdatePanelLCD); + + var command = string.Empty; lock (_lockVhf1DialObject1) { switch (_vhf1CockpitPresetActiveButton) @@ -846,29 +859,34 @@ private void SendDecVHFPresetCommand() case 1: { - DCSBIOS.Send("VHF_RADIO_ON_OFF 1\n"); + command = "VHF_RADIO_ON_OFF 1\n"; break; } case 2: { - DCSBIOS.Send("VHF_RADIO_CHAN_A 1\n"); + command = "VHF_RADIO_CHAN_A 1\n"; break; } case 3: { - DCSBIOS.Send("VHF_RADIO_CHAN_B 1\n"); + command = "VHF_RADIO_CHAN_B 1\n"; break; } case 4: { - DCSBIOS.Send("VHF_RADIO_CHAN_C 1\n"); + command = "VHF_RADIO_CHAN_C 1\n"; break; } } } + + if (!string.IsNullOrEmpty(command)) + { + await DCSBIOS.SendAsync(command); + } } public override void ClearSettings(bool setIsDirty = false) { } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69SA342.cs b/src/NonVisuals/Radios/RadioPanelPZ69SA342.cs index 97da48cf8..3f8bcb37b 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69SA342.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69SA342.cs @@ -1,4 +1,6 @@ -using NonVisuals.BindingClasses.BIP; +using System.Globalization; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -69,7 +71,7 @@ private enum AdfDigit private readonly string _amRadioFreqTenths = "AM_RADIO_FREQ_TENTHS"; private readonly string _amRadioFreqHundrendths = "AM_RADIO_FREQ_HUNDREDTHS"; private readonly int[] _dialPositionsWholeNumbers = { 0, 6553, 13107, 19660, 26214, 32767, 39321, 45874, 52428, 58981 }; - private readonly int[] _dialPositionsDecial100S = { 0, 16383, 32767, 49151 }; + private readonly int[] _dialPositionsDecimal100S = { 0, 16383, 32767, 49151 }; private double _vhfAmBigFrequencyStandby = 118; private double _vhfAmSmallFrequencyStandby; private double _vhfAmSavedCockpitBigFrequency; @@ -91,15 +93,14 @@ private enum AdfDigit private volatile uint _vhfAmCockpitDecimal10SFrequencyValue = 6553; private volatile uint _vhfAmCockpitDecimal100SFrequencyValue = 6553; - private Thread _vhfAmSyncThread; - private long _vhfAmThreadNowSynching; + private Task _vhfAmSyncTask; + private CancellationTokenSource _vhfAmSyncTaskTokenSource = new(); private long _vhfAmValue1WaitingForFeedback; // 10s private long _vhfAmValue2WaitingForFeedback; // 1s private long _vhfAmValue3WaitingForFeedback; // Decimal 10s private long _vhfAmValue4WaitingForFeedback; // Decimal 100s private readonly ClickSkipper _vhfAmLeftDialSkipper = new(2); private readonly ClickSkipper _vhfAmRightDialSkipper = new(2); - private volatile bool _shutdownVHFAMThread; /*COM2 SA342 FM PR4G Radio*/ // Large dial 0-7 Presets 1, 2, 3, 4, 5, 6, 0, RG @@ -110,7 +111,7 @@ private enum AdfDigit private const string FM_RADIO_PRESET_COMMAND_INC = "FM_RADIO_CHANNEL INC\n"; private const string FM_RADIO_PRESET_COMMAND_DEC = "FM_RADIO_CHANNEL DEC\n"; private readonly object _lockFmRadioPresetObject = new(); - + /*NAV1 SA342 UHF Radio*/ // Large dial 225-399 // Small dial 000-975 where only 2 digits can be used @@ -146,7 +147,7 @@ private enum AdfDigit private readonly ClickSkipper _uhfSmallFrequencySkipper = new(2); /*ADF SA342*/ - /*Large dial Counter Clockwise 100s increase*/ + /*Large dial Counterclockwise 100s increase*/ /*Large dial Clockwise 10s increase*/ /*Small dial 1s and decimals*/ private readonly string _adfSwitchUnit = "ADF1_ADF2_SELECT"; @@ -170,7 +171,7 @@ private enum AdfDigit // Large dial Mode selector (VENT, C.M DEC, V.S DER, TPS CAP,P.P, BUT) // Small dial Doppler modes ARRET, VEILLE, TERRE, MER, ANEMO,TEST SOL. // Large - // private readonly string _nadirParameter = "NADIR_PARAMETER"; + // private readonly string _nadirParameter = "NADIR_PARAMETER"; private readonly string _nadirDopplerMode = "NADIR_DOPPLER_MODE"; private const string NADIR_MODE_COMMAND_INC = "NADIR_PARAMETER INC\n"; private const string NADIR_MODE_COMMAND_DEC = "NADIR_PARAMETER DEC\n"; @@ -188,7 +189,7 @@ private enum AdfDigit public RadioPanelPZ69SA342(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -199,7 +200,7 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownVHFAMThread = true; + _vhfAmSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachDataListener(this); } @@ -233,7 +234,7 @@ public override void InitPanel() BIOSEventHandler.AttachDataListener(this); StartListeningForHidPanelChanges(); } - + public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) { UpdateCounter(e.Address, e.Data); @@ -355,7 +356,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) } } - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342 knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsSA342 knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsSA342.UPPER_FREQ_SWITCH || knob == RadioPanelPZ69KnobsSA342.LOWER_FREQ_SWITCH)) { @@ -390,13 +391,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342 knob) case CurrentSA342RadioMode.UHF: { - SendUhfToDCSBIOS(); + await SendUhfToDCSBIOSAsync(); break; } case CurrentSA342RadioMode.ADF: { - DCSBIOS.Send(ADF_SWITCH_UNIT_COMMAND); + await DCSBIOS.SendAsync(ADF_SWITCH_UNIT_COMMAND); break; } } @@ -421,13 +422,13 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342 knob) case CurrentSA342RadioMode.UHF: { - SendUhfToDCSBIOS(); + await SendUhfToDCSBIOSAsync(); break; } case CurrentSA342RadioMode.ADF: { - DCSBIOS.Send(ADF_SWITCH_UNIT_COMMAND); + await DCSBIOS.SendAsync(ADF_SWITCH_UNIT_COMMAND); break; } } @@ -438,7 +439,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342 knob) private void SendVhfAmToDCSBIOS() { - if (VhfAmNowSyncing()) + if (_vhfAmSyncTask?.Status == TaskStatus.Running) { return; } @@ -452,148 +453,156 @@ private void SendVhfAmToDCSBIOS() var desiredPositionDialWholeNumbers = int.Parse(frequencyAsString.Substring(1, 2)); var desiredPositionDecimals = frequencyAsString.Length < 7 ? int.Parse(frequencyAsString.Substring(4, 2) + "0") : int.Parse(frequencyAsString.Substring(4, 3)); - // #1 - _shutdownVHFAMThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFAMThread = false; - _vhfAmSyncThread = new Thread(() => VhfAmSynchThreadMethod(desiredPositionDialWholeNumbers, desiredPositionDecimals)); - _vhfAmSyncThread.Start(); + _vhfAmSyncTaskTokenSource = new(); + _vhfAmSyncTask = Task.Run(() => SyncVhfAmAsync(desiredPositionDialWholeNumbers, desiredPositionDecimals, _vhfAmSyncTaskTokenSource.Token)); } - private void VhfAmSynchThreadMethod(int desiredPositionDialWholeNumbers, int desiredPositionDialDecimals) + private async Task SyncVhfAmAsync(int desiredPositionDialWholeNumbers, int desiredPositionDialDecimals, CancellationToken cancellationToken) { try { - try + /* + * COM1 VHF AM + */ + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + long dial1Time = 0; + long dial2Time = 0; + long dial3Time = 0; + long dial4Time = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + do { - /* - * COM1 VHF AM - * - */ - Interlocked.Exchange(ref _vhfAmThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1Time = 0; - long dial2Time = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmValue1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfAmValue1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfAmValue2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vhfAmValue2WaitingForFeedback); // Let's do an ugly reset + } + + if (Interlocked.Read(ref _vhfAmValue1WaitingForFeedback) == 0 || Interlocked.Read(ref _vhfAmValue2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfAmValue1WaitingForFeedback) == 0 || Interlocked.Read(ref _vhfAmValue2WaitingForFeedback) == 0) + lock (_lockVhfAm10SObject) { - lock (_lockVhfAm10SObject) + lock (_lockVhfAm1SObject) { - lock (_lockVhfAm1SObject) + var frequencyWholeNumbers = GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpit10SFrequencyValue) + string.Empty + + GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpit1SFrequencyValue); + if (int.Parse(frequencyWholeNumbers) != desiredPositionDialWholeNumbers) { - var frequencyWholeNumbers = GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpit10SFrequencyValue) + string.Empty - + GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpit1SFrequencyValue); - if (int.Parse(frequencyWholeNumbers) != desiredPositionDialWholeNumbers) + if (int.Parse(frequencyWholeNumbers) < desiredPositionDialWholeNumbers) { - var command = string.Empty; - if (int.Parse(frequencyWholeNumbers) < desiredPositionDialWholeNumbers) - { - command = VHF_AM_LEFT_DIAL_DIAL_COMMAND_INC; - } - - if (int.Parse(frequencyWholeNumbers) > desiredPositionDialWholeNumbers) - { - command = VHF_AM_LEFT_DIAL_DIAL_COMMAND_DEC; - } + dial1Time = DateTime.Now.Ticks; + dial2Time = DateTime.Now.Ticks; + command = VHF_AM_LEFT_DIAL_DIAL_COMMAND_INC; + } - DCSBIOS.Send(command); + if (int.Parse(frequencyWholeNumbers) > desiredPositionDialWholeNumbers) + { dial1Time = DateTime.Now.Ticks; - dial1SendCount++; - Interlocked.Exchange(ref _vhfAmValue1WaitingForFeedback, 1); - Interlocked.Exchange(ref _vhfAmValue2WaitingForFeedback, 1); - Reset(ref dial1Timeout); + dial2Time = DateTime.Now.Ticks; + command = VHF_AM_LEFT_DIAL_DIAL_COMMAND_DEC; } } } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1Time = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + dial2SendCount++; + Interlocked.Exchange(ref _vhfAmValue1WaitingForFeedback, 1); + Interlocked.Exchange(ref _vhfAmValue2WaitingForFeedback, 1); } - if (Interlocked.Read(ref _vhfAmValue3WaitingForFeedback) == 0 || Interlocked.Read(ref _vhfAmValue4WaitingForFeedback) == 0) + Reset(ref dial1Timeout); + Reset(ref dial2Timeout); + } + else + { + dial1Time = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfAmValue3WaitingForFeedback) == 0 || Interlocked.Read(ref _vhfAmValue4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfAmDecimal10SObject) { - lock (_lockVhfAmDecimal10SObject) + lock (_lockVhfAmDecimal100SObject) { - lock (_lockVhfAmDecimal100SObject) + var cockpitFrequencyDecimals = GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpitDecimal10SFrequencyValue) + string.Empty + + GetVhfAmDialFrequencyFromRawValue(1, _vhfAmCockpitDecimal100SFrequencyValue); + if (int.Parse(cockpitFrequencyDecimals) != desiredPositionDialDecimals) { - var cockpitFrequencyDecimals = GetVhfAmDialFrequencyFromRawValue(0, _vhfAmCockpitDecimal10SFrequencyValue) + string.Empty - + GetVhfAmDialFrequencyFromRawValue(1, _vhfAmCockpitDecimal100SFrequencyValue); - if (int.Parse(cockpitFrequencyDecimals) != desiredPositionDialDecimals) - { - /*Debug.Print("cockpit frequencyDecimals = " + int.Parse(frequencyDecimals)); - Debug.Print("desiredPositionDialDecimals = " + desiredPositionDialDecimals); - Debug.Print("cockpit _vhfAmCockpitDecimal10sFrequencyValue RAW = " + _vhfAmCockpitDecimal10sFrequencyValue); - Debug.Print("cockpit _vhfAmCockpitDecimal100sFrequencyValue RAW = " + _vhfAmCockpitDecimal100sFrequencyValue);*/ - DCSBIOS.Send( - SwitchVhfAmDecimalDirectionUp(int.Parse(cockpitFrequencyDecimals), desiredPositionDialDecimals) - ? VHF_AM_RIGHT_DIAL_DIAL_COMMAND_INC - : VHF_AM_RIGHT_DIAL_DIAL_COMMAND_DEC); - dial2Time = DateTime.Now.Ticks; - dial2SendCount++; - Interlocked.Exchange(ref _vhfAmValue3WaitingForFeedback, 1); - Interlocked.Exchange(ref _vhfAmValue4WaitingForFeedback, 1); - Reset(ref dial2Time); - } + command = SwitchVhfAmDecimalDirectionUp(int.Parse(cockpitFrequencyDecimals), desiredPositionDialDecimals) + ? VHF_AM_RIGHT_DIAL_DIAL_COMMAND_INC + : VHF_AM_RIGHT_DIAL_DIAL_COMMAND_DEC; + dial3Time = DateTime.Now.Ticks; + dial4Time = DateTime.Now.Ticks; } } } - else - { - dial2Time = DateTime.Now.Ticks; - } - if (dial1SendCount > 30 || dial2SendCount > 40) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + dial4SendCount++; + Interlocked.Exchange(ref _vhfAmValue3WaitingForFeedback, 1); + Interlocked.Exchange(ref _vhfAmValue4WaitingForFeedback, 1); + Reset(ref dial2Time); } + } + else + { + dial2Time = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 30 || dial2SendCount > 40 || dial3SendCount > 30 || dial4SendCount > 40) // two last added without checking how they work + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1Time) || IsTooShort(dial2Time)) && !_shutdownVHFAMThread); - SwapCockpitStandbyFrequencyVhfAm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS } + while (IsTooShort(dial1Time) || IsTooShort(dial2Time) || IsTooShort(dial3Time) || IsTooShort(dial4Time)); + + SwapCockpitStandbyFrequencyVhfAm(); + ShowFrequenciesOnPanel(); + } + catch (ThreadAbortException) + { } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfAmThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } - private void SendUhfToDCSBIOS() + private async Task SendUhfToDCSBIOSAsync() { // "399.950" [7] // "399.95" [6] - var frequencyAsString = (_uhfBigFrequencyStandby + "." + _uhfSmallFrequencyStandby.ToString().PadLeft(2, '0')).PadRight(6, '0'); + var frequencyAsString = (_uhfBigFrequencyStandby + "." + _uhfSmallFrequencyStandby.ToString(CultureInfo.InvariantCulture).PadLeft(2, '0')).PadRight(6, '0'); const int sleepLength = 100; - foreach (char c in frequencyAsString) + + foreach (var c in frequencyAsString) { Debug.Print("CHAR IS " + c); switch (c) @@ -601,90 +610,90 @@ private void SendUhfToDCSBIOS() case '0': { // Debug.Print("Sending 0 "); - DCSBIOS.Send(UHF_BUTTON0_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON0_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON0_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON0_COMMAND_OFF); break; } case '1': { // Debug.Print("Sending 1 "); - DCSBIOS.Send(UHF_BUTTON1_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON1_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON1_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON1_COMMAND_OFF); break; } case '2': { // Debug.Print("Sending 2 "); - DCSBIOS.Send(UHF_BUTTON2_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON2_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON2_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON2_COMMAND_OFF); break; } case '3': { // Debug.Print("Sending 3 "); - DCSBIOS.Send(UHF_BUTTON3_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON3_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON3_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON3_COMMAND_OFF); break; } case '4': { // Debug.Print("Sending 4 "); - DCSBIOS.Send(UHF_BUTTON4_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON4_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON4_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON4_COMMAND_OFF); break; } case '5': { // Debug.Print("Sending 5 "); - DCSBIOS.Send(UHF_BUTTON5_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON5_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON5_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON5_COMMAND_OFF); break; } case '6': { // Debug.Print("Sending 6 "); - DCSBIOS.Send(UHF_BUTTON6_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON6_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON6_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON6_COMMAND_OFF); break; } case '7': { // Debug.Print("Sending 7 "); - DCSBIOS.Send(UHF_BUTTON7_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON7_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON7_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON7_COMMAND_OFF); break; } case '8': { // Debug.Print("Sending 8 "); - DCSBIOS.Send(UHF_BUTTON8_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON8_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON8_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON8_COMMAND_OFF); break; } case '9': { // Debug.Print("Sending 9 "); - DCSBIOS.Send(UHF_BUTTON9_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON9_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON9_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON9_COMMAND_OFF); break; } } @@ -693,15 +702,15 @@ private void SendUhfToDCSBIOS() if (frequencyAsString.Length == 6) { // Debug.Print("Sending 0 "); - DCSBIOS.Send(UHF_BUTTON0_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON0_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON0_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON0_COMMAND_OFF); } // Debug.Print("Sending VALIDATE "); - DCSBIOS.Send(UHF_BUTTON_VALIDATE_COMMAND_ON); + await DCSBIOS.SendAsync(UHF_BUTTON_VALIDATE_COMMAND_ON); Thread.Sleep(sleepLength); - DCSBIOS.Send(UHF_BUTTON_VALIDATE_COMMAND_OFF); + await DCSBIOS.SendAsync(UHF_BUTTON_VALIDATE_COMMAND_OFF); } private void ShowFrequenciesOnPanel() @@ -726,7 +735,7 @@ private void ShowFrequenciesOnPanel() { case CurrentSA342RadioMode.VHFAM: { - if (VhfAmNowSyncing()) + if (_vhfAmSyncTask?.Status == TaskStatus.Running) { return; } @@ -815,15 +824,15 @@ private void ShowFrequenciesOnPanel() case CurrentSA342RadioMode.NADIR: { uint tmpValueMode; - uint tmpValueDopper; + uint tmpValueDoppler; lock (_lockNADIRUnitObject) { tmpValueMode = _nadirModeCockpitValue + 1; - tmpValueDopper = _nadirDopplerModeCockpitValue + 1; + tmpValueDoppler = _nadirDopplerModeCockpitValue + 1; } SetPZ69DisplayBytesUnsignedInteger(ref bytes, tmpValueMode, PZ69LCDPosition.UPPER_ACTIVE_LEFT); - SetPZ69DisplayBytesUnsignedInteger(ref bytes, tmpValueDopper, PZ69LCDPosition.UPPER_STBY_RIGHT); + SetPZ69DisplayBytesUnsignedInteger(ref bytes, tmpValueDoppler, PZ69LCDPosition.UPPER_STBY_RIGHT); break; } @@ -839,7 +848,7 @@ private void ShowFrequenciesOnPanel() { case CurrentSA342RadioMode.VHFAM: { - if (VhfAmNowSyncing()) + if (_vhfAmSyncTask?.Status == TaskStatus.Running) { return; } @@ -951,7 +960,7 @@ private void ShowFrequenciesOnPanel() Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -984,7 +993,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.VHFFM: { - DCSBIOS.Send(FM_RADIO_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(FM_RADIO_PRESET_COMMAND_INC); break; } @@ -1019,13 +1028,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf10SDialSkipper.Click(GetAdfCommand(AdfDigit.Digit10S, true)); + await _adf10SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digit10S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_MODE_COMMAND_INC); + await DCSBIOS.SendAsync(NADIR_MODE_COMMAND_INC); break; } } @@ -1051,7 +1060,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.VHFFM: { - DCSBIOS.Send(FM_RADIO_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(FM_RADIO_PRESET_COMMAND_DEC); break; } @@ -1086,13 +1095,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf100SDialSkipper.Click(GetAdfCommand(AdfDigit.Digit100S, true)); + await _adf100SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digit100S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_MODE_COMMAND_DEC); + await DCSBIOS.SendAsync(NADIR_MODE_COMMAND_DEC); break; } } @@ -1151,13 +1160,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf1SDialSkipper.Click(GetAdfCommand(AdfDigit.Digits1S, true)); + await _adf1SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digits1S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_DOPPLER_COMMAND_INC); + await DCSBIOS.SendAsync(NADIR_DOPPLER_COMMAND_INC); break; } } @@ -1216,13 +1225,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf1SDialSkipper.Click(GetAdfCommand(AdfDigit.Digits1S, false)); + await _adf1SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digits1S, false)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_DOPPLER_COMMAND_DEC); + await DCSBIOS.SendAsync(NADIR_DOPPLER_COMMAND_DEC); break; } } @@ -1253,7 +1262,7 @@ private void AdjustFrequency(IEnumerable hashSet) //@ max value break; }*/ - DCSBIOS.Send(FM_RADIO_PRESET_COMMAND_INC); + await DCSBIOS.SendAsync(FM_RADIO_PRESET_COMMAND_INC); break; } @@ -1288,13 +1297,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf10SDialSkipper.Click(GetAdfCommand(AdfDigit.Digit10S, true)); + await _adf10SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digit10S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_MODE_COMMAND_INC); + await DCSBIOS.SendAsync(NADIR_MODE_COMMAND_INC); break; } } @@ -1320,7 +1329,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.VHFFM: { - DCSBIOS.Send(FM_RADIO_PRESET_COMMAND_DEC); + await DCSBIOS.SendAsync(FM_RADIO_PRESET_COMMAND_DEC); break; } @@ -1355,13 +1364,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf100SDialSkipper.Click(GetAdfCommand(AdfDigit.Digit100S, true)); + await _adf100SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digit100S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_MODE_COMMAND_DEC); + await DCSBIOS.SendAsync(NADIR_MODE_COMMAND_DEC); break; } } @@ -1420,13 +1429,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf1SDialSkipper.Click(GetAdfCommand(AdfDigit.Digits1S, true)); + await _adf1SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digits1S, true)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_DOPPLER_COMMAND_INC); + await DCSBIOS.SendAsync(NADIR_DOPPLER_COMMAND_INC); break; } } @@ -1485,13 +1494,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSA342RadioMode.ADF: { - _adf1SDialSkipper.Click(GetAdfCommand(AdfDigit.Digits1S, false)); + await _adf1SDialSkipper.ClickAsync(GetAdfCommand(AdfDigit.Digits1S, false)); break; } case CurrentSA342RadioMode.NADIR: { - DCSBIOS.Send(NADIR_DOPPLER_COMMAND_DEC); + await DCSBIOS.SendAsync(NADIR_DOPPLER_COMMAND_DEC); break; } } @@ -1518,199 +1527,197 @@ private void CheckFrequenciesForValidity() } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { - lock (LockLCDUpdateObject) + Interlocked.Increment(ref _doUpdatePanelLCD); + foreach (var radioPanelKnobObject in hashSet) { - Interlocked.Increment(ref _doUpdatePanelLCD); - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobSA342)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobSA342)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsSA342.UPPER_VHFAM: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsSA342.UPPER_VHFAM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.VHFAM; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.VHFAM; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_VHFFM: + case RadioPanelPZ69KnobsSA342.UPPER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.VHFFM; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_UHF: + case RadioPanelPZ69KnobsSA342.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_ADF: + case RadioPanelPZ69KnobsSA342.UPPER_ADF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.ADF; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.ADF; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_NADIR: + case RadioPanelPZ69KnobsSA342.UPPER_NADIR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.NADIR; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.NADIR; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_NAV2: - case RadioPanelPZ69KnobsSA342.UPPER_XPDR: + case RadioPanelPZ69KnobsSA342.UPPER_NAV2: + case RadioPanelPZ69KnobsSA342.UPPER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentSA342RadioMode.NO_USE; - } - break; + _currentUpperRadioMode = CurrentSA342RadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_VHFAM: + case RadioPanelPZ69KnobsSA342.LOWER_VHFAM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.VHFAM; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.VHFAM; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_VHFFM: + case RadioPanelPZ69KnobsSA342.LOWER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.VHFFM; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_UHF: + case RadioPanelPZ69KnobsSA342.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_ADF: + case RadioPanelPZ69KnobsSA342.LOWER_ADF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.ADF; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.ADF; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_NADIR: + case RadioPanelPZ69KnobsSA342.LOWER_NADIR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.NADIR; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.NADIR; } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_NAV2: - case RadioPanelPZ69KnobsSA342.LOWER_XPDR: + case RadioPanelPZ69KnobsSA342.LOWER_NAV2: + case RadioPanelPZ69KnobsSA342.LOWER_XPDR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentSA342RadioMode.NO_USE; - } - break; + _currentLowerRadioMode = CurrentSA342RadioMode.NO_USE; } + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsSA342.UPPER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsSA342.UPPER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsSA342.UPPER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsSA342.UPPER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsSA342.LOWER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsSA342.LOWER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsSA342.LOWER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsSA342.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsSA342.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsSA342.UPPER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342.UPPER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsSA342.UPPER_FREQ_SWITCH); } + break; + } - case RadioPanelPZ69KnobsSA342.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsSA342.LOWER_FREQ_SWITCH: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsSA342.LOWER_FREQ_SWITCH); - } - break; + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsSA342.LOWER_FREQ_SWITCH); } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_SA342, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_SA342, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } public override void ClearSettings(bool setIsDirty = false) @@ -1733,7 +1740,7 @@ private string GetVhfAmDialFrequencyFromRawValue(int dial, uint position) { case 0: { - for (int i = 0; i < _dialPositionsWholeNumbers.Length; i++) + for (var i = 0; i < _dialPositionsWholeNumbers.Length; i++) { if (_dialPositionsWholeNumbers[i] == position) { @@ -1768,7 +1775,7 @@ private string GetVhfAmDialFrequencyForPosition(VhfAmDigit vhfAmDigit, uint posi case VhfAmDigit.Third: case VhfAmDigit.Fourth: { - for (int i = 0; i < _dialPositionsWholeNumbers.Length; i++) + for (var i = 0; i < _dialPositionsWholeNumbers.Length; i++) { if (_dialPositionsWholeNumbers[i] == position) { @@ -1847,7 +1854,7 @@ private void SwapCockpitStandbyFrequencyVhfAm() private bool CorrectPositionWholeNumbers(uint value) { - for (int i = 0; i < _dialPositionsWholeNumbers.Length; i++) + for (var i = 0; i < _dialPositionsWholeNumbers.Length; i++) { if (_dialPositionsWholeNumbers[i] == value) { @@ -1859,9 +1866,9 @@ private bool CorrectPositionWholeNumbers(uint value) private bool CorrectPositionDecimal100S(uint value) { - for (int i = 0; i < _dialPositionsDecial100S.Length; i++) + for (var i = 0; i < _dialPositionsDecimal100S.Length; i++) { - if (_dialPositionsDecial100S[i] == value) + if (_dialPositionsDecimal100S[i] == value) { return true; } @@ -1869,11 +1876,6 @@ private bool CorrectPositionDecimal100S(uint value) return false; } - private bool VhfAmNowSyncing() - { - return Interlocked.Read(ref _vhfAmThreadNowSynching) > 0; - } - private static bool SwitchVhfAmDecimalDirectionUp(int cockpitValue, int desiredValue) { var upCount = 0; @@ -1964,3 +1966,4 @@ public override void AddOrUpdateBIPLinkBinding(PanelSwitchOnOff panelSwitchOnOff public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnOff, OSCommand operatingSystemCommand) { } } } + diff --git a/src/NonVisuals/Radios/RadioPanelPZ69SRS.cs b/src/NonVisuals/Radios/RadioPanelPZ69SRS.cs index bffd4cf43..eab0ffb60 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69SRS.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69SRS.cs @@ -1,4 +1,6 @@ -namespace NonVisuals.Radios +using System.Threading.Tasks; + +namespace NonVisuals.Radios { using System; using System.Collections.Generic; @@ -124,7 +126,7 @@ public void SRSDataReceived(object sender) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { @@ -346,6 +348,8 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { Logger.Error(ex); } + + return Task.CompletedTask; } private void AdjustFrequency(IEnumerable hashSet) diff --git a/src/NonVisuals/Radios/RadioPanelPZ69T45C.cs b/src/NonVisuals/Radios/RadioPanelPZ69T45C.cs index 521a5b1ed..1aebc30d7 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69T45C.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69T45C.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -73,28 +74,28 @@ Small dial private DCSBIOSOutput _vuhf1DcsbiosOutputDial3FrequencyNumber; private DCSBIOSOutput _vuhf1DcsbiosOutputDial4FrequencyNumber; - private const string VUHF1_1ST_DIAL_INCREASE = "COMM_1_FREQ_10 INC\n"; - private const string VUHF1_1ST_DIAL_DECREASE = "COMM_1_FREQ_10 DEC\n"; - private const string VUHF1_1ST_DIAL_NEUTRAL = "COMM_1_FREQ_10 1\n"; + private const string VUHF1_1_ST_DIAL_INCREASE = "COMM_1_FREQ_10 INC\n"; + private const string VUHF1_1_ST_DIAL_DECREASE = "COMM_1_FREQ_10 DEC\n"; + private const string VUHF1_1_ST_DIAL_NEUTRAL = "COMM_1_FREQ_10 1\n"; - private const string VUHF1_2ND_DIAL_INCREASE = "COMM_1_FREQ_1 INC\n"; - private const string VUHF1_2ND_DIAL_DECREASE = "COMM_1_FREQ_1 DEC\n"; - private const string VUHF1_2ND_DIAL_NEUTRAL = "COMM_1_FREQ_1 1\n"; + private const string VUHF1_2_ND_DIAL_INCREASE = "COMM_1_FREQ_1 INC\n"; + private const string VUHF1_2_ND_DIAL_DECREASE = "COMM_1_FREQ_1 DEC\n"; + private const string VUHF1_2_ND_DIAL_NEUTRAL = "COMM_1_FREQ_1 1\n"; - private const string VUHF1_3RD_DIAL_INCREASE = "COMM_1_FREQ_010 INC\n"; - private const string VUHF1_3RD_DIAL_DECREASE = "COMM_1_FREQ_010 DEC\n"; - private const string VUHF1_3RD_DIAL_NEUTRAL = "COMM_1_FREQ_010 1\n"; + private const string VUHF1_3_RD_DIAL_INCREASE = "COMM_1_FREQ_010 INC\n"; + private const string VUHF1_3_RD_DIAL_DECREASE = "COMM_1_FREQ_010 DEC\n"; + private const string VUHF1_3_RD_DIAL_NEUTRAL = "COMM_1_FREQ_010 1\n"; - private const string VUHF1_4TH_DIAL_INCREASE = "COMM_1_FREQ_100 INC\n"; - private const string VUHF1_4TH_DIAL_DECREASE = "COMM_1_FREQ_100 DEC\n"; - private const string VUHF1_4TH_DIAL_NEUTRAL = "COMM_1_FREQ_100 1\n"; + private const string VUHF1_4_TH_DIAL_INCREASE = "COMM_1_FREQ_100 INC\n"; + private const string VUHF1_4_TH_DIAL_DECREASE = "COMM_1_FREQ_100 DEC\n"; + private const string VUHF1_4_TH_DIAL_NEUTRAL = "COMM_1_FREQ_100 1\n"; /*private DCSBIOSOutput _vuhf1DcsbiosOutputMode; private volatile uint _vuhf1CockpitMode; // OFF = 0*/ //private readonly ClickSpeedDetector _vuhf1ModeClickSpeedDetector = new(8); private byte _skipVuhf1SmallFreqChange; - private long _vuhf1ThreadNowSynching; - private Thread _vuhf1SyncThread; + private Task _vuhf1SyncTask; + private CancellationTokenSource _vuhf1SyncTaskTokenSource = new(); private long _vuhf1Dial1WaitingForFeedback; private long _vuhf1Dial2WaitingForFeedback; private long _vuhf1Dial3WaitingForFeedback; @@ -128,28 +129,28 @@ Small dial private DCSBIOSOutput _vuhf2DcsbiosOutputDial3FrequencyNumber; private DCSBIOSOutput _vuhf2DcsbiosOutputDial4FrequencyNumber; - private const string VUHF2_1ST_DIAL_INCREASE = "COMM_2_FREQ_10 INC\n"; - private const string VUHF2_1ST_DIAL_DECREASE = "COMM_2_FREQ_10 DEC\n"; - private const string VUHF2_1ST_DIAL_NEUTRAL = "COMM_2_FREQ_10 1\n"; + private const string VUHF2_1_ST_DIAL_INCREASE = "COMM_2_FREQ_10 INC\n"; + private const string VUHF2_1_ST_DIAL_DECREASE = "COMM_2_FREQ_10 DEC\n"; + private const string VUHF2_1_ST_DIAL_NEUTRAL = "COMM_2_FREQ_10 1\n"; - private const string VUHF2_2ND_DIAL_INCREASE = "COMM_2_FREQ_1 INC\n"; - private const string VUHF2_2ND_DIAL_DECREASE = "COMM_2_FREQ_1 DEC\n"; - private const string VUHF2_2ND_DIAL_NEUTRAL = "COMM_2_FREQ_1 1\n"; + private const string VUHF2_2_ND_DIAL_INCREASE = "COMM_2_FREQ_1 INC\n"; + private const string VUHF2_2_ND_DIAL_DECREASE = "COMM_2_FREQ_1 DEC\n"; + private const string VUHF2_2_ND_DIAL_NEUTRAL = "COMM_2_FREQ_1 1\n"; - private const string VUHF2_3RD_DIAL_INCREASE = "COMM_2_FREQ_010 INC\n"; - private const string VUHF2_3RD_DIAL_DECREASE = "COMM_2_FREQ_010 DEC\n"; - private const string VUHF2_3RD_DIAL_NEUTRAL = "COMM_2_FREQ_010 1\n"; + private const string VUHF2_3_RD_DIAL_INCREASE = "COMM_2_FREQ_010 INC\n"; + private const string VUHF2_3_RD_DIAL_DECREASE = "COMM_2_FREQ_010 DEC\n"; + private const string VUHF2_3_RD_DIAL_NEUTRAL = "COMM_2_FREQ_010 1\n"; - private const string VUHF2_4TH_DIAL_INCREASE = "COMM_2_FREQ_100 INC\n"; - private const string VUHF2_4TH_DIAL_DECREASE = "COMM_2_FREQ_100 DEC\n"; - private const string VUHF2_4TH_DIAL_NEUTRAL = "COMM_2_FREQ_100 1\n"; + private const string VUHF2_4_TH_DIAL_INCREASE = "COMM_2_FREQ_100 INC\n"; + private const string VUHF2_4_TH_DIAL_DECREASE = "COMM_2_FREQ_100 DEC\n"; + private const string VUHF2_4_TH_DIAL_NEUTRAL = "COMM_2_FREQ_100 1\n"; /*private DCSBIOSOutput _vuhf2DcsbiosOutputMode; private volatile uint _vuhf2CockpitMode; // OFF = 0 private readonly ClickSpeedDetector _vuhf2ModeClickSpeedDetector = new(8);*/ private byte _skipVuhf2SmallFreqChange; - private long _vuhf2ThreadNowSynching; - private Thread _vuhf2SyncThread; + private Task _vuhf2SyncTask; + private CancellationTokenSource _vuhf2SyncTaskTokenSource = new(); private long _vuhf2Dial1WaitingForFeedback; private long _vuhf2Dial2WaitingForFeedback; private long _vuhf2Dial3WaitingForFeedback; @@ -170,8 +171,8 @@ Small dial private volatile uint _tacanCockpitOnesDialPos = 1; private const string TACAN_TENS_DIAL_COMMAND = "TACAN_CHAN_10 "; private const string TACAN_ONES_DIAL_COMMAND = "TACAN_CHAN_1 "; - private Thread _tacanSyncThread; - private long _tacanThreadNowSynching; + private Task _tacanSyncTask; + private CancellationTokenSource _tacanSyncTaskTokenSource = new(); private long _tacanTensWaitingForFeedback; private long _tacanOnesWaitingForFeedback; @@ -190,8 +191,8 @@ Small dial private volatile uint _vorCockpitKhzDialPos = 2; private const string VOR_MHZ_DIAL_COMMAND = "VOR_ILS_FREQ_1 "; private const string VOR_KHZ_DIAL_COMMAND = "VOR_ILS_FREQ_50 "; - private Thread _vorSyncThread; - private long _vorThreadNowSynching; + private Task _vorSyncTask; + private CancellationTokenSource _vorSyncTaskTokenSource = new(); private long _vorMhzWaitingForFeedback; private long _vorKhzWaitingForFeedback; @@ -200,7 +201,7 @@ Small dial private long _doUpdatePanelLCD; public RadioPanelPZ69T45C(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -211,10 +212,10 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownVUHF1Thread = true; - _shutdownTACANThread = true; - _shutdownVORThread = true; - _shutdownVUHF2Thread = true; + _vuhf1SyncTaskTokenSource.Cancel(); + _tacanSyncTaskTokenSource.Cancel(); + _vorSyncTaskTokenSource.Cancel(); + _vuhf2SyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -491,7 +492,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsT45C knob) private void SendVUHF1ToDCSBIOS() { - if (VUHF1NowSyncing()) + if (_vuhf1SyncTask?.Status == TaskStatus.Running) { return; } @@ -523,168 +524,185 @@ private void SendVUHF1ToDCSBIOS() desiredDial4Value = int.Parse(frequencyAsString.Substring(5, 2)); } - _shutdownVUHF1Thread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVUHF1Thread = false; - _vuhf1SyncThread = new Thread(() => VUHF1SynchThreadMethod(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value)); - _vuhf1SyncThread.Start(); + _vuhf1SyncTaskTokenSource = new(); + _vuhf1SyncTask = Task.Run(() => SyncVUHF1Async(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value, _vuhf1SyncTaskTokenSource.Token)); } - private volatile bool _shutdownVUHF1Thread; - private void VUHF1SynchThreadMethod(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4) + private async Task SyncVUHF1Async(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + do { - Interlocked.Exchange(ref _vuhf1ThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf1Dial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vuhf1Dial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf1Dial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf1Dial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf1Dial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf1Dial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf1Dial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf1Dial4WaitingForFeedback); // Let's do an ugly reset + } - if (Interlocked.Read(ref _vuhf1Dial1WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vuhf1Dial1WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVuhf1BigFreqObject) { - lock (_lockVuhf1BigFreqObject) + if (_vuhf1CockpitDial1Frequency != desiredValueDial1) { - if (_vuhf1CockpitDial1Frequency != desiredValueDial1) - { - dial1OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf1CockpitDial1Frequency < desiredValueDial1 ? VUHF1_1ST_DIAL_INCREASE : VUHF1_1ST_DIAL_DECREASE); - DCSBIOS.Send(VUHF1_1ST_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf1Dial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = _vuhf1CockpitDial1Frequency < desiredValueDial1 ? VUHF1_1_ST_DIAL_INCREASE : VUHF1_1_ST_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF1_1_ST_DIAL_NEUTRAL); + dial1SendCount++; + Interlocked.Exchange(ref _vuhf1Dial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhf1Dial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhf1Dial2WaitingForFeedback) == 0) + lock (_lockVuhf1BigFreqObject) { - lock (_lockVuhf1BigFreqObject) + if (_vuhf1CockpitDial2Frequency != desiredValueDial2) { - if (_vuhf1CockpitDial2Frequency != desiredValueDial2) - { - dial2OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf1CockpitDial2Frequency < desiredValueDial2 ? VUHF1_2ND_DIAL_INCREASE : VUHF1_2ND_DIAL_DECREASE); - DCSBIOS.Send(VUHF1_2ND_DIAL_NEUTRAL); - dial2SendCount++; - Interlocked.Exchange(ref _vuhf1Dial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = _vuhf1CockpitDial2Frequency < desiredValueDial2 ? VUHF1_2_ND_DIAL_INCREASE : VUHF1_2_ND_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF1_2_ND_DIAL_NEUTRAL); + dial2SendCount++; + Interlocked.Exchange(ref _vuhf1Dial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhf1Dial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhf1Dial3WaitingForFeedback) == 0) + lock (_lockVuhf1Dial3FreqObject) { - lock (_lockVuhf1Dial3FreqObject) + if (_vuhf1CockpitDial3Frequency != desiredValueDial3) { - if (_vuhf1CockpitDial3Frequency != desiredValueDial3) - { - dial3OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf1CockpitDial3Frequency < desiredValueDial3 ? VUHF1_3RD_DIAL_INCREASE : VUHF1_3RD_DIAL_DECREASE); - DCSBIOS.Send(VUHF1_3RD_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf1Dial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = _vuhf1CockpitDial3Frequency < desiredValueDial3 ? VUHF1_3_RD_DIAL_INCREASE : VUHF1_3_RD_DIAL_DECREASE; } - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF1_3_RD_DIAL_NEUTRAL); + dial3SendCount++; + Interlocked.Exchange(ref _vuhf1Dial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhf1Dial4WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhf1Dial4WaitingForFeedback) == 0) + lock (_lockVuhf1Dial4FreqObject) { - lock (_lockVuhf1Dial4FreqObject) + if (_vuhf1CockpitDial4Frequency != desiredValueDial4) { - if (_vuhf1CockpitDial4Frequency != desiredValueDial4) - { - dial4OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf1CockpitDial4Frequency < desiredValueDial4 ? VUHF1_4TH_DIAL_INCREASE : VUHF1_4TH_DIAL_DECREASE); - DCSBIOS.Send(VUHF1_4TH_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf1Dial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = _vuhf1CockpitDial4Frequency < desiredValueDial4 ? VUHF1_4_TH_DIAL_INCREASE : VUHF1_4_TH_DIAL_DECREASE; } } - else - { - dial4OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF1_4_TH_DIAL_NEUTRAL); + dial4SendCount++; + Interlocked.Exchange(ref _vuhf1Dial4WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial4Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVUHF1Thread); - SwapCockpitStandbyFrequencyVuhf1(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + + SwapCockpitStandbyFrequencyVuhf1(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vuhf1ThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendVUHF2ToDCSBIOS() { - if (VUHF2NowSyncing()) + if (_vuhf2SyncTask?.Status == TaskStatus.Running) { return; } @@ -716,168 +734,185 @@ private void SendVUHF2ToDCSBIOS() desiredDial4Value = int.Parse(frequencyAsString.Substring(5, 2)); } - _shutdownVUHF2Thread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVUHF2Thread = false; - _vuhf2SyncThread = new Thread(() => VUHF2SynchThreadMethod(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value)); - _vuhf2SyncThread.Start(); + _vuhf2SyncTaskTokenSource = new(); + _vuhf2SyncTask = Task.Run(() => SyncVUHF2Async(desiredDial1Value, desiredDial2Value, desiredDial3Value, desiredDial4Value, _vuhf2SyncTaskTokenSource.Token)); } - private volatile bool _shutdownVUHF2Thread; - private void VUHF2SynchThreadMethod(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4) + private async Task SyncVUHF2Async(int desiredValueDial1, int desiredValueDial2, int desiredValueDial3, int desiredValueDial4, CancellationToken cancellationToken) { try { - try + var dial1Timeout = DateTime.Now.Ticks; + var dial2Timeout = DateTime.Now.Ticks; + var dial3Timeout = DateTime.Now.Ticks; + var dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + do { - Interlocked.Exchange(ref _vuhf2ThreadNowSynching, 1); - var dial1Timeout = DateTime.Now.Ticks; - var dial2Timeout = DateTime.Now.Ticks; - var dial3Timeout = DateTime.Now.Ticks; - var dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - do + if (IsTimedOut(ref dial1Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf2Dial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vuhf2Dial1WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf2Dial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf2Dial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf2Dial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf2Dial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vuhf2Dial4WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vuhf2Dial4WaitingForFeedback); // Let's do an ugly reset + } - if (Interlocked.Read(ref _vuhf2Dial1WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vuhf2Dial1WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVuhf2BigFreqObject) { - lock (_lockVuhf2BigFreqObject) + if (_vuhf2CockpitDial1Frequency != desiredValueDial1) { - if (_vuhf2CockpitDial1Frequency != desiredValueDial1) - { - dial1OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf2CockpitDial1Frequency < desiredValueDial1 ? VUHF2_1ST_DIAL_INCREASE : VUHF2_1ST_DIAL_DECREASE); - DCSBIOS.Send(VUHF2_1ST_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf2Dial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = _vuhf2CockpitDial1Frequency < desiredValueDial1 ? VUHF2_1_ST_DIAL_INCREASE : VUHF2_1_ST_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF2_1_ST_DIAL_NEUTRAL); + dial1SendCount++; + Interlocked.Exchange(ref _vuhf2Dial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhf2Dial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhf2Dial2WaitingForFeedback) == 0) + lock (_lockVuhf2BigFreqObject) { - lock (_lockVuhf2BigFreqObject) + if (_vuhf2CockpitDial2Frequency != desiredValueDial2) { - if (_vuhf2CockpitDial2Frequency != desiredValueDial2) - { - dial2OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf2CockpitDial2Frequency < desiredValueDial2 ? VUHF2_2ND_DIAL_INCREASE : VUHF2_2ND_DIAL_DECREASE); - DCSBIOS.Send(VUHF2_2ND_DIAL_NEUTRAL); - dial2SendCount++; - Interlocked.Exchange(ref _vuhf2Dial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = _vuhf2CockpitDial2Frequency < desiredValueDial2 ? VUHF2_2_ND_DIAL_INCREASE : VUHF2_2_ND_DIAL_DECREASE; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF2_2_ND_DIAL_NEUTRAL); + dial2SendCount++; + Interlocked.Exchange(ref _vuhf2Dial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vuhf2Dial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vuhf2Dial3WaitingForFeedback) == 0) + lock (_lockVuhf2Dial3FreqObject) { - lock (_lockVuhf2Dial3FreqObject) + if (_vuhf2CockpitDial3Frequency != desiredValueDial3) { - if (_vuhf2CockpitDial3Frequency != desiredValueDial3) - { - dial3OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf2CockpitDial3Frequency < desiredValueDial3 ? VUHF2_3RD_DIAL_INCREASE : VUHF2_3RD_DIAL_DECREASE); - DCSBIOS.Send(VUHF2_3RD_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf2Dial3WaitingForFeedback, 1); - } + dial3OkTime = DateTime.Now.Ticks; + command = _vuhf2CockpitDial3Frequency < desiredValueDial3 ? VUHF2_3_RD_DIAL_INCREASE : VUHF2_3_RD_DIAL_DECREASE; } - Reset(ref dial3Timeout); } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF2_3_RD_DIAL_NEUTRAL); + dial3SendCount++; + Interlocked.Exchange(ref _vuhf2Dial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vuhf2Dial4WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vuhf2Dial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVuhf2Dial4FreqObject) { - lock (_lockVuhf2Dial4FreqObject) + if (_vuhf2CockpitDial4Frequency != desiredValueDial4) { - if (_vuhf2CockpitDial4Frequency != desiredValueDial4) - { - dial4OkTime = DateTime.Now.Ticks; - DCSBIOS.Send(_vuhf2CockpitDial4Frequency < desiredValueDial4 ? VUHF2_4TH_DIAL_INCREASE : VUHF2_4TH_DIAL_DECREASE); - DCSBIOS.Send(VUHF2_4TH_DIAL_NEUTRAL); - dial1SendCount++; - Interlocked.Exchange(ref _vuhf2Dial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); + dial4OkTime = DateTime.Now.Ticks; + command = _vuhf2CockpitDial4Frequency < desiredValueDial4 ? VUHF2_4_TH_DIAL_INCREASE : VUHF2_4_TH_DIAL_DECREASE; } } - else - { - dial4OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + await DCSBIOS.SendAsync(VUHF2_4_TH_DIAL_NEUTRAL); + dial4SendCount++; + Interlocked.Exchange(ref _vuhf2Dial4WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial4Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVUHF2Thread); - SwapCockpitStandbyFrequencyVuhf2(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial4OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 5) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + + SwapCockpitStandbyFrequencyVuhf2(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vuhf2ThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendTacanToDCSBIOS() { - if (TacanNowSyncing()) + if (_tacanSyncTask?.Status == TaskStatus.Running) { return; } @@ -894,105 +929,104 @@ private void SendTacanToDCSBIOS() // 120 // #1 = 12 (position = value) // #2 = 0 (position = value) - _shutdownTACANThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownTACANThread = false; - _tacanSyncThread = new Thread(() => TacanSynchThreadMethod(_tacanTensFrequencyStandby, _tacanOnesFrequencyStandby)); - _tacanSyncThread.Start(); + _tacanSyncTaskTokenSource = new(); + _tacanSyncTask = Task.Run(() => SyncTacanAsync(_tacanTensFrequencyStandby, _tacanOnesFrequencyStandby, _tacanSyncTaskTokenSource.Token)); } - private volatile bool _shutdownTACANThread; - private void TacanSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2) + private async Task SyncTacanAsync(int desiredPositionDial1, int desiredPositionDial2, CancellationToken cancellationToken) { try { - try - { - Interlocked.Exchange(ref _tacanThreadNowSynching, 1); + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; + do + { + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _tacanTensWaitingForFeedback); // Let's do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _tacanTensWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _tacanOnesWaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _tacanOnesWaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _tacanTensWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _tacanTensWaitingForFeedback) == 0) + lock (_lockTacanTensDialObject) { - lock (_lockTacanTensDialObject) + if (_tacanCockpitTensDialPos != desiredPositionDial1) { - if (_tacanCockpitTensDialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = TACAN_TENS_DIAL_COMMAND + (_tacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _tacanTensWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = TACAN_TENS_DIAL_COMMAND + (_tacanCockpitTensDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _tacanTensWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _tacanOnesWaitingForFeedback) == 0) + if (Interlocked.Read(ref _tacanOnesWaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockTacanOnesObject) { - // Common.DebugP("b"); - lock (_lockTacanOnesObject) + if (_tacanCockpitOnesDialPos != desiredPositionDial2) { - if (_tacanCockpitOnesDialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - var str = TACAN_ONES_DIAL_COMMAND + (_tacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _tacanOnesWaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = TACAN_ONES_DIAL_COMMAND + (_tacanCockpitOnesDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10 /*|| dial3SendCount > 2*/) + + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _tacanOnesWaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial2Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownTACANThread); - SwapCockpitStandbyFrequencyTacan(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial2OkTime = DateTime.Now.Ticks; + } + if (dial1SendCount > 12 || dial2SendCount > 10 /*|| dial3SendCount > 2*/) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); + + SwapCockpitStandbyFrequencyTacan(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _tacanThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); @@ -1000,114 +1034,115 @@ private void TacanSynchThreadMethod(int desiredPositionDial1, int desiredPositio private void SendVorToDCSBIOS() { - if (VorNowSyncing()) + if (_vorSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyVor(); - _shutdownVORThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVORThread = false; - _vorSyncThread = new Thread(() => VorSynchThreadMethod(_vorMhzFrequencyStandby, _vorKhzFrequencyStandby)); - _vorSyncThread.Start(); + + _vorSyncTaskTokenSource = new(); + _vorSyncTask = Task.Run(() => SyncVorAsync(_vorMhzFrequencyStandby, _vorKhzFrequencyStandby, _vorSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVORThread; - private void VorSynchThreadMethod(int desiredPositionDial1, int desiredPositionDial2) + + private async Task SyncVorAsync(int desiredPositionDial1, int desiredPositionDial2, CancellationToken cancellationToken) { try { - try - { - Interlocked.Exchange(ref _vorThreadNowSynching, 1); + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; + do + { + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _vorMhzWaitingForFeedback); // Let's do an ugly reset + } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vorMhzWaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vorKhzWaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vorKhzWaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _vorMhzWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vorMhzWaitingForFeedback) == 0) + lock (_lockVorMhzDialObject) { - - lock (_lockVorMhzDialObject) + if (_vorCockpitMhzDialPos != desiredPositionDial1) { - if (_vorCockpitMhzDialPos != desiredPositionDial1) - { - dial1OkTime = DateTime.Now.Ticks; - var str = VOR_MHZ_DIAL_COMMAND + (_vorCockpitMhzDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vorMhzWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = VOR_MHZ_DIAL_COMMAND + (_vorCockpitMhzDialPos < desiredPositionDial1 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vorMhzWaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vorKhzWaitingForFeedback) == 0) + if (Interlocked.Read(ref _vorKhzWaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVorKhzObject) { - // Common.DebugP("b"); - lock (_lockVorKhzObject) + if (_vorCockpitKhzDialPos != desiredPositionDial2) { - if (_vorCockpitKhzDialPos != desiredPositionDial2) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - var str = VOR_KHZ_DIAL_COMMAND + (_vorCockpitKhzDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vorKhzWaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + command = VOR_KHZ_DIAL_COMMAND + (_vorCockpitKhzDialPos < desiredPositionDial2 ? DCSBIOS_INCREASE_COMMAND : DCSBIOS_DECREASE_COMMAND); } } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 12 || dial2SendCount > 10) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vorKhzWaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial2Timeout); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownVORThread); - SwapStandbyFrequencyVor(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (dial1SendCount > 12 || dial2SendCount > 10) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); + + SwapStandbyFrequencyVor(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vorThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } + Interlocked.Increment(ref _doUpdatePanelLCD); } @@ -1757,6 +1792,7 @@ private void AdjustFrequency(IEnumerable hashSet) } } } + Interlocked.Increment(ref _doUpdatePanelLCD); ShowFrequenciesOnPanel(); } @@ -1986,7 +2022,7 @@ private void CheckFrequenciesForValidity() } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override Task PZ69KnobChangedAsync(IEnumerable hashSet) { lock (LockLCDUpdateObject) { @@ -2138,7 +2174,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_upperButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsT45C.UPPER_FREQ_SWITCH); } @@ -2155,7 +2191,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_lowerButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsT45C.LOWER_FREQ_SWITCH); } @@ -2171,8 +2207,11 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) PluginManager.DoEvent(DCSAircraft.SelectedAircraft.Description, HIDInstance, PluginGamingPanelEnum.PZ69RadioPanel_PreProg_T45C, (int)radioPanelKnob.RadioPanelPZ69Knob, radioPanelKnob.IsOn, null); } } + AdjustFrequency(hashSet); } + + return Task.CompletedTask; } public override void ClearSettings(bool setIsDirty = false) { } @@ -2181,7 +2220,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobT45C.GetRadioPanelKnobs(); @@ -2290,88 +2329,12 @@ private void SwapStandbyFrequencyVor() _vorKhzFrequencyStandby = _vorSavedCockpitKhzFrequency; } - private bool TacanNowSyncing() - { - return Interlocked.Read(ref _tacanThreadNowSynching) > 0; - } - - private bool VorNowSyncing() - { - return Interlocked.Read(ref _vorThreadNowSynching) > 0; - } - - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) - { - } - - public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) - { - } - - public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) - { - } - - public override void AddOrUpdateDCSBIOSBinding(PanelSwitchOnOff panelSwitchOnOff, List dcsbiosInputs, string description, bool isSequenced) - { - } - - public override void AddOrUpdateBIPLinkBinding(PanelSwitchOnOff panelSwitchOnOff, BIPLinkBase bipLink) - { - } - - public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnOff, OSCommand operatingSystemCommand) - { - } - - private bool VUHF1NowSyncing() - { - return Interlocked.Read(ref _vuhf1ThreadNowSynching) > 0; - } - - private bool VUHF2NowSyncing() - { - return Interlocked.Read(ref _vuhf2ThreadNowSynching) > 0; - } - - /*private static string GetCommandDirection10Dial(int desiredDialPosition, uint actualDialPosition) - { - var counterUp = 0; - var counterDown = 0; - - var tmpActual = (int)actualDialPosition; - while (true) - { - counterUp++; - tmpActual++; - if (tmpActual > 9) - { - tmpActual = 0; - } - - if (tmpActual == desiredDialPosition) - { - break; - } - } - - tmpActual = (int)actualDialPosition; - while (true) - { - counterDown++; - tmpActual--; - if (tmpActual < 0) - { - tmpActual = 9; - } - - if (tmpActual == desiredDialPosition) - { - break; - } - } - return counterUp > counterDown ? DCSBIOS_DECREASE_COMMAND : DCSBIOS_INCREASE_COMMAND; - }*/ + public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } + public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) { } + public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) { } + public override void AddOrUpdateDCSBIOSBinding(PanelSwitchOnOff panelSwitchOnOff, List dcsbiosInputs, string description, bool isSequenced) { } + public override void AddOrUpdateBIPLinkBinding(PanelSwitchOnOff panelSwitchOnOff, BIPLinkBase bipLink) { } + public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnOff, OSCommand operatingSystemCommand) { } } } diff --git a/src/NonVisuals/Radios/RadioPanelPZ69UH1H.cs b/src/NonVisuals/Radios/RadioPanelPZ69UH1H.cs index 9ae8c53df..93a1111af 100644 --- a/src/NonVisuals/Radios/RadioPanelPZ69UH1H.cs +++ b/src/NonVisuals/Radios/RadioPanelPZ69UH1H.cs @@ -1,3 +1,4 @@ +using System.Threading.Tasks; using NonVisuals.BindingClasses.BIP; using NonVisuals.Helpers; @@ -63,12 +64,11 @@ private enum CurrentUH1HRadioMode // Small dial 0.000 - 0.975 [step 0 2 5 7] private readonly object _lockVhfCommDialsObject1 = new(); private readonly object _lockVhfCommDialsObject2 = new(); - private volatile uint _vhfCommBigFrequencyStandby = 116; - private volatile uint _vhfCommSmallFrequencyStandby; + private uint _vhfCommBigFrequencyStandby = 116; + private uint _vhfCommSmallFrequencyStandby; private volatile uint _vhfCommSavedCockpitSmallFrequency; private volatile uint _vhfCommSavedCockpitBigFrequency = 116; private double _vhfCommCockpitFrequency = 116.00; - private long _vhfCommThreadNowSynching; private volatile uint _vhfCommCockpitDial1Frequency = 116; private volatile uint _vhfCommCockpitDial2Frequency = 95; private long _vhfCommDial1FreqWaitingForFeedback; @@ -76,7 +76,8 @@ private enum CurrentUH1HRadioMode private DCSBIOSOutput _vhfCommDcsbiosOutputCockpitFrequency; private const string VHF_COMM_FREQ1_DIAL_COMMAND = "VHFCOMM_MHZ "; private const string VHF_COMM_FREQ2_DIAL_COMMAND = "VHFCOMM_KHZ "; - private Thread _vhfCommSyncThread; + private Task _vhfCommSyncTask; + private CancellationTokenSource _vhfCommSyncTaskTokenSource = new(); /*AN/ARC-51BX UHF radio set*/ /* UHF 225.00 to 399.95 MHz */ @@ -104,8 +105,8 @@ private enum CurrentUH1HRadioMode private const string UHF_FREQ1_DIAL_COMMAND = "UHF_10MHZ "; // 20-39 private const string UHF_FREQ2_DIAL_COMMAND = "UHF_1MHZ "; // 0 1 2 3 4 5 6 7 8 9 private const string UHF_FREQ3_DIAL_COMMAND = "UHF_50KHZ "; // 00 - 95 - private Thread _uhfSyncThread; - private long _uhfThreadNowSynching; + private Task _uhfSyncTask; + private CancellationTokenSource _uhfSyncTaskTokenSource = new(); private long _uhfDial1WaitingForFeedback; private long _uhfDial2WaitingForFeedback; private long _uhfDial3WaitingForFeedback; @@ -116,8 +117,8 @@ private enum CurrentUH1HRadioMode // Small dial 0.00-0.95 [step of 0.05] private readonly object _lockVhfNavDialsObject1 = new(); private readonly object _lockVhfNavDialsObject2 = new(); - private volatile uint _vhfNavBigFrequencyStandby = 107; - private volatile uint _vhfNavSmallFrequencyStandby; + private uint _vhfNavBigFrequencyStandby = 107; + private uint _vhfNavSmallFrequencyStandby; private volatile uint _vhfNavSavedCockpitBigFrequency = 107; private volatile uint _vhfNavSavedCockpitSmallFrequency; private DCSBIOSOutput _vhfNavDcsbiosOutputCockpitFrequency; @@ -126,8 +127,8 @@ private enum CurrentUH1HRadioMode private volatile uint _vhfNavCockpitDial2Frequency; private const string VHF_NAV_FREQ1_DIAL_COMMAND = "VHFNAV_MHZ "; private const string VHF_NAV_FREQ2_DIAL_COMMAND = "VHFNAV_KHZ "; - private Thread _vhfNavSyncThread; - private long _vhfNavThreadNowSynching; + private Task _vhfNavSyncTask; + private CancellationTokenSource _vhfNavSyncTaskTokenSource = new(); private long _vhfNavDial1WaitingForFeedback; private long _vhfNavDial2WaitingForFeedback; @@ -153,13 +154,12 @@ private enum CurrentUH1HRadioMode private const string VHF_FM_FREQ_2DIAL_COMMAND = "VHFFM_FREQ2 "; // 0 1 2 3 4 5 6 7 8 9 private const string VHF_FM_FREQ_3DIAL_COMMAND = "VHFFM_FREQ3 "; // 0 1 2 3 4 5 6 7 8 9 private const string VHF_FM_FREQ_4DIAL_COMMAND = "VHFFM_FREQ4 "; // 0 5 - private Thread _vhfFmSyncThread; - private long _vhfFmThreadNowSynching; + private Task _vhfFmSyncTask; + private CancellationTokenSource _vhfFmSyncTaskTokenSource = new(); private long _vhfFmDial1WaitingForFeedback; private long _vhfFmDial2WaitingForFeedback; private long _vhfFmDial3WaitingForFeedback; private long _vhfFmDial4WaitingForFeedback; - private volatile bool _shutdownVHFFMThread; /*UH-1H ADF*/ /* @@ -189,15 +189,15 @@ STANDBY SCREEN - SIGNAL STRENGTH private const string ADF_GAIN_KNOB_COMMAND_INC = "ADF_GAIN -2000\n"; private const string ADF_GAIN_KNOB_COMMAND_DEC = "ADF_GAIN +2000\n"; private const string ADF_FREQUENCY_BAND_COMMAND = "ADF_BAND "; - private Thread _adfSyncThread; - private long _adfThreadNowSynching; + private Task _adfSyncTask; + private CancellationTokenSource _adfSyncTaskTokenSource = new(); private long _adfFrequencyBandWaitingForFeedback; private readonly object _lockShowFrequenciesOnPanelObject = new(); private long _doUpdatePanelLCD; public RadioPanelPZ69UH1H(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -208,11 +208,11 @@ protected override void Dispose(bool disposing) { if (disposing) { - _shutdownVHFCommThread = true; - _shutdownUHFThread = true; - _shutdownVHFNavThread = true; - _shutdownVHFFMThread = true; - _shutdownADFThread = true; + _vhfCommSyncTaskTokenSource.Cancel(); + _uhfSyncTaskTokenSource.Cancel(); + _vhfNavSyncTaskTokenSource.Cancel(); + _vhfFmSyncTaskTokenSource.Cancel(); + _adfSyncTaskTokenSource.Cancel(); BIOSEventHandler.DetachStringListener(this); BIOSEventHandler.DetachDataListener(this); } @@ -303,7 +303,7 @@ public void DCSBIOSStringReceived(object sender, DCSBIOSStringDataEventArgs e) _vhfCommCockpitDial2Frequency = uint.Parse(e.StringData.Substring(4, 2)); // 975 - // Do not round this. Rounding means that the synch process thinks the frequency is OK which it isn't + // Do not round this. Rounding means that the sync process thinks the frequency is OK which it isn't if (tmp != _vhfCommCockpitDial2Frequency) { Interlocked.Increment(ref _doUpdatePanelLCD); @@ -570,7 +570,7 @@ private void UpdateCockpitAdfFrequency() } } */ - private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H knob) + private async Task SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsUH1H knob) { if (IgnoreSwitchButtonOnce() && (knob == RadioPanelPZ69KnobsUH1H.UPPER_FREQ_SWITCH || knob == RadioPanelPZ69KnobsUH1H.LOWER_FREQ_SWITCH)) { @@ -594,7 +594,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H knob) { case CurrentUH1HRadioMode.INTERCOMM: { - SendUhfPresetChannelChangeToDCSBIOS(); + await SendUhfPresetChannelChangeToDCSBIOSAsync(); break; } @@ -637,7 +637,7 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H knob) { case CurrentUH1HRadioMode.INTERCOMM: { - SendUhfPresetChannelChangeToDCSBIOS(); + await SendUhfPresetChannelChangeToDCSBIOSAsync(); break; } @@ -676,96 +676,9 @@ private void SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H knob) } } - /* - private void SendIntercommToDCSBIOS() - { - if (IntercommSyncing()) - { - return; - } - SaveCockpitIntercomm(); - if (_intercommSyncThread != null) - { - _intercommSyncThread.Aborttt(); - } - - _intercommSyncThread = new Thread(IntercommSynchThreadMethod); - _intercommSyncThread.Start(); - } - - private void IntercommSynchThreadMethod() - { - try - { - try - { - String str; - Interlocked.Exchange(ref _intercommThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - var dial1SendCount = 0; - - do - { - if (IsTimedOut(ref dial1Timeout, ResetSyncTimeout, "INTERCOMM dial1Timeout")) - { - ResetWaitingForFeedBack(ref _intercommDialWaitingForFeedback); //Lets do an ugly reset - Common.DebugP("Resetting SYNC for INTERCOMM"); - } - - //0 - 5 - if (Interlocked.Read(ref _intercommDialWaitingForFeedback) == 0) - { - lock (_lockIntercommDialObject) - { - - - if (_intercommDialPosStandby != _intercommCockpitDial1Pos) - { - dial1OkTime = DateTime.Now.Ticks; - str = IntercommDialCommand + (_intercommDialPosStandby < _intercommCockpitDial1Pos ? Decrease : Increase); - Common.DebugP("Sending " + str); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _intercommDialWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); - } - } - else - { - dial1OkTime = DateTime.Now.Ticks; - } - - if (dial1SendCount > 5) - { - //"Race" condition detected? - dial1SendCount = 0; - Thread.Sleep(5000); - } - - Thread.Sleep(SynchSleepTime); //Should be enough to get an update cycle from DCS-BIOS - } while (IsTooShort(dial1OkTime)); - - SwapCockpitIntercomm(); - ShowFrequenciesOnPanel(); - } - finally - { - ResetWaitingForFeedBack(ref _intercommThreadNowSynching); - } - } - catch (ThreadAbortException) - { } - catch (Exception ex) - { - logger.Error(ex); - } - } - */ private void SendVhfCommToDCSBIOS() { - if (VhfCommSyncing()) + if (_vhfCommSyncTask?.Status == TaskStatus.Running) { return; } @@ -779,120 +692,123 @@ private void SendVhfCommToDCSBIOS() // 0.00 - 0.95 // Send INC / DEC until frequency is correct. NOT THE DIALS! - _shutdownVHFCommThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFCommThread = false; - _vhfCommSyncThread = new Thread(VhfCommSynchThreadMethod); - _vhfCommSyncThread.Start(); + _vhfCommSyncTaskTokenSource = new(); + _vhfCommSyncTask = Task.Run(() => SyncVhfCommAsync(_vhfCommSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVHFCommThread; - private void VhfCommSynchThreadMethod() + private async Task SyncVhfCommAsync(CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + + // Reason for this is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + var localVhfCommBigFrequencyStandby = _vhfCommBigFrequencyStandby; + var localVhfCommSmallFrequencyStandby = _vhfCommSmallFrequencyStandby; + + do { - Interlocked.Exchange(ref _vhfCommThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; + if (IsTimedOut(ref dial1Timeout)) + { + ResetWaitingForFeedBack(ref _vhfCommDial1FreqWaitingForFeedback); // Let's do an ugly reset + } - // Reason for this is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - var localVhfCommBigFrequencyStandby = _vhfCommBigFrequencyStandby; - var localVhfCommSmallFrequencyStandby = _vhfCommSmallFrequencyStandby; + if (IsTimedOut(ref dial2Timeout)) + { + ResetWaitingForFeedBack(ref _vhfCommDial2FreqWaitingForFeedback); // Let's do an ugly reset + } - do + if (Interlocked.Read(ref _vhfCommDial1FreqWaitingForFeedback) == 0) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfCommDial1FreqWaitingForFeedback); // Lets do an ugly reset - } + var command = string.Empty; - if (IsTimedOut(ref dial2Timeout)) + lock (_lockVhfCommDialsObject1) { - ResetWaitingForFeedBack(ref _vhfCommDial2FreqWaitingForFeedback); // Lets do an ugly reset + if (localVhfCommBigFrequencyStandby != _vhfCommCockpitDial1Frequency) + { + dial1OkTime = DateTime.Now.Ticks; + command = VHF_COMM_FREQ1_DIAL_COMMAND + GetCommandDirectionForVhfCommDial1(localVhfCommBigFrequencyStandby, _vhfCommCockpitDial1Frequency); + } } - string str; - if (Interlocked.Read(ref _vhfCommDial1FreqWaitingForFeedback) == 0) + if (!string.IsNullOrEmpty(command)) { - lock (_lockVhfCommDialsObject1) - { - if (localVhfCommBigFrequencyStandby != _vhfCommCockpitDial1Frequency) - { - dial1OkTime = DateTime.Now.Ticks; - str = VHF_COMM_FREQ1_DIAL_COMMAND + GetCommandDirectionForVhfCommDial1(localVhfCommBigFrequencyStandby, _vhfCommCockpitDial1Frequency); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfCommDial1FreqWaitingForFeedback, 1); - } - Reset(ref dial1Timeout); - } + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfCommDial1FreqWaitingForFeedback, 1); } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfCommDial2FreqWaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfCommDial2FreqWaitingForFeedback) == 0) + lock (_lockVhfCommDialsObject2) { - lock (_lockVhfCommDialsObject2) + if (localVhfCommSmallFrequencyStandby != _vhfCommCockpitDial2Frequency) { - if (localVhfCommSmallFrequencyStandby != _vhfCommCockpitDial2Frequency) - { - dial2OkTime = DateTime.Now.Ticks; - str = VHF_COMM_FREQ2_DIAL_COMMAND + GetCommandDirectionForVhfCommDial2(localVhfCommSmallFrequencyStandby, _vhfCommCockpitDial2Frequency); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfCommDial2FreqWaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = VHF_COMM_FREQ2_DIAL_COMMAND + GetCommandDirectionForVhfCommDial2(localVhfCommSmallFrequencyStandby, _vhfCommCockpitDial2Frequency); } } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 20 || dial2SendCount > 25) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfCommDial2FreqWaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 20 || dial2SendCount > 25) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownVHFCommThread); - SwapCockpitStandbyFrequencyVhfComm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Logger.Error(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); + + SwapCockpitStandbyFrequencyVhfComm(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfCommThreadNowSynching, 0); + Logger.Error(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendUhfToDCSBIOS() { - if (UhfSyncing()) + if (_uhfSyncTask?.Status == TaskStatus.Running) { return; } @@ -910,655 +826,653 @@ private void SendUhfToDCSBIOS() // Send INC / DEC until frequency is correct. NOT THE DIALS! - _shutdownUHFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownUHFThread = false; - _uhfSyncThread = new Thread(UhfSynchThreadMethod); - _uhfSyncThread.Start(); + _uhfSyncTaskTokenSource = new(); + _uhfSyncTask = Task.Run(() => SyncUhfAsync(_uhfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownUHFThread; - private void UhfSynchThreadMethod() + + private async Task SyncUhfAsync(CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + + // Reason for having it outside the loop is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + // 225.95 + var filler = string.Empty; + if (_uhfSmallFrequencyStandby < 10) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; + filler = "0"; + } + + var standbyFrequency = double.Parse(_uhfBigFrequencyStandby + "." + filler + _uhfSmallFrequencyStandby, NumberFormatInfoFullDisplay); + var dial1StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(0, 2)); + var dial2StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(2, 1)); + var dial3StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(4, 2)); - // Reason for having it outside the loop is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - // 225.95 - var filler = string.Empty; - if (_uhfSmallFrequencyStandby < 10) + do + { + if (IsTimedOut(ref dial1Timeout)) { - filler = "0"; + ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Let's do an ugly reset } - var standbyFrequency = double.Parse(_uhfBigFrequencyStandby + "." + filler + _uhfSmallFrequencyStandby, NumberFormatInfoFullDisplay); - var dial1StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(0, 2)); - var dial2StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(2, 1)); - var dial3StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(4, 2)); - - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _uhfDial3WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - string str; - if (Interlocked.Read(ref _uhfDial1WaitingForFeedback) == 0) + lock (_lockUhfDialsObject1) { - lock (_lockUhfDialsObject1) + if (dial1StandbyFrequency != _uhfCockpitDial1Frequency) { - if (dial1StandbyFrequency != _uhfCockpitDial1Frequency) - { - dial1OkTime = DateTime.Now.Ticks; - str = UHF_FREQ1_DIAL_COMMAND + GetCommandDirectionForUhfDial1(dial1StandbyFrequency, _uhfCockpitDial1Frequency); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = UHF_FREQ1_DIAL_COMMAND + GetCommandDirectionForUhfDial1(dial1StandbyFrequency, _uhfCockpitDial1Frequency); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _uhfDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial2WaitingForFeedback) == 0) + lock (_lockUhfDialsObject2) { - lock (_lockUhfDialsObject2) + if (dial2StandbyFrequency != _uhfCockpitDial2Frequency) { - if (dial2StandbyFrequency != _uhfCockpitDial2Frequency) - { - dial2OkTime = DateTime.Now.Ticks; - str = UHF_FREQ2_DIAL_COMMAND + GetCommandDirectionForUhfDial2(dial2StandbyFrequency, _uhfCockpitDial2Frequency); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + dial2OkTime = DateTime.Now.Ticks; + command = UHF_FREQ2_DIAL_COMMAND + GetCommandDirectionForUhfDial2(dial2StandbyFrequency, _uhfCockpitDial2Frequency); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _uhfDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _uhfDial3WaitingForFeedback) == 0) + lock (_lockUhfDialsObject3) { - lock (_lockUhfDialsObject3) + if (dial3StandbyFrequency != _uhfCockpitDial3Frequency) { - if (dial3StandbyFrequency != _uhfCockpitDial3Frequency) - { - dial3OkTime = DateTime.Now.Ticks; - str = UHF_FREQ3_DIAL_COMMAND + GetCommandDirectionForUhfDial3(dial3StandbyFrequency, _uhfCockpitDial3Frequency); - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); + dial3OkTime = DateTime.Now.Ticks; + command = UHF_FREQ3_DIAL_COMMAND + GetCommandDirectionForUhfDial3(dial3StandbyFrequency, _uhfCockpitDial3Frequency); } } - else - { - dial3OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 19 || dial2SendCount > 9 || dial3SendCount > 20) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _uhfDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 19 || dial2SendCount > 9 || dial3SendCount > 20) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)) && !_shutdownUHFThread); - SwapCockpitStandbyFrequencyUhf(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Logger.Error(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime)); + + SwapCockpitStandbyFrequencyUhf(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _uhfThreadNowSynching, 0); + Logger.Error(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendVhfNavToDCSBIOS() { - if (VhfNavSyncing()) + if (_vhfNavSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyVhfNav(); - _shutdownVHFNavThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFNavThread = false; - _vhfNavSyncThread = new Thread(VhfNavSynchThreadMethod); - _vhfNavSyncThread.Start(); + + _vhfNavSyncTaskTokenSource = new(); + _vhfNavSyncTask = Task.Run(() => SyncVhfNavAsync(_vhfNavSyncTaskTokenSource.Token)); } - private volatile bool _shutdownVHFNavThread; - private void VhfNavSynchThreadMethod() + + private async Task SyncVhfNavAsync(CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + + // Reason for having it outside the loop is to separate the standby frequency from the sync loop + // If not the sync would pick up any changes made by the user during the + // sync process + // 107.95 + var filler = string.Empty; + if (_vhfNavSmallFrequencyStandby < 10) { - Interlocked.Exchange(ref _vhfNavThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - - // Reason for having it outside the loop is to separate the standby frequency from the sync loop - // If not the sync would pick up any changes made by the user during the - // sync process - // 107.95 - var filler = string.Empty; - if (_vhfNavSmallFrequencyStandby < 10) + filler = "0"; + } + + var standbyFrequency = double.Parse(_vhfNavBigFrequencyStandby + "." + filler + _vhfNavSmallFrequencyStandby, NumberFormatInfoFullDisplay); + var dial1StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(0, 3)); + var dial2StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(4, 2)); + + do + { + if (IsTimedOut(ref dial1Timeout)) { - filler = "0"; + ResetWaitingForFeedBack(ref _vhfNavDial1WaitingForFeedback); // Let's do an ugly reset } - var standbyFrequency = double.Parse(_vhfNavBigFrequencyStandby + "." + filler + _vhfNavSmallFrequencyStandby, NumberFormatInfoFullDisplay); - var dial1StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(0, 3)); - var dial2StandbyFrequency = uint.Parse(standbyFrequency.ToString("0.00", NumberFormatInfoFullDisplay).Substring(4, 2)); - - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfNavDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfNavDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfNavDial2WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _vhfNavDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - string str; - if (Interlocked.Read(ref _vhfNavDial1WaitingForFeedback) == 0) + lock (_lockVhfNavDialsObject1) { - lock (_lockVhfNavDialsObject1) + if (dial1StandbyFrequency != _vhfNavCockpitDial1Frequency) { - if (dial1StandbyFrequency != _vhfNavCockpitDial1Frequency) - { - dial1OkTime = DateTime.Now.Ticks; - str = VHF_NAV_FREQ1_DIAL_COMMAND + GetCommandDirectionForVhfNavDial1(dial1StandbyFrequency, _vhfNavCockpitDial1Frequency); - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfNavDial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + dial1OkTime = DateTime.Now.Ticks; + command = VHF_NAV_FREQ1_DIAL_COMMAND + GetCommandDirectionForVhfNavDial1(dial1StandbyFrequency, _vhfNavCockpitDial1Frequency); } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfNavDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfNavDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfNavDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfNavDialsObject2) { - lock (_lockVhfNavDialsObject2) + if (dial2StandbyFrequency != _vhfNavCockpitDial2Frequency) { - if (dial2StandbyFrequency != _vhfNavCockpitDial2Frequency) - { - dial2OkTime = DateTime.Now.Ticks; + dial2OkTime = DateTime.Now.Ticks; - // Compatible : GetCommandDirectionForUhfDial3 - str = VHF_NAV_FREQ2_DIAL_COMMAND + GetCommandDirectionForUhfDial3(dial2StandbyFrequency, _vhfNavCockpitDial2Frequency); - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfNavDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + // Compatible : GetCommandDirectionForUhfDial3 + command = VHF_NAV_FREQ2_DIAL_COMMAND + GetCommandDirectionForUhfDial3(dial2StandbyFrequency, _vhfNavCockpitDial2Frequency); } } - else - { - dial2OkTime = DateTime.Now.Ticks; - } - if (dial1SendCount > 19 || dial2SendCount > 9) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfNavDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (dial1SendCount > 19 || dial2SendCount > 9) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + Thread.Sleep(5000); } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)) && !_shutdownVHFNavThread); - SwapCockpitStandbyFrequencyVhfNav(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Logger.Error(ex); + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime)); + + SwapCockpitStandbyFrequencyVhfNav(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfNavThreadNowSynching, 0); + Logger.Error(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendVhfFmToDCSBIOS() { - if (VhfFmSyncing()) + if (_vhfFmSyncTask?.Status == TaskStatus.Running) { return; } SaveCockpitFrequencyVhfFm(); - _shutdownVHFFMThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownVHFFMThread = false; - _vhfFmSyncThread = new Thread(VhfFmSynchThreadMethod); - _vhfFmSyncThread.Start(); + + _vhfFmSyncTaskTokenSource = new(); + _vhfFmSyncTask = Task.Run(() => SyncVhfFmAsync(_vhfFmSyncTaskTokenSource.Token)); } - private void VhfFmSynchThreadMethod() + private async Task SyncVhfFmAsync(CancellationToken cancellationToken) { try { - try + long dial1Timeout = DateTime.Now.Ticks; + long dial2Timeout = DateTime.Now.Ticks; + long dial3Timeout = DateTime.Now.Ticks; + long dial4Timeout = DateTime.Now.Ticks; + long dial1OkTime = 0; + long dial2OkTime = 0; + long dial3OkTime = 0; + long dial4OkTime = 0; + var dial1SendCount = 0; + var dial2SendCount = 0; + var dial3SendCount = 0; + var dial4SendCount = 0; + + // Frequency selector 1 + // 3 4 5 6 7 + // Pos 0 1 2 3 4 + + // Frequency selector 2 + // 0 1 2 3 4 5 + + // Frequency selector 3 + // 0 1 2 3 4 5 6 7 8 9 + + // Frequency selector 4 + // 0 5 + // Pos 0 1 + + // Large dial 30 - 75 [step of 1] + // Small dial 0.00-0.95 [step of 0.05] + var filler = string.Empty; + if (_vhfFmSmallFrequencyStandby < 10) { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 1); - long dial1Timeout = DateTime.Now.Ticks; - long dial2Timeout = DateTime.Now.Ticks; - long dial3Timeout = DateTime.Now.Ticks; - long dial4Timeout = DateTime.Now.Ticks; - long dial1OkTime = 0; - long dial2OkTime = 0; - long dial3OkTime = 0; - long dial4OkTime = 0; - var dial1SendCount = 0; - var dial2SendCount = 0; - var dial3SendCount = 0; - var dial4SendCount = 0; - - // Frequency selector 1 - // 3 4 5 6 7 - // Pos 0 1 2 3 4 - - // Frequency selector 2 - // 0 1 2 3 4 5 - - // Frequency selector 3 - // 0 1 2 3 4 5 6 7 8 9 - - // Frequency selector 4 - // 0 5 - // Pos 0 1 - - // Large dial 30 - 75 [step of 1] - // Small dial 0.00-0.95 [step of 0.05] - var filler = string.Empty; - if (_vhfFmSmallFrequencyStandby < 10) - { - filler = "0"; - } + filler = "0"; + } - var frequencyAsString = _vhfFmBigFrequencyStandby + "." + filler + _vhfFmSmallFrequencyStandby; + var frequencyAsString = _vhfFmBigFrequencyStandby + "." + filler + _vhfFmSmallFrequencyStandby; - // 75.95 - var desiredFreqDial1Pos = int.Parse(frequencyAsString.Substring(0, 1)) - 3; - var desiredFreqDial2Pos = int.Parse(frequencyAsString.Substring(1, 1)); - var desiredFreqDial3Pos = int.Parse(frequencyAsString.Substring(3, 1)); - var desiredFreqDial4Pos = int.Parse(frequencyAsString.Substring(4, 1)); - if (desiredFreqDial4Pos == 5) + // 75.95 + var desiredFreqDial1Pos = int.Parse(frequencyAsString.Substring(0, 1)) - 3; + var desiredFreqDial2Pos = int.Parse(frequencyAsString.Substring(1, 1)); + var desiredFreqDial3Pos = int.Parse(frequencyAsString.Substring(3, 1)); + var desiredFreqDial4Pos = int.Parse(frequencyAsString.Substring(4, 1)); + if (desiredFreqDial4Pos == 5) + { + // 0 -> pos 0 + // 5 -> pos 1 + desiredFreqDial4Pos = 1; + } + + do + { + if (IsTimedOut(ref dial1Timeout)) { - // 0 -> pos 0 - // 5 -> pos 1 - desiredFreqDial4Pos = 1; + ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Let's do an ugly reset } - do + if (IsTimedOut(ref dial2Timeout)) { - if (IsTimedOut(ref dial1Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial1WaitingForFeedback); // Lets do an ugly reset - } + ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial2Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial2WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial3Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial3Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial3WaitingForFeedback); // Lets do an ugly reset - } + if (IsTimedOut(ref dial4Timeout)) + { + ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Let's do an ugly reset + } - if (IsTimedOut(ref dial4Timeout)) - { - ResetWaitingForFeedBack(ref _vhfFmDial4WaitingForFeedback); // Lets do an ugly reset - } + if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial1WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject1) { - lock (_lockVhfFmDialsObject1) + if (_vhfFmCockpitFreq1DialPos != desiredFreqDial1Pos) { - if (_vhfFmCockpitFreq1DialPos != desiredFreqDial1Pos) - { - dial1OkTime = DateTime.Now.Ticks; - } + dial1OkTime = DateTime.Now.Ticks; + } - if (_vhfFmCockpitFreq1DialPos < desiredFreqDial1Pos) - { - const string str = VHF_FM_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq1DialPos > desiredFreqDial1Pos) - { - const string str = VHF_FM_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial1SendCount++; - Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); - } - Reset(ref dial1Timeout); + if (_vhfFmCockpitFreq1DialPos < desiredFreqDial1Pos) + { + command = VHF_FM_FREQ_1DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq1DialPos > desiredFreqDial1Pos) + { + command = VHF_FM_FREQ_1DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial1OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial1SendCount++; + Interlocked.Exchange(ref _vhfFmDial1WaitingForFeedback, 1); } + Reset(ref dial1Timeout); + } + else + { + dial1OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfFmDial2WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfFmDialsObject2) { - lock (_lockVhfFmDialsObject2) + if (_vhfFmCockpitFreq2DialPos != desiredFreqDial2Pos) { - if (_vhfFmCockpitFreq2DialPos != desiredFreqDial2Pos) - { - dial2OkTime = DateTime.Now.Ticks; - } + dial2OkTime = DateTime.Now.Ticks; + } - if (_vhfFmCockpitFreq2DialPos < desiredFreqDial2Pos) - { - const string str = VHF_FM_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq2DialPos > desiredFreqDial2Pos) - { - const string str = VHF_FM_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial2SendCount++; - Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); - } - Reset(ref dial2Timeout); + if (_vhfFmCockpitFreq2DialPos < desiredFreqDial2Pos) + { + command = VHF_FM_FREQ_2DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq2DialPos > desiredFreqDial2Pos) + { + command = VHF_FM_FREQ_2DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial2OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial2SendCount++; + Interlocked.Exchange(ref _vhfFmDial2WaitingForFeedback, 1); } + Reset(ref dial2Timeout); + } + else + { + dial2OkTime = DateTime.Now.Ticks; + } + + if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + { + var command = string.Empty; - if (Interlocked.Read(ref _vhfFmDial3WaitingForFeedback) == 0) + lock (_lockVhfFmDialsObject3) { - lock (_lockVhfFmDialsObject3) + if (_vhfFmCockpitFreq3DialPos != desiredFreqDial3Pos) { - if (_vhfFmCockpitFreq3DialPos != desiredFreqDial3Pos) - { - dial3OkTime = DateTime.Now.Ticks; - } + dial3OkTime = DateTime.Now.Ticks; + } - if (_vhfFmCockpitFreq3DialPos < desiredFreqDial3Pos) - { - const string str = VHF_FM_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq3DialPos > desiredFreqDial3Pos) - { - const string str = VHF_FM_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial3SendCount++; - Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); - } - Reset(ref dial3Timeout); + if (_vhfFmCockpitFreq3DialPos < desiredFreqDial3Pos) + { + command = VHF_FM_FREQ_3DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq3DialPos > desiredFreqDial3Pos) + { + command = VHF_FM_FREQ_3DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } } - else + + if (!string.IsNullOrEmpty(command)) { - dial3OkTime = DateTime.Now.Ticks; + await DCSBIOS.SendAsync(command); + dial3SendCount++; + Interlocked.Exchange(ref _vhfFmDial3WaitingForFeedback, 1); } + Reset(ref dial3Timeout); + } + else + { + dial3OkTime = DateTime.Now.Ticks; + } - if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + if (Interlocked.Read(ref _vhfFmDial4WaitingForFeedback) == 0) + { + var command = string.Empty; + + lock (_lockVhfFmDialsObject4) { - lock (_lockVhfFmDialsObject4) + if (_vhfFmCockpitFreq4DialPos != desiredFreqDial4Pos) { - if (_vhfFmCockpitFreq4DialPos != desiredFreqDial4Pos) - { - dial4OkTime = DateTime.Now.Ticks; - } + dial4OkTime = DateTime.Now.Ticks; + } - if (_vhfFmCockpitFreq4DialPos < desiredFreqDial4Pos) - { - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - else if (_vhfFmCockpitFreq4DialPos > desiredFreqDial4Pos) - { - const string str = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - dial4SendCount++; - Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); - } - Reset(ref dial4Timeout); + if (_vhfFmCockpitFreq4DialPos < desiredFreqDial4Pos) + { + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_vhfFmCockpitFreq4DialPos > desiredFreqDial4Pos) + { + command = VHF_FM_FREQ_4DIAL_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - dial4OkTime = DateTime.Now.Ticks; } - if (dial1SendCount > 4 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 2) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - dial1SendCount = 0; - dial2SendCount = 0; - dial3SendCount = 0; - dial4SendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + dial4SendCount++; + Interlocked.Exchange(ref _vhfFmDial4WaitingForFeedback, 1); } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + Reset(ref dial4Timeout); + } + else + { + dial4OkTime = DateTime.Now.Ticks; } - while ((IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)) && !_shutdownVHFFMThread); - SwapCockpitStandbyFrequencyVhfFm(); - ShowFrequenciesOnPanel(); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Common.ShowErrorMessageBox(ex); + if (dial1SendCount > 4 || dial2SendCount > 10 || dial3SendCount > 10 || dial4SendCount > 2) + { + // "Race" condition detected? + dial1SendCount = 0; + dial2SendCount = 0; + dial3SendCount = 0; + dial4SendCount = 0; + Thread.Sleep(5000); + } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; } + while (IsTooShort(dial1OkTime) || IsTooShort(dial2OkTime) || IsTooShort(dial3OkTime) || IsTooShort(dial4OkTime)); + + SwapCockpitStandbyFrequencyVhfFm(); + ShowFrequenciesOnPanel(); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _vhfFmThreadNowSynching, 0); + Common.ShowErrorMessageBox(ex); } + Interlocked.Increment(ref _doUpdatePanelLCD); } private void SendAdfBandChangeToDCSBIOS() { - _shutdownADFThread = true; - Thread.Sleep(Constants.ThreadShutDownWaitTime); - _shutdownADFThread = false; - _adfSyncThread = new Thread(AdfBandChangeSynchThreadMethod); - _adfSyncThread.Start(); + if (_adfSyncTask?.Status == TaskStatus.Running) return; + + _adfSyncTaskTokenSource = new(); + _adfSyncTask = Task.Run(() => SyncAdfBandChangeAsync(_adfSyncTaskTokenSource.Token)); } - private volatile bool _shutdownADFThread; - private void AdfBandChangeSynchThreadMethod() + private async Task SyncAdfBandChangeAsync(CancellationToken cancellationToken) { try { - try + long freqBandDialTimeout = DateTime.Now.Ticks; + long freqBandDialOkTime = 0; + var freqBandDialSendCount = 0; + var once = true; + + // Frequency Band selector + // Pos 0 1 2 + switch (_adfStandbyFrequencyBand) { - Interlocked.Exchange(ref _adfThreadNowSynching, 1); - long freqBandDialTimeout = DateTime.Now.Ticks; - long freqBandDialOkTime = 0; - var freqBandDialSendCount = 0; - var once = true; + case 0: + { + _increaseAdfBand = true; + _adfStandbyFrequencyBand = 1; + break; + } - // Frequency Band selector - // Pos 0 1 2 - switch (_adfStandbyFrequencyBand) - { - case 0: - { - _increaseAdfBand = true; - _adfStandbyFrequencyBand = 1; - break; - } + case 1: + { + _adfStandbyFrequencyBand = _increaseAdfBand ? 2 : (uint)0; + break; + } - case 1: - { - _adfStandbyFrequencyBand = _increaseAdfBand ? 2 : (uint)0; - break; - } + case 2: + { + _increaseAdfBand = false; + _adfStandbyFrequencyBand = 1; + break; + } + } - case 2: - { - _increaseAdfBand = false; - _adfStandbyFrequencyBand = 1; - break; - } + var desiredFreqBandDialPos = _adfStandbyFrequencyBand; + + do + { + if (IsTimedOut(ref freqBandDialTimeout)) + { + ResetWaitingForFeedBack(ref _adfFrequencyBandWaitingForFeedback); // Let's do an ugly reset } - var desiredFreqBandDialPos = _adfStandbyFrequencyBand; - - do + if (Interlocked.Read(ref _adfFrequencyBandWaitingForFeedback) == 0) { - if (IsTimedOut(ref freqBandDialTimeout)) - { - ResetWaitingForFeedBack(ref _adfFrequencyBandWaitingForFeedback); // Lets do an ugly reset - } + var command = string.Empty; - if (Interlocked.Read(ref _adfFrequencyBandWaitingForFeedback) == 0) + lock (_lockAdfFrequencyBandObject) { - lock (_lockAdfFrequencyBandObject) + if (_adfCockpitFrequencyBand != desiredFreqBandDialPos) { - if (_adfCockpitFrequencyBand != desiredFreqBandDialPos) - { - freqBandDialOkTime = DateTime.Now.Ticks; - once = false; - } - - if (_adfCockpitFrequencyBand < desiredFreqBandDialPos) - { - const string str = ADF_FREQUENCY_BAND_COMMAND + DCSBIOS_INCREASE_COMMAND; - DCSBIOS.Send(str); - freqBandDialSendCount++; - Interlocked.Exchange(ref _adfFrequencyBandWaitingForFeedback, 1); - } - else if (_adfCockpitFrequencyBand > desiredFreqBandDialPos) - { - const string str = ADF_FREQUENCY_BAND_COMMAND + DCSBIOS_DECREASE_COMMAND; - DCSBIOS.Send(str); - freqBandDialSendCount++; - Interlocked.Exchange(ref _adfFrequencyBandWaitingForFeedback, 1); - } + freqBandDialOkTime = DateTime.Now.Ticks; + once = false; + } - Reset(ref freqBandDialTimeout); + if (_adfCockpitFrequencyBand < desiredFreqBandDialPos) + { + command = ADF_FREQUENCY_BAND_COMMAND + DCSBIOS_INCREASE_COMMAND; + } + else if (_adfCockpitFrequencyBand > desiredFreqBandDialPos) + { + command = ADF_FREQUENCY_BAND_COMMAND + DCSBIOS_DECREASE_COMMAND; } - } - else - { - freqBandDialOkTime = DateTime.Now.Ticks; } - if (freqBandDialSendCount > 3) + if (!string.IsNullOrEmpty(command)) { - // "Race" condition detected? - freqBandDialSendCount = 0; - Thread.Sleep(5000); + await DCSBIOS.SendAsync(command); + freqBandDialSendCount++; + Interlocked.Exchange(ref _adfFrequencyBandWaitingForFeedback, 1); } + Reset(ref freqBandDialTimeout); + } + else + { + freqBandDialOkTime = DateTime.Now.Ticks; + } - Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + if (freqBandDialSendCount > 3) + { + // "Race" condition detected? + freqBandDialSendCount = 0; + Thread.Sleep(5000); + } - if (once) - { - UpdateCockpitAdfFrequency(_adfCockpitFrequencyRaw); - once = false; - } + Thread.Sleep(SynchSleepTime); // Should be enough to get an update cycle from DCS-BIOS + + if (cancellationToken.IsCancellationRequested) break; + + if (once) + { + UpdateCockpitAdfFrequency(_adfCockpitFrequencyRaw); + once = false; } - while (IsTooShort(freqBandDialOkTime) && !_shutdownADFThread); - } - catch (ThreadAbortException) - { - } - catch (Exception ex) - { - Logger.Error(ex); } + while (IsTooShort(freqBandDialOkTime)); } - finally + catch (Exception ex) { - Interlocked.Exchange(ref _adfThreadNowSynching, 0); + Logger.Error(ex); } Interlocked.Increment(ref _doUpdatePanelLCD); } - private void SendUhfPresetChannelChangeToDCSBIOS() + private async Task SendUhfPresetChannelChangeToDCSBIOSAsync() { try { @@ -1582,7 +1496,7 @@ private void SendUhfPresetChannelChangeToDCSBIOS() } } - DCSBIOS.Send(_uhfIncreasePresetChannel ? UHF_PRESET_DIAL_COMMAND_INC : UHF_PRESET_DIAL_COMMAND_DEC); + await DCSBIOS.SendAsync(_uhfIncreasePresetChannel ? UHF_PRESET_DIAL_COMMAND_INC : UHF_PRESET_DIAL_COMMAND_DEC); Interlocked.Increment(ref _doUpdatePanelLCD); } catch (Exception ex) @@ -1925,7 +1839,7 @@ 5 bytes lower right LCD [16- 20] Interlocked.Decrement(ref _doUpdatePanelLCD); } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { if (SkipCurrentFrequencyChange()) { @@ -1945,7 +1859,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_VOLUME_KNOB_COMMAND_DEC); + await _interCommSkipper.ClickAsync(INTERCOMM_VOLUME_KNOB_COMMAND_DEC); break; } @@ -1998,7 +1912,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_TUNE_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_TUNE_KNOB_COMMAND_INC); break; } } @@ -2011,7 +1925,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_VOLUME_KNOB_COMMAND_INC); + await _interCommSkipper.ClickAsync(INTERCOMM_VOLUME_KNOB_COMMAND_INC); break; } @@ -2063,7 +1977,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_TUNE_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_TUNE_KNOB_COMMAND_DEC); break; } } @@ -2076,7 +1990,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_DIAL_COMMAND_INC); + await _interCommSkipper.ClickAsync(INTERCOMM_DIAL_COMMAND_INC); break; } @@ -2125,7 +2039,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_GAIN_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_GAIN_KNOB_COMMAND_INC); break; } } @@ -2138,7 +2052,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_DIAL_COMMAND_DEC); + await _interCommSkipper.ClickAsync(INTERCOMM_DIAL_COMMAND_DEC); break; } @@ -2186,7 +2100,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_GAIN_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_GAIN_KNOB_COMMAND_DEC); break; } } @@ -2199,7 +2113,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_VOLUME_KNOB_COMMAND_DEC); + await _interCommSkipper.ClickAsync(INTERCOMM_VOLUME_KNOB_COMMAND_DEC); break; } @@ -2252,7 +2166,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_TUNE_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_TUNE_KNOB_COMMAND_INC); break; } } @@ -2265,7 +2179,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_VOLUME_KNOB_COMMAND_INC); + await _interCommSkipper.ClickAsync(INTERCOMM_VOLUME_KNOB_COMMAND_INC); break; } @@ -2317,7 +2231,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_TUNE_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_TUNE_KNOB_COMMAND_DEC); break; } } @@ -2330,7 +2244,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_DIAL_COMMAND_INC); + await _interCommSkipper.ClickAsync(INTERCOMM_DIAL_COMMAND_INC); break; } @@ -2379,7 +2293,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_GAIN_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(ADF_GAIN_KNOB_COMMAND_INC); break; } } @@ -2392,7 +2306,7 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentUH1HRadioMode.INTERCOMM: { - _interCommSkipper.Click(INTERCOMM_DIAL_COMMAND_DEC); + await _interCommSkipper.ClickAsync(INTERCOMM_DIAL_COMMAND_DEC); break; } @@ -2440,7 +2354,7 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentUH1HRadioMode.ADF: { - DCSBIOS.Send(ADF_GAIN_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(ADF_GAIN_KNOB_COMMAND_DEC); break; } } @@ -2576,230 +2490,229 @@ public static uint QuarterFrequencyStandbyAdjust(uint frequency, bool increase) return result; } - - protected override void PZ69KnobChanged(IEnumerable hashSet) + + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobUH1H)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobUH1H)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsUH1H.UPPER_INTERCOMM: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsUH1H.UPPER_INTERCOMM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.INTERCOMM; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.INTERCOMM; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_VHFCOMM: + case RadioPanelPZ69KnobsUH1H.UPPER_VHFCOMM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.VHFCOMM; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.VHFCOMM; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_UHF: + case RadioPanelPZ69KnobsUH1H.UPPER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.UHF; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_VHFFM: + case RadioPanelPZ69KnobsUH1H.UPPER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.VHFFM; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_VHFNAV: + case RadioPanelPZ69KnobsUH1H.UPPER_VHFNAV: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.VHFNAV; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.VHFNAV; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_ADF: + case RadioPanelPZ69KnobsUH1H.UPPER_ADF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentUpperRadioMode = CurrentUH1HRadioMode.ADF; - } - break; + _currentUpperRadioMode = CurrentUH1HRadioMode.ADF; } + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_DME: - { - break; - } + case RadioPanelPZ69KnobsUH1H.UPPER_DME: + { + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_INTERCOMM: + case RadioPanelPZ69KnobsUH1H.LOWER_INTERCOMM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.INTERCOMM; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.INTERCOMM; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_VHFCOMM: + case RadioPanelPZ69KnobsUH1H.LOWER_VHFCOMM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.VHFCOMM; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.VHFCOMM; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_UHF: + case RadioPanelPZ69KnobsUH1H.LOWER_UHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.UHF; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.UHF; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_VHFFM: + case RadioPanelPZ69KnobsUH1H.LOWER_VHFFM: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.VHFFM; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.VHFFM; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_VHFNAV: + case RadioPanelPZ69KnobsUH1H.LOWER_VHFNAV: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.VHFNAV; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.VHFNAV; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_ADF: + case RadioPanelPZ69KnobsUH1H.LOWER_ADF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - _currentLowerRadioMode = CurrentUH1HRadioMode.ADF; - } - break; + _currentLowerRadioMode = CurrentUH1HRadioMode.ADF; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_DME: - { - break; - } + case RadioPanelPZ69KnobsUH1H.LOWER_DME: + { + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.UPPER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.UPPER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.UPPER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.UPPER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_LARGE_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.LOWER_LARGE_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_LARGE_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.LOWER_LARGE_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_SMALL_FREQ_WHEEL_INC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.LOWER_SMALL_FREQ_WHEEL_INC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_SMALL_FREQ_WHEEL_DEC: - { - break; - } + case RadioPanelPZ69KnobsUH1H.LOWER_SMALL_FREQ_WHEEL_DEC: + { + break; + } - case RadioPanelPZ69KnobsUH1H.UPPER_FREQ_SWITCH: + case RadioPanelPZ69KnobsUH1H.UPPER_FREQ_SWITCH: + { + if (_currentUpperRadioMode == CurrentUH1HRadioMode.ADF) { - if (_currentUpperRadioMode == CurrentUH1HRadioMode.ADF) + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendAdfBandChangeToDCSBIOS(); - } + SendAdfBandChangeToDCSBIOS(); } - else + } + else + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H.UPPER_FREQ_SWITCH); - } + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsUH1H.UPPER_FREQ_SWITCH); } - break; } + break; + } - case RadioPanelPZ69KnobsUH1H.LOWER_FREQ_SWITCH: + case RadioPanelPZ69KnobsUH1H.LOWER_FREQ_SWITCH: + { + if (_currentLowerRadioMode == CurrentUH1HRadioMode.ADF) { - if (_currentLowerRadioMode == CurrentUH1HRadioMode.ADF) + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendAdfBandChangeToDCSBIOS(); - } + SendAdfBandChangeToDCSBIOS(); } - else + } + else + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SendFrequencyToDCSBIOS(RadioPanelPZ69KnobsUH1H.LOWER_FREQ_SWITCH); - } + await SendFrequencyToDCSBIOSAsync(RadioPanelPZ69KnobsUH1H.LOWER_FREQ_SWITCH); } - break; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_UH1H, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_UH1H, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } - + public override void ClearSettings(bool setIsDirty = false) { } @@ -2808,7 +2721,7 @@ public override DcsOutputAndColorBinding CreateDcsOutputAndColorBinding(SaitekPa { throw new Exception("Radio Panel does not support color bindings with DCS-BIOS."); } - + private void CreateRadioKnobs() { SaitekPanelKnobs = RadioPanelKnobUH1H.GetRadioPanelKnobs(); @@ -3086,49 +2999,12 @@ private void SwapCockpitStandbyFrequencyVhfNav() _vhfNavSmallFrequencyStandby = _vhfNavSavedCockpitSmallFrequency; } - private bool VhfCommSyncing() - { - return Interlocked.Read(ref _vhfCommThreadNowSynching) > 0; - } - - private bool VhfFmSyncing() - { - return Interlocked.Read(ref _vhfFmThreadNowSynching) > 0; - } - - private bool UhfSyncing() - { - return Interlocked.Read(ref _uhfThreadNowSynching) > 0; - } - - private bool VhfNavSyncing() - { - return Interlocked.Read(ref _vhfNavThreadNowSynching) > 0; - } - - public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) - { - } - - public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) - { - } - - public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) - { - } - - public override void AddOrUpdateDCSBIOSBinding(PanelSwitchOnOff panelSwitchOnOff, List dcsbiosInputs, string description, bool isSequenced) - { - } - - public override void AddOrUpdateBIPLinkBinding(PanelSwitchOnOff panelSwitchOnOff, BIPLinkBase bipLink) - { - } - - public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnOff, OSCommand operatingSystemCommand) - { - } + public override void RemoveSwitchFromList(object controlList, PanelSwitchOnOff panelSwitchOnOff) { } + public override void AddOrUpdateKeyStrokeBinding(PanelSwitchOnOff panelSwitchOnOff, string keyPress, KeyPressLength keyPressLength) { } + public override void AddOrUpdateSequencedKeyBinding(PanelSwitchOnOff panelSwitchOnOff, string description, SortedList keySequence) { } + public override void AddOrUpdateDCSBIOSBinding(PanelSwitchOnOff panelSwitchOnOff, List dcsbiosInputs, string description, bool isSequenced) { } + public override void AddOrUpdateBIPLinkBinding(PanelSwitchOnOff panelSwitchOnOff, BIPLinkBase bipLink) { } + public override void AddOrUpdateOSCommandBinding(PanelSwitchOnOff panelSwitchOnOff, OSCommand operatingSystemCommand) { } } } diff --git a/src/NonVisuals/Radios/RadiopanelPZ69P47D.cs b/src/NonVisuals/Radios/RadiopanelPZ69P47D.cs index 62eb6f873..d5b28ed3c 100644 --- a/src/NonVisuals/Radios/RadiopanelPZ69P47D.cs +++ b/src/NonVisuals/Radios/RadiopanelPZ69P47D.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -90,9 +91,21 @@ private enum CurrentP47DRadioMode private readonly object _lockShowFrequenciesOnPanelObject = new(); private long _doUpdatePanelLCD; + private const string RADIO_CHANNEL_A = "RCTRL_A "; + private const string RADIO_CHANNEL_B = "RCTRL_B "; + private const string RADIO_CHANNEL_C = "RCTRL_C "; + private const string RADIO_CHANNEL_D = "RCTRL_D "; + + private const string RADIO_OFF = "RCTRL_OFF "; + private const string RADIO_MODE = "RCTRL_T_MODE "; + private const string RADIO_FREQ_SELECTOR = "DETROLA_FREQU_SEL "; + + private const string DEC = " DEC\n"; + private const string INC = " INC\n"; + public RadioPanelPZ69P47D(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -239,13 +252,12 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) - { + foreach (var radioPanelKnobObject in hashSet) { var radioPanelKnob = (RadioPanelKnobP47D)radioPanelKnobObject; @@ -336,9 +348,9 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_upperButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button - DCSBIOS.Send(VHF_RADIO_LIGHT_SWITCH_COMMAND); + await DCSBIOS.SendAsync(VHF_RADIO_LIGHT_SWITCH_COMMAND); } _upperButtonPressedAndDialRotated = false; @@ -357,9 +369,9 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (!_lowerButtonPressedAndDialRotated) { - // Do not synch if user has pressed the button to configure the radio + // Do not sync if user has pressed the button to configure the radio // Do when user releases button - DCSBIOS.Send(VHF_RADIO_LIGHT_SWITCH_COMMAND); + await DCSBIOS.SendAsync(VHF_RADIO_LIGHT_SWITCH_COMMAND); } _lowerButtonPressedAndDialRotated = false; @@ -379,8 +391,8 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) null); } } - AdjustFrequency(hashSet); - } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -388,7 +400,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -410,12 +422,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP47DRadioMode.VHF: { - _vhfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentP47DRadioMode.DETROLA: { - SendLFVolumeCommand(true); + await SendLFVolumeCommandAsync(true); break; } case CurrentP47DRadioMode.NO_USE: @@ -433,12 +445,12 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentP47DRadioMode.VHF: { // MODE - _vhfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentP47DRadioMode.DETROLA: { - SendLFVolumeCommand(false); + await SendLFVolumeCommandAsync(false); break; } } @@ -454,17 +466,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF_VOLUME_KNOB_COMMAND_INC); } else { - _vhfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(true)); + await _vhfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(true)); } break; } case CurrentP47DRadioMode.DETROLA: { - SendLFFrequencyCommand(true); + await SendLFFrequencyCommandAsync(true); break; } } @@ -480,17 +492,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF_VOLUME_KNOB_COMMAND_DEC); } else { - _vhfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(false)); + await _vhfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(false)); } break; } case CurrentP47DRadioMode.DETROLA: { - SendLFFrequencyCommand(false); + await SendLFFrequencyCommandAsync(false); break; } } @@ -503,12 +515,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP47DRadioMode.VHF: { - _vhfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _vhfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentP47DRadioMode.DETROLA: { - SendLFVolumeCommand(true); + await SendLFVolumeCommandAsync(true); break; } } @@ -521,12 +533,12 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentP47DRadioMode.VHF: { - _vhfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _vhfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentP47DRadioMode.DETROLA: { - SendLFVolumeCommand(false); + await SendLFVolumeCommandAsync(false); break; } } @@ -542,17 +554,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF_VOLUME_KNOB_COMMAND_INC); + await DCSBIOS.SendAsync(VHF_VOLUME_KNOB_COMMAND_INC); } - else + else { - _vhfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(true)); + await _vhfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(true)); } break; } case CurrentP47DRadioMode.DETROLA: { - SendLFFrequencyCommand(true); + await SendLFFrequencyCommandAsync(true); break; } } @@ -568,17 +580,17 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - DCSBIOS.Send(VHF_VOLUME_KNOB_COMMAND_DEC); + await DCSBIOS.SendAsync(VHF_VOLUME_KNOB_COMMAND_DEC); } else { - _vhfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(false)); + await _vhfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(false)); } break; } case CurrentP47DRadioMode.DETROLA: { - SendLFFrequencyCommand(false); + await SendLFFrequencyCommandAsync(false); break; } } @@ -780,15 +792,15 @@ private void SetLowerRadioMode(CurrentP47DRadioMode currentP47DRadioMode) Logger.Error(ex); } } - - private void SendLFFrequencyCommand(bool increase) + + private async Task SendLFFrequencyCommandAsync(bool increase) { - DCSBIOS.Send(GetDetrolaFrequencyStringCommand(increase, _lfFrequencyDialChangeMonitor.ClickAndCheck() ? _lfFrequencyChangeValue * 10 : _lfFrequencyChangeValue)); + await DCSBIOS.SendAsync(GetDetrolaFrequencyStringCommand(increase, _lfFrequencyDialChangeMonitor.ClickAndCheck() ? _lfFrequencyChangeValue * 10 : _lfFrequencyChangeValue)); } - private void SendLFVolumeCommand(bool increase) + private async Task SendLFVolumeCommandAsync(bool increase) { - DCSBIOS.Send(GetDetrolaVolumeStringCommand(increase, _lfVolumeDialChangeMonitor.ClickAndCheck() ? _lfVolumeChangeValue * 10 : _lfVolumeChangeValue)); + await DCSBIOS.SendAsync(GetDetrolaVolumeStringCommand(increase, _lfVolumeDialChangeMonitor.ClickAndCheck() ? _lfVolumeChangeValue * 10 : _lfVolumeChangeValue)); } private string GetHFRadioChannelStringCommand(bool moveUp) @@ -800,44 +812,44 @@ private string GetHFRadioChannelStringCommand(bool moveUp) if ((_vhfRadioOffCockpitButton == 1 || _vhfRadioOffCockpitButton == 0) && _vhfRadioChannelACockpitButton == 0 && _vhfRadioChannelBCockpitButton == 0 && _vhfRadioChannelCCockpitButton == 0 && _vhfRadioChannelDCockpitButton == 0) { - return "RCTRL_A INC\n"; + return RADIO_CHANNEL_A + INC; } if (_vhfRadioChannelACockpitButton == 1) { - return "RCTRL_B INC\n"; + return RADIO_CHANNEL_B + INC; } if (_vhfRadioChannelBCockpitButton == 1) { - return "RCTRL_C INC\n"; + return RADIO_CHANNEL_C + INC; } if (_vhfRadioChannelCCockpitButton == 1) { - return "RCTRL_D INC\n"; + return RADIO_CHANNEL_D + INC; } } else { if (_vhfRadioChannelDCockpitButton == 1) { - return "RCTRL_C INC\n"; + return RADIO_CHANNEL_C + INC; } if (_vhfRadioChannelCCockpitButton == 1) { - return "RCTRL_B INC\n"; + return RADIO_CHANNEL_B + INC; } if (_vhfRadioChannelBCockpitButton == 1) { - return "RCTRL_A INC\n"; + return RADIO_CHANNEL_A + INC; } if (_vhfRadioChannelACockpitButton == 1) { - return "RCTRL_OFF INC\n"; + return RADIO_OFF + INC; } } } @@ -850,9 +862,9 @@ private string GetHFRadioModeStringCommand(bool moveUp) { if (moveUp) { - return "RCTRL_T_MODE " + (_vhfRadioModeCockpitDialPosition + 1) + "\n"; + return RADIO_MODE + (_vhfRadioModeCockpitDialPosition + 1) + "\n"; } - return "RCTRL_T_MODE " + (_vhfRadioModeCockpitDialPosition - 1) + "\n"; + return RADIO_MODE + (_vhfRadioModeCockpitDialPosition - 1) + "\n"; } } @@ -860,15 +872,12 @@ private string GetDetrolaFrequencyStringCommand(bool moveUp, uint changeValue) { lock (_lockLFRadioFrequencyDialObject1) { - uint newValue; if (moveUp) { - newValue = _lfRadioFrequencyDCSBIOSValue + changeValue > 0xFFFF ? 0xFFFF : _lfRadioFrequencyDCSBIOSValue + changeValue; - return $"DETROLA_FREQU_SEL {newValue}\n"; + return RADIO_FREQ_SELECTOR + (_lfRadioFrequencyDCSBIOSValue + changeValue > 0xFFFF ? 0xFFFF : _lfRadioFrequencyDCSBIOSValue + changeValue); } - newValue = _lfRadioFrequencyDCSBIOSValue < changeValue ? 0 : _lfRadioFrequencyDCSBIOSValue - changeValue; - return $"DETROLA_FREQU_SEL {newValue}\n"; + return RADIO_FREQ_SELECTOR + (_lfRadioFrequencyDCSBIOSValue < changeValue ? 0 : _lfRadioFrequencyDCSBIOSValue - changeValue); } } diff --git a/src/NonVisuals/Radios/RadiopanelPZ69SpitfireLFMkIX.cs b/src/NonVisuals/Radios/RadiopanelPZ69SpitfireLFMkIX.cs index f975f1b71..b8b6b59a6 100644 --- a/src/NonVisuals/Radios/RadiopanelPZ69SpitfireLFMkIX.cs +++ b/src/NonVisuals/Radios/RadiopanelPZ69SpitfireLFMkIX.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; namespace NonVisuals.Radios { @@ -83,7 +84,7 @@ private enum CurrentSpitfireLFMkIXRadioMode private DCSBIOSOutput _iffDiffDcsbiosOutputDial; private volatile uint _iffBiffCockpitDialPos = 1; private volatile uint _iffDiffCockpitDialPos; - private readonly ClickSkipper _iffBiffDialSkipper = new(2); + private readonly ClickSkipper _iffBiffDialSkipper = new(2); private readonly ClickSkipper _iffDiffDialSkipper = new(2); private const string IFFB_COMMAND_INC = "IFF_B INC\n"; private const string IFFB_COMMAND_DEC = "IFF_B DEC\n"; @@ -92,9 +93,20 @@ private enum CurrentSpitfireLFMkIXRadioMode private readonly object _lockShowFrequenciesOnPanelObject = new(); private long _doUpdatePanelLCD; + private const string RADIO_CHANNEL_A = "RCTRL_A "; + private const string RADIO_CHANNEL_B = "RCTRL_B "; + private const string RADIO_CHANNEL_C = "RCTRL_C "; + private const string RADIO_CHANNEL_D = "RCTRL_D "; + + private const string RADIO_OFF = "RCTRL_OFF "; + private const string RADIO_MODE = "RCTRL_T_MODE "; + + private const string DEC = " DEC\n"; + private const string INC = " INC\n"; + public RadioPanelPZ69SpitfireLFMkIX(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -237,13 +249,12 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) - { + foreach (var radioPanelKnobObject in hashSet) { var radioPanelKnob = (RadioPanelKnobSpitfireLFMkIX)radioPanelKnobObject; @@ -331,7 +342,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (radioPanelKnob.IsOn) { - DCSBIOS.Send(HF_RADIO_LIGHT_SWITCH_COMMAND); + await DCSBIOS.SendAsync(HF_RADIO_LIGHT_SWITCH_COMMAND); } } break; @@ -343,7 +354,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) { if (radioPanelKnob.IsOn) { - DCSBIOS.Send(HF_RADIO_LIGHT_SWITCH_COMMAND); + await DCSBIOS.SendAsync(HF_RADIO_LIGHT_SWITCH_COMMAND); } } break; @@ -362,8 +373,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - AdjustFrequency(hashSet); - } + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -371,7 +381,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -393,13 +403,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _hfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffDiffDialSkipper.Click(IFFD_COMMAND_INC); + await _iffDiffDialSkipper.ClickAsync(IFFD_COMMAND_INC); break; } @@ -418,13 +428,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { // MODE - _hfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _hfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffDiffDialSkipper.Click(IFFD_COMMAND_DEC); + await _iffDiffDialSkipper.ClickAsync(IFFD_COMMAND_DEC); break; } } @@ -438,13 +448,13 @@ private void AdjustFrequency(IEnumerable hashSet) case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { // CHANNEL - _hfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(true)); + await _hfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(true)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffBiffDialSkipper.Click(IFFB_COMMAND_INC); + await _iffBiffDialSkipper.ClickAsync(IFFB_COMMAND_INC); break; } } @@ -457,13 +467,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(false)); + await _hfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(false)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffBiffDialSkipper.Click(IFFB_COMMAND_DEC); + await _iffBiffDialSkipper.ClickAsync(IFFB_COMMAND_DEC); break; } } @@ -476,13 +486,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(true)); + await _hfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(true)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffDiffDialSkipper.Click(IFFD_COMMAND_INC); + await _iffDiffDialSkipper.ClickAsync(IFFD_COMMAND_INC); break; } } @@ -495,13 +505,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioModePresetDialSkipper.Click(GetHFRadioModeStringCommand(false)); + await _hfRadioModePresetDialSkipper.ClickAsync(GetHFRadioModeStringCommand(false)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffDiffDialSkipper.Click(IFFD_COMMAND_DEC); + await _iffDiffDialSkipper.ClickAsync(IFFD_COMMAND_DEC); break; } } @@ -514,13 +524,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(true)); + await _hfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(true)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffBiffDialSkipper.Click(IFFB_COMMAND_INC); + await _iffBiffDialSkipper.ClickAsync(IFFB_COMMAND_INC); break; } } @@ -533,13 +543,13 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentSpitfireLFMkIXRadioMode.HFRADIO: { - _hfRadioChannelPresetDialSkipper.Click(GetHFRadioChannelStringCommand(false)); + await _hfRadioChannelPresetDialSkipper.ClickAsync(GetHFRadioChannelStringCommand(false)); break; } case CurrentSpitfireLFMkIXRadioMode.IFF: { - _iffBiffDialSkipper.Click(IFFB_COMMAND_DEC); + await _iffBiffDialSkipper.ClickAsync(IFFB_COMMAND_DEC); break; } } @@ -745,7 +755,7 @@ private void SetLowerRadioMode(CurrentSpitfireLFMkIXRadioMode currentSpitfireLFM Logger.Error(ex); } } - + private string GetHFRadioChannelStringCommand(bool moveUp) { lock (_lockHFRadioPresetDialObject1) @@ -755,47 +765,48 @@ private string GetHFRadioChannelStringCommand(bool moveUp) if ((_hfRadioOffCockpitButton == 1 || _hfRadioOffCockpitButton == 0) && _hfRadioChannelACockpitButton == 0 && _hfRadioChannelBCockpitButton == 0 && _hfRadioChannelCCockpitButton == 0 && _hfRadioChannelDCockpitButton == 0) { - return "RCTRL_A INC\n"; + return RADIO_CHANNEL_A + INC; } if (_hfRadioChannelACockpitButton == 1) { - return "RCTRL_B INC\n"; + return RADIO_CHANNEL_B + INC; } if (_hfRadioChannelBCockpitButton == 1) { - return "RCTRL_C INC\n"; + return RADIO_CHANNEL_C + INC; } if (_hfRadioChannelCCockpitButton == 1) { - return "RCTRL_D INC\n"; + return RADIO_CHANNEL_D + INC; } } else { if (_hfRadioChannelDCockpitButton == 1) { - return "RCTRL_C INC\n"; + return RADIO_CHANNEL_C + INC; } if (_hfRadioChannelCCockpitButton == 1) { - return "RCTRL_B INC\n"; + return RADIO_CHANNEL_B + INC; } if (_hfRadioChannelBCockpitButton == 1) { - return "RCTRL_A INC\n"; + return RADIO_CHANNEL_A + INC; } if (_hfRadioChannelACockpitButton == 1) { - return "RCTRL_OFF INC\n"; + return RADIO_OFF + INC; } } } + return null; } @@ -805,9 +816,9 @@ private string GetHFRadioModeStringCommand(bool moveUp) { if (moveUp) { - return "RCTRL_T_MODE " + (_hfRadioModeCockpitDialPosition + 1) + "\n"; + return RADIO_MODE + (_hfRadioModeCockpitDialPosition + 1) + "\n"; } - return "RCTRL_T_MODE " + (_hfRadioModeCockpitDialPosition - 1) + "\n"; + return RADIO_MODE + (_hfRadioModeCockpitDialPosition - 1) + "\n"; } } diff --git a/src/NonVisuals/Radios/RadiopanelPZ69Yak52.cs b/src/NonVisuals/Radios/RadiopanelPZ69Yak52.cs index 645b4507e..70c4346f1 100644 --- a/src/NonVisuals/Radios/RadiopanelPZ69Yak52.cs +++ b/src/NonVisuals/Radios/RadiopanelPZ69Yak52.cs @@ -1,4 +1,5 @@ -using NonVisuals.BindingClasses.BIP; +using System.Threading.Tasks; +using NonVisuals.BindingClasses.BIP; using NonVisuals.Helpers; namespace NonVisuals.Radios @@ -134,7 +135,7 @@ private enum CurrentYak52RadioMode public RadioPanelPZ69Yak52(HIDSkeleton hidSkeleton) : base(hidSkeleton) - {} + { } private bool _disposed; // Protected implementation of Dispose pattern. @@ -296,179 +297,178 @@ public override void DcsBiosDataReceived(object sender, DCSBIOSDataEventArgs e) } } - protected override void PZ69KnobChanged(IEnumerable hashSet) + protected override async Task PZ69KnobChangedAsync(IEnumerable hashSet) { try { Interlocked.Increment(ref _doUpdatePanelLCD); - lock (LockLCDUpdateObject) + + foreach (var radioPanelKnobObject in hashSet) { - foreach (var radioPanelKnobObject in hashSet) - { - var radioPanelKnob = (RadioPanelKnobYak52)radioPanelKnobObject; + var radioPanelKnob = (RadioPanelKnobYak52)radioPanelKnobObject; - switch (radioPanelKnob.RadioPanelPZ69Knob) - { - case RadioPanelPZ69KnobsYak52.UPPER_VHF: + switch (radioPanelKnob.RadioPanelPZ69Knob) + { + case RadioPanelPZ69KnobsYak52.UPPER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentYak52RadioMode.VHF); - } - - break; + SetUpperRadioMode(CurrentYak52RadioMode.VHF); } - case RadioPanelPZ69KnobsYak52.UPPER_ADF_FRONT: - { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentYak52RadioMode.ADF_FRONT); - } - break; - } - case RadioPanelPZ69KnobsYak52.UPPER_ADF_REAR: + break; + } + case RadioPanelPZ69KnobsYak52.UPPER_ADF_FRONT: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentYak52RadioMode.ADF_REAR); - } - - break; + SetUpperRadioMode(CurrentYak52RadioMode.ADF_FRONT); } - case RadioPanelPZ69KnobsYak52.UPPER_GMK: - { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentYak52RadioMode.GMK); - } - break; + break; + } + case RadioPanelPZ69KnobsYak52.UPPER_ADF_REAR: + { + if (radioPanelKnob.IsOn) + { + SetUpperRadioMode(CurrentYak52RadioMode.ADF_REAR); } - case RadioPanelPZ69KnobsYak52.UPPER_NO_USE1: - case RadioPanelPZ69KnobsYak52.UPPER_NO_USE2: - case RadioPanelPZ69KnobsYak52.UPPER_NO_USE3: + + break; + } + case RadioPanelPZ69KnobsYak52.UPPER_GMK: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetUpperRadioMode(CurrentYak52RadioMode.NO_USE); - } - break; + SetUpperRadioMode(CurrentYak52RadioMode.GMK); } - case RadioPanelPZ69KnobsYak52.LOWER_VHF: + break; + } + case RadioPanelPZ69KnobsYak52.UPPER_NO_USE1: + case RadioPanelPZ69KnobsYak52.UPPER_NO_USE2: + case RadioPanelPZ69KnobsYak52.UPPER_NO_USE3: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentYak52RadioMode.VHF); - } - break; + SetUpperRadioMode(CurrentYak52RadioMode.NO_USE); } - case RadioPanelPZ69KnobsYak52.LOWER_ADF_FRONT: + break; + } + + case RadioPanelPZ69KnobsYak52.LOWER_VHF: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentYak52RadioMode.ADF_FRONT); - } - break; + SetLowerRadioMode(CurrentYak52RadioMode.VHF); } - case RadioPanelPZ69KnobsYak52.LOWER_ADF_REAR: + break; + } + case RadioPanelPZ69KnobsYak52.LOWER_ADF_FRONT: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentYak52RadioMode.ADF_REAR); - } - break; + SetLowerRadioMode(CurrentYak52RadioMode.ADF_FRONT); } - case RadioPanelPZ69KnobsYak52.LOWER_GMK: + break; + } + case RadioPanelPZ69KnobsYak52.LOWER_ADF_REAR: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentYak52RadioMode.GMK); - } - - break; + SetLowerRadioMode(CurrentYak52RadioMode.ADF_REAR); } - case RadioPanelPZ69KnobsYak52.LOWER_NO_USE1: - case RadioPanelPZ69KnobsYak52.LOWER_NO_USE2: - case RadioPanelPZ69KnobsYak52.LOWER_NO_USE3: + break; + } + case RadioPanelPZ69KnobsYak52.LOWER_GMK: + { + if (radioPanelKnob.IsOn) { - if (radioPanelKnob.IsOn) - { - SetLowerRadioMode(CurrentYak52RadioMode.NO_USE); - } - break; + SetLowerRadioMode(CurrentYak52RadioMode.GMK); } - case RadioPanelPZ69KnobsYak52.UPPER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsYak52.UPPER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsYak52.UPPER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsYak52.UPPER_SMALL_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsYak52.LOWER_LARGE_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsYak52.LOWER_LARGE_FREQ_WHEEL_DEC: - case RadioPanelPZ69KnobsYak52.LOWER_SMALL_FREQ_WHEEL_INC: - case RadioPanelPZ69KnobsYak52.LOWER_SMALL_FREQ_WHEEL_DEC: + break; + } + case RadioPanelPZ69KnobsYak52.LOWER_NO_USE1: + case RadioPanelPZ69KnobsYak52.LOWER_NO_USE2: + case RadioPanelPZ69KnobsYak52.LOWER_NO_USE3: + { + if (radioPanelKnob.IsOn) { - // Ignore - break; + SetLowerRadioMode(CurrentYak52RadioMode.NO_USE); } + break; + } - case RadioPanelPZ69KnobsYak52.UPPER_FREQ_SWITCH: - { - _upperButtonPressed = radioPanelKnob.IsOn; + case RadioPanelPZ69KnobsYak52.UPPER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsYak52.UPPER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsYak52.UPPER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsYak52.UPPER_SMALL_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsYak52.LOWER_LARGE_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsYak52.LOWER_LARGE_FREQ_WHEEL_DEC: + case RadioPanelPZ69KnobsYak52.LOWER_SMALL_FREQ_WHEEL_INC: + case RadioPanelPZ69KnobsYak52.LOWER_SMALL_FREQ_WHEEL_DEC: + { + // Ignore + break; + } - if (_currentUpperRadioMode == CurrentYak52RadioMode.VHF) + case RadioPanelPZ69KnobsYak52.UPPER_FREQ_SWITCH: + { + _upperButtonPressed = radioPanelKnob.IsOn; + + if (_currentUpperRadioMode == CurrentYak52RadioMode.VHF) + { + if (!radioPanelKnob.IsOn) { - if (!radioPanelKnob.IsOn) + if (!_upperButtonPressedAndDialRotated) { - if (!_upperButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF_RADIO_SQUELCH_TOGGLE_COMMAND); - } - - _upperButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF_RADIO_SQUELCH_TOGGLE_COMMAND); } - } - break; + _upperButtonPressedAndDialRotated = false; + } } - case RadioPanelPZ69KnobsYak52.LOWER_FREQ_SWITCH: - { - _lowerButtonPressed = radioPanelKnob.IsOn; - if (_currentLowerRadioMode == CurrentYak52RadioMode.VHF) + break; + } + case RadioPanelPZ69KnobsYak52.LOWER_FREQ_SWITCH: + { + _lowerButtonPressed = radioPanelKnob.IsOn; + + if (_currentLowerRadioMode == CurrentYak52RadioMode.VHF) + { + if (!radioPanelKnob.IsOn) { - if (!radioPanelKnob.IsOn) + if (!_lowerButtonPressedAndDialRotated) { - if (!_lowerButtonPressedAndDialRotated) - { - // Do not synch if user has pressed the button to configure the radio - // Do when user releases button - DCSBIOS.Send(VHF_RADIO_SQUELCH_TOGGLE_COMMAND); - } - - _lowerButtonPressedAndDialRotated = false; + // Do not sync if user has pressed the button to configure the radio + // Do when user releases button + await DCSBIOS.SendAsync(VHF_RADIO_SQUELCH_TOGGLE_COMMAND); } + + _lowerButtonPressedAndDialRotated = false; } - break; } - } + break; + } + } - if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) - { - PluginManager.DoEvent( - DCSAircraft.SelectedAircraft.Description, - HIDInstance, - PluginGamingPanelEnum.PZ69RadioPanel_PreProg_Yak52, - (int)radioPanelKnob.RadioPanelPZ69Knob, - radioPanelKnob.IsOn, - null); - } + if (PluginManager.PlugSupportActivated && PluginManager.HasPlugin()) + { + PluginManager.DoEvent( + DCSAircraft.SelectedAircraft.Description, + HIDInstance, + PluginGamingPanelEnum.PZ69RadioPanel_PreProg_Yak52, + (int)radioPanelKnob.RadioPanelPZ69Knob, + radioPanelKnob.IsOn, + null); } - AdjustFrequency(hashSet); } + + await AdjustFrequencyAsync(hashSet); } catch (Exception ex) { @@ -476,7 +476,7 @@ protected override void PZ69KnobChanged(IEnumerable hashSet) } } - private void AdjustFrequency(IEnumerable hashSet) + private async Task AdjustFrequencyAsync(IEnumerable hashSet) { try { @@ -498,24 +498,24 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentYak52RadioMode.VHF: { - SendVHFMhzCommand(true); + await SendVHFMhzCommandAsync(true); break; } case CurrentYak52RadioMode.ADF_FRONT: { - DCSBIOS.Send(ADF_FRONT_VOLUME_INC); + await DCSBIOS.SendAsync(ADF_FRONT_VOLUME_INC); break; } case CurrentYak52RadioMode.ADF_REAR: { - DCSBIOS.Send(ADF_REAR_VOLUME_INC); + await DCSBIOS.SendAsync(ADF_REAR_VOLUME_INC); break; } case CurrentYak52RadioMode.GMK: - { - DCSBIOS.Send(_upperButtonPressed ? GMK_HEMISPHERE_SELECTOR_INC : GetGMKHeadingSelectorCommand(true)); - break; - } + { + await DCSBIOS.SendAsync(_upperButtonPressed ? GMK_HEMISPHERE_SELECTOR_INC : GetGMKHeadingSelectorCommand(true)); + break; + } case CurrentYak52RadioMode.NO_USE: { break; @@ -530,22 +530,22 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentYak52RadioMode.VHF: { - SendVHFMhzCommand(false); + await SendVHFMhzCommandAsync(false); break; } case CurrentYak52RadioMode.ADF_FRONT: { - DCSBIOS.Send(ADF_FRONT_VOLUME_DEC); + await DCSBIOS.SendAsync(ADF_FRONT_VOLUME_DEC); break; } case CurrentYak52RadioMode.ADF_REAR: { - DCSBIOS.Send(ADF_REAR_VOLUME_DEC); + await DCSBIOS.SendAsync(ADF_REAR_VOLUME_DEC); break; } case CurrentYak52RadioMode.GMK: { - DCSBIOS.Send(_upperButtonPressed ? GMK_HEMISPHERE_SELECTOR_DEC : GetGMKHeadingSelectorCommand(false)); + await DCSBIOS.SendAsync(_upperButtonPressed ? GMK_HEMISPHERE_SELECTOR_DEC : GetGMKHeadingSelectorCommand(false)); break; } } @@ -561,29 +561,29 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - SendVHFVolumeCommand(true); + await SendVHFVolumeCommandAsync(true); } else { - SendVHFKhzCommand(true); + await SendVHFKhzCommandAsync(true); } break; } case CurrentYak52RadioMode.ADF_FRONT: { - _adfFrontClickSkipper.Click(ADF_FRONT_CHANNEL_INC); + await _adfFrontClickSkipper.ClickAsync(ADF_FRONT_CHANNEL_INC); break; } case CurrentYak52RadioMode.ADF_REAR: { - _adfRearClickSkipper.Click(ADF_REAR_CHANNEL_INC); + await _adfRearClickSkipper.ClickAsync(ADF_REAR_CHANNEL_INC); break; } case CurrentYak52RadioMode.GMK: - { - DCSBIOS.Send(_upperButtonPressed ? GMK_MODE_SELECTOR_INC : GMK_LATITUDE_SELECTOR_INC); - break; - } + { + await DCSBIOS.SendAsync(_upperButtonPressed ? GMK_MODE_SELECTOR_INC : GMK_LATITUDE_SELECTOR_INC); + break; + } } break; } @@ -597,29 +597,29 @@ private void AdjustFrequency(IEnumerable hashSet) if (_upperButtonPressed) { _upperButtonPressedAndDialRotated = true; - SendVHFVolumeCommand(false); + await SendVHFVolumeCommandAsync(false); } else { - SendVHFKhzCommand(false); + await SendVHFKhzCommandAsync(false); } break; } case CurrentYak52RadioMode.ADF_FRONT: { - _adfFrontClickSkipper.Click(ADF_FRONT_CHANNEL_DEC); + await _adfFrontClickSkipper.ClickAsync(ADF_FRONT_CHANNEL_DEC); break; } case CurrentYak52RadioMode.ADF_REAR: { - _adfRearClickSkipper.Click(ADF_REAR_CHANNEL_DEC); + await _adfRearClickSkipper.ClickAsync(ADF_REAR_CHANNEL_DEC); break; } case CurrentYak52RadioMode.GMK: - { - DCSBIOS.Send(_upperButtonPressed ? GMK_MODE_SELECTOR_DEC : GMK_LATITUDE_SELECTOR_DEC); - break; - } + { + await DCSBIOS.SendAsync(_upperButtonPressed ? GMK_MODE_SELECTOR_DEC : GMK_LATITUDE_SELECTOR_DEC); + break; + } } break; } @@ -630,22 +630,22 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentYak52RadioMode.VHF: { - SendVHFMhzCommand(true); + await SendVHFMhzCommandAsync(true); break; } case CurrentYak52RadioMode.ADF_FRONT: { - DCSBIOS.Send(ADF_FRONT_VOLUME_INC); + await DCSBIOS.SendAsync(ADF_FRONT_VOLUME_INC); break; } case CurrentYak52RadioMode.ADF_REAR: { - DCSBIOS.Send(ADF_REAR_VOLUME_INC); + await DCSBIOS.SendAsync(ADF_REAR_VOLUME_INC); break; } case CurrentYak52RadioMode.GMK: { - DCSBIOS.Send(_lowerButtonPressed ? GMK_HEMISPHERE_SELECTOR_INC : GetGMKHeadingSelectorCommand(true)); + await DCSBIOS.SendAsync(_lowerButtonPressed ? GMK_HEMISPHERE_SELECTOR_INC : GetGMKHeadingSelectorCommand(true)); break; } } @@ -658,22 +658,22 @@ private void AdjustFrequency(IEnumerable hashSet) { case CurrentYak52RadioMode.VHF: { - SendVHFMhzCommand(false); + await SendVHFMhzCommandAsync(false); break; } case CurrentYak52RadioMode.ADF_FRONT: { - DCSBIOS.Send(ADF_FRONT_VOLUME_DEC); + await DCSBIOS.SendAsync(ADF_FRONT_VOLUME_DEC); break; } case CurrentYak52RadioMode.ADF_REAR: { - DCSBIOS.Send(ADF_REAR_VOLUME_DEC); + await DCSBIOS.SendAsync(ADF_REAR_VOLUME_DEC); break; } case CurrentYak52RadioMode.GMK: { - DCSBIOS.Send(_lowerButtonPressed ? GMK_HEMISPHERE_SELECTOR_DEC : GetGMKHeadingSelectorCommand(false)); + await DCSBIOS.SendAsync(_lowerButtonPressed ? GMK_HEMISPHERE_SELECTOR_DEC : GetGMKHeadingSelectorCommand(false)); break; } } @@ -689,29 +689,29 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - SendVHFVolumeCommand(true); + await SendVHFVolumeCommandAsync(true); } else { - SendVHFKhzCommand(true); + await SendVHFKhzCommandAsync(true); } break; } case CurrentYak52RadioMode.ADF_FRONT: { - _adfFrontClickSkipper.Click(ADF_FRONT_CHANNEL_INC); + await _adfFrontClickSkipper.ClickAsync(ADF_FRONT_CHANNEL_INC); break; } case CurrentYak52RadioMode.ADF_REAR: { - _adfRearClickSkipper.Click(ADF_REAR_CHANNEL_INC); + await _adfRearClickSkipper.ClickAsync(ADF_REAR_CHANNEL_INC); break; } case CurrentYak52RadioMode.GMK: - { - DCSBIOS.Send(_lowerButtonPressed ? GMK_MODE_SELECTOR_INC : GMK_LATITUDE_SELECTOR_INC); - break; - } + { + await DCSBIOS.SendAsync(_lowerButtonPressed ? GMK_MODE_SELECTOR_INC : GMK_LATITUDE_SELECTOR_INC); + break; + } } break; } @@ -725,29 +725,29 @@ private void AdjustFrequency(IEnumerable hashSet) if (_lowerButtonPressed) { _lowerButtonPressedAndDialRotated = true; - SendVHFVolumeCommand(false); + await SendVHFVolumeCommandAsync(false); } else { - SendVHFKhzCommand(false); + await SendVHFKhzCommandAsync(false); } break; } case CurrentYak52RadioMode.ADF_FRONT: { - _adfFrontClickSkipper.Click(ADF_FRONT_CHANNEL_DEC); + await _adfFrontClickSkipper.ClickAsync(ADF_FRONT_CHANNEL_DEC); break; } case CurrentYak52RadioMode.ADF_REAR: { - _adfRearClickSkipper.Click(ADF_REAR_CHANNEL_DEC); + await _adfRearClickSkipper.ClickAsync(ADF_REAR_CHANNEL_DEC); break; } case CurrentYak52RadioMode.GMK: - { - DCSBIOS.Send(_lowerButtonPressed ? GMK_MODE_SELECTOR_DEC : GMK_LATITUDE_SELECTOR_DEC); - break; - } + { + await DCSBIOS.SendAsync(_lowerButtonPressed ? GMK_MODE_SELECTOR_DEC : GMK_LATITUDE_SELECTOR_DEC); + break; + } } break; } @@ -962,63 +962,49 @@ private void SetLowerRadioMode(CurrentYak52RadioMode currentYak52RadioMode) } } - private void SendVHFMhzCommand(bool increase) + private async Task SendVHFMhzCommandAsync(bool increase) { var s = GetVHFRadioMhzDialStringCommand(increase); if (string.IsNullOrEmpty(s)) { return; } - DCSBIOS.Send(s); + await DCSBIOS.SendAsync(s); } - private void SendVHFKhzCommand(bool increase) + private async Task SendVHFKhzCommandAsync(bool increase) { var s = GetVHFRadioKhzDialStringCommand(increase); if (string.IsNullOrEmpty(s)) { return; } - DCSBIOS.Send(s); + await DCSBIOS.SendAsync(s); } - private void SendVHFVolumeCommand(bool increase) + + private async Task SendVHFVolumeCommandAsync(bool increase) { var s = GetVHFRadioVolumeDialStringCommand(increase); if (string.IsNullOrEmpty(s)) { return; } - DCSBIOS.Send(s); + await DCSBIOS.SendAsync(s); } private string GetVHFRadioMhzDialStringCommand(bool moveUp) { - if (moveUp) - { - - return $"{VHF_RADIO_MHZ_DIAL} {_vhfRadioMhzDialChangeValue} \n"; - } - return $"{VHF_RADIO_MHZ_DIAL} -{_vhfRadioMhzDialChangeValue} \n"; + return moveUp ? $"{VHF_RADIO_MHZ_DIAL} {_vhfRadioMhzDialChangeValue} \n" : $"{VHF_RADIO_MHZ_DIAL} -{_vhfRadioMhzDialChangeValue} \n"; } private string GetVHFRadioKhzDialStringCommand(bool moveUp) { - if (moveUp) - { - - return $"{VHF_RADIO_KHZ_DIAL} {_vhfRadioKhzDialChangeValue}\n"; - } - return $"{VHF_RADIO_KHZ_DIAL} -{_vhfRadioKhzDialChangeValue} \n"; + return moveUp ? $"{VHF_RADIO_KHZ_DIAL} {_vhfRadioKhzDialChangeValue}\n" : $"{VHF_RADIO_KHZ_DIAL} -{_vhfRadioKhzDialChangeValue} \n"; } private string GetVHFRadioVolumeDialStringCommand(bool moveUp) { - if (moveUp) - { - - return $"{VHF_RADIO_VOLUME_DIAL} {_vhfRadioVolumeDialChangeValue} \n"; - } - return $"{VHF_RADIO_VOLUME_DIAL} -{_vhfRadioVolumeDialChangeValue} \n"; + return moveUp ? $"{VHF_RADIO_VOLUME_DIAL} {_vhfRadioVolumeDialChangeValue} \n" : $"{VHF_RADIO_VOLUME_DIAL} -{_vhfRadioVolumeDialChangeValue} \n"; } private string GetGMKHeadingSelectorCommand(bool moveUp)