Skip to content

Commit

Permalink
Update frankenpedal .net code and arduino
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnyruz committed Apr 15, 2021
1 parent 10da902 commit 8c7d389
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 364 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
145 changes: 43 additions & 102 deletions FrankenPedal/Arduino/frankenpedal/frankenpedal.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,106 +3,61 @@
#include <EEPROM.h>

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);
}

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) {
Expand Down Expand Up @@ -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;
}
6 changes: 0 additions & 6 deletions FrankenPedal/FrankenPedal.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions FrankenPedal/FrankenPedal/Defaults.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
"ComPort": "COM10",
"BaudRate": 115200,
"LeftPedal": "LB",
"MiddlePedal": "MB",
"RightPedal": "RB"
"BaudRate": 115200,
"PedalBindings": [ "LB", "MB", "RB" ]
}
67 changes: 23 additions & 44 deletions FrankenPedal/FrankenPedal/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using System.IO.Ports;
using System.Text.Json;

namespace FrankenPedal
namespace FrankenPedalV2
{
class Program
{
Expand All @@ -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)
Expand All @@ -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.");
}
Expand Down Expand Up @@ -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)
Expand All @@ -149,31 +130,29 @@ static void DisplayHelp<T>(ParserResult<T> 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
Expand Down
5 changes: 0 additions & 5 deletions FrankenPedal/FrankenPedalV2/Defaults.json

This file was deleted.

19 changes: 0 additions & 19 deletions FrankenPedal/FrankenPedalV2/FrankenPedalV2.csproj

This file was deleted.

Loading

0 comments on commit 8c7d389

Please sign in to comment.