diff --git a/Arduino_Controller/Arduino Code/ArduinoController/ArduinoController.ino b/Arduino_Controller/Arduino Code/ArduinoController/ArduinoController.ino index e6758d8..030433f 100644 --- a/Arduino_Controller/Arduino Code/ArduinoController/ArduinoController.ino +++ b/Arduino_Controller/Arduino Code/ArduinoController/ArduinoController.ino @@ -215,7 +215,7 @@ void loadPedalBindings() { for (int i = PEDAL_COUNT-1; i >= 0; i--) { char c = EEPROM.read(i); if (byte(c) == byte(0xFF)) { //EEPROM never written, default to right mouse button - EEPROM.write(0, '@'); + EEPROM.write(i, '@'); c = '@'; } pedalFunction[i] = c; diff --git a/FrankenPedal/Arduino/frankenpedal/frankenpedal.ino b/FrankenPedal/Arduino/frankenpedal/frankenpedal.ino index 24a47ef..f4a5c0f 100644 --- a/FrankenPedal/Arduino/frankenpedal/frankenpedal.ino +++ b/FrankenPedal/Arduino/frankenpedal/frankenpedal.ino @@ -3,24 +3,20 @@ #include const int pedalLeftPin = 2; -const int pedalMiddlePin = A3; -const int pedalRightPin = 3; +const int pedalMiddlePin = 3; +const int pedalRightPin = 4; const int pedalPin [] = {pedalLeftPin, pedalMiddlePin, pedalRightPin}; -boolean currentPedalVal[3]; -char pedalFunction [] = {'~','!','@'}; +const int PEDAL_COUNT = ( sizeof(pedalPin) / sizeof(int) ); -bool leftIsPressed = false; -bool rightIsPressed = false; -bool middleIsPressed = false; - -char currentlyMapping = '-'; +bool currentPedalVal[PEDAL_COUNT]; +char pedalFunction[PEDAL_COUNT]; +bool pedalStatus[PEDAL_COUNT]; void setup() { - pinMode(pedalLeftPin, INPUT_PULLUP); - pinMode(pedalMiddlePin, INPUT_PULLUP); - pinMode(pedalRightPin, INPUT_PULLUP); - + for (int i = 0; i < PEDAL_COUNT; i++) { + pinMode(pedalPin[i], INPUT_PULLUP); + } loadBindings(); Serial.begin(115200); @@ -28,81 +24,40 @@ void setup() { void loop() { - // BUTTON PRESS LOGIC! - if (debounce(0)) // Left Pedal is physically pressed - { - if (!leftIsPressed) // If we have not already sent the DOWN event for this press - { - //Serial.println("PRESS_LEFT"); - leftIsPressed = true; - pressPedal(0); - } - } - else if (leftIsPressed) // Left Pedal is physically released but we have not yet sent the UP event - { - //Serial.println("RELEASE_LEFT"); - leftIsPressed = false; - releasePedal(0); - } - - if (debounce(1)) // Middle Pedal is physically pressed - { - if (!middleIsPressed) // If we have not already sent the DOWN event for this press - { - //Serial.println("PRESS_MIDDLE"); - middleIsPressed = true; - pressPedal(1); + for (int i = 0; i < PEDAL_COUNT; i++) { + bool isOn = debounce(i); + if (isOn) { + if (!pedalStatus[i]) { + pedalStatus[i] = true; + pressPedal(i); + } } - } - else if (middleIsPressed) // Middle Pedal is physically released but we have not yet sent the UP event - { - //Serial.println("RELEASE_MIDDLE"); - middleIsPressed = false; - releasePedal(1); - } - - if (debounce(2)) // Right Pedal is physically pressed - { - if (!rightIsPressed) // If we have not already sent the DOWN event for this press - { - //Serial.println("PRESS_RIGHT"); - rightIsPressed = true; - pressPedal(2); + else if (pedalStatus[i]) { + pedalStatus[i] = false; + releasePedal(i); } } - else if (rightIsPressed) // Right Pedal is physically released but we have not yet sent the UP event - { - //Serial.println("RELEASE_RIGHT"); - rightIsPressed = false; - releasePedal(2); - } // PEDAL CONFIGURATION LOGIC + // CONFIGURATION LOGIC if (Serial.available()) { - char c = Serial.read(); // Reads single character from serial input - if (currentlyMapping == '-' && ( c == '1' || c == '2' || c == '3' )) { - currentlyMapping = c; - } - else { - switch (currentlyMapping) { - case '1': - pedalFunction[0] = c; - EEPROM.update(0, c); - break; - case '2': - pedalFunction[1] = c; - EEPROM.update(1, c); - break; - case '3': - pedalFunction[2] = c; - EEPROM.update(2, c); - break; - default: - break; + char c = Serial.read(); + + //CONFIGURE PEDAL FUNCTIONS + if (c == '#') { + int i = 0; + while (Serial.available()) { + c = Serial.read(); + if (i < PEDAL_COUNT) { + pedalFunction[i] = c; + EEPROM.update(i, c); + Serial.print("Setting Button To: "); + Serial.println(c); } - currentlyMapping = '-'; - } - } + i++; + } + } + } } boolean debounce(int thisPedal) { @@ -150,26 +105,12 @@ void releasePedal(int pedal) { } void loadBindings() { - char left = EEPROM.read(0); - char middle = EEPROM.read(1); - char right = EEPROM.read(2); - - if (byte(left) == byte(0xFF)) { // EEPROM never written, default to left mouse button - left = '~'; - EEPROM.write(0, left); - } - - if (byte(middle) == byte(0xFF)) { // EEPROM never written, default to middle mouse button - middle = '!'; - EEPROM.write(1, middle); - } - - if (byte(right) == byte(0xFF)) { // EEPROM never written, default to right mouse button - right = '@'; - EEPROM.write(2, right); + for (int i = 0; i < PEDAL_COUNT; i++) { + char c = EEPROM.read(i); + if (byte(c) == byte(0xFF)) { //EEPROM never written, default to right mouse button + EEPROM.write(i, '@'); + c = '@'; + } + pedalFunction[i] = c; } - - pedalFunction[0] = left; - pedalFunction[1] = middle; - pedalFunction[2] = right; } diff --git a/FrankenPedal/FrankenPedal.sln b/FrankenPedal/FrankenPedal.sln index db1fb66..bbc197a 100644 --- a/FrankenPedal/FrankenPedal.sln +++ b/FrankenPedal/FrankenPedal.sln @@ -5,8 +5,6 @@ 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 @@ -17,10 +15,6 @@ 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/FrankenPedal/Defaults.json b/FrankenPedal/FrankenPedal/Defaults.json index c48472e..836e0f8 100644 --- a/FrankenPedal/FrankenPedal/Defaults.json +++ b/FrankenPedal/FrankenPedal/Defaults.json @@ -1,7 +1,5 @@ { "ComPort": "COM10", - "BaudRate": 115200, - "LeftPedal": "LB", - "MiddlePedal": "MB", - "RightPedal": "RB" + "BaudRate": 115200, + "PedalBindings": [ "LB", "MB", "RB" ] } \ No newline at end of file diff --git a/FrankenPedal/FrankenPedal/Program.cs b/FrankenPedal/FrankenPedal/Program.cs index 5b1d0a3..a629339 100644 --- a/FrankenPedal/FrankenPedal/Program.cs +++ b/FrankenPedal/FrankenPedal/Program.cs @@ -5,7 +5,7 @@ using System.IO.Ports; using System.Text.Json; -namespace FrankenPedal +namespace FrankenPedalV2 { class Program { @@ -21,18 +21,11 @@ public class Options [Option('d', "defaults", Required = false, HelpText = "Applies Default Bindings From Defaults.json file", SetName = "Default")] public bool Default { get; set; } - [Option('l', "left", Required = false, HelpText = "Set the character binding for the left pedal", SetName = "Manual")] - public string Left { get; set; } - - [Option('m', "middle", Required = false, HelpText = "Set the character binding for the middle pedal", SetName = "Manual")] - public string Middle { get; set; } - - [Option('r', "right", Required = false, HelpText = "Set the character binding for the right pedal", SetName = "Manual")] - public string Right { 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) @@ -45,7 +38,7 @@ static void Main(string[] args) { try { - if (!o.Default && !(o.Left?.Length > 0 || o.Middle?.Length > 0 || o.Right?.Length > 0)) + if (!o.Default && !(o.PedalBindings.Length > 0)) { throw new Exception("No Binding Arguments Provided! Run with --help to see usage information."); } @@ -82,49 +75,37 @@ static void Main(string[] args) if (o.Default) //User supplied the "defaults" command line argument { - if (defaults == null || !(defaults.LeftPedal?.Length > 0 || defaults.MiddlePedal?.Length > 0 || defaults.RightPedal?.Length > 0)) + if (defaults == null || !(defaults.PedalBindings?.Length > 0)) { throw new Exception("No Default Values found in Defaults.json"); } - string ControlMessage = ""; - if (defaults.LeftPedal?.Length > 0) - { - ControlMessage += $"1{defaults.LeftPedal.ToControlChar()}"; - } - if (defaults.MiddlePedal?.Length > 0) - { - ControlMessage += $"2{defaults.MiddlePedal.ToControlChar()}"; - } - if (defaults.RightPedal?.Length > 0) + string ControlMessage = "#"; + foreach (var b in defaults.PedalBindings) { - ControlMessage += $"3{defaults.RightPedal.ToControlChar()}"; + ControlMessage += $"{b.Trim().ToControlChar()}"; } + //Console.WriteLine($"DEBUG: Would Write Out: {ControlMessage}"); port.Open(); port.WriteLine(ControlMessage); port.Close(); - //Console.WriteLine($"Would Write Out: {ControlMessage}"); } else //User has not supplied the --defaults command line argument, mapping command line values { - string ControlMessage = ""; - if (o.Left != null) - { - ControlMessage += $"1{o.Left.ToControlChar()}"; - } - if (o.Middle != null) - { - ControlMessage += $"2{o.Middle.ToControlChar()}"; - } - if (o.Right != null) + var bindingArray = o.PedalBindings.Split(','); + + string ControlMessage = "#"; + foreach (var b in bindingArray) { - ControlMessage += $"3{o.Right.ToControlChar()}"; + ControlMessage += $"{b.Trim().ToControlChar()}"; } + + //Console.WriteLine($"DEBUG: Would Write Out: {ControlMessage}"); port.Open(); port.WriteLine(ControlMessage); port.Close(); - //Console.WriteLine($"Would Write Out: {ControlMessage}"); + } } catch (Exception e) @@ -149,31 +130,29 @@ static void DisplayHelp(ParserResult result) var helpText = HelpText.AutoBuild(result, h => { h.AdditionalNewLineAfterOption = false; - h.Heading = "FrankenPedal v 0.1.0"; //change header + 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 firmware."); + "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:\nFrankenPedal.exe -p COM4 -b 9600 -l a -m LB -r '@'"); - h.AddPreOptionsLine("\nExample - Apply defaults from Defaults.json\nFrankenPedal.exe -p COM4 -d"); + 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 LeftPedal { get; set; } - public string MiddlePedal { get; set; } - public string RightPedal { get; set; } + public string[] PedalBindings { get; set; } } public static class ControlExtensions diff --git a/FrankenPedal/FrankenPedalV2/Defaults.json b/FrankenPedal/FrankenPedalV2/Defaults.json deleted file mode 100644 index 62cb844..0000000 --- a/FrankenPedal/FrankenPedalV2/Defaults.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "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 deleted file mode 100644 index 3d76fde..0000000 --- a/FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj +++ /dev/null @@ -1,19 +0,0 @@ - - - - Exe - netcoreapp3.1 - - - - - - - - - - PreserveNewest - - - - diff --git a/FrankenPedal/FrankenPedalV2/Program.cs b/FrankenPedal/FrankenPedalV2/Program.cs deleted file mode 100644 index 40a34dc..0000000 --- a/FrankenPedal/FrankenPedalV2/Program.cs +++ /dev/null @@ -1,175 +0,0 @@ -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 deleted file mode 100644 index c8f1da2..0000000 --- a/FrankenPedal/FrankenPedalV2/Properties/launchSettings.json +++ /dev/null @@ -1,8 +0,0 @@ -{ - "profiles": { - "FrakenPedal": { - "commandName": "Project", - "commandLineArgs": "-d" - } - } -} \ No newline at end of file