Skip to content

Commit

Permalink
implement vibration strength
Browse files Browse the repository at this point in the history
  • Loading branch information
Valkirie committed Nov 26, 2021
1 parent ed6d23f commit b866e44
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 12 deletions.
36 changes: 35 additions & 1 deletion ControllerHelper/ControllerHelper.Designer.cs

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

18 changes: 18 additions & 0 deletions ControllerHelper/ControllerHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ public void UpdateSettings(Dictionary<string, string> args)
cB_gyro.Checked = bool.Parse(args["gyrometer"]);
cB_accelero.Checked = bool.Parse(args["accelerometer"]);
tB_PullRate.Value = int.Parse(args["HIDrate"]);
tB_VibrationStr.Value = int.Parse(args["HIDstrength"]);
cB_UDPEnable.Checked = bool.Parse(args["DSUEnabled"]);
tB_UDPIP.Text = args["DSUip"];
tB_UDPPort.Value = int.Parse(args["DSUport"]);
Expand Down Expand Up @@ -354,6 +355,23 @@ private void tB_PullRate_Scroll(object sender, EventArgs e)
});
}

private void tB_VibrationStr_Scroll(object sender, EventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate ()
{
toolTip1.SetToolTip(tB_VibrationStr, $"{tB_VibrationStr.Value}%");
});

PipeClient.SendMessage(new PipeMessage
{
Code = PipeCode.CLIENT_SETTINGS,
args = new Dictionary<string, string>
{
{ "HIDstrength", $"{tB_VibrationStr.Value}" }
}
});
}

private void b_UDPApply_Click(object sender, EventArgs e)
{
PipeClient.SendMessage(new PipeMessage
Expand Down
7 changes: 6 additions & 1 deletion ControllerService/ControllerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class ControllerService : IHostedService

private string DSUip, HIDmode;
private bool HIDcloaked, HIDuncloakonclose, DSUEnabled;
private int DSUport, HIDrate;
private int DSUport, HIDrate, HIDstrength;

private readonly ILogger<ControllerService> logger;

Expand All @@ -59,6 +59,7 @@ public ControllerService(ILogger<ControllerService> logger)
DSUip = Properties.Settings.Default.DSUip;
DSUport = Properties.Settings.Default.DSUport;
HIDrate = Properties.Settings.Default.HIDrate;
HIDstrength = Properties.Settings.Default.HIDstrength;

// initialize log
logger.LogInformation("{0} ({1})", CurrentAssembly.GetName(), fileVersionInfo.ProductVersion);
Expand Down Expand Up @@ -236,6 +237,9 @@ private void ApplySetting(string name, object prev_value, object value)
case "HIDrate":
PhysicalController.SetPollRate((int)value);
break;
case "HIDstrength":
PhysicalController.SetVibrationStrength((int)value);
break;
case "DSUEnabled":
switch((bool)value)
{
Expand Down Expand Up @@ -269,6 +273,7 @@ public Task StartAsync(CancellationToken cancellationToken)
PhysicalController.SetVirtualController(VirtualController);
PhysicalController.SetGyroscope(Gyrometer);
PhysicalController.SetAccelerometer(Accelerometer);
PhysicalController.SetVibrationStrength(HIDstrength);

// start the Pipe Server
PipeServer.Start();
Expand Down
12 changes: 12 additions & 0 deletions ControllerService/Properties/Settings.Designer.cs

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

3 changes: 3 additions & 0 deletions ControllerService/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@
<Setting Name="HIDuncloakonclose" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="HIDstrength" Type="System.Int32" Scope="User">
<Value Profile="(Default)">100</Value>
</Setting>
</Settings>
</SettingsFile>
23 changes: 13 additions & 10 deletions ControllerService/XInputController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public struct XInputStateSecret
private static extern int XInputGetStateSecret14(int playerIndex, out XInputStateSecret struc);
#endregion

private const float F_ACC_RES_PER_G = 8192.0f;
private const float F_GYRO_RES_IN_DEG_SEC = 16.0f;

public Controller controller;
public Gamepad gamepad;
public XInputStateSecret state_s;
Expand All @@ -52,20 +55,14 @@ public struct XInputStateSecret
public Vector3 Acceleration;

private Timer UpdateTimer;
private float strength;

public UserIndex index;
public bool muted;

public long microseconds;
private Stopwatch stopwatch;

private byte FrameCounter = 0; // always 0 on USB

private const int ACC_RES_PER_G = 8192;
private const float F_ACC_RES_PER_G = ACC_RES_PER_G;
private const int GYRO_RES_IN_DEG_SEC = 16;
private const float F_GYRO_RES_IN_DEG_SEC = GYRO_RES_IN_DEG_SEC;

private object updateLock = new();

private DS4_REPORT_EX outDS4Report;
Expand Down Expand Up @@ -107,6 +104,12 @@ public void SetPollRate(int HIDrate)
logger.LogInformation("Virtual {0} report interval set to {1}ms", vcontroller.GetType().Name, UpdateTimer.Interval);
}

public void SetVibrationStrength(float strength)
{
this.strength = strength / 100.0f;
logger.LogInformation("Virtual {0} vibration strength set to {1}%", vcontroller.GetType().Name, strength);
}

public Dictionary<string, string> ToArgs()
{
return new Dictionary<string, string>() {
Expand Down Expand Up @@ -146,8 +149,8 @@ private void DS4_FeedbackReceived(object sender, DualShock4FeedbackReceivedEvent
{
Vibration inputMotor = new Vibration()
{
LeftMotorSpeed = (ushort)(e.LargeMotor * ushort.MaxValue / byte.MaxValue),
RightMotorSpeed = (ushort)(e.SmallMotor * ushort.MaxValue / byte.MaxValue),
LeftMotorSpeed = (ushort)((e.LargeMotor * ushort.MaxValue / byte.MaxValue) * strength),
RightMotorSpeed = (ushort)((e.SmallMotor * ushort.MaxValue / byte.MaxValue) * strength),
};
controller.SetVibration(inputMotor);
}
Expand Down Expand Up @@ -271,7 +274,7 @@ private unsafe void DS4_UpdateReport(object sender, ElapsedEventArgs e)
if (touch.OutputClickButton)
tempSpecial |= DualShock4SpecialButton.Touchpad.Value;

outDS4Report.bSpecial = (byte)(tempSpecial | (FrameCounter << 2));
outDS4Report.bSpecial = (byte)(tempSpecial | (0 << 2));
}

if (!muted)
Expand Down

0 comments on commit b866e44

Please sign in to comment.