diff --git a/Joysticks/octavi.joystick.json b/Joysticks/octavi.joystick.json index 188aa315..c285d06c 100644 --- a/Joysticks/octavi.joystick.json +++ b/Joysticks/octavi.joystick.json @@ -4,7 +4,31 @@ "VendorId": "0x04D8", "ProductId": "0xE6D6", "Inputs": [ - + { + "Id": 0, + "Type": "Button", + "Label": "COM1" + }, + { + "Id": 1, + "Type": "Button", + "Label": "COM2" + }, + { + "Id": 2, + "Type": "Button", + "Label": "NAV1" + }, + { + "Id": 3, + "Type": "Button", + "Label": "NAV2" + }, + { + "Id": 7, + "Type": "Button", + "Label": "XPDR" + } ], "Outputs": [ { diff --git a/MobiFlight/Joysticks/Octavi/Octavi.cs b/MobiFlight/Joysticks/Octavi/Octavi.cs index c13ffcf2..b089e45a 100644 --- a/MobiFlight/Joysticks/Octavi/Octavi.cs +++ b/MobiFlight/Joysticks/Octavi/Octavi.cs @@ -13,11 +13,13 @@ internal class Octavi : Joystick protected HidDeviceInputReceiver inputReceiver; protected ReportDescriptor reportDescriptor; - private readonly OctaviHandler octaviHandler = new OctaviHandler(); + private readonly OctaviHandler octaviHandler; + private readonly JoystickDefinition Definition; public Octavi(SharpDX.DirectInput.Joystick joystick, JoystickDefinition definition) : base(joystick, definition) { - + this.Definition = definition; + octaviHandler = new OctaviHandler(this.Definition); } /// diff --git a/MobiFlight/Joysticks/Octavi/OctaviHandler.cs b/MobiFlight/Joysticks/Octavi/OctaviHandler.cs index ac5fbc7f..3562f65d 100644 --- a/MobiFlight/Joysticks/Octavi/OctaviHandler.cs +++ b/MobiFlight/Joysticks/Octavi/OctaviHandler.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Linq; namespace MobiFlight.Joysticks.Octavi { @@ -14,31 +15,45 @@ internal class OctaviHandler private enum OctaviEncoder { OUTER_INC, OUTER_DEC, INNER_INC, INNER_DEC } public IEnumerable JoystickButtonNames { get; private set; } + public List contexts; + public struct Context + { + public OctaviReport.OctaviState state; + public bool isShifted; + public string name; - public OctaviHandler() + public Context(OctaviReport.OctaviState State, bool IsShifted, string Name) + { + state = State; + isShifted = IsShifted; + name = Name; + } + } + + private JoystickDefinition definition; + + public OctaviHandler(JoystickDefinition definition = null) { + this.definition = definition; encoderMappings = new Dictionary<(OctaviReport.OctaviState state, bool isShifted, OctaviEncoder encoder), int>(); buttonMappings = new Dictionary<(OctaviReport.OctaviState state, bool isShifted, OctaviReport.OctaviButtons button), int>(); - // All possible states of the Octavi IFR1 - var contexts = new (OctaviReport.OctaviState state, bool isShifted, string name)[] { - // shifted - (OctaviReport.OctaviState.STATE_COM1, false, "COM1"), - (OctaviReport.OctaviState.STATE_COM2, false, "COM2"), - (OctaviReport.OctaviState.STATE_NAV1, false, "NAV1"), - (OctaviReport.OctaviState.STATE_NAV2, false, "NAV2"), - (OctaviReport.OctaviState.STATE_FMS1, false, "FMS1"), - (OctaviReport.OctaviState.STATE_FMS2, false, "FMS2"), - (OctaviReport.OctaviState.STATE_AP, false, "AP"), - (OctaviReport.OctaviState.STATE_XPDR, false, "XPDR"), - //unshifted - (OctaviReport.OctaviState.STATE_COM1, true, "COM1^"), - (OctaviReport.OctaviState.STATE_COM2, true, "COM2^"), - (OctaviReport.OctaviState.STATE_NAV1, true, "NAV1^"), - (OctaviReport.OctaviState.STATE_NAV2, true, "NAV2^"), - (OctaviReport.OctaviState.STATE_XPDR, true, "XPDR^"), - }; + this.contexts = new List(); + var octaviStateType = typeof(OctaviReport.OctaviState); + foreach(var state in Enum.GetValues(octaviStateType).Cast()) + { + var stateName = Enum.GetName(octaviStateType, state).Replace("STATE_", ""); + var context = new Context(state, false, stateName); + this.contexts.Add(context); + } + foreach (var input in definition.Inputs) + { + var state = (OctaviReport.OctaviState)input.Id; + var stateName = Enum.GetName(octaviStateType, state).Replace("STATE_", ""); + var context = new Context(state, true, stateName + "^"); + this.contexts.Add(context); + } foreach (var context in contexts) { // Encoders @@ -51,9 +66,7 @@ public OctaviHandler() buttonMappings.Add((context.state, context.isShifted, OctaviReport.OctaviButtons.HID_BTN_TOG), ToButton($"Button_{context.name}_TOG")); // CRSR - if (context.state == OctaviReport.OctaviState.STATE_FMS1 || - context.state == OctaviReport.OctaviState.STATE_FMS2 || - context.state == OctaviReport.OctaviState.STATE_AP) + if (!definition.Inputs.Any(JoystickInput => JoystickInput.Id == (int) context.state)) { buttonMappings.Add((context.state, context.isShifted, OctaviReport.OctaviButtons.HID_ENC_SW), ToButton($"Button_{context.name}_CRSR")); } @@ -95,7 +108,7 @@ public OctaviHandler() JoystickButtonNames = buttons.AsReadOnly(); } - private void AddAutopilotButtonMappings((OctaviReport.OctaviState state, bool isShifted, string name) context) + private void AddAutopilotButtonMappings(Context context) { buttonMappings.Add((context.state, context.isShifted, OctaviReport.OctaviButtons.HID_BTN_AP), ToButton("Button_AP_AP")); buttonMappings.Add((context.state, context.isShifted, OctaviReport.OctaviButtons.HID_BTN_AP_HDG), ToButton("Button_AP_HDG")); @@ -126,20 +139,9 @@ private int ToButton(string buttonName) { isInShiftMode = false; // reset shift mode on context change } - else if (pressed.HasFlag(OctaviReport.OctaviButtons.HID_ENC_SW)) + else if (pressed.HasFlag(OctaviReport.OctaviButtons.HID_ENC_SW) && this.definition.Inputs.Any(JoystickInput => JoystickInput.Id == (int)report.contextState)) { - switch (report.contextState) - { - case OctaviReport.OctaviState.STATE_COM1: - case OctaviReport.OctaviState.STATE_COM2: - case OctaviReport.OctaviState.STATE_NAV1: - case OctaviReport.OctaviState.STATE_NAV2: - case OctaviReport.OctaviState.STATE_XPDR: - isInShiftMode = !isInShiftMode; - break; - default: - break; - } + isInShiftMode = !isInShiftMode; } // Encoders (Note: No RELEASE events required for encoders)