From 8717f9d7167c887aa0ab1efc267ff9300fea00ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:46:49 +0200 Subject: [PATCH] refactor: only printing if previous or next slide is pressed --- JoyConRead.cs | 53 ++++++++++++++++++++++----------------------------- 1 file changed, 23 insertions(+), 30 deletions(-) diff --git a/JoyConRead.cs b/JoyConRead.cs index f94fbee..66309b1 100644 --- a/JoyConRead.cs +++ b/JoyConRead.cs @@ -4,6 +4,7 @@ using wtf.cluster.JoyCon; using wtf.cluster.JoyCon.Calibration; using wtf.cluster.JoyCon.ExtraData; using wtf.cluster.JoyCon.HomeLed; +using wtf.cluster.JoyCon.InputData; using wtf.cluster.JoyCon.InputReports; using wtf.cluster.JoyCon.Rumble; @@ -14,18 +15,11 @@ public class JoyConRead { Console.OutputEncoding = Encoding.UTF8; HidDevice? device = null; - // Get a list of all HID devices DeviceList list = DeviceList.Local; if (OperatingSystem.IsWindows()) { - // Get all devices developed by Nintendo by vendor ID var nintendos = list.GetHidDevices(0x057e); - // Get the first Nintendo controller device = nintendos.FirstOrDefault(); - // It works fine for Windows, but... } else { - // Linux has more complicated HID device management, we can't get device's vendor ID, - // so let's filter devices by their report descriptor - // It should work in Windows too, so it's more multiplatform solution var hidDevices = list.GetHidDevices(); foreach (var d in hidDevices) { var rd = d.GetReportDescriptor(); @@ -54,16 +48,10 @@ public class JoyConRead { Console.WriteLine("No controller. Please connect Joy-Con or Pro controller via Bluetooth."); return; } - // Create a new JoyCon instance based on the HID device JoyCon joycon = new(device); - // Start controller polling joycon.Start(); - - // First of all, we need to set format of the input reports, - // most of the time you will need to use InputReportMode.Full mode await joycon.SetInputReportModeAsync(JoyCon.InputReportType.Simple); - // Get some information about the controller DeviceInfo deviceInfo = await joycon.GetDeviceInfoAsync(); Console.WriteLine( $"Type: {deviceInfo.ControllerType}, Firmware: {deviceInfo.FirmwareVersionMajor}.{deviceInfo.FirmwareVersionMinor}"); @@ -76,24 +64,8 @@ public class JoyConRead { Console.WriteLine("Colors not specified, seems like the controller is grey."); } - // Save the current cursor position - (var cX, var cY) = (Console.CursorLeft, Console.CursorTop); + joycon.ReportReceived += OnJoyconOnReportReceived; - // Subscribe to the input reports - joycon.ReportReceived += (sender, input) => { - Console.SetCursorPosition(cX, cY); - // Check the type of the input report, most likely it will be InputWithImu - if (input is InputSimple j) { - // Some base data from the controller - Console.WriteLine( - $"Buttons: ({j.Buttons}) "); - } else { - Console.WriteLine($"Invalid input report type: {input.GetType()}"); - } - return Task.CompletedTask; - }; - - // Error handling joycon.StoppedOnError += (_, ex) => { Console.WriteLine(); Console.WriteLine($"Critical error: {ex.Message}"); @@ -108,4 +80,25 @@ public class JoyConRead { Console.WriteLine(); Console.WriteLine("Stopped."); } + + private static Task OnJoyconOnReportReceived(JoyCon _, IJoyConReport input) { + if (input is InputSimple j) { + bool prev = PreviousPressed(j.Buttons); + bool next = NextPressed(j.Buttons); + if(prev) + Console.WriteLine("Previous Slide"); + if(next) + Console.WriteLine("Next Slide"); + } else { + Console.WriteLine($"Invalid input report type: {input.GetType()}"); + } + return Task.CompletedTask; + } + + private static bool PreviousPressed(ButtonsSimple input) { + return input.LorR || input.Left; + } + private static bool NextPressed(ButtonsSimple input) { + return input.ZLorZR || input.Down; + } } \ No newline at end of file