Skip to content

Commit

Permalink
Allow swapping Tap/Hold function of COM/NAV button
Browse files Browse the repository at this point in the history
  • Loading branch information
nguyenquyhy committed Dec 5, 2020
1 parent b682b99 commit 112aa40
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 72 deletions.
12 changes: 8 additions & 4 deletions FlightStreamDeck.AddOn/PropertyInspector/NavCom.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
<option value="XPDR">XPDR</option>
</select>
</div>
<div class="sdpi-item">
<details class="message info">
<summary>Hold the button to open the numpad</summary>
</details>
<div class="sdpi-item" type="field">
<div class="sdpi-item-label">Tap/Hold functions</div>
<select class="sdpi-item-value select" id="HoldFunction" onchange="updateData()">
<option value="Numpad">Swap/Numpad</option>
<option value="Swap">Numpad/Swap</option>
</select>
</div>
<div type="group" class="sdpi-item" id="messagegroup">
<div class="sdpi-item-label">Dependencies</div>
Expand All @@ -46,6 +48,7 @@
<script language="JavaScript" type="text/javascript">
function loadSettings(settings) {
if (settings['Type']) { Type.value = settings['Type'] };
if (settings['HoldFunction']) { Type.value = settings['HoldFunction'] };
if (settings['BattMasterValue']) { BattMasterValue.value = settings['BattMasterValue'] };
if (settings['AvionicsValue']) { AvionicsValue.value = settings['AvionicsValue'] };
}
Expand All @@ -54,6 +57,7 @@
console.log('update data');
const payload = {
"Type": Type.value,
"HoldFunction": HoldFunction.value,
"BattMasterValue": BattMasterValue.value,
"AvionicsValue": AvionicsValue.value,
};
Expand Down
162 changes: 94 additions & 68 deletions FlightStreamDeck.Logics/Actions/NavComAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace FlightStreamDeck.Logics.Actions
public class NavComSettings
{
public string Type { get; set; }
public string HoldFunction { get; set; }
public string AvionicsValue { get; set; }
public string BattMasterValue { get; set; }
}
Expand All @@ -40,7 +41,7 @@ public class NavComAction : StreamDeckAction<NavComSettings>
private readonly Timer timer;

private IdentifiableDeviceInfo device;

private NavComSettings settings;

private TOGGLE_VALUE? dependantOnAvionics;
Expand Down Expand Up @@ -68,71 +69,14 @@ private async void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
timer.Stop();

if (settings?.Type != null && set != null && mask != null && lastDependant)
// Handle hold
if (settings.HoldFunction != "Swap")
{
var set = this.set;
var mask = this.mask;
var min = settings.Type switch
{
"NAV1" => minNavVal,
"NAV2" => minNavVal,
"COM1" => minComVal,
"COM2" => minComVal,
"XPDR" => minXpdrVal,
_ => throw new ArgumentException($"{settings.Type} is not supported for numpad")
};
DeckLogic.NumpadParams = new NumpadParams(
settings.Type,
min,
settings.Type switch
{
"NAV1" => maxNavVal,
"NAV2" => maxNavVal,
"COM1" => maxComVal,
"COM2" => maxComVal,
"XPDR" => maxXpdrVal,
_ => throw new ArgumentException($"{settings.Type} is not supported for numpad")
},
mask
);
DeckLogic.NumpadTcs = new TaskCompletionSource<(string, bool)>();

var toggle = this.toggle;

this.initializationTcs = new TaskCompletionSource<bool>();

await StreamDeck.SwitchToProfileAsync(registration.PluginUUID,
device.Id,
device.Type == DeviceType.StreamDeckXL ? "Profiles/Numpad_XL" : "Profiles/Numpad");

await initializationTcs.Task;

var (value, swap) = await DeckLogic.NumpadTcs.Task;
if (!string.IsNullOrEmpty(value))
{
value += min.Substring(value.Length);

if (settings.Type == "NAV1" || settings.Type == "NAV2" || settings.Type == "COM1" || settings.Type == "COM2")
{
// NOTE: SimConnect ignore first 1
value = value[1..];
}

// BCD encode
uint data = 0;
for (var i = 0; i < value.Length; i++)
{
uint digit = (byte)value[i] - (uint)48;
data = data * 16 + digit;
}
flightConnector.Trigger(set.Value, data);

if (toggle != null && swap)
{
await Task.Delay(500);
flightConnector.Trigger(toggle.Value);
}
}
await SwitchToNumpad();
}
else
{
SwapFrequencies();
}
}

Expand Down Expand Up @@ -183,10 +127,14 @@ protected override Task OnKeyUp(ActionEventArgs<KeyPayload> args)
{
timer.Stop();

// Transfer
if (toggle != null)
// Click
if (settings.HoldFunction != "Swap")
{
flightConnector.Trigger(toggle.Value);
SwapFrequencies();
}
else
{
return SwitchToNumpad();
}
}
}
Expand Down Expand Up @@ -343,5 +291,83 @@ private void SwitchTo(string type)
flightConnector.RegisterSimValues(dependantOnBatt.Value);
}
}

private void SwapFrequencies()
{
if (toggle != null)
{
flightConnector.Trigger(toggle.Value);
}
}

private async Task SwitchToNumpad()
{
if (settings?.Type != null && set != null && mask != null && lastDependant)
{
var set = this.set;
var mask = this.mask;
var min = settings.Type switch
{
"NAV1" => minNavVal,
"NAV2" => minNavVal,
"COM1" => minComVal,
"COM2" => minComVal,
"XPDR" => minXpdrVal,
_ => throw new ArgumentException($"{settings.Type} is not supported for numpad")
};
DeckLogic.NumpadParams = new NumpadParams(
settings.Type,
min,
settings.Type switch
{
"NAV1" => maxNavVal,
"NAV2" => maxNavVal,
"COM1" => maxComVal,
"COM2" => maxComVal,
"XPDR" => maxXpdrVal,
_ => throw new ArgumentException($"{settings.Type} is not supported for numpad")
},
mask
);
DeckLogic.NumpadTcs = new TaskCompletionSource<(string, bool)>();

var toggle = this.toggle;

this.initializationTcs = new TaskCompletionSource<bool>();

await StreamDeck.SwitchToProfileAsync(registration.PluginUUID,
device.Id,
device.Type == DeviceType.StreamDeckXL ? "Profiles/Numpad_XL" : "Profiles/Numpad");

await initializationTcs.Task;

var (value, swap) = await DeckLogic.NumpadTcs.Task;
if (!string.IsNullOrEmpty(value))
{
value += min.Substring(value.Length);

if (settings.Type == "NAV1" || settings.Type == "NAV2" || settings.Type == "COM1" || settings.Type == "COM2")
{
// NOTE: SimConnect ignore first 1
value = value[1..];
}

// BCD encode
uint data = 0;
for (var i = 0; i < value.Length; i++)
{
uint digit = (byte)value[i] - (uint)48;
data = data * 16 + digit;
}
flightConnector.Trigger(set.Value, data);

if (toggle != null && swap)
{
await Task.Delay(500);
flightConnector.Trigger(toggle.Value);
}
}
}
}
}
}

0 comments on commit 112aa40

Please sign in to comment.