Skip to content

Commit

Permalink
Merge pull request #68 from Snoothy/feature/long-to-short
Browse files Browse the repository at this point in the history
Feature/long to short
  • Loading branch information
Snoothy authored Jan 3, 2019
2 parents 913b1d5 + 781c75d commit 1e3d242
Show file tree
Hide file tree
Showing 29 changed files with 148 additions and 118 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Changed

- `Button to Axis` parameters changed to two axis values and option for initialization
- Plugin updates are now of type short instead of long. Some operations are performed using int, to avoid wrap-around or crashes.
- Subscription and Bind Mode callbacks are now executed as Tasks and are an Action<short> rather than dynamic
- Default blocking to true while UCR GUI does not support selecting block

## [0.6.0] - 2018-12-03

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<img src="icon.png" align="right" />

# Universal Control Remapper
[![GitHub release](https://img.shields.io/badge/release-v0.6.0-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.6.0) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.9.11-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/Snoothy/UCR?svg=true)](https://ci.appveyor.com/project/Snoothy/ucr) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Snoothy_UCR&metric=alert_status)](https://sonarcloud.io/dashboard?id=Snoothy_UCR)
[![GitHub release](https://img.shields.io/badge/release-v0.6.0-blue.svg)](https://github.com/Snoothy/UCR/releases/tag/v0.6.0) [![IOWrapper version](https://img.shields.io/badge/IOWrapper-v0.10.0-blue.svg)](https://github.com/evilC/IOWrapper) [![license](https://img.shields.io/github/license/snoothy/ucr.svg)](https://github.com/Snoothy/UCR/blob/master/LICENSE) [![Github All Releases](https://img.shields.io/github/downloads/snoothy/ucr/total.svg)](https://github.com/Snoothy/UCR/releases) [![AppVeyor](https://ci.appveyor.com/api/projects/status/github/Snoothy/UCR?svg=true)](https://ci.appveyor.com/project/Snoothy/ucr) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Snoothy_UCR&metric=alert_status)](https://sonarcloud.io/dashboard?id=Snoothy_UCR)

Universal Control Remapper is a complete rewrite of the original [UCR](https://github.com/evilC/UCR), created in collaboration with [evilC](https://github.com/evilC/).

Expand Down
9 changes: 5 additions & 4 deletions UCR.Core/Managers/BindingManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private ProviderDescriptor GetProviderDescriptor(Device device)
};
}

private void InputChanged(ProviderDescriptor providerDescriptor, DeviceDescriptor deviceDescriptor, BindingReport bindingReport, int value)
private void InputChanged(ProviderDescriptor providerDescriptor, DeviceDescriptor deviceDescriptor, BindingReport bindingReport, short value)
{
if (!DeviceBinding.MapCategory(bindingReport.Category).Equals(_deviceBinding.DeviceBindingCategory)) return;
if (!IsInputValid(bindingReport.Category, value)) return;
Expand All @@ -121,7 +121,7 @@ private void InputChanged(ProviderDescriptor providerDescriptor, DeviceDescripto
EndBindMode();
}

private bool IsInputValid(BindingCategory bindingCategory, int value)
private bool IsInputValid(BindingCategory bindingCategory, short value)
{
switch (DeviceBinding.MapCategory(bindingCategory))
{
Expand All @@ -131,8 +131,9 @@ private bool IsInputValid(BindingCategory bindingCategory, int value)
case DeviceBindingCategory.Momentary:
return value != 0;
case DeviceBindingCategory.Range:
return Constants.AxisMaxValue * 0.4 < Math.Abs(value)
&& Constants.AxisMaxValue * 0.6 > Math.Abs(value);
var wideVal = Functions.WideAbs(value);
return Constants.AxisMaxValue * 0.4 < wideVal
&& Constants.AxisMaxValue * 0.6 > wideVal;
default:
return false;
}
Expand Down
16 changes: 8 additions & 8 deletions UCR.Core/Models/Binding/DeviceBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ private set
}


public delegate void ValueChanged(long value);
public delegate void ValueChanged(short value);

private ValueChanged _callback;
private Action<short> _callback;

[XmlIgnore]
public ValueChanged Callback
public Action<short> Callback
{
get => InputChanged;
set
Expand All @@ -75,9 +75,9 @@ public ValueChanged Callback
[XmlIgnore]
public ValueChanged OutputSink { get; set; }

private long _currentValue;
private short _currentValue;
[XmlIgnore]
public long CurrentValue
public short CurrentValue
{
get => _currentValue;
set
Expand All @@ -92,7 +92,7 @@ public DeviceBinding()
Guid = Guid.NewGuid();
}

public DeviceBinding(ValueChanged callback, Profile profile, DeviceIoType deviceIoType)
public DeviceBinding(Action<short> callback, Profile profile, DeviceIoType deviceIoType)
{
Callback = callback;
Profile = profile;
Expand Down Expand Up @@ -150,7 +150,7 @@ public static DeviceBindingCategory MapCategory(BindingCategory bindingInfoCateg
}
}

public void WriteOutput(long value)
public void WriteOutput(short value)
{
CurrentValue = value;
OutputSink?.Invoke(value);
Expand All @@ -170,7 +170,7 @@ private void OnEndBindModeHandler(DeviceBinding deviceBinding)
Profile.Context.BindingManager.EndBindModeHandler -= OnEndBindModeHandler;
}

private void InputChanged(long value)
private void InputChanged(short value)
{
CurrentValue = value;
_callback(value);
Expand Down
6 changes: 3 additions & 3 deletions UCR.Core/Models/CallbackMultiplexer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ public class CallbackMultiplexer
{
private DeviceBinding.ValueChanged _mappingUpdate;
private readonly int _index;
private readonly List<long> _cache;
private readonly List<short> _cache;

public CallbackMultiplexer(List<long> cache, int index, DeviceBinding.ValueChanged mappingUpdate)
public CallbackMultiplexer(List<short> cache, int index, DeviceBinding.ValueChanged mappingUpdate)
{
_mappingUpdate = mappingUpdate;
_index = index;
_cache = cache;
}

public void Update(long value)
public void Update(short value)
{
_cache[_index] = value;
_mappingUpdate(value);
Expand Down
8 changes: 4 additions & 4 deletions UCR.Core/Models/Mapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Mapping

/* Runtime */
private Profile Profile { get; set; }
private List<long> InputCache { get; set; }
private List<short> InputCache { get; set; }
private List<CallbackMultiplexer> Multiplexer { get; set; }

[XmlIgnore]
Expand Down Expand Up @@ -54,8 +54,8 @@ internal bool IsBound()

internal void PrepareMapping()
{
InputCache = new List<long>();
DeviceBindings.ForEach(_ => InputCache.Add(0L));
InputCache = new List<short>();
DeviceBindings.ForEach(_ => InputCache.Add(0));
Multiplexer = new List<CallbackMultiplexer>();
for (var i = 0; i < DeviceBindings.Count; i++)
{
Expand Down Expand Up @@ -87,7 +87,7 @@ internal Mapping GetOverridenMapping()
return null;
}

public void Update(long value)
public void Update(short value)
{
foreach (var plugin in Plugins)
{
Expand Down
4 changes: 2 additions & 2 deletions UCR.Core/Models/Plugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public virtual void InitializeCacheValues()

}

public virtual void Update(params long[] values)
public virtual void Update(params short[] values)
{

}
Expand All @@ -118,7 +118,7 @@ public virtual void OnDeactivate()

#region Plugin methods

protected void WriteOutput(int number, long value)
protected void WriteOutput(int number, short value)
{
Outputs[number].WriteOutput(value);
}
Expand Down
2 changes: 1 addition & 1 deletion UCR.Core/Models/Subscription/OutputSubscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public OutputSubscription(DeviceBinding deviceBinding, Guid subscriptionStateGui
}


private void WriteOutput(long value)
private void WriteOutput(short value)
{
DeviceBinding.Profile.Context.IOController.SetOutputstate(SubscriptionsManager.GetOutputSubscriptionRequest(SubscriptionStateGuid, DeviceSubscription), SubscriptionsManager.GetBindingDescriptor(DeviceBinding), (int)value);
}
Expand Down
10 changes: 5 additions & 5 deletions UCR.Core/Utilities/AxisHelpers/CircularDeadZoneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private void PrecalculateValues()
}
}

public long[] ApplyRangeDeadZone(long[] values)
public short[] ApplyRangeDeadZone(short[] values)
{
var x = values[0];
var y = values[1];
Expand All @@ -52,17 +52,17 @@ public long[] ApplyRangeDeadZone(long[] values)
var inputAngle = Math.Atan2(x, y);
if (inputRadius < _deadzoneRadius)
{
return new long[] { 0, 0 };
return new short[] { 0, 0 };
}
var adjustedRadius = inputRadius - _deadzoneRadius;
var outputRadius = adjustedRadius * _scaleFactor;

var outX = (long)(outputRadius * Math.Sin(inputAngle));
var outY = (long)(outputRadius * Math.Cos(inputAngle));
var outX = (int)(outputRadius * Math.Sin(inputAngle));
var outY = (int)(outputRadius * Math.Cos(inputAngle));
if (outX == -32769) outX = -32768;
if (outY == -32769) outY = -32768;

var output = new[] { outX, outY };
var output = new[] { (short)outX, (short)outY };
return output;

}
Expand Down
12 changes: 6 additions & 6 deletions UCR.Core/Utilities/AxisHelpers/DeadZoneHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ private void PrecalculateValues()
}
}

public long ApplyRangeDeadZone(long value)
public short ApplyRangeDeadZone(short value)
{
var absValue = Math.Abs(value);
if (absValue < Math.Round(_deadzoneCutoff))
var wideVal = Functions.WideAbs(value);
if (wideVal < Math.Round(_deadzoneCutoff))
{
return 0;
}

var sign = Math.Sign(value);
var adjustedValue = (absValue - _deadzoneCutoff) * _scaleFactor;
var newValue = (long) Math.Round(adjustedValue * sign);
var adjustedValue = (wideVal - _deadzoneCutoff) * _scaleFactor;
var newValue = (int) Math.Round(adjustedValue * sign);
if (newValue < -32768) newValue = -32768; // ToDo: Negative values can go up to -32777 (9 over), can this be improved?
//Debug.WriteLine($"Pre-DZ: {value}, Post-DZ: {newValue}, Cutoff: {_deadzoneCutoff}");
return newValue;
return (short) newValue;
}
}
}
6 changes: 3 additions & 3 deletions UCR.Core/Utilities/AxisHelpers/SensitivityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ private void PrecalculateValues()
_sens = _scaleFactor / 100d;
}

public long ApplyRangeSensitivity(long value)
public short ApplyRangeSensitivity(short value)
{
//var sensitivityPercent = (sensitivity / 100.0);
if (IsLinear) return Functions.ClampAxisRange((long)Math.Round(value * _scaleFactor));
if (IsLinear) return Functions.ClampAxisRange((int) Math.Round(value * _scaleFactor));

//var sens = _scaleFactor / 100d;
//double AxisRange = 1d * (Constants.AxisMaxValue - Constants.AxisMinValue);
Expand All @@ -51,7 +51,7 @@ public long ApplyRangeSensitivity(long value)
// calculate (Sensitivity * Value) + ( (1-Sensitivity) * Value^3 )
double valout = (_sens * val11) + ((1d - _sens) * Math.Pow(val11, 3d));
// Map value back to AxisRange
value = (long)Math.Round(((valout + 1d) / 2d) * _axisRange + (1d * Constants.AxisMinValue));
value = (short) Math.Round(((valout + 1d) / 2d) * _axisRange + (1d * Constants.AxisMinValue));

return value;
}
Expand Down
41 changes: 30 additions & 11 deletions UCR.Core/Utilities/Functions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,32 @@ public static class Functions
/// </summary>
/// <param name="value">The raw value of the axis</param>
/// <returns>The inverted value of the axis</returns>
public static long Invert(long value)
public static short Invert(short value)
{
if (value == 0) return 0;
if (value >= Constants.AxisMaxValue)
if (value == Constants.AxisMaxValue)
{
return Constants.AxisMinValue;
}

if (value <= Constants.AxisMinValue)
if (value == Constants.AxisMinValue)
{
return Constants.AxisMaxValue;
}

return value * -1;
return (short) (value * -1);
}

/// <summary>
/// Ensures that an axis value is within permitted range
/// </summary>
/// <param name="value">The raw axis value</param>
/// <returns>The clamped axis value</returns>
public static long ClampAxisRange(long value)
public static short ClampAxisRange(int value)
{
if (value == 0) return value;
if (value == 0) return 0;
if (value <= Constants.AxisMinValue) return Constants.AxisMinValue;
return value >= Constants.AxisMaxValue ? Constants.AxisMaxValue : value;
return (short) (value >= Constants.AxisMaxValue ? Constants.AxisMaxValue : value);
}

/// <summary>
Expand All @@ -47,7 +47,7 @@ public static long ClampAxisRange(long value)
/// <param name="axis">The value of the axis</param>
/// <param name="positiveRange">Set to true for the high half, else the low half</param>
/// <returns>The new value for the split axis. If axis is negative and high is specified, returns 0. If axis is positive and low is specified, returns 0</returns>
public static long SplitAxis(long axis, bool positiveRange)
public static short SplitAxis(short axis, bool positiveRange)
{
long value;
if (axis == 0) return Constants.AxisMinValue;
Expand All @@ -68,13 +68,32 @@ public static long SplitAxis(long axis, bool positiveRange)

if (value == 32768) value = 32767;

return value;
return (short) value;
}

/// <summary>
/// A Safe Abs, that widens
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static int WideAbs(short value)
{
return Math.Abs((int)value);
}

/// <summary>
/// A Safe Abs, that does not widen
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public static short SafeAbs(short value)
{
return value == short.MinValue ? short.MaxValue : Math.Abs(value);
}

public static long GetRangeFromPercentage(long percentage)
public static short GetRangeFromPercentage(short percentage)
{
return (long) (Constants.AxisMaxValue * (percentage / 100.0));
return (short) (Constants.AxisMaxValue * (percentage / 100.0));
}
}
}
8 changes: 4 additions & 4 deletions UCR.Plugins/Remapper/AxesToAxes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ private void Initialize()
_linearSenstitivityScaleFactor = ((double)Sensitivity / 100);
}

public override void Update(params long[] values)
public override void Update(params short[] values)
{
var outputValues = new long[] {values[0], values[1]};
var outputValues = new short[] {values[0], values[1]};
if (DeadZone != 0)
{
if (CircularDz)
Expand All @@ -77,8 +77,8 @@ public override void Update(params long[] values)
{
if (Linear)
{
outputValues[0] = (long)(outputValues[0] * _linearSenstitivityScaleFactor);
outputValues[1] = (long)(outputValues[1] * _linearSenstitivityScaleFactor);
outputValues[0] = (short) (outputValues[0] * _linearSenstitivityScaleFactor);
outputValues[1] = (short) (outputValues[1] * _linearSenstitivityScaleFactor);
}
else
{
Expand Down
6 changes: 3 additions & 3 deletions UCR.Plugins/Remapper/AxisInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ public override void InitializeCacheValues()
Initialize();
}

public override void Update(params long[] values)
public override void Update(params short[] values)
{
}

private void Initialize()
{
var value = (long)((Percentage / 100) * Constants.AxisMaxAbsValue);
var value = (int)((Percentage / 100) * Constants.AxisMaxAbsValue);
value = Functions.ClampAxisRange(value);
WriteOutput(0, value);
WriteOutput(0, (short) value);
}
}
}
Loading

0 comments on commit 1e3d242

Please sign in to comment.