Skip to content

Commit

Permalink
Support proper Honeycomb Button and Axis Labels (#1027)
Browse files Browse the repository at this point in the history
  • Loading branch information
DocMoebiuz authored Dec 8, 2022
1 parent 3ac2366 commit 34be739
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 72 deletions.
2 changes: 1 addition & 1 deletion MobiFlight/ExecutionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ void mobiFlightCache_OnButtonPressed(object sender, InputEventArgs e)
eventAction = MobiFlightAnalogInput.InputEventIdToString(0) + " => " +e.Value;
}

var msgEventLabel = $"{e.Name} => {e.DeviceId} {(e.ExtPin.HasValue ? $":{e.ExtPin}" : "")} => {eventAction}";
var msgEventLabel = $"{e.Name} => {e.DeviceLabel} {(e.ExtPin.HasValue ? $":{e.ExtPin}" : "")} => {eventAction}";

lock (inputCache)
{
Expand Down
64 changes: 44 additions & 20 deletions MobiFlight/Joystick.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
using System;
using SharpDX.DirectInput;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Web.SessionState;
using SharpDX;
using SharpDX.DirectInput;

namespace MobiFlight
{
Expand Down Expand Up @@ -94,7 +89,7 @@ public Joystick(SharpDX.DirectInput.Joystick joystick)
this.joystick = joystick;
}

private void EnumerateDevices()
protected virtual void EnumerateDevices()
{
foreach (DeviceObjectInstance device in this.joystick.GetObjects())
{
Expand All @@ -115,23 +110,26 @@ private void EnumerateDevices()
if (IsAxis && Axes.Count < joystick.Capabilities.AxeCount)
{
String OffsetAxisName = "Unknown";
var FriendlyAxisName = name;
try
{
OffsetAxisName = GetAxisNameForUsage(usage);
FriendlyAxisName = GetFriendlyAxisName(name);

} catch (ArgumentOutOfRangeException ex)
{
Log.Instance.log($"Axis can't be mapped: {joystick.Information.InstanceName} Aspect: {aspect} Offset: {offset} Usage: {usage} Axis: {name}.", LogSeverity.Error);
Log.Instance.log($"Axis can't be mapped: {joystick.Information.InstanceName} Aspect: {aspect} Offset: {offset} Usage: {usage} Axis: {name} Label: {FriendlyAxisName}.", LogSeverity.Error);
continue;
}
Axes.Add(new JoystickDevice() { Name = AxisPrefix + OffsetAxisName, Label = name, Type = JoystickDeviceType.Axis });
Log.Instance.log($"Added {joystick.Information.InstanceName} Aspect {aspect} + Offset: {offset} Usage: {usage} Axis: {name}.", LogSeverity.Debug);
Axes.Add(new JoystickDevice() { Name = AxisPrefix + OffsetAxisName, Label = FriendlyAxisName, Type = JoystickDeviceType.Axis });
Log.Instance.log($"Added {joystick.Information.InstanceName} Aspect {aspect} + Offset: {offset} Usage: {usage} Axis: {name} Label: {FriendlyAxisName}.", LogSeverity.Debug);

}
else if (IsButton)
{
String ButtonName = CorrectButtonIndexForButtonName(name, Buttons.Count + 1);
String ButtonName = GetFriendlyButtonName(name, Buttons.Count + 1);
Buttons.Add(new JoystickDevice() { Name = ButtonPrefix + (Buttons.Count + 1), Label = ButtonName, Type = JoystickDeviceType.Button });
Log.Instance.log($"Added {joystick.Information.InstanceName} Aspect: {aspect} Offset: {offset} Usage: {usage} Button: {name}.", LogSeverity.Debug);
Log.Instance.log($"Added {joystick.Information.InstanceName} Aspect: {aspect} Offset: {offset} Usage: {usage} Button: {name} Label: {ButtonName}.", LogSeverity.Debug);
}
else if (IsPOV)
{
Expand All @@ -151,9 +149,19 @@ private void EnumerateDevices()
}
}

private string CorrectButtonIndexForButtonName(string name, int v)
protected string GetFriendlyAxisName(string name)
{
return MapDeviceNameToLabel(name);
}

protected string GetFriendlyButtonName(string name, int v)
{
return Regex.Replace(name, @"\d+", v.ToString()).ToString();
return MapDeviceNameToLabel(Regex.Replace(name, @"\d+", v.ToString()).ToString());
}

protected virtual string MapDeviceNameToLabel(string deviceName)
{
return deviceName;
}

public void Connect(IntPtr handle)
Expand All @@ -174,7 +182,8 @@ virtual protected void EnumerateOutputDevices()
public List<ListItem> GetAvailableDevices()
{
List<ListItem> result = new List<ListItem>();
Buttons.ForEach((item) =>

GetButtonsSorted().ForEach((item) =>
{
result.Add(item.ToListItem());
});
Expand All @@ -189,6 +198,16 @@ public List<ListItem> GetAvailableDevices()
return result;
}

protected virtual List<JoystickDevice> GetButtonsSorted()
{
return Buttons;
}

protected virtual List<JoystickDevice> GetAxisSorted()
{
return Axes;
}

public List<ListItem> GetAvailableOutputDevices()
{
List<ListItem> result = new List<ListItem>();
Expand All @@ -206,6 +225,7 @@ public void Update()
try
{
joystick.Poll();

JoystickState newState = joystick.GetCurrentState();
UpdateButtons(newState);
UpdateAxis(newState);
Expand Down Expand Up @@ -243,7 +263,8 @@ private void UpdatePOV(JoystickState newState)
OnButtonPressed?.Invoke(this, new InputEventArgs()
{
Name = Name,
DeviceId = POV[index].Label,
DeviceId = POV[index].Name,
DeviceLabel = POV[index].Label,
Serial = SerialPrefix + joystick.Information.InstanceGuid.ToString(),
Type = DeviceType.Button,
Value = (int)MobiFlightButton.InputEvent.RELEASE
Expand All @@ -258,7 +279,8 @@ private void UpdatePOV(JoystickState newState)
OnButtonPressed?.Invoke(this, new InputEventArgs()
{
Name = Name,
DeviceId = POV[index].Label,
DeviceId = POV[index].Name,
DeviceLabel = POV[index].Label,
Serial = SerialPrefix + joystick.Information.InstanceGuid.ToString(),
Type = DeviceType.Button,
Value = (int)MobiFlightButton.InputEvent.PRESS
Expand Down Expand Up @@ -286,7 +308,8 @@ private void UpdateAxis(JoystickState newState)
OnButtonPressed?.Invoke(this, new InputEventArgs()
{
Name = Name,
DeviceId = Axes[CurrentAxis].Label,
DeviceId = Axes[CurrentAxis].Name,
DeviceLabel = Axes[CurrentAxis].Label,
Serial = SerialPrefix + joystick.Information.InstanceGuid.ToString(),
Type = DeviceType.AnalogInput,
Value = newValue
Expand All @@ -308,7 +331,8 @@ private void UpdateButtons(JoystickState newState)
OnButtonPressed?.Invoke(this, new InputEventArgs()
{
Name = Name,
DeviceId = Buttons[i].Label,
DeviceId = Buttons[i].Name,
DeviceLabel = Buttons[i].Label,
Serial = SerialPrefix + joystick.Information.InstanceGuid.ToString(),
Type = DeviceType.Button,
Value = newState.Buttons[i] ? 0 : 1
Expand Down
9 changes: 8 additions & 1 deletion MobiFlight/JoystickManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,15 @@ public void Connect(IntPtr Handle)
if (d.InstanceName == "Bravo Throttle Quadrant")
{
js = new Joysticks.HoneycombBravo(new SharpDX.DirectInput.Joystick(di, d.InstanceGuid));
} else
} else if (d.InstanceName == "Saitek Aviator Stick")
{
js = new Joysticks.SaitekAviatorStick(new SharpDX.DirectInput.Joystick(di, d.InstanceGuid));
}
else
{
js = new Joystick(new SharpDX.DirectInput.Joystick(di, d.InstanceGuid));
}


if (!HasAxisOrButtons(js)) continue;

Expand Down
138 changes: 100 additions & 38 deletions MobiFlight/Joysticks/HoneycombBravo.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,85 @@
using System;
using HidSharp;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HidSharp;

namespace MobiFlight.Joysticks
{
internal class HoneycombBravo : Joystick
internal class HoneycombBravo : LabeledJoystick
{
int VendorId = 0x294B;
int ProductId = 0x1901;
HidStream Stream { get; set; }
HidDevice Device { get; set; }
public HoneycombBravo(SharpDX.DirectInput.Joystick joystick) : base(joystick)
{

public HoneycombBravo(SharpDX.DirectInput.Joystick joystick) : base(joystick) {
Labels = new Dictionary<string, string>()
{
// Mode
{ "Button 17", "Mode - IAS" },
{ "Button 18", "Mode - CRS" },
{ "Button 19", "Mode - HDG" },
{ "Button 20", "Mode - VS" },
{ "Button 21", "Mode - ALT" },
// AP Buttons
{ "Button 1", "AP - HDG" },
{ "Button 2", "AP - NAV" },
{ "Button 3", "AP - APR" },
{ "Button 4", "AP - REV" },
{ "Button 5", "AP - ALT" },
{ "Button 6", "AP - VS" },
{ "Button 7", "AP - IAS" },
{ "Button 8", "AP - Autopilot" },
// Generic Encoder
{ "Button 13", "Encoder - Right" },
{ "Button 14", "Encoder - Left" },
// Gear
{ "Button 31", "Gear - Up" },
{ "Button 32", "Gear - Down" },
// Flaps
{ "Button 16", "Flaps - Up" },
{ "Button 15", "Flaps - Down" },
// Trim
{ "Button 22", "Trim - Nose Down" },
{ "Button 23", "Trim - Nose Up" },
// Levers
{ "Button 24", "Lever 1 - Detent" },
{ "Button 25", "Lever 2 - Detent" },
{ "Button 26", "Lever 3 - Detent" },
{ "Button 27", "Lever 4 - Detent" },
{ "Button 28", "Lever 5 - Detent" },
{ "Button 33", "Lever 6 - Detent" },
//
{ "Button 29", "Lever 1 - Button" },
{ "Button 9", "Lever 2 - Button" },
{ "Button 10", "Lever 3 - Button" },
{ "Button 11", "Lever 4 - Button" },
{ "Button 12", "Lever 5 - Button" },
//
{ "Button 30", "Lever 3 - Reverser" },
{ "Button 48", "Lever 4 - Reverser" },

//
{ "Button 34", "Switch 1 - Up" },
{ "Button 35", "Switch 1 - Down" },
{ "Button 36", "Switch 2 - Up" },
{ "Button 37", "Switch 2 - Down" },
{ "Button 38", "Switch 3 - Up" },
{ "Button 39", "Switch 3 - Down" },
{ "Button 40", "Switch 4 - Up" },
{ "Button 41", "Switch 4 - Down" },
{ "Button 42", "Switch 5 - Up" },
{ "Button 43", "Switch 5 - Down" },
{ "Button 44", "Switch 6 - Up" },
{ "Button 45", "Switch 6 - Down" },
{ "Button 46", "Switch 7 - Up" },
{ "Button 47", "Switch 7 - Down" },
// Axis
{ "Y Axis", "Axis - Lever 1" },
{ "X Axis", "Axis - Lever 2" },
{ "Z Rotation", "Axis - Lever 3" },
{ "Y Rotation", "Axis - Lever 4" },
{ "X Rotation", "Axis - Lever 5" },
{ "Z Axis", "Axis - Lever 6" },
};
}

public void Connect()
Expand Down Expand Up @@ -43,39 +107,37 @@ protected override void SendData(byte[] data)
protected override void EnumerateOutputDevices()
{
base.EnumerateOutputDevices();
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - HDG", Name = "AP.hdg", Byte = 1, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - NAV", Name = "AP.nav", Byte = 1, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - APR", Name = "AP.apr", Byte = 1, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - REV", Name = "AP.rev", Byte = 1, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - ALT", Name = "AP.alt", Byte = 1, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - VS", Name = "AP.vs", Byte = 1, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - IAS", Name = "AP.ias", Byte = 1, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - On/Off", Name = "AP.autopilot", Byte = 1, Bit = 7 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - HDG", Name = "AP.hdg", Byte = 1, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - NAV", Name = "AP.nav", Byte = 1, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - APR", Name = "AP.apr", Byte = 1, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - REV", Name = "AP.rev", Byte = 1, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - ALT", Name = "AP.alt", Byte = 1, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - VS", Name = "AP.vs", Byte = 1, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - IAS", Name = "AP.ias", Byte = 1, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "AP Mode - On/Off", Name = "AP.autopilot", Byte = 1, Bit = 7 });
// -- Byte 2
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Left Green", Name = "Gear.LeftGreen", Byte = 2, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Left Red", Name = "Gear.LeftRed", Byte = 2, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Center Green", Name = "Gear.CenterGreen", Byte = 2, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Center Red", Name = "Gear.CenterRed", Byte = 2, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Right Green", Name = "Gear.RightGreen", Byte = 2, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Right Red", Name = "Gear.RightRed", Byte = 2, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Master Warning", Name = "Light.MasterWarn", Byte = 2, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Engine Fire", Name = "Light.EngineFire", Byte = 2, Bit = 7 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Left Green", Name = "Gear.LeftGreen", Byte = 2, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Left Red", Name = "Gear.LeftRed", Byte = 2, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Center Green", Name = "Gear.CenterGreen", Byte = 2, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Center Red", Name = "Gear.CenterRed", Byte = 2, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Right Green", Name = "Gear.RightGreen", Byte = 2, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "Gear - Right Red", Name = "Gear.RightRed", Byte = 2, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Master Warning", Name = "Light.MasterWarn", Byte = 2, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Engine Fire", Name = "Light.EngineFire", Byte = 2, Bit = 7 });
// -- Byte 3
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Oil Pressure", Name = "Light.LowOil", Byte = 3, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Fuel Pressure", Name = "Light.LowFuel", Byte = 3, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Anti Ice", Name = "Light.Antiice", Byte = 3, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Starter Engaged", Name = "Light.Starter", Byte = 3, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - APU", Name = "Light.APU", Byte = 3, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Master Caution", Name = "Light.MasterCaution", Byte = 3, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Vacuum", Name = "Light.Vacuum", Byte = 3, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Hyd Pressure", Name = "Light.LowHydPressURE", Byte = 3, Bit = 7 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Oil Pressure", Name = "Light.LowOil", Byte = 3, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Fuel Pressure", Name = "Light.LowFuel", Byte = 3, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Anti Ice", Name = "Light.Antiice", Byte = 3, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Starter Engaged", Name = "Light.Starter", Byte = 3, Bit = 3 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - APU", Name = "Light.APU", Byte = 3, Bit = 4 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Master Caution", Name = "Light.MasterCaution", Byte = 3, Bit = 5 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Vacuum", Name = "Light.Vacuum", Byte = 3, Bit = 6 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Hyd Pressure", Name = "Light.LowHydPressURE", Byte = 3, Bit = 7 });
// -- Byte 4
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Aux Fuel Pump", Name = "Lights.AuxFuelPump", Byte = 4, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Parking Brake", Name = "Lights.ParkingBrake", Byte = 4, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Volts", Name = "Lights.LowVolts", Byte = 4, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Door", Name = "Lights.door", Byte = 4, Bit = 3 });


Lights.Add(new JoystickOutputDevice() { Label = "Lights - Aux Fuel Pump", Name = "Lights.AuxFuelPump", Byte = 4, Bit = 0 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Parking Brake", Name = "Lights.ParkingBrake", Byte = 4, Bit = 1 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Low Volts", Name = "Lights.LowVolts", Byte = 4, Bit = 2 });
Lights.Add(new JoystickOutputDevice() { Label = "Lights - Door", Name = "Lights.door", Byte = 4, Bit = 3 });
}
}
}
Loading

0 comments on commit 34be739

Please sign in to comment.