Commit 8717f9d7 authored by Gerard Gascón's avatar Gerard Gascón
Browse files

refactor: only printing if previous or next slide is pressed

parent 4909f82b
Loading
Loading
Loading
Loading
+23 −30
Original line number Diff line number Diff line
@@ -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);

		// 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;
		};
		joycon.ReportReceived += OnJoyconOnReportReceived;

		// 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