From 42f5ed78fe88ae15a70111dc8e139ad9fa9e141d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerard=20Gasc=C3=B3n?= <52170489+GerardGascon@users.noreply.github.com> Date: Sat, 15 Jun 2024 13:20:08 +0200 Subject: [PATCH] refactor: Moved wiimote reading to event-based calls --- .../.idea/workspace.xml | 140 ++++++++++++++++++ SlidePresenter/JoyConRead.cs | 7 +- SlidePresenter/WiimoteRead.cs | 55 +++---- 3 files changed, 168 insertions(+), 34 deletions(-) create mode 100644 .idea/.idea.SwitchSlidePresenter/.idea/workspace.xml diff --git a/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml b/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml new file mode 100644 index 0000000..6d4b518 --- /dev/null +++ b/.idea/.idea.SwitchSlidePresenter/.idea/workspace.xml @@ -0,0 +1,140 @@ + + + + SwitchSlidePresenter.csproj + + + + + + + + + + + { + "lastFilter": { + "state": "OPEN", + "assignee": "GerardGascon" + } +} + { + "selectedUrlAndAccountId": { + "url": "https://github.com/GerardGascon/Switch-Slide-Presenter.git", + "accountId": "0af66c52-cbb7-4844-ad24-01b5c5b9bee8" + } +} + + + + + + + { + "associatedIndex": 6 +} + + + + + + + { + "keyToString": { + ".NET Project.SwitchSlidePresenter.executor": "Run", + "Publish to folder.Publish ControllerSlidePresenter to folder.executor": "Run", + "RunOnceActivity.ShowReadmeOnStart": "true", + "git-widget-placeholder": "master", + "node.js.detected.package.eslint": "true", + "node.js.detected.package.tslint": "true", + "node.js.selected.package.eslint": "(autodetect)", + "node.js.selected.package.tslint": "(autodetect)", + "nodejs_package_manager_path": "npm", + "vue.rearranger.settings.migration": "true" + }, + "keyToStringList": { + "rider.external.source.directories": [ + "C:\\Users\\ggasc\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\DecompilerCache", + "C:\\Users\\ggasc\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\SourcesCache", + "C:\\Users\\ggasc\\AppData\\Local\\Symbols\\src" + ] + } +} + + + + + + + + + + + + + + + + + 1718404015037 + + + + + + + + + + + + + \ No newline at end of file diff --git a/SlidePresenter/JoyConRead.cs b/SlidePresenter/JoyConRead.cs index 56aa05a..09e750f 100644 --- a/SlidePresenter/JoyConRead.cs +++ b/SlidePresenter/JoyConRead.cs @@ -16,7 +16,7 @@ public class JoyConRead : IGamepadReader { HidDevice? device = GetHidDevice(); if (device == null) { - Console.WriteLine("No controller. Please connect Joy-Con or Pro controller via Bluetooth."); + Console.WriteLine("No controller. Please connect Joy-Con via Bluetooth."); Console.WriteLine("Press any key to exit program."); Console.ReadKey(); return; @@ -39,11 +39,14 @@ public class JoyConRead : IGamepadReader { Console.WriteLine("JoyCon ready for presenting."); Console.WriteLine("Press Enter to exit program."); - while (Console.ReadKey().Key != ConsoleKey.Enter) { } + while (Console.ReadKey().Key != ConsoleKey.Enter) { + await Task.Yield(); + } joycon.Stop(); Console.WriteLine(); Console.WriteLine("Stopped."); + await Task.CompletedTask; } private static async Task LogDeviceInfo(JoyCon joycon) { diff --git a/SlidePresenter/WiimoteRead.cs b/SlidePresenter/WiimoteRead.cs index 7ab8dca..1a25b90 100644 --- a/SlidePresenter/WiimoteRead.cs +++ b/SlidePresenter/WiimoteRead.cs @@ -6,39 +6,37 @@ public class WiimoteRead : IGamepadReader { public event Action NextSlide; public event Action PrevSlide; - private const int RetryDelay = 1000; - public async Task Read() { Wiimote wiimote = new(); - while (string.IsNullOrEmpty(wiimote.HIDDevicePath)) { - wiimote.Connect(); - if (string.IsNullOrEmpty(wiimote.HIDDevicePath)) { - Console.WriteLine("Wiimote connection failed, trying again..."); - await Task.Delay(RetryDelay); - } else { - Console.WriteLine("Wiimote ready for presenting!"); - } + wiimote.Connect(); + + if (string.IsNullOrEmpty(wiimote.HIDDevicePath)) { + Console.WriteLine("No controller. Please connect Wiimote via Bluetooth."); + Console.WriteLine("Press any key to exit program."); + Console.ReadKey(); + return; } - ButtonState previousState = wiimote.WiimoteState.ButtonState; - while (true) { - if (PreviousPressed(wiimote.WiimoteState.ButtonState, previousState)) { - PrevSlide?.Invoke(); - } - if (NextPressed(wiimote.WiimoteState.ButtonState, previousState)) { - NextSlide?.Invoke(); - } - previousState = wiimote.WiimoteState.ButtonState; + wiimote.WiimoteChanged += WiimoteChanged; + Console.WriteLine("Wiimote ready for presenting."); + Console.WriteLine("Press Enter to exit program."); + while (Console.ReadKey().Key != ConsoleKey.Enter) { await Task.Yield(); + } + wiimote.Disconnect(); - if (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Enter) - continue; - wiimote.Disconnect(); + Console.WriteLine(); + Console.WriteLine("Stopped."); + await Task.CompletedTask; + } - Console.WriteLine(); - Console.WriteLine("Stopped."); - break; + private void WiimoteChanged(object? sender, WiimoteChangedEventArgs e) { + if (PreviousPressed(e.WiimoteState.ButtonState)) { + PrevSlide?.Invoke(); + } + if (NextPressed(e.WiimoteState.ButtonState)) { + NextSlide?.Invoke(); } } @@ -48,11 +46,4 @@ public class WiimoteRead : IGamepadReader { private static bool NextPressed(ButtonState input) { return input.A || input.Right; } - - private static bool PreviousPressed(ButtonState input, ButtonState previousState) { - return PreviousPressed(input) && !PreviousPressed(previousState); - } - private static bool NextPressed(ButtonState input, ButtonState previousState) { - return NextPressed(input) && !NextPressed(previousState); - } } \ No newline at end of file