refactor: only printing if previous or next slide is pressed

This commit is contained in:
Gerard Gascón 2024-06-06 17:46:49 +02:00
parent 4909f82bb2
commit 8717f9d716

View file

@ -4,6 +4,7 @@ using wtf.cluster.JoyCon;
using wtf.cluster.JoyCon.Calibration; using wtf.cluster.JoyCon.Calibration;
using wtf.cluster.JoyCon.ExtraData; using wtf.cluster.JoyCon.ExtraData;
using wtf.cluster.JoyCon.HomeLed; using wtf.cluster.JoyCon.HomeLed;
using wtf.cluster.JoyCon.InputData;
using wtf.cluster.JoyCon.InputReports; using wtf.cluster.JoyCon.InputReports;
using wtf.cluster.JoyCon.Rumble; using wtf.cluster.JoyCon.Rumble;
@ -14,18 +15,11 @@ public class JoyConRead {
Console.OutputEncoding = Encoding.UTF8; Console.OutputEncoding = Encoding.UTF8;
HidDevice? device = null; HidDevice? device = null;
// Get a list of all HID devices
DeviceList list = DeviceList.Local; DeviceList list = DeviceList.Local;
if (OperatingSystem.IsWindows()) { if (OperatingSystem.IsWindows()) {
// Get all devices developed by Nintendo by vendor ID
var nintendos = list.GetHidDevices(0x057e); var nintendos = list.GetHidDevices(0x057e);
// Get the first Nintendo controller
device = nintendos.FirstOrDefault(); device = nintendos.FirstOrDefault();
// It works fine for Windows, but...
} else { } 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(); var hidDevices = list.GetHidDevices();
foreach (var d in hidDevices) { foreach (var d in hidDevices) {
var rd = d.GetReportDescriptor(); var rd = d.GetReportDescriptor();
@ -54,16 +48,10 @@ public class JoyConRead {
Console.WriteLine("No controller. Please connect Joy-Con or Pro controller via Bluetooth."); Console.WriteLine("No controller. Please connect Joy-Con or Pro controller via Bluetooth.");
return; return;
} }
// Create a new JoyCon instance based on the HID device
JoyCon joycon = new(device); JoyCon joycon = new(device);
// Start controller polling
joycon.Start(); 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); await joycon.SetInputReportModeAsync(JoyCon.InputReportType.Simple);
// Get some information about the controller
DeviceInfo deviceInfo = await joycon.GetDeviceInfoAsync(); DeviceInfo deviceInfo = await joycon.GetDeviceInfoAsync();
Console.WriteLine( Console.WriteLine(
$"Type: {deviceInfo.ControllerType}, Firmware: {deviceInfo.FirmwareVersionMajor}.{deviceInfo.FirmwareVersionMinor}"); $"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."); Console.WriteLine("Colors not specified, seems like the controller is grey.");
} }
// Save the current cursor position joycon.ReportReceived += OnJoyconOnReportReceived;
(var cX, var cY) = (Console.CursorLeft, Console.CursorTop);
// 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) => { joycon.StoppedOnError += (_, ex) => {
Console.WriteLine(); Console.WriteLine();
Console.WriteLine($"Critical error: {ex.Message}"); Console.WriteLine($"Critical error: {ex.Message}");
@ -108,4 +80,25 @@ public class JoyConRead {
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Stopped."); 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;
}
} }