From 45a241dcf6237ab7be4af3dc3aeafba514146e37 Mon Sep 17 00:00:00 2001 From: xqdoo00o Date: Sat, 27 Apr 2019 20:41:50 +0800 Subject: [PATCH 1/2] feat for non-original controller --- BetterJoyForCemu/Config.cs | 53 ++++++++-- BetterJoyForCemu/Joycon.cs | 129 +++++++++++++++++------ BetterJoyForCemu/MainForm.Designer.cs | 87 +++++++++------- BetterJoyForCemu/MainForm.cs | 142 +++++++++++++++++++++++++- BetterJoyForCemu/Program.cs | 11 +- 5 files changed, 348 insertions(+), 74 deletions(-) diff --git a/BetterJoyForCemu/Config.cs b/BetterJoyForCemu/Config.cs index 7bb13af..d4db3f5 100644 --- a/BetterJoyForCemu/Config.cs +++ b/BetterJoyForCemu/Config.cs @@ -10,24 +10,48 @@ namespace BetterJoyForCemu { const string PATH = "settings"; static Dictionary variables = new Dictionary(); - public static void Init() { + public static void Init(List> caliData) { variables["ProgressiveScan"] = true; variables["StartInTray"] = false; if (File.Exists(PATH)) { using (StreamReader file = new StreamReader(PATH)) { string line = String.Empty; + int lineNO = 0; while ((line = file.ReadLine()) != null) { string[] vs = line.Split(); try { - variables[vs[0]] = Boolean.Parse(vs[1]); + if (lineNO < 2){ + variables[vs[0]] = Boolean.Parse(vs[1]); + } else { + caliData.Clear(); + for (int i = 0; i < vs.Length; i++){ + string[] caliArr = vs[i].Split(','); + float[] newArr = new float[6]; + for (int j = 1; j < caliArr.Length; j++){ + newArr[j-1] = float.Parse(caliArr[j]); + } + caliData.Add(new KeyValuePair( + caliArr[0], + newArr + )); + } + } } catch { } + lineNO++; } } } else { using (StreamWriter file = new StreamWriter(PATH)) { foreach (string k in variables.Keys) file.WriteLine(String.Format("{0} {1}", k, variables[k])); + string caliStr = ""; + for(int i = 0; i < caliData.Count; i++){ + string space = " "; + if (i == 0) space = ""; + caliStr += space + caliData[i].Key + "," + String.Join(",", caliData[i].Value); + } + file.WriteLine(caliStr); } } } @@ -39,13 +63,28 @@ namespace BetterJoyForCemu { return variables[key]; } + public static void SaveCaliData(List> caliData){ + string[] txt = File.ReadAllLines(PATH); + string caliStr = ""; + for (int i = 0; i < caliData.Count; i++){ + string space = " "; + if (i == 0) space = ""; + caliStr += space + caliData[i].Key + "," + String.Join(",", caliData[i].Value); + } + txt[2] = caliStr; + File.WriteAllLines(PATH, txt); + } + public static void Save(string key, bool value) { variables[key] = value; - - using (StreamWriter file = new StreamWriter(PATH, false)) { - foreach (string k in variables.Keys) - file.WriteLine(String.Format("{0} {1}", k, variables[k])); + string[] txt = File.ReadAllLines(PATH); + int NO = 0; + foreach (string k in variables.Keys) + { + txt[NO] = String.Format("{0} {1}", k, variables[k]); + NO++; } + File.WriteAllLines(PATH, txt); } } -} +} \ No newline at end of file diff --git a/BetterJoyForCemu/Joycon.cs b/BetterJoyForCemu/Joycon.cs index bf763c8..4735aa9 100644 --- a/BetterJoyForCemu/Joycon.cs +++ b/BetterJoyForCemu/Joycon.cs @@ -102,6 +102,17 @@ namespace BetterJoyForCemu { private Int16[] gyr_sensiti = { 0, 0, 0 }; private Vector3 gyr_g; + private short[] acc_sen = new short[3]{ + 16384, + 16384, + 16384 + }; + private short[] gyr_sen = new short[3]{ + 18642, + 18642, + 18642 + }; + private Int16[] pro_hor_offset = { -710, 0, 0 }; private Int16[] left_hor_offset = { 0, 0, 0 }; private Int16[] right_hor_offset = { 0, 0, 0 }; @@ -219,7 +230,13 @@ namespace BetterJoyForCemu { public byte LED = 0x0; - public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, string path, int id = 0, bool isPro=false, bool usb = false) { + public string serial_number; + + private float[] activeData; + + public Joycon(IntPtr handle_, bool imu, bool localize, float alpha, bool left, string path, string serialNum, int id = 0, bool isPro=false) { + serial_number = serialNum; + activeData = new float[6]; handle = handle_; imu_enabled = imu; do_localize = localize; @@ -230,7 +247,7 @@ namespace BetterJoyForCemu { PadId = id; LED = (byte)(0x1 << PadId); this.isPro = isPro; - isUSB = usb; + isUSB = serialNum == "000000000001"; this.path = path; @@ -245,6 +262,11 @@ namespace BetterJoyForCemu { } } + public void getActiveData() + { + this.activeData = form.activeCaliData(serial_number); + } + public void ReceiveRumble(object sender, Nefarius.ViGEm.Client.Targets.Xbox360.Xbox360FeedbackReceivedEventArgs e) { SetRumble(lowFreq, highFreq, (float) e.LargeMotor / (float) 255, rumblePeriod); @@ -520,13 +542,21 @@ namespace BetterJoyForCemu { 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); + ushort[] cal = form.nonOriginal ? new ushort[6]{ + 2048, + 2048, + 2048, + 2048, + 2048, + 2048 + }: stick_cal; + ushort dz = form.nonOriginal ? (ushort)200 : deadzone; + stick = CenterSticks(stick_precal, cal, dz); 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, form.nonOriginal ? cal : stick2_cal, deadzone2); } // Read other Joycon's sticks @@ -677,33 +707,74 @@ namespace BetterJoyForCemu { acc_r[1] = (Int16)(report_buf[15 + n * 12] | ((report_buf[16 + n * 12] << 8) & 0xff00)); acc_r[2] = (Int16)(report_buf[17 + n * 12] | ((report_buf[18 + n * 12] << 8) & 0xff00)); - Int16[] offset; - if (isPro) - offset = pro_hor_offset; - else if (isLeft) - offset = left_hor_offset; - else - offset = right_hor_offset; - for (int i = 0; i < 3; ++i) { - switch (i) { - case 0: - acc_g.X = (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; - gyr_g.X = (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); + if (form.nonOriginal) + { + for (int i = 0; i < 3; ++i) + { + switch (i) + { + case 0: + acc_g.X = (acc_r[i] - activeData[3]) * (1.0f / acc_sen[i]) * 4.0f; + gyr_g.X = (gyr_r[i] - activeData[0]) * (816.0f / gyr_sen[i]); + if (form.calibrate) + { + form.xA.Add(acc_r[i]); + form.xG.Add(gyr_r[i]); + } + break; + case 1: + acc_g.Y = (!isLeft ? -1 : 1) * (acc_r[i] - activeData[4]) * (1.0f / acc_sen[i]) * 4.0f; + gyr_g.Y = -(!isLeft ? -1 : 1) * (gyr_r[i] - activeData[1]) * (816.0f / gyr_sen[i]); + if (form.calibrate) + { + form.yA.Add(acc_r[i]); + form.yG.Add(gyr_r[i]); + } + break; + case 2: + acc_g.Z = (!isLeft ? -1 : 1) * (acc_r[i] - activeData[5]) * (1.0f / acc_sen[i]) * 4.0f; + gyr_g.Z = -(!isLeft ? -1 : 1) * (gyr_r[i] - activeData[2]) * (816.0f / gyr_sen[i]); + if (form.calibrate) + { + form.zA.Add(acc_r[i]); + form.zG.Add(gyr_r[i]); + } + break; + } + } + } + else + { + Int16[] offset; + if (isPro) + offset = pro_hor_offset; + else if (isLeft) + offset = left_hor_offset; + else + offset = right_hor_offset; - break; - case 1: - acc_g.Y = (!isLeft ? -1 : 1) * (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; - gyr_g.Y = -(!isLeft ? -1 : 1) * (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); + for (int i = 0; i < 3; ++i) + { + switch (i) + { + case 0: + acc_g.X = (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; + gyr_g.X = (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); - break; - case 2: - acc_g.Z = (!isLeft ? -1 : 1) * (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; - gyr_g.Z = -(!isLeft ? -1 : 1) * (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); - - break; - } - } + break; + case 1: + acc_g.Y = (!isLeft ? -1 : 1) * (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; + gyr_g.Y = -(!isLeft ? -1 : 1) * (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); + break; + case 2: + acc_g.Z = (!isLeft ? -1 : 1) * (acc_r[i] - offset[i]) * (1.0f / (acc_sensiti[i] - acc_neutral[i])) * 4.0f; + gyr_g.Z = -(!isLeft ? -1 : 1) * (gyr_r[i] - gyr_neutral[i]) * (816.0f / (gyr_sensiti[i] - gyr_neutral[i])); + break; + } + } + } + if (other == null && !isPro) { // single joycon mode; Z do not swap, rest do if (isLeft) { diff --git a/BetterJoyForCemu/MainForm.Designer.cs b/BetterJoyForCemu/MainForm.Designer.cs index 48dc270..bbd8c2b 100644 --- a/BetterJoyForCemu/MainForm.Designer.cs +++ b/BetterJoyForCemu/MainForm.Designer.cs @@ -50,6 +50,7 @@ this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel(); this.rightPanel = new System.Windows.Forms.Panel(); this.button1 = new System.Windows.Forms.Button(); + this.AutoCalibrate = new System.Windows.Forms.Button(); this.contextMenu.SuspendLayout(); this.conCntrls.SuspendLayout(); this.groupBox1.SuspendLayout(); @@ -58,19 +59,19 @@ // // console // - this.console.Location = new System.Drawing.Point(12, 132); + this.console.Location = new System.Drawing.Point(12, 122); this.console.Multiline = true; this.console.Name = "console"; this.console.ReadOnly = true; - this.console.Size = new System.Drawing.Size(262, 100); + this.console.Size = new System.Drawing.Size(262, 93); this.console.TabIndex = 2; // // label1 // this.label1.AutoSize = true; - this.label1.Location = new System.Drawing.Point(9, 116); + this.label1.Location = new System.Drawing.Point(9, 107); this.label1.Name = "label1"; - this.label1.Size = new System.Drawing.Size(80, 13); + this.label1.Size = new System.Drawing.Size(89, 12); this.label1.TabIndex = 1; this.label1.Text = "Console Output"; this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter; @@ -91,21 +92,21 @@ this.contextMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.exitToolStripMenuItem}); this.contextMenu.Name = "contextMenu"; - this.contextMenu.Size = new System.Drawing.Size(93, 26); + this.contextMenu.Size = new System.Drawing.Size(97, 26); // // exitToolStripMenuItem // this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; - this.exitToolStripMenuItem.Size = new System.Drawing.Size(92, 22); + this.exitToolStripMenuItem.Size = new System.Drawing.Size(96, 22); this.exitToolStripMenuItem.Text = "Exit"; this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // version_lbl // this.version_lbl.AutoSize = true; - this.version_lbl.Location = new System.Drawing.Point(246, 239); + this.version_lbl.Location = new System.Drawing.Point(246, 221); this.version_lbl.Name = "version_lbl"; - this.version_lbl.Size = new System.Drawing.Size(28, 13); + this.version_lbl.Size = new System.Drawing.Size(29, 12); this.version_lbl.TabIndex = 2; this.version_lbl.Text = "v6.0"; // @@ -114,10 +115,10 @@ this.passiveScanBox.AutoSize = true; this.passiveScanBox.Checked = true; this.passiveScanBox.CheckState = System.Windows.Forms.CheckState.Checked; - this.passiveScanBox.Location = new System.Drawing.Point(12, 238); + this.passiveScanBox.Location = new System.Drawing.Point(12, 220); this.passiveScanBox.Name = "passiveScanBox"; this.passiveScanBox.RightToLeft = System.Windows.Forms.RightToLeft.Yes; - this.passiveScanBox.Size = new System.Drawing.Size(91, 17); + this.passiveScanBox.Size = new System.Drawing.Size(96, 16); this.passiveScanBox.TabIndex = 4; this.passiveScanBox.Text = "Passive Scan"; this.passiveScanBox.UseVisualStyleBackColor = true; @@ -126,9 +127,9 @@ // linkLabel1 // this.linkLabel1.AutoSize = true; - this.linkLabel1.Location = new System.Drawing.Point(196, 239); + this.linkLabel1.Location = new System.Drawing.Point(196, 221); this.linkLabel1.Name = "linkLabel1"; - this.linkLabel1.Size = new System.Drawing.Size(42, 13); + this.linkLabel1.Size = new System.Drawing.Size(41, 12); this.linkLabel1.TabIndex = 5; this.linkLabel1.TabStop = true; this.linkLabel1.Text = "Donate"; @@ -144,45 +145,45 @@ this.conCntrls.Controls.Add(this.con3); this.conCntrls.Controls.Add(this.con2); this.conCntrls.Controls.Add(this.con1); - this.conCntrls.Location = new System.Drawing.Point(12, 12); + this.conCntrls.Location = new System.Drawing.Point(12, 11); this.conCntrls.Name = "conCntrls"; - this.conCntrls.Size = new System.Drawing.Size(262, 100); + this.conCntrls.Size = new System.Drawing.Size(262, 92); this.conCntrls.TabIndex = 0; this.conCntrls.TabStop = false; this.conCntrls.Text = "Connected Controllers"; // // loc4 // - this.loc4.Location = new System.Drawing.Point(198, 80); + this.loc4.Location = new System.Drawing.Point(198, 74); this.loc4.Name = "loc4"; - this.loc4.Size = new System.Drawing.Size(58, 20); + this.loc4.Size = new System.Drawing.Size(58, 18); this.loc4.TabIndex = 7; this.loc4.Text = "Locate"; this.loc4.UseVisualStyleBackColor = true; // // loc3 // - this.loc3.Location = new System.Drawing.Point(134, 80); + this.loc3.Location = new System.Drawing.Point(134, 74); this.loc3.Name = "loc3"; - this.loc3.Size = new System.Drawing.Size(58, 20); + this.loc3.Size = new System.Drawing.Size(58, 18); this.loc3.TabIndex = 6; this.loc3.Text = "Locate"; this.loc3.UseVisualStyleBackColor = true; // // loc2 // - this.loc2.Location = new System.Drawing.Point(70, 80); + this.loc2.Location = new System.Drawing.Point(70, 74); this.loc2.Name = "loc2"; - this.loc2.Size = new System.Drawing.Size(58, 20); + this.loc2.Size = new System.Drawing.Size(58, 18); this.loc2.TabIndex = 5; this.loc2.Text = "Locate"; this.loc2.UseVisualStyleBackColor = true; // // loc1 // - this.loc1.Location = new System.Drawing.Point(6, 80); + this.loc1.Location = new System.Drawing.Point(6, 74); this.loc1.Name = "loc1"; - this.loc1.Size = new System.Drawing.Size(58, 20); + this.loc1.Size = new System.Drawing.Size(58, 18); this.loc1.TabIndex = 4; this.loc1.Text = "Locate"; this.loc1.UseVisualStyleBackColor = true; @@ -192,9 +193,9 @@ this.con4.BackgroundImage = global::BetterJoyForCemu.Properties.Resources.cross; this.con4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.con4.Enabled = false; - this.con4.Location = new System.Drawing.Point(198, 19); + this.con4.Location = new System.Drawing.Point(198, 18); this.con4.Name = "con4"; - this.con4.Size = new System.Drawing.Size(58, 58); + this.con4.Size = new System.Drawing.Size(58, 54); this.con4.TabIndex = 3; this.con4.TabStop = false; this.con4.UseVisualStyleBackColor = true; @@ -204,9 +205,9 @@ this.con3.BackgroundImage = global::BetterJoyForCemu.Properties.Resources.cross; this.con3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.con3.Enabled = false; - this.con3.Location = new System.Drawing.Point(134, 19); + this.con3.Location = new System.Drawing.Point(134, 18); this.con3.Name = "con3"; - this.con3.Size = new System.Drawing.Size(58, 58); + this.con3.Size = new System.Drawing.Size(58, 54); this.con3.TabIndex = 2; this.con3.TabStop = false; this.con3.UseVisualStyleBackColor = true; @@ -216,9 +217,9 @@ this.con2.BackgroundImage = global::BetterJoyForCemu.Properties.Resources.cross; this.con2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.con2.Enabled = false; - this.con2.Location = new System.Drawing.Point(70, 19); + this.con2.Location = new System.Drawing.Point(70, 18); this.con2.Name = "con2"; - this.con2.Size = new System.Drawing.Size(58, 58); + this.con2.Size = new System.Drawing.Size(58, 54); this.con2.TabIndex = 1; this.con2.TabStop = false; this.con2.UseVisualStyleBackColor = true; @@ -228,9 +229,9 @@ this.con1.BackgroundImage = global::BetterJoyForCemu.Properties.Resources.cross; this.con1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch; this.con1.Enabled = false; - this.con1.Location = new System.Drawing.Point(6, 19); + this.con1.Location = new System.Drawing.Point(6, 18); this.con1.Name = "con1"; - this.con1.Size = new System.Drawing.Size(58, 58); + this.con1.Size = new System.Drawing.Size(58, 54); this.con1.TabIndex = 0; this.con1.TabStop = false; this.btnTip.SetToolTip(this.con1, "Click on Joycons to join/split them"); @@ -251,10 +252,10 @@ // startInTrayBox // this.startInTrayBox.AutoSize = true; - this.startInTrayBox.Location = new System.Drawing.Point(107, 238); + this.startInTrayBox.Location = new System.Drawing.Point(107, 220); this.startInTrayBox.Name = "startInTrayBox"; this.startInTrayBox.RightToLeft = System.Windows.Forms.RightToLeft.Yes; - this.startInTrayBox.Size = new System.Drawing.Size(83, 17); + this.startInTrayBox.Size = new System.Drawing.Size(102, 16); this.startInTrayBox.TabIndex = 6; this.startInTrayBox.Text = "Start in Tray"; this.startInTrayBox.UseVisualStyleBackColor = true; @@ -262,9 +263,9 @@ // // btn_open3rdP // - this.btn_open3rdP.Location = new System.Drawing.Point(188, 112); + this.btn_open3rdP.Location = new System.Drawing.Point(188, 103); this.btn_open3rdP.Name = "btn_open3rdP"; - this.btn_open3rdP.Size = new System.Drawing.Size(86, 20); + this.btn_open3rdP.Size = new System.Drawing.Size(86, 18); this.btn_open3rdP.TabIndex = 7; this.btn_open3rdP.Text = "Add Controllers"; this.btn_open3rdP.UseVisualStyleBackColor = true; @@ -321,16 +322,31 @@ this.button1.Text = "Apply"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); + // AutoCalibrate + // + this.AutoCalibrate.Location = new System.Drawing.Point(111, 103); + this.AutoCalibrate.Name = "AutoCalibrate"; + this.AutoCalibrate.Size = new System.Drawing.Size(71, 18); + this.AutoCalibrate.TabIndex = 8; + this.AutoCalibrate.Text = "Calibrate"; + this.AutoCalibrate.UseVisualStyleBackColor = true; + this.AutoCalibrate.Click += new System.EventHandler(this.StartCalibrate); + if (!this.nonOriginal) + { + this.AutoCalibrate.Hide(); + } // // MainForm // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.ClientSize = new System.Drawing.Size(573, 261); this.Controls.Add(this.foldLbl); this.Controls.Add(this.rightPanel); + this.ClientSize = new System.Drawing.Size(284, 241); + this.Controls.Add(this.AutoCalibrate); this.Controls.Add(this.btn_open3rdP); this.Controls.Add(this.startInTrayBox); this.Controls.Add(this.conCntrls); @@ -383,5 +399,6 @@ private System.Windows.Forms.Panel rightPanel; private System.Windows.Forms.Button button1; private System.Windows.Forms.Label foldLbl; + private System.Windows.Forms.Button AutoCalibrate; } } \ No newline at end of file diff --git a/BetterJoyForCemu/MainForm.cs b/BetterJoyForCemu/MainForm.cs index b7bfc1e..22473e7 100644 --- a/BetterJoyForCemu/MainForm.cs +++ b/BetterJoyForCemu/MainForm.cs @@ -15,9 +15,29 @@ using System.Xml.Linq; namespace BetterJoyForCemu { public partial class MainForm : Form { + public bool nonOriginal = Boolean.Parse(ConfigurationManager.AppSettings["NonOriginalController"]); public List