Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename AxisToButton to AxisToButtons, add new AxisToButton, add new va… #137

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- `Button to Filter`: Allows toggling of a filter using a button
- `Axis to Filter`: Allows toggling of a filter using an axis
- Added device cache allowing configuration of disconnected devices
- Added new AxisToButton plugin with differing values for press and release

### Fixed

Expand All @@ -22,6 +23,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- Updated to IOWrapper v0.10.6
- AxisToButton is now called AxisToButtons

## [0.8.0] - 2019-07-27

Expand Down
5 changes: 5 additions & 0 deletions UCR.Core/Utilities/InputValidation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ public static PropertyValidationResult ValidatePercentage(double value)
return ValidateRange(value, 0.0, 100.0);
}

public static PropertyValidationResult ValidateSignedPercentage(double value)
{
return ValidateRange(value, -100.0, 100.0);
}

public static PropertyValidationResult ValidateRange(double value, double min, double max)
{
if (value > max) return new PropertyValidationResult(false, $"Value must be {max} or less");
Expand Down
61 changes: 40 additions & 21 deletions UCR.Plugins/Remapper/AxisToButton.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.Reflection;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Models;
Expand All @@ -8,62 +9,80 @@

namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis to Button", Group = "Button", Description = "Map from one axis to two buttons")]
[Plugin("Axis to Button", Group = "Button", Description = "Map from one pedal style axis to a button, with press and release threshold")]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
[PluginOutput(DeviceBindingCategory.Momentary, "Button high")]
[PluginOutput(DeviceBindingCategory.Momentary, "Button low")]
[PluginOutput(DeviceBindingCategory.Momentary, "Button")]
public class AxisToButton : Plugin
{
[PluginGui("Invert", Order = 0)]
public bool Invert { get; set; }

[PluginGui("Dead zone", Order = 1)]
public int DeadZone { get; set; }
[PluginGui("Press at axis value", Order = 1)]
public double PressPercent { get; set; }

private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper();
[PluginGui("Release at axis value", Order = 2)]
public double ReleasePercent { get; set; }

private bool _pressed;
private double _pressThresh;
private double _releaseThresh;

public AxisToButton()
{
DeadZone = 30;
PressPercent = -80;
ReleasePercent = -100;
}

public override void InitializeCacheValues()
{
Initialize();
_pressThresh = Functions.GetRangeFromPercentage(PressPercent);
_releaseThresh = Functions.GetRangeFromPercentage(ReleasePercent);
}

public override void Update(params short[] values)
{
var value = values[0];
if (Invert) value = Functions.Invert(value);
switch (Math.Sign(_deadZoneHelper.ApplyRangeDeadZone(value)))
//Debug.WriteLine($"Current: {value}, Press @: {_pressThresh} Release @: {_releaseThresh}, Pressed: {_pressed}");
if (_pressed)
{
case 0:
if (value <= _releaseThresh)
{
_pressed = false;
WriteOutput(0, 0);
WriteOutput(1, 0);
break;
case 1:
}
}
else
{
if (value >= _pressThresh)
{
_pressed = true;
WriteOutput(0, 1);
WriteOutput(1, 0);
break;
case -1:
WriteOutput(0, 0);
WriteOutput(1, 1);
break;
}
}
}

private void Initialize()
{
_deadZoneHelper.Percentage = DeadZone;
}

public override PropertyValidationResult Validate(PropertyInfo propertyInfo, dynamic value)
{
switch (propertyInfo.Name)
{
case nameof(DeadZone):
return InputValidation.ValidatePercentage(value);
case nameof(PressPercent):
if (value < ReleasePercent)
{
return new PropertyValidationResult(false, "Press must be higher or equal to Release");
}
return InputValidation.ValidateSignedPercentage(value);
case nameof(ReleasePercent):
if (value > PressPercent)
{
return new PropertyValidationResult(false, "Release must be lower or equal to Press");
}
return InputValidation.ValidateSignedPercentage(value);
}

return PropertyValidationResult.ValidResult;
Expand Down
72 changes: 72 additions & 0 deletions UCR.Plugins/Remapper/AxisToButtons.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Reflection;
using HidWizards.UCR.Core.Attributes;
using HidWizards.UCR.Core.Models;
using HidWizards.UCR.Core.Models.Binding;
using HidWizards.UCR.Core.Utilities;
using HidWizards.UCR.Core.Utilities.AxisHelpers;

namespace HidWizards.UCR.Plugins.Remapper
{
[Plugin("Axis to Buttons", Group = "Button", Description = "Map from one axis to two buttons")]
[PluginInput(DeviceBindingCategory.Range, "Axis")]
[PluginOutput(DeviceBindingCategory.Momentary, "Button high")]
[PluginOutput(DeviceBindingCategory.Momentary, "Button low")]
public class AxisToButtons : Plugin
{
[PluginGui("Invert", Order = 0)]
public bool Invert { get; set; }

[PluginGui("Dead zone", Order = 1)]
public int DeadZone { get; set; }

private readonly DeadZoneHelper _deadZoneHelper = new DeadZoneHelper();

public AxisToButtons()
{
DeadZone = 30;
}

public override void InitializeCacheValues()
{
Initialize();
}

public override void Update(params short[] values)
{
var value = values[0];
if (Invert) value = Functions.Invert(value);
switch (Math.Sign(_deadZoneHelper.ApplyRangeDeadZone(value)))
{
case 0:
WriteOutput(0, 0);
WriteOutput(1, 0);
break;
case 1:
WriteOutput(0, 1);
WriteOutput(1, 0);
break;
case -1:
WriteOutput(0, 0);
WriteOutput(1, 1);
break;
}
}

private void Initialize()
{
_deadZoneHelper.Percentage = DeadZone;
}

public override PropertyValidationResult Validate(PropertyInfo propertyInfo, dynamic value)
{
switch (propertyInfo.Name)
{
case nameof(DeadZone):
return InputValidation.ValidatePercentage(value);
}

return PropertyValidationResult.ValidResult;
}
}
}
3 changes: 2 additions & 1 deletion UCR.Plugins/UCR.Plugins.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,10 @@
<Compile Include="Remapper\AxisSplitter.cs" />
<Compile Include="Remapper\AxesToAxes.cs" />
<Compile Include="Remapper\AxisInitializer.cs" />
<Compile Include="Remapper\AxisToButton.cs" />
<Compile Include="Remapper\ButtonToEvent.cs" />
<Compile Include="Remapper\AxisToAxis.cs" />
<Compile Include="Remapper\AxisToButton.cs" />
<Compile Include="Remapper\AxisToButtons.cs" />
<Compile Include="Remapper\ButtonsToAxis.cs" />
<Compile Include="Remapper\ButtonToAxis.cs" />
<Compile Include="Remapper\ButtonToButton.cs" />
Expand Down