diff --git a/FrankenPedal/FrankenPedal.sln b/FrankenPedal/FrankenPedal.sln index bbc197a..db1fb66 100644 --- a/FrankenPedal/FrankenPedal.sln +++ b/FrankenPedal/FrankenPedal.sln @@ -5,6 +5,8 @@ VisualStudioVersion = 16.0.31005.135 MinimumVisualStudioVersion = 10.0.40219.1 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrankenPedal", "FrankenPedal\FrankenPedal.csproj", "{30197E84-0A50-49E4-A523-BD4BE7557490}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FrankenPedalV2", "FrankenPedalV2\FrankenPedalV2.csproj", "{2D906087-A0FF-478A-9BD8-4CCDB51F2517}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -15,6 +17,10 @@ Global {30197E84-0A50-49E4-A523-BD4BE7557490}.Debug|Any CPU.Build.0 = Debug|Any CPU {30197E84-0A50-49E4-A523-BD4BE7557490}.Release|Any CPU.ActiveCfg = Release|Any CPU {30197E84-0A50-49E4-A523-BD4BE7557490}.Release|Any CPU.Build.0 = Release|Any CPU + {2D906087-A0FF-478A-9BD8-4CCDB51F2517}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {2D906087-A0FF-478A-9BD8-4CCDB51F2517}.Debug|Any CPU.Build.0 = Debug|Any CPU + {2D906087-A0FF-478A-9BD8-4CCDB51F2517}.Release|Any CPU.ActiveCfg = Release|Any CPU + {2D906087-A0FF-478A-9BD8-4CCDB51F2517}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/FrankenPedal/FrankenPedalV2/Defaults.json b/FrankenPedal/FrankenPedalV2/Defaults.json new file mode 100644 index 0000000..62cb844 --- /dev/null +++ b/FrankenPedal/FrankenPedalV2/Defaults.json @@ -0,0 +1,5 @@ +{ + "ComPort": "COM10", + "BaudRate": 115200, + "PedalBindings": ["LB","MB","RB"] +} \ No newline at end of file diff --git a/FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj b/FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj new file mode 100644 index 0000000..3d76fde --- /dev/null +++ b/FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj @@ -0,0 +1,19 @@ + + + + Exe + netcoreapp3.1 + + + + + + + + + + PreserveNewest + + + + diff --git a/FrankenPedal/FrankenPedalV2/Program.cs b/FrankenPedal/FrankenPedalV2/Program.cs new file mode 100644 index 0000000..40a34dc --- /dev/null +++ b/FrankenPedal/FrankenPedalV2/Program.cs @@ -0,0 +1,175 @@ +using CommandLine; +using CommandLine.Text; +using System; +using System.IO; +using System.IO.Ports; +using System.Text.Json; + +namespace FrankenPedalV2 +{ + class Program + { + public class Options + { + + [Option('p', "port", Required = false, HelpText = "Set the COM Port for the pedal to configure (i.e. COM4)")] + public string ComPort { get; set; } + + [Option('b', "baud", Required = false, Default = 115200, HelpText = "Set the baud rate for the COM port.")] + public int BaudRate { get; set; } + + [Option('d', "defaults", Required = false, HelpText = "Applies Default Bindings From Defaults.json file", SetName = "Default")] + public bool Default { get; set; } + + [Option('v', "verbose", Required = false, Default = false, HelpText = "Show binding process information and errors.")] + public bool Verbose { get; set; } + + [Option('s', "set", Required = false, HelpText = "Set pedal bindings separated by commas", SetName = "Manual")] + public string PedalBindings { get; set; } + } + + static void Main(string[] args) + { + + var parser = new Parser(with => with.HelpWriter = null); + var parserResult = parser.ParseArguments(args); + parserResult + .WithParsed(o => + { + try + { + if (!o.Default && !(o.PedalBindings.Length > 0)) + { + throw new Exception("No Binding Arguments Provided! Run with --help to see usage information."); + } + + SerialPort port = null; + PedalDefaults defaults = null; + if (File.Exists("Defaults.json")) //Defaults.json successfully found + { + defaults = JsonSerializer.Deserialize(File.ReadAllText("Defaults.json")); + if (o.ComPort?.Length > 0) //Deault port overridden by command like argument + { + port = new SerialPort(o.ComPort, o.BaudRate); + } + else if (defaults.ComPort?.Length > 0) //Grab port + Baud Rate from Defaults.json file + { + port = new SerialPort(defaults.ComPort, defaults.BaudRate); + } + else //COM port not specified in Defaults.json or Command Line Argument + { + throw new Exception("Must specifiy COM port in command line or Defaults.json"); + } + } + else //Defaults.json NOT found + { + if (o.ComPort?.Length > 0) + { + port = new SerialPort(o.ComPort, o.BaudRate); + } + else + { + throw new Exception("Must specifiy COM port in command line or Defaults.json"); + } + } + + if (o.Default) //User supplied the "defaults" command line argument + { + if (defaults == null || !(defaults.PedalBindings?.Length > 0)) + { + throw new Exception("No Default Values found in Defaults.json"); + } + + string ControlMessage = "#"; + foreach (var b in defaults.PedalBindings) + { + ControlMessage += $"{b.Trim().ToControlChar()}"; + } + + Console.WriteLine($"DEBUG: Would Write Out: {ControlMessage}"); + port.Open(); + port.WriteLine(ControlMessage); + port.Close(); + } + else //User has not supplied the --defaults command line argument, mapping command line values + { + var bindingArray = o.PedalBindings.Split(','); + + string ControlMessage = "#"; + foreach (var b in bindingArray) + { + ControlMessage += $"{b.Trim().ToControlChar()}"; + } + + //Console.WriteLine($"DEBUG: Would Write Out: {ControlMessage}"); + port.Open(); + port.WriteLine(ControlMessage); + port.Close(); + + } + } + catch (Exception e) + { + if (e.Source == "System.IO.Ports") + { + Console.WriteLine($"ERROR: {e.Message.Replace("file", "port")}"); + } + else + { + Console.WriteLine($"ERROR: {e.Message}"); + } + System.Environment.Exit(1); + } + Console.WriteLine("Binding Successful!"); + }) + .WithNotParsed(errs => DisplayHelp(parserResult)); + } + + static void DisplayHelp(ParserResult result) + { + var helpText = HelpText.AutoBuild(result, h => + { + h.AdditionalNewLineAfterOption = false; + h.Heading = "FrankenPedal v 0.2.0"; //change header + h.AddPreOptionsLine("This application allows you to map keyboard " + + "and mouse inputs to the 3 pedals of an Infinity Pedal. This " + + "requires you to modify the pedal with an Arduino Leonardo microcontroller " + + "flashed with my wireless firmware."); + h.AddPreOptionsLine(""); + h.AddPreOptionsLine("Reserved chars are:"); + h.AddPreOptionsLine(" - '~' or LB (Left Mouse Click)"); + h.AddPreOptionsLine(" - '!' or MB (Middle Mouse Click)"); + h.AddPreOptionsLine(" - '@' or RB (Right Mouse Click)."); + h.AddPreOptionsLine("\nExample - Set left to 'a', middle to left mouse, right to right mouse:\nFrankenPedalV2.exe -p COM4 -b 115200 -l a -m LB -r '@'"); + h.AddPreOptionsLine("\nExample - Apply defaults from Defaults.json\nFrankenPedalV2.exe -p COM4 -d"); + return HelpText.DefaultParsingErrorsHandler(result, h); + }, e => e); + Console.WriteLine(helpText); + } + } + + public class PedalDefaults + { + public string ComPort { get; set; } + public int BaudRate { get; set; } + public string[] PedalBindings { get; set; } + } + + public static class ControlExtensions + { + public static char ToControlChar(this string input) + { + switch (input.ToUpperInvariant()) + { + case "LB": + return '~'; + case "MB": + return '!'; + case "RB": + return '@'; + default: + return input.ToCharArray()[0]; + } + } + } +} diff --git a/FrankenPedal/FrankenPedalV2/Properties/launchSettings.json b/FrankenPedal/FrankenPedalV2/Properties/launchSettings.json new file mode 100644 index 0000000..c8f1da2 --- /dev/null +++ b/FrankenPedal/FrankenPedalV2/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "FrakenPedal": { + "commandName": "Project", + "commandLineArgs": "-d" + } + } +} \ No newline at end of file