refactor: only printing if previous or next slide is pressed
This commit is contained in:
parent
4909f82bb2
commit
8717f9d716
1 changed files with 23 additions and 30 deletions
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue