Skip to content
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
44 changes: 44 additions & 0 deletions Buttplug.Test/Client/ButtplugClientDeviceCommandTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,49 @@ public void TestNonRotatePercentRejectsNegativeValue()
.Should()
.Throw<ButtplugDeviceException>();
}

[Test]
public void TestToStepValuePositive()
{
var p = PercentOrSteps.FromPercent(0.5);
Assert.AreEqual(50, p.ToStepValue(-100, 100));

p = PercentOrSteps.FromPercent(1.0);
Assert.AreEqual(100, p.ToStepValue(-100, 100));

p = PercentOrSteps.FromPercent(0.0);
Assert.AreEqual(0, p.ToStepValue(-100, 100));
}

[Test]
public void TestToStepValueNegative()
{
var p = PercentOrSteps.FromPercent(-0.5, -1.0);
// Math.Floor(-0.5 * -100 * -1) = Math.Floor(-0.5 * 100) = Math.Floor(-50) = -50
Assert.AreEqual(-50, p.ToStepValue(-100, 100));

p = PercentOrSteps.FromPercent(-1.0, -1.0);
Assert.AreEqual(-100, p.ToStepValue(-100, 100));
}

[Test]
public void TestToStepValueAsymmetric()
{
var p = PercentOrSteps.FromPercent(0.555);
Assert.AreEqual(56, p.ToStepValue(-10, 100));

p = PercentOrSteps.FromPercent(-0.555, -1.0);
Assert.AreEqual(-6, p.ToStepValue(-10, 100));
}

[Test]
public void TestToStepValueSteps()
{
var s = PercentOrSteps.FromSteps(42);
Assert.AreEqual(42, s.ToStepValue(-100, 100));

s = PercentOrSteps.FromSteps(-42);
Assert.AreEqual(-42, s.ToStepValue(-100, 100));
}
}
}
13 changes: 12 additions & 1 deletion Buttplug/Client/ButtplugClientDeviceCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,25 @@ public static PercentOrSteps FromPercent(double percent, double minPercent = 0.0
/// <summary>
/// Converts this value to an actual step value given a max step count.
/// </summary>
/// <param name="minSteps">The maximum number of steps in the negative direction.</param>
/// <param name="maxSteps">The maximum number of steps.</param>
/// <returns>The calculated step value.</returns>
public int ToStepValue(int maxSteps)
public int ToStepValue(int minSteps, int maxSteps)
{
if (_steps.HasValue)
{
return _steps.Value;
}

if (!_percent.HasValue)
{
return 0;
}

if (_percent.Value < 0)
{
return (int)Math.Floor(_percent.Value * minSteps * -1);
}
return (int)Math.Ceiling(_percent.Value * maxSteps);
}
}
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Client/ButtplugClientDeviceFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,16 @@ public async Task RunOutputAsync(DeviceOutputCommand command, CancellationToken

// Get the output definition to determine step range
var outputDef = FeatureDefinition.GetOutput(command.OutputType);
int minSteps = (command.OutputType == OutputType.Rotate) ? -100 : 0; // Default if not specified
int maxSteps = 100; // Default if not specified
if (outputDef?.Value != null && outputDef.Value.Length >= 2)
{
minSteps = outputDef.Value[0];
maxSteps = outputDef.Value[1];
}

// Convert to actual step value
int actualValue = command.Value.ToStepValue(maxSteps);
int actualValue = command.Value.ToStepValue(minSteps, maxSteps);

OutputCmd cmd;
if (command.OutputType == OutputType.HwPositionWithDuration)
Expand Down