Skip to content

Commit

Permalink
Octavi shift modes controlled by .json file (#1688)
Browse files Browse the repository at this point in the history
* Octavi shift modes controlled by .json file

* Update MobiFlight/Joysticks/Octavi/OctaviHandler.cs

Co-authored-by: Sebastian M <[email protected]>

* Octavi shift modes controlled by .json file-Reviewed

* Octavi shift modes controlled by .json file-Reviewed_2

---------

Co-authored-by: Felix von Plehwe <[email protected]>
Co-authored-by: Sebastian M <[email protected]>
  • Loading branch information
3 people authored Feb 26, 2024
1 parent a716951 commit 8b35ad5
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 39 deletions.
26 changes: 25 additions & 1 deletion Joysticks/octavi.joystick.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
{
Expand Down
6 changes: 4 additions & 2 deletions MobiFlight/Joysticks/Octavi/Octavi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
Expand Down
74 changes: 38 additions & 36 deletions MobiFlight/Joysticks/Octavi/OctaviHandler.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace MobiFlight.Joysticks.Octavi
{
Expand All @@ -14,31 +15,45 @@ internal class OctaviHandler
private enum OctaviEncoder { OUTER_INC, OUTER_DEC, INNER_INC, INNER_DEC }

public IEnumerable<string> JoystickButtonNames { get; private set; }
public List<Context> 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<Context>();
var octaviStateType = typeof(OctaviReport.OctaviState);
foreach(var state in Enum.GetValues(octaviStateType).Cast<OctaviReport.OctaviState>())
{
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
Expand All @@ -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"));
}
Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 8b35ad5

Please sign in to comment.