Skip to content

Commit

Permalink
Introduce FrankenPedalV2
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyruz committed Apr 13, 2021
1 parent e2c6637 commit 10da902
Show file tree
Hide file tree
Showing 5 changed files with 213 additions and 0 deletions.
6 changes: 6 additions & 0 deletions FrankenPedal/FrankenPedal.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
5 changes: 5 additions & 0 deletions FrankenPedal/FrankenPedalV2/Defaults.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"ComPort": "COM10",
"BaudRate": 115200,
"PedalBindings": ["LB","MB","RB"]
}
19 changes: 19 additions & 0 deletions FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="System.IO.Ports" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<None Update="Defaults.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
175 changes: 175 additions & 0 deletions FrankenPedal/FrankenPedalV2/Program.cs
Original file line number Diff line number Diff line change
@@ -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<Options>(args);
parserResult
.WithParsed<Options>(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<PedalDefaults>(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<T>(ParserResult<T> 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];
}
}
}
}
8 changes: 8 additions & 0 deletions FrankenPedal/FrankenPedalV2/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"FrakenPedal": {
"commandName": "Project",
"commandLineArgs": "-d"
}
}
}

0 comments on commit 10da902

Please sign in to comment.