Skip to content

Commit

Permalink
Scale XY coordinates according to new StickScalingFactor configuratio…
Browse files Browse the repository at this point in the history
…n option. Closes Davidobot#630 (Davidobot#954)
  • Loading branch information
fabiosantoscode authored Aug 4, 2022
1 parent 5e3d6ac commit 461f5f8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions BetterJoyForCemu/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
<add key="stick2_cal" value="0x780,0x780,0x780,0x830,0x780,0x780"/>
<add key="deadzone2" value="200"/>

<!-- Scales the xy coordinates each of the joysticks report. Increase this if you can't push the stick to its maximum. -->
<add key="StickScalingFactor" value="1.00" />
<add key="StickScalingFactor2" value="1.00" />

<!-- Allows use of gyroscope tilting to get full control of the slider values (big triggers)-->
<!-- Works on pro controller and joined joycons (pro controller case - triggers combined, joycons case - separate tilt)-->
<!-- Default: false -->
Expand Down
18 changes: 15 additions & 3 deletions BetterJoyForCemu/Joycon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,9 @@ private void Poll() {

bool swapAB = Boolean.Parse(ConfigurationManager.AppSettings["SwapAB"]);
bool swapXY = Boolean.Parse(ConfigurationManager.AppSettings["SwapXY"]);
float stickScalingFactor = float.Parse(ConfigurationManager.AppSettings["StickScalingFactor"]);
float stickScalingFactor2 = float.Parse(ConfigurationManager.AppSettings["StickScalingFactor2"]);

private int ProcessButtonsAndStick(byte[] report_buf) {
if (report_buf[0] == 0x00) throw new ArgumentException("received undefined report. This is probably a bug");
if (!isSnes) {
Expand All @@ -906,12 +909,12 @@ private int ProcessButtonsAndStick(byte[] report_buf) {

stick_precal[0] = (UInt16)(stick_raw[0] | ((stick_raw[1] & 0xf) << 8));
stick_precal[1] = (UInt16)((stick_raw[1] >> 4) | (stick_raw[2] << 4));
stick = CenterSticks(stick_precal, stick_cal, deadzone);
stick = CenterSticks(stick_precal, stick_cal, deadzone, isLeft ? stickScalingFactor : stickScalingFactor2);

if (isPro) {
stick2_precal[0] = (UInt16)(stick2_raw[0] | ((stick2_raw[1] & 0xf) << 8));
stick2_precal[1] = (UInt16)((stick2_raw[1] >> 4) | (stick2_raw[2] << 4));
stick2 = CenterSticks(stick2_precal, stick2_cal, deadzone2);
stick2 = CenterSticks(stick2_precal, stick2_cal, deadzone2, stickScalingFactor2);
}

// Read other Joycon's sticks
Expand Down Expand Up @@ -1108,7 +1111,7 @@ public void Begin() {
}

// Should really be called calculating stick data
private float[] CenterSticks(UInt16[] vals, ushort[] cal, ushort dz) {
private float[] CenterSticks(UInt16[] vals, ushort[] cal, ushort dz, float scaling_factor) {
ushort[] t = cal;

float[] s = { 0, 0 };
Expand All @@ -1118,6 +1121,15 @@ private float[] CenterSticks(UInt16[] vals, ushort[] cal, ushort dz) {

s[0] = dx / (dx > 0 ? t[0] : t[4]);
s[1] = dy / (dy > 0 ? t[1] : t[5]);

if (scaling_factor != 1.0f) {
s[0] *= scaling_factor;
s[1] *= scaling_factor;

s[0] = Math.Max(Math.Min(s[0], 1.0f), -1.0f);
s[1] = Math.Max(Math.Min(s[1], 1.0f), -1.0f);
}

return s;
}

Expand Down

0 comments on commit 461f5f8

Please sign in to comment.