refactor: Moved wiimote reading to event-based calls

This commit is contained in:
Gerard Gascón 2024-06-15 13:20:08 +02:00
parent e8996a382d
commit 42f5ed78fe
3 changed files with 168 additions and 34 deletions

View file

@ -0,0 +1,140 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="AutoGeneratedRunConfigurationManager">
<projectFile>SwitchSlidePresenter.csproj</projectFile>
</component>
<component name="AutoImportSettings">
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="756a98cd-4cfe-4a7d-9110-f232a435843b" name="Changes" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="DpaMonitoringSettings">
<option name="autoShow" value="false" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="GitHubPullRequestSearchHistory">{
&quot;lastFilter&quot;: {
&quot;state&quot;: &quot;OPEN&quot;,
&quot;assignee&quot;: &quot;GerardGascon&quot;
}
}</component>
<component name="GithubPullRequestsUISettings">{
&quot;selectedUrlAndAccountId&quot;: {
&quot;url&quot;: &quot;https://github.com/GerardGascon/Switch-Slide-Presenter.git&quot;,
&quot;accountId&quot;: &quot;0af66c52-cbb7-4844-ad24-01b5c5b9bee8&quot;
}
}</component>
<component name="HighlightingSettingsPerFile">
<setting file="file://$USER_HOME$/AppData/Local/Symbols/src/dotnet/runtime/bf5e279d9239bfef5bb1b8d6212f1b971c434606/src/coreclr/System.Private.CoreLib/src/System/Threading/Monitor.CoreCLR.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$APPLICATION_CONFIG_DIR$/resharper-host/DecompilerCache/decompiler/045ef3cb636f4aaa894e8cefcc5e4b367e00/c1/d22d509a/Wiimote.cs" root0="SKIP_HIGHLIGHTING" />
<setting file="file://$PROJECT_DIR$/SlidePresenter/Program.cs" root0="FORCE_HIGHLIGHTING" />
</component>
<component name="MetaFilesCheckinStateConfiguration" checkMetaFiles="true" />
<component name="ProjectColorInfo">{
&quot;associatedIndex&quot;: 6
}</component>
<component name="ProjectId" id="2htCW7BWCLo4IJC4dn1HIEGkg1I" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true">
<ConfirmationsSetting value="2" id="Add" />
</component>
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;.NET Project.SwitchSlidePresenter.executor&quot;: &quot;Run&quot;,
&quot;Publish to folder.Publish ControllerSlidePresenter to folder.executor&quot;: &quot;Run&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;,
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;,
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;,
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;,
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;,
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot;
},
&quot;keyToStringList&quot;: {
&quot;rider.external.source.directories&quot;: [
&quot;C:\\Users\\ggasc\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\DecompilerCache&quot;,
&quot;C:\\Users\\ggasc\\AppData\\Roaming\\JetBrains\\Rider2024.1\\resharper-host\\SourcesCache&quot;,
&quot;C:\\Users\\ggasc\\AppData\\Local\\Symbols\\src&quot;
]
}
}</component>
<component name="RunManager" selected="Publish to folder.Publish ControllerSlidePresenter to folder">
<configuration name="Publish ControllerSlidePresenter to folder" type="DotNetFolderPublish" factoryName="Publish to folder">
<riderPublish configuration="Release" delete_existing_files="true" platform="Any CPU" runtime="Portable" target_folder="$PROJECT_DIR$/SlidePresenter/bin/Release/net8.0/publish" target_framework="net8.0" uuid_high="-467478054054508763" uuid_low="-8110965388391740835" />
<method v="2" />
</configuration>
<configuration name="ControllerSlidePresenter" type="DotNetProject" factoryName=".NET Project">
<option name="EXE_PATH" value="$PROJECT_DIR$/SlidePresenter/bin/Debug/net8.0/ControllerSlidePresenter.exe" />
<option name="PROGRAM_PARAMETERS" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/SlidePresenter/bin/Debug/net8.0" />
<option name="PASS_PARENT_ENVS" value="1" />
<option name="USE_EXTERNAL_CONSOLE" value="0" />
<option name="USE_MONO" value="0" />
<option name="RUNTIME_ARGUMENTS" value="" />
<option name="PROJECT_PATH" value="$PROJECT_DIR$/SlidePresenter/ControllerSlidePresenter.csproj" />
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="1" />
<option name="PROJECT_KIND" value="DotNetCore" />
<option name="PROJECT_TFM" value="net8.0" />
<method v="2">
<option name="Build" />
</method>
</configuration>
<list>
<item itemvalue=".NET Project.ControllerSlidePresenter" />
<item itemvalue="Publish to folder.Publish ControllerSlidePresenter to folder" />
</list>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="756a98cd-4cfe-4a7d-9110-f232a435843b" name="Changes" comment="" />
<created>1718404015037</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1718404015037</updated>
<workItem from="1718404016227" duration="5433000" />
</task>
<task id="LOCAL-00001" summary="feat: Added wiimote support and cleaned up the project">
<option name="closed" value="true" />
<created>1718408820801</created>
<option name="number" value="00001" />
<option name="presentableId" value="LOCAL-00001" />
<option name="project" value="LOCAL" />
<updated>1718408820801</updated>
</task>
<task id="LOCAL-00002" summary="rename: switch slide project to controller slide">
<option name="closed" value="true" />
<created>1718408956240</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1718408956240</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="3" />
</component>
<component name="UnityCheckinConfiguration" checkUnsavedScenes="true" />
<component name="UnityProjectConfiguration" hasMinimizedUI="false" />
<component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
<MESSAGE value="feat: Added wiimote support and cleaned up the project" />
<MESSAGE value="rename: switch slide project to controller slide" />
<option name="LAST_COMMIT_MESSAGE" value="rename: switch slide project to controller slide" />
</component>
</project>

View file

@ -16,7 +16,7 @@ public class JoyConRead : IGamepadReader {
HidDevice? device = GetHidDevice(); HidDevice? device = GetHidDevice();
if (device == null) { 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.WriteLine("Press any key to exit program.");
Console.ReadKey(); Console.ReadKey();
return; return;
@ -39,11 +39,14 @@ public class JoyConRead : IGamepadReader {
Console.WriteLine("JoyCon ready for presenting."); Console.WriteLine("JoyCon ready for presenting.");
Console.WriteLine("Press Enter to exit program."); Console.WriteLine("Press Enter to exit program.");
while (Console.ReadKey().Key != ConsoleKey.Enter) { } while (Console.ReadKey().Key != ConsoleKey.Enter) {
await Task.Yield();
}
joycon.Stop(); joycon.Stop();
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Stopped."); Console.WriteLine("Stopped.");
await Task.CompletedTask;
} }
private static async Task LogDeviceInfo(JoyCon joycon) { private static async Task LogDeviceInfo(JoyCon joycon) {

View file

@ -6,39 +6,37 @@ public class WiimoteRead : IGamepadReader {
public event Action NextSlide; public event Action NextSlide;
public event Action PrevSlide; public event Action PrevSlide;
private const int RetryDelay = 1000;
public async Task Read() { public async Task Read() {
Wiimote wiimote = new(); Wiimote wiimote = new();
while (string.IsNullOrEmpty(wiimote.HIDDevicePath)) {
wiimote.Connect(); wiimote.Connect();
if (string.IsNullOrEmpty(wiimote.HIDDevicePath)) { if (string.IsNullOrEmpty(wiimote.HIDDevicePath)) {
Console.WriteLine("Wiimote connection failed, trying again..."); Console.WriteLine("No controller. Please connect Wiimote via Bluetooth.");
await Task.Delay(RetryDelay); Console.WriteLine("Press any key to exit program.");
} else { Console.ReadKey();
Console.WriteLine("Wiimote ready for presenting!"); return;
}
} }
ButtonState previousState = wiimote.WiimoteState.ButtonState; wiimote.WiimoteChanged += WiimoteChanged;
while (true) {
if (PreviousPressed(wiimote.WiimoteState.ButtonState, previousState)) {
PrevSlide?.Invoke();
}
if (NextPressed(wiimote.WiimoteState.ButtonState, previousState)) {
NextSlide?.Invoke();
}
previousState = wiimote.WiimoteState.ButtonState;
Console.WriteLine("Wiimote ready for presenting.");
Console.WriteLine("Press Enter to exit program.");
while (Console.ReadKey().Key != ConsoleKey.Enter) {
await Task.Yield(); await Task.Yield();
}
if (!Console.KeyAvailable || Console.ReadKey().Key != ConsoleKey.Enter)
continue;
wiimote.Disconnect(); wiimote.Disconnect();
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Stopped."); Console.WriteLine("Stopped.");
break; await Task.CompletedTask;
}
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) { private static bool NextPressed(ButtonState input) {
return input.A || input.Right; 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);
}
} }