Skip to content

Commit

Permalink
Port to pipeline elements
Browse files Browse the repository at this point in the history
  • Loading branch information
X9VoiD committed Jan 8, 2022
1 parent 068abb2 commit 9586204
Show file tree
Hide file tree
Showing 12 changed files with 187 additions and 121 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# EditorConfig is awesome: https://EditorConfig.org

# top-most EditorConfig file
root = true

[*]
indent_style = space
indent_size = 4
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{csproj,props,target}]
indent_size = 2
8 changes: 3 additions & 5 deletions .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,16 @@ jobs:

steps:
- uses: actions/checkout@v2
with:
with:
submodules: true

- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.100
dotnet-version: '6.0'

- name: Filters Build
env:
framework: net5
run: dotnet build ${{ matrix.Filters }}/${{ matrix.Filters }}.csproj -f $framework -o ./build
run: dotnet build ${{ matrix.Filters }}/${{ matrix.Filters }}.csproj -o ./build

- name: Upload Filters artifact
uses: actions/upload-artifact@master
Expand Down
2 changes: 1 addition & 1 deletion .modules/OpenTabletDriver
Submodule OpenTabletDriver updated 722 files
31 changes: 31 additions & 0 deletions Common/MillimeterAsyncPositionedPipelineElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System.Numerics;
using OpenTabletDriver.Plugin.Attributes;
using OpenTabletDriver.Plugin.Output;
using OpenTabletDriver.Plugin.Tablet;

namespace TabletDriverFilters
{
public abstract class MillimeterAsyncPositionedPipelineElement : AsyncPositionedPipelineElement<IDeviceReport>
{
[TabletReference]
public TabletReference TabletReference { set => HandleTabletReferenceInternal(value); }

protected Vector2 MillimeterScale;

private void HandleTabletReferenceInternal(TabletReference tabletReference)
{
var digitizer = tabletReference.Properties.Specifications.Digitizer;
MillimeterScale = new Vector2
{
X = digitizer.Width / digitizer.MaxX,
Y = digitizer.Height / digitizer.MaxY
};
HandleTabletReference(tabletReference);
}

protected virtual void HandleTabletReference(TabletReference tabletReference)
{
// Override when needed
}
}
}
35 changes: 35 additions & 0 deletions Common/MillimeterPositionedPipelineElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using System.Numerics;
using OpenTabletDriver.Plugin.Attributes;
using OpenTabletDriver.Plugin.Output;
using OpenTabletDriver.Plugin.Tablet;

namespace TabletDriverFilters
{
public abstract class MillimeterPositionedPipelineElement : IPositionedPipelineElement<IDeviceReport>
{
protected Vector2 MillimeterScale;

[TabletReference]
public TabletReference TabletReference { set => HandleTabletReferenceInternal(value); }
public abstract PipelinePosition Position { get; }
public abstract event Action<IDeviceReport> Emit;
public abstract void Consume(IDeviceReport value);

private void HandleTabletReferenceInternal(TabletReference tabletReference)
{
var digitizer = tabletReference.Properties.Specifications.Digitizer;
MillimeterScale = new Vector2
{
X = digitizer.Width / digitizer.MaxX,
Y = digitizer.Height / digitizer.MaxY
};
HandleTabletReference(tabletReference);
}

protected virtual void HandleTabletReference(TabletReference tabletReference)
{
// Override when needed
}
}
}
93 changes: 42 additions & 51 deletions DevocubFilters/AntiChatter.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
using System;
using System.Numerics;
using OpenTabletDriver.Plugin;
using OpenTabletDriver.Plugin.Attributes;
using OpenTabletDriver.Plugin.Tablet.Interpolator;
using OpenTabletDriver.Plugin.Timers;
using OpenTabletDriver.Plugin.Output;
using OpenTabletDriver.Plugin.Tablet;

namespace TabletDriverFilters.Devocub
{
using static MathF;

[PluginName("Devocub Antichatter")]
public class Antichatter : Interpolator
public class Antichatter : MillimeterAsyncPositionedPipelineElement
{
private const string LATENCY_TOOLTIP =
"Smoothing latency\n"
Expand Down Expand Up @@ -43,7 +40,7 @@ public class Antichatter : Interpolator
+ "\n"
+ " - Smooth: Latency ~10 ms, Strength 3, Multiplier 100, OffsetX 1.5, OffsetY 1.\n"
+ " Change OffsetX between 0-2 to switch between stickiness and smooth.\n"
+ " Increase Strength to 4-10 to get sharper changes in smoothing. Decrease Strength to 1-2 to get more smoothing.\n"
+ " Increase Strength to 4-10 to get harper. Decrease Strength to 1-2 to get more smoothing.\n"
+ "\n"
+ " - Low latency: Set Offset Y to 0 (and potentially set Latency to 1-10 ms. However, with some settings this can break smoothing, usually OffsetY 0 is enough to being able to go to lowest latency).";
private const string PREDICTION_TOOLTIP =
Expand Down Expand Up @@ -73,10 +70,7 @@ public class Antichatter : Interpolator
+ " Smoothing: Latency 40ms, Strength 3, Multiplier 10, OffsetX 1, OffsetY 1\n"
+ " Prediction: Strength 4, Sharpness 0.75, Offset 2.5, OffsetY 1";

public Antichatter(ITimer scheduler) : base(scheduler)
{
GetMMScale();
}
public override PipelinePosition Position => PipelinePosition.PreTransform;

[SliderProperty("Latency", 0f, 1000f, 2f), DefaultPropertyValue(2f), ToolTip(LATENCY_TOOLTIP)]
public float Latency
Expand All @@ -98,8 +92,8 @@ public float Latency
public float AntichatterOffsetY { set; get; }

[BooleanProperty("Prediction", ""), ToolTip(PREDICTION_TOOLTIP)]

public bool PredictionEnabled { set; get; }

[Property("Prediction Strength"), DefaultPropertyValue(1.1f), ToolTip(PREDICTION_TOOLTIP)]
public float PredictionStrength { set; get; }

Expand All @@ -117,45 +111,52 @@ public float Latency
private float timerInterval => 1000 / Frequency;
private float latency = 2.0f;
private float weight;
private Vector2 mmScale;
private Vector2 position;
private Vector2 prevTargetPos, targetPos, calcTarget;
private SyntheticTabletReport report;

public override void UpdateState(SyntheticTabletReport report)
protected override void ConsumeState()
{
this.targetPos = report.Position * mmScale;

if (PredictionEnabled)
if (State is ITabletReport report)
{
// Calculate predicted position onNewPacket
if (this.prevTargetPos.X != this.targetPos.X || this.prevTargetPos.Y != this.targetPos.Y)
{
// Calculate distance between last 2 packets and prediction
var delta = this.targetPos - this.prevTargetPos;
var distance = Vector2.Distance(this.prevTargetPos, this.targetPos);
var predictionModifier = 1 / Cosh((distance - PredictionOffsetX) * PredictionSharpness) * PredictionStrength + PredictionOffsetY;

// Apply prediction
delta *= predictionModifier;

// Update predicted position
this.calcTarget = this.targetPos + delta;
this.targetPos = report.Position * MillimeterScale;

// Update old position for further prediction
this.prevTargetPos = this.targetPos;
if (PredictionEnabled)
{
// Calculate predicted position onNewPacket
if (this.prevTargetPos.X != this.targetPos.X || this.prevTargetPos.Y != this.targetPos.Y)
{
// Calculate distance between last 2 packets and prediction
var delta = this.targetPos - this.prevTargetPos;
var distance = Vector2.Distance(this.prevTargetPos, this.targetPos);
var predictionModifier = 1 / MathF.Cosh((distance - PredictionOffsetX) * PredictionSharpness) * PredictionStrength + PredictionOffsetY;

// Apply prediction
delta *= predictionModifier;

// Update predicted position
this.calcTarget = this.targetPos + delta;

// Update old position for further prediction
this.prevTargetPos = this.targetPos;
}
}
else
calcTarget = targetPos;
}
else
calcTarget = targetPos;

this.report = report;
}

public override SyntheticTabletReport Interpolate()
protected override void UpdateState()
{
this.report.Position = Filter(this.calcTarget) / mmScale;
return this.report;
if (State is ITabletReport report)
{
report.Position = Filter(calcTarget) / MillimeterScale;
State = report;
}

if (PenIsInRange() || State is not ITabletReport)
{
OnEmit();
}
}

public Vector2 Filter(Vector2 calcTarget)
Expand All @@ -173,7 +174,7 @@ public Vector2 Filter(Vector2 calcTarget)

// Devocub smoothing
// Increase weight of filter in {formula} times
var weightModifier = (float)(Pow(distance + AntichatterOffsetX, AntichatterStrength * -1) * AntichatterMultiplier);
var weightModifier = (float)(MathF.Pow(distance + AntichatterOffsetX, AntichatterStrength * -1) * AntichatterMultiplier);

// Limit minimum
if (weightModifier + AntichatterOffsetY < 0)
Expand All @@ -194,15 +195,5 @@ private void SetWeight(float latency)
float target = 1 - THRESHOLD;
this.weight = 1f - (1f / MathF.Pow(1f / target, 1f / stepCount));
}

private void GetMMScale()
{
var digitizer = Info.Driver.Tablet.Digitizer;
this.mmScale = new Vector2
{
X = digitizer.Width / digitizer.MaxX,
Y = digitizer.Height / digitizer.MaxY
};
}
}
}
9 changes: 0 additions & 9 deletions DevocubFilters/DevocubFilters.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
</ItemGroup>

</Project>
12 changes: 12 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<Project>

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="$(MSBuildThisFileDirectory)/.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
<Compile Include="$(MSBuildThisFileDirectory)/Common/*.cs" />
</ItemGroup>

</Project>
9 changes: 0 additions & 9 deletions HawkuFilters/HawkuFilters.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net5</TargetFramework>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="../.modules/OpenTabletDriver/OpenTabletDriver.Plugin/OpenTabletDriver.Plugin.csproj" />
</ItemGroup>

</Project>
Loading

0 comments on commit 9586204

Please sign in to comment.