- Implement gyro-to-joystick; still needs testing on joycons

This commit is contained in:
David Khachaturov 2021-04-29 16:22:55 +01:00
parent 4423bcf881
commit d0709ac6ff
2 changed files with 39 additions and 19 deletions

View file

@ -82,15 +82,25 @@
<add key="UseIncrementalLights" value="true" />
<!-- Determines whether or not to translate gyro movements into joystick ("joy") or mouse movement ("mouse"), or have no effect ("none") -->
<!-- When "joy", turn gyro movements into joystick movement (left/right depends on setting) [not yet implemented]-->
<!-- When "joy_left" or "joy_right", turn gyro movements into respective left/right joystick (mouse-like) movements -->
<!-- When "mouse", turn gyro movements into mouse movement. Press either stick-button to reset to middle of primary display -->
<!-- Default: none -->
<add key="GyroToJoyOrMouse" value="none"/>
<!-- Beta value of AHRS. Affects divergence of filter -->
<!-- Default: 0.05 -->
<add key="AHRS_beta" value="0.05"/>
<!-- Sensitivity of gyro-to-mouse movements -->
<!-- Default: 1200; 800 -->
<add key="GyroMouseSensitivityX" value="1200"/>
<add key="GyroMouseSensitivityY" value="800"/>
<!-- Gyro Hold/Toggle activation -->
<!-- Sensitivity of gyro-to-joystick movements -->
<!-- Default: 40.0; 10.0 -->
<add key="GyroStickSensitivityX" value="40.0"/>
<add key="GyroStickSensitivityY" value="10.0"/>
<!-- Stick range reduction when gyro-to-joystick is enabled and active; divides range by factor (so 1 is no change; 1.5 is halved range (with deadzone in mind)) -->
<!-- Default: 1.5 -->
<add key="GyroStickReduction" value="1.5"/>
<!-- Gyro Hold/Toggle activation; true will require the mapped button to be continuously held down to keep gyro active -->
<!-- Default: true [hold], false [toggle] -->
<add key="GyroHoldToggle" value="true"/>
<!-- When two joycons are connected, it would take the gyro movement of the right joycon for mouse movement. This swaps that -->

View file

@ -279,7 +279,8 @@ namespace BetterJoyForCemu {
bool thirdParty = false;
private float[] activeData;
private MadgwickAHRS AHRS = new MadgwickAHRS(0.005f, 0.01f); // for getting filtered Euler angles of rotation; 5ms sampling rate
static float AHRS_beta = float.Parse(ConfigurationManager.AppSettings["AHRS_beta"]);
private MadgwickAHRS AHRS = new MadgwickAHRS(0.005f, AHRS_beta); // for getting filtered Euler angles of rotation; 5ms sampling rate
public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, string path, string serialNum, int id = 0, bool isPro = false, bool isSnes = false, bool thirdParty = false) {
serial_number = serialNum;
@ -688,6 +689,9 @@ namespace BetterJoyForCemu {
string extraGyroFeature = ConfigurationManager.AppSettings["GyroToJoyOrMouse"];
int GyroMouseSensitivityX = Int32.Parse(ConfigurationManager.AppSettings["GyroMouseSensitivityX"]);
int GyroMouseSensitivityY = Int32.Parse(ConfigurationManager.AppSettings["GyroMouseSensitivityY"]);
float GyroStickSensitivityX = float.Parse(ConfigurationManager.AppSettings["GyroStickSensitivityX"]);
float GyroStickSensitivityY = float.Parse(ConfigurationManager.AppSettings["GyroStickSensitivityY"]);
float GyroStickReduction = float.Parse(ConfigurationManager.AppSettings["GyroStickReduction"]);
bool GyroHoldToggle = Boolean.Parse(ConfigurationManager.AppSettings["GyroHoldToggle"]);
bool GyroAnalogSliders = Boolean.Parse(ConfigurationManager.AppSettings["GyroAnalogSliders"]);
int GyroAnalogSensitivity = Int32.Parse(ConfigurationManager.AppSettings["GyroAnalogSensitivity"]);
@ -787,11 +791,7 @@ namespace BetterJoyForCemu {
}
}
if (extraGyroFeature == "joy") {
// TODO
} else if (extraGyroFeature == "mouse" && (isPro || (other == null) || (other != null && (Boolean.Parse(ConfigurationManager.AppSettings["GyroMouseLeftHanded"]) ? isLeft : !isLeft)))) {
string res_val = Config.Value("active_gyro");
if (res_val.StartsWith("joy_")) {
int i = Int32.Parse(res_val.Substring(4));
if (GyroHoldToggle) {
@ -805,6 +805,16 @@ namespace BetterJoyForCemu {
}
}
if (extraGyroFeature.Substring(0, 3) == "joy") {
if (Config.Value("active_gyro") == "0" || active_gyro) {
float[] control_stick = (extraGyroFeature == "joy_left") ? stick : stick2;
float dx = (GyroStickSensitivityX * (cur_rotation[1] - cur_rotation[4])); // yaw
float dy = -(GyroStickSensitivityY * (cur_rotation[0] - cur_rotation[3])); // pitch
control_stick[0] = Math.Max(-1.0f, Math.Min(1.0f, control_stick[0] / GyroStickReduction + dx));
control_stick[1] = Math.Max(-1.0f, Math.Min(1.0f, control_stick[1] / GyroStickReduction + dy));
}
} else if (extraGyroFeature == "mouse" && (isPro || (other == null) || (other != null && (Boolean.Parse(ConfigurationManager.AppSettings["GyroMouseLeftHanded"]) ? isLeft : !isLeft)))) {
// gyro data is in degrees/s
if (Config.Value("active_gyro") == "0" || active_gyro) {
int dx = (int)(GyroMouseSensitivityX * (cur_rotation[1] - cur_rotation[4])); // yaw