Skip to content

Commit

Permalink
add ability to change to default profile set in config file
Browse files Browse the repository at this point in the history
  • Loading branch information
paul committed Mar 17, 2020
1 parent 2b6fd2a commit f15734b
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions GmmkUtil/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="DefaultProfile" value="1"/>
</appSettings>
</configuration>
14 changes: 14 additions & 0 deletions GmmkUtil/GmmkUtil.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>0.1.1</Version>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<DebugType>full</DebugType>
<DebugSymbols>true</DebugSymbols>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DebugType>none</DebugType>
<DebugSymbols>false</DebugSymbols>
</PropertyGroup>
Expand All @@ -15,7 +21,15 @@
<PackageReference Include="CommandLineParser" Version="2.7.82" />
<PackageReference Include="Device.Net" Version="3.1.0" />
<PackageReference Include="Hid.Net" Version="3.1.0" />
<PackageReference Include="System.Configuration.ConfigurationManager" Version="4.7.0" />
<PackageReference Include="Usb.Net" Version="3.1.0" />
</ItemGroup>

<ItemGroup>
<None Update="App.config">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
<ExcludeFromSingleFile>true</ExcludeFromSingleFile>
</None>
</ItemGroup>

</Project>
2 changes: 0 additions & 2 deletions GmmkUtil/Keyboard.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public static async Task<byte[]> SetProfile(this IKeyboard keyboard, int profile
return await keyboard.Device.WriteAndReadAsync(writeBuffer);
}

#if DEBUG
// although the keyboard isn't use, demanding one ensures factories have been initialised
public static void ShowAllDevices(this IKeyboard keyboard)
{
Expand All @@ -78,6 +77,5 @@ public static void ShowAllDevices(this IKeyboard keyboard)
Console.WriteLine($" manu={device.Manufacturer}|prod={device.ProductName}|label={device.Label}|sn={device.SerialNumber}|usagePage={device.UsagePage}|usage={device.Usage}|vers={device.VersionNumber}|prodId={device.ProductId}|vend={device.VendorId}|rbuff={device.ReadBufferSize}|wbuff={device.WriteBufferSize}");
}
}
#endif
}
}
7 changes: 5 additions & 2 deletions GmmkUtil/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ public class Options
[Option('p', "setprofile", HelpText = "If keyboard connected, immediately set lighting to profile x (1-3)",Required = false)]
public int Profile { get; set; }

[Option('d', "setdefaultprofile", HelpText = "If keyboard connected, immediately set lighting to the default profile in App.config", Required =false)]
public bool DefaultProfile { get; set; }

[Option('a', "showall", HelpText ="Show all connected usb devices", Required = false
#if !DEBUG
,Hidden = true
Expand All @@ -15,7 +18,7 @@ public class Options
public bool ShowAll { get; set; }

// Todo!
//[Option('i', "initprofile", HelpText = "Stay running and monitor for keyboard being inserted, when it is set lighting to profile x (1-3)", Min = 1, Max = 3)]
//public int InitProfile { get; set; }
[Option('i', "initprofile", HelpText = "Stay running and monitor for keyboard being inserted, when it is set lighting to profile x (1-3)", Min = 1, Max = 3)]
public int InitProfile { get; set; }
}
}
27 changes: 22 additions & 5 deletions GmmkUtil/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommandLine;
using CommandLine.Text;
using System;
using System.Configuration;

namespace GmmkUtil
{
Expand All @@ -14,23 +15,34 @@ static void Main(string[] args)
ParserResult.WithParsed<Options>(o =>
{
Keyboard keyboard = null;
if (o.ShowAll || o.Profile > 0)
if (o.ShowAll || o.Profile > 0 || o.DefaultProfile || o.InitProfile > 0)
{
keyboard = new Keyboard();

#if DEBUG
if (o.ShowAll)
{

keyboard?.ShowAllDevices();
}
#endif

if (o.Profile > 0 && o.DefaultProfile) throw new InvalidOperationException("Cant specify default and numbered profiles");

if (o.Profile > 0)
{
if (o.Profile < 1 || o.Profile > 3) throw new ArgumentOutOfRangeException("Profile", "Valid values: 1-3");
var response = keyboard?.Connect()?.Result?.SetProfile(o.Profile)?.Result;
ValidateProfile(o.Profile);
var profResponse = keyboard?.Connect()?.Result?.SetProfile(o.Profile)?.Result;
}
else if (o.DefaultProfile)
{
int defaultProfile;
if (!int.TryParse(ConfigurationManager.AppSettings["DefaultProfile"], out defaultProfile)) ValidateProfile(0);
var defResponse = keyboard?.Connect()?.Result?.SetProfile(defaultProfile)?.Result;
}
else if (o.InitProfile > 0)
{
ValidateProfile(o.InitProfile);
}

}
else
{
Expand All @@ -42,5 +54,10 @@ static void Main(string[] args)
Console.WriteLine("Parser Fail");
});
}

private static void ValidateProfile(int profile)
{
if (profile < 1 || profile > 3) throw new ArgumentOutOfRangeException("Profile", "Valid values: 1-3");
}
}
}
8 changes: 8 additions & 0 deletions GmmkUtil/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"GmmkUtil": {
"commandName": "Project",
"commandLineArgs": "-d"
}
}
}

0 comments on commit f15734b

Please sign in to comment.