Skip to content

Commit

Permalink
add --initprofile to set profile whenever keyboard plugged in
Browse files Browse the repository at this point in the history
  • Loading branch information
paul committed Mar 29, 2020
1 parent f15734b commit 908afbe
Show file tree
Hide file tree
Showing 12 changed files with 274 additions and 165 deletions.
16 changes: 16 additions & 0 deletions GmmkLib/GmmkLib.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>GmmkUtil.GmmkLib</RootNamespace>
</PropertyGroup>

<ItemGroup>
<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="Kolossi.EasyLife" Version="1.0.3" />
<PackageReference Include="Usb.Net" Version="3.1.0" />
</ItemGroup>

</Project>
147 changes: 147 additions & 0 deletions GmmkLib/Keyboard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
using Device.Net;
using EasyLife;
using Hid.Net.Windows;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using Usb.Net.Windows;

namespace GmmkUtil.GmmkLib
{
public interface IKeyboard
{
IDevice Device { get; set; }

DeviceListener DeviceListener { get; set; }

bool Connected { get; set; }

int DefaultProfile { get; set; }

void Initialized(object sender, DeviceEventArgs e);
void Disconnected(object sender, DeviceEventArgs e);
}

public class Keyboard : IKeyboard
{
#if (LIBUSB)
public const int POLL_MSECS = 6000;
#else
public const int POLL_MSECS = 3000;
#endif

public IDevice Device { get; set; }

public DeviceListener DeviceListener { get; set; }
public bool Connected { get; set; }

public int DefaultProfile { get; set; }

public Keyboard()
{
DebugTracer tracer = new DebugTracer();
DebugLogger logger = new DebugLogger();

WindowsUsbDeviceFactory.Register(logger, tracer);
WindowsHidDeviceFactory.Register(logger, tracer);
}

public void Initialized(object sender, DeviceEventArgs e)
{
//if (Connected) return;
"Initialized Enter".ConsoleLogLine();
var device = e.Device;
$"Setting {DefaultProfile}".ConsoleLogLine();
var defResponse = device.SetProfile(DefaultProfile)?.Result;
"Connected = true".ConsoleLogLine();
Connected = true;
"Initialized Exit".ConsoleLogLine();
}

public void Disconnected(object sender, DeviceEventArgs e)
{
"Connected Enter".ConsoleLogLine();
"Connected = true".ConsoleLogLine();
Connected = false;
"Disconnected Exit".ConsoleLogLine();
}
}

public static class KeyboardMethods
{
public const string SET_PROFILE = "04dd03042c00000055aaff02450c2f650001{0:x2}08000000000102030405060807090b0a0c0d0e0f10111214000000000000000000000000000000000000000000";

// Todo!
//public const string SET_KEY_COLOUR = "0403031103030200{0:x2}{1:x2}{2:x2}0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";

public static async Task<IKeyboard> Connect(this IKeyboard keyboard)
{
List<FilterDeviceDefinition> deviceDefinitions = GetFilterDeviceDefinitions();

var devices = await DeviceManager.Current.GetDevicesAsync(deviceDefinitions);
keyboard.Device = devices.FirstOrDefault();
await keyboard.Device.InitializeAsync();
return keyboard;
}

private static List<FilterDeviceDefinition> GetFilterDeviceDefinitions()
{
return new List<FilterDeviceDefinition>
{
new FilterDeviceDefinition{ DeviceType= DeviceType.Hid, VendorId= 3141, UsagePage=65308 }
};
}

public static async Task<byte[]> Send(this IKeyboard keyboard, string hexString)
{
var writeBuffer = hexString.ToByteArray();

return await keyboard.Device.WriteAndReadAsync(writeBuffer);
}

public static async Task<byte[]> SetProfile(this IDevice device, int profileNum)
{
if (profileNum < 1 || profileNum > 3) throw new ArgumentOutOfRangeException("profileNum", "Must be 1-3 only");
var writeBuffer = string.Format(SET_PROFILE, profileNum - 1).ToByteArray();

//Write the data to the device
return await device.WriteAndReadAsync(writeBuffer);
}

// although the keyboard isn't use, demanding one ensures factories have been initialised
public static void ShowAllDevices(this IKeyboard keyboard)
{
var devices = DeviceManager.Current.GetConnectedDeviceDefinitionsAsync(null).Result;
Console.WriteLine("Currently connected devices: ");
foreach (var device in devices)
{

Console.WriteLine(device.DeviceId);
if (device == null) continue;
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}");
}
}

public static void Listen(this IKeyboard keyboard)
{
"Listen Enter".ConsoleLogLine();
keyboard.Device?.Close();
keyboard.DeviceListener = new DeviceListener(GetFilterDeviceDefinitions(), Keyboard.POLL_MSECS);
keyboard.DeviceListener.DeviceInitialized += keyboard.Initialized;
keyboard.DeviceListener.DeviceDisconnected += keyboard.Disconnected;
keyboard.DeviceListener.Start();
"Listen Exit".ConsoleLogLine();
}

public static void StopListening(this IKeyboard keyboard)
{
"StopListening Enter".ConsoleLogLine();
keyboard.DeviceListener?.Stop();
keyboard.DeviceListener?.Dispose();
keyboard.DeviceListener = null;
"StopListening Exit".ConsoleLogLine();
}
}
}
7 changes: 5 additions & 2 deletions GmmkUtil/Options.cs → GmmkLib/Options.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CommandLine;

namespace GmmkUtil
namespace GmmkUtil.GmmkLib
{
public class Options
{
Expand All @@ -10,6 +10,9 @@ public class Options
[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('v', "verbose", HelpText = "Console logging", Required = false)]
public bool Verbose { get; set; }

[Option('a', "showall", HelpText ="Show all connected usb devices", Required = false
#if !DEBUG
,Hidden = true
Expand All @@ -18,7 +21,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)]
[Option('i', "initprofile", HelpText = "Stay running and monitor for keyboard being inserted, when it is set lighting to profile x (1-3)", Required = false)]
public int InitProfile { get; set; }
}
}
2 changes: 1 addition & 1 deletion GmmkUtil/Utils.cs → GmmkLib/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using System.Linq;

namespace GmmkUtil
namespace GmmkUtil.GmmkLib
{
public static class Utils
{
Expand Down
12 changes: 9 additions & 3 deletions GmmkUtil.sln
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.28307.1022
# Visual Studio Version 16
VisualStudioVersion = 16.0.29920.165
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GmmkUtil", "GmmkUtil\GmmkUtil.csproj", "{D8A91CF1-21D2-4718-9836-0BD1C1DC6F00}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "GmmkUtilConsole", "GmmkUtilConsole\GmmkUtilConsole.csproj", "{D8A91CF1-21D2-4718-9836-0BD1C1DC6F00}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GmmkLib", "GmmkLib\GmmkLib.csproj", "{CAE2D2B9-3093-48CE-86EB-97EAD36551A8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand All @@ -15,6 +17,10 @@ Global
{D8A91CF1-21D2-4718-9836-0BD1C1DC6F00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D8A91CF1-21D2-4718-9836-0BD1C1DC6F00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D8A91CF1-21D2-4718-9836-0BD1C1DC6F00}.Release|Any CPU.Build.0 = Release|Any CPU
{CAE2D2B9-3093-48CE-86EB-97EAD36551A8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CAE2D2B9-3093-48CE-86EB-97EAD36551A8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CAE2D2B9-3093-48CE-86EB-97EAD36551A8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CAE2D2B9-3093-48CE-86EB-97EAD36551A8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 0 additions & 6 deletions GmmkUtil/App.config

This file was deleted.

81 changes: 0 additions & 81 deletions GmmkUtil/Keyboard.cs

This file was deleted.

63 changes: 0 additions & 63 deletions GmmkUtil/Program.cs

This file was deleted.

Loading

0 comments on commit 908afbe

Please sign in to comment.