Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added BetterJoy/Icons/nes.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added BetterJoy/Icons/nes_charging.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified BetterJoy/Icons/src/controllers.psd
Binary file not shown.
59 changes: 48 additions & 11 deletions BetterJoy/Joycon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,14 @@ public enum Button
Shoulder22 = 19
}

public enum ControllerType
public enum ControllerType : byte
{
Pro,
JoyconLeft,
JoyconRight,
SNES,
N64
Pro = 0x01,
JoyconLeft = 0x02,
JoyconRight = 0x03,
SNES = 0x04,
N64 = 0x05,
NES = 0x06,
}

public enum DebugType
Expand Down Expand Up @@ -244,7 +245,7 @@ private set

private long _timestampActivity = Stopwatch.GetTimestamp();

public readonly ControllerType Type;
public ControllerType Type { get; private set; }

public EventHandler<StateChangedEventArgs> StateChanged;

Expand Down Expand Up @@ -312,6 +313,7 @@ public Joycon(

public bool IsPro => Type is ControllerType.Pro;
public bool IsSNES => Type == ControllerType.SNES;
public bool IsNES => Type == ControllerType.NES;
public bool IsN64 => Type == ControllerType.N64;
public bool IsJoycon => Type is ControllerType.JoyconRight or ControllerType.JoyconLeft;
public bool IsLeft => Type != ControllerType.JoyconRight;
Expand Down Expand Up @@ -489,6 +491,12 @@ public void Attach()
// do not always send a response so we don't check if there is one
SetReportMode(ReportMode.SimpleHID, false);

//Make sure we're not actually a nes controller
if (Type == ControllerType.JoyconRight)
{
CheckIfRightIsNes();
}

var ok = DumpCalibrationData();
if (!ok)
{
Expand All @@ -504,7 +512,7 @@ public void Attach()
{
SetIMUSensitivity();
}

SetRumble(true);
SetNFCIR(false);
SetReportMode(ReportMode.StandardFull);
Expand Down Expand Up @@ -717,6 +725,32 @@ private bool SetReportMode(ReportMode reportMode, bool checkResponse = true)
return true;
}

private void CheckIfRightIsNes()

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should throw a DeviceComFailedException if it failed so it tries to reconnect the controller like so :

private void CheckIfRightIsNes()
{
    Span<byte> response = stackalloc byte[ReportLength];
    var ok = false;

    for (var i = 0; i < 5; ++i)
    {
        var respLength = SubcommandCheck(0x02, [], response, false);
        if (respLength >= 20)
        {
            // NES controllers share the hardware ID of the right joycon, but respond here differently.
            if (response[17] == 0x0A || response[17] == 0x09)
            {
                Type = ControllerType.NES;
            }

            ok = true;
            break;
        }
    }

    if (!ok)
    {
        throw new DeviceComFailedException("reset device info");
    }
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
Span<byte> response = stackalloc byte[ReportLength];

for (var i = 0; i < 5; ++i)
{
var respLength = SubcommandCheck(0x02, [], response, false);

if (respLength > 0)
{
//The NES controllers both share the hardware id of a normal right joycon
//To identify it, we need to query the hardware directly
//Right NES: 0x0A
//Left NES: 0x09
if (response[17] == 0x0A || response[17] == 0x09)
{
Type = ControllerType.NES;
}

return;
}
}

throw new DeviceComFailedException("reset device info");
}

private void SetLowPowerState(bool enable)
{
SubcommandCheck(0x08, [enable ? (byte)0x01 : (byte)0x00]);
Expand Down Expand Up @@ -2446,17 +2480,17 @@ private float CalculateRange(ushort range)

private bool CalibrationDataSupported()
{
return !IsSNES && !IsThirdParty;
return !IsSNES && !IsNES && !IsThirdParty;
}

private bool SticksSupported()
{
return !IsSNES;
return !IsSNES && !IsNES;
}

public bool IMUSupported()
{
return !IsSNES && !IsN64;
return !IsSNES && !IsN64 && !IsNES;
}

private bool UseGyroAnalogSliders()
Expand Down Expand Up @@ -2936,6 +2970,7 @@ private static OutputControllerXbox360InputState MapToXbox360Input(Joycon input)

var isPro = input.IsPro;
var isSNES = input.IsSNES;
var isNES = input.IsNES;
var isN64 = input.IsN64;
var isJoycon = input.IsJoycon;
var isLeft = input.IsLeft;
Expand Down Expand Up @@ -3115,6 +3150,7 @@ public static OutputControllerDualShock4InputState MapToDualShock4Input(Joycon i

var isPro = input.IsPro;
var isSNES = input.IsSNES;
var isNES = input.IsNES;
var isN64 = input.IsN64;
var isJoycon = input.IsJoycon;
var isLeft = input.IsLeft;
Expand Down Expand Up @@ -3300,6 +3336,7 @@ public static string GetControllerName(ControllerType type)
ControllerType.JoyconRight => "Right joycon",
ControllerType.Pro => "Pro controller",
ControllerType.SNES => "SNES controller",
ControllerType.NES => "NES controller",
ControllerType.N64 => "N64 controller",
_ => "Controller"
};
Expand Down
3 changes: 3 additions & 0 deletions BetterJoy/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1285,6 +1285,9 @@ private void SetControllerImage(Button button, Joycon.ControllerType controllerT
case Joycon.ControllerType.SNES:
temp = charging ? Resources.snes_charging : Resources.snes;
break;
case Joycon.ControllerType.NES:
temp = charging ? Resources.nes_charging : Resources.nes;
break;
case Joycon.ControllerType.N64:
temp = charging ? Resources.n64_charging : Resources.n64;
break;
Expand Down
3 changes: 3 additions & 0 deletions BetterJoy/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class JoyconManager
private const ushort ProductR = 0x2007;
private const ushort ProductPro = 0x2009;
private const ushort ProductSNES = 0x2017;
private const ushort ProductNES = 0x2007;
private const ushort ProductN64 = 0x2019;

public readonly bool EnableIMU = true;
Expand Down Expand Up @@ -699,6 +700,8 @@ private ushort TypeToProdId(byte type)
return ProductSNES;
case 5:
return ProductN64;
case 6:
return ProductNES;
}

return 0;
Expand Down
21 changes: 20 additions & 1 deletion BetterJoy/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions BetterJoy/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@
<data name="snes" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\snes.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="nes" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\nes.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="jc_left_s" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\jc_left_s.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down Expand Up @@ -160,6 +163,9 @@
<data name="snes_charging" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\snes_charging.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="nes_charging" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\nes_charging.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
<data name="n64" type="System.Resources.ResXFileRef, System.Windows.Forms">
<value>..\Icons\n64.png;System.Drawing.Bitmap, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a</value>
</data>
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Check out the [wiki](https://github.com/Davidobot/BetterJoy/wiki)! There, you'll

## USB Mode
* Plug the controller into your computer.

## Disconnecting \[Windows 10]
1. Go into "Bluetooth and other devices settings"
1. Under the first category "Mouse, keyboard, & pen", there should be the pro controller.
Expand Down Expand Up @@ -123,7 +123,7 @@ The built binaries are located under

*BetterJoy\bin\PLATFORM\CONFIGURATION*

where `PLATFORM` and `CONFIGURATION` are the one provided at build time.
where `PLATFORM` and `CONFIGURATION` are the one provided at build time.

# Acknowledgements
A massive thanks goes out to [rajkosto](https://github.com/rajkosto/) for putting up with 17 emails and replying very quickly to my silly queries. The UDP server is also mostly taken from his [ScpToolkit](https://github.com/rajkosto/ScpToolkit) repo.
Expand All @@ -137,4 +137,4 @@ A last thanks goes out to [dekuNukem](https://github.com/dekuNukem/Nintendo_Swit
Massive *thank you* to **all** code contributors!

Icons (modified): "[Switch Pro Controller](https://thenounproject.com/term/nintendo-switch/930119/)", "[
Switch Detachable Controller Left](https://thenounproject.com/remsing/uploads/?i=930115)", "[Switch Detachable Controller Right](https://thenounproject.com/remsing/uploads/?i=930121)" icons by Chad Remsing from [the Noun Project](https://thenounproject.com/). [Super Nintendo Controller](https://thenounproject.com/themizarkshow/collection/vectogram/?i=193592) icon by Mark Davis from the [the Noun Project](https://thenounproject.com/); icon modified by [Amy Alexander](https://www.linkedin.com/in/-amy-alexander/). [Switch Nintendo 64 Controller](https://thenounproject.com/icon/game-controller-193588/) icon by Mark Davis from the [the Noun Project](https://thenounproject.com/); icon modified by [d3xMachina](https://www.github.com/d3xMachina).
Switch Detachable Controller Left](https://thenounproject.com/remsing/uploads/?i=930115)", "[Switch Detachable Controller Right](https://thenounproject.com/remsing/uploads/?i=930121)" icons by Chad Remsing from [the Noun Project](https://thenounproject.com/). [Super Nintendo Controller](https://thenounproject.com/themizarkshow/collection/vectogram/?i=193592) icon by Mark Davis from the [the Noun Project](https://thenounproject.com/); icon modified by [Amy Alexander](https://www.linkedin.com/in/-amy-alexander/). [Switch Nintendo 64 Controller](https://thenounproject.com/icon/game-controller-193588/) icon by Mark Davis from the [the Noun Project](https://thenounproject.com/); icon modified by [d3xMachina](https://www.github.com/d3xMachina). [NES Controller](https://thenounproject.com/icon/nes-gamepad-949405/) icon by Viktor Korobkov from [the Noun Project](http://thenounproject.com/); icon modified by anon.