Skip to content

Commit

Permalink
chore: Add motion-first-sent status to all protocols
Browse files Browse the repository at this point in the history
Protocols all start with "last device state variables" that use
primitive, non-nullable types. This means if the first command we send
to a vibration device is 0, then the device thinks it's already at
zero and doesn't sent it. Added boolean values that protocols can
change in order to start skipping repeated commands after the first
one is sent.

Fixes #564
  • Loading branch information
qdot committed Feb 12, 2019
1 parent 1a5b78b commit bff181a
Show file tree
Hide file tree
Showing 14 changed files with 42 additions and 14 deletions.
4 changes: 4 additions & 0 deletions Buttplug/Devices/ButtplugDeviceProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public abstract class ButtplugDeviceProtocol : IButtplugDeviceProtocol
[NotNull]
protected IButtplugDeviceImpl Interface;

protected bool SentVibration = false;
protected bool SentLinear = false;
protected bool SentRotation = false;

/// <inheritdoc />
public IEnumerable<Type> AllowedMessageTypes => MsgFuncs.Keys;

Expand Down
3 changes: 2 additions & 1 deletion Buttplug/Devices/Protocols/CuemeProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ private Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg, Cance
newVibratorSpeeds[v.Index] = v.Speed;
}

if (newVibratorSpeeds.SequenceEqual(_vibratorSpeeds))
if (newVibratorSpeeds.SequenceEqual(_vibratorSpeeds) && SentVibration)
{
return Task.FromResult(new Ok(aMsg.Id) as ButtplugMessage);
}
Expand All @@ -183,6 +183,7 @@ private Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg, Cance
_updateValueTimer.Enabled = true;
}

SentVibration = true;
return Task.FromResult(new Ok(aMsg.Id) as ButtplugMessage);
}
}
Expand Down
3 changes: 2 additions & 1 deletion Buttplug/Devices/Protocols/CycloneX10Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ private async Task<ButtplugMessage> HandleRotateCmd(ButtplugDeviceMessage aMsg,
_speed = i.Speed;
}

if (!changed)
if (!changed && SentRotation)
{
return new Ok(cmdMsg.Id);
}

SentRotation = true;
// todo These comments don't match the actual settings below?!?!?!
// [6] pause 0x30 + 0-1
// [7] speed 0x30 + 0-10
Expand Down
3 changes: 2 additions & 1 deletion Buttplug/Devices/Protocols/KiirooGen1Protocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,12 @@ private async Task<ButtplugMessage> HandleVibrateCmd([NotNull] ButtplugDeviceMes
cmdMsg.Id);
}

if (Math.Abs(_deviceSpeed - v.Speed) < 0.001)
if (Math.Abs(_deviceSpeed - v.Speed) < 0.001 && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;
_deviceSpeed = v.Speed;
}

Expand Down
5 changes: 4 additions & 1 deletion Buttplug/Devices/Protocols/KiirooGen2VibeProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ private async Task<ButtplugMessage> HandleVibrateCmd([NotNull] ButtplugDeviceMes
changed = true;
}

if (!changed)

if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

var data = new[]
{
(byte)Convert.ToUInt16(_vibratorSpeeds[_devInfo.VibeOrder[0]] * 100),
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/LiBoProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
_vibratorSpeed[v.Index] = v.Speed;
}

if (!changed)
if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

// Map a 0 - 100% value to a 0 - 3 value since 0 * x == 0 this will turn off the vibe if
// speed is 0.00
var mode = (int)Math.Ceiling(_vibratorSpeed[0] * 3);
Expand Down
3 changes: 2 additions & 1 deletion Buttplug/Devices/Protocols/LovenseProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,12 @@ await Interface.WriteValueAsync(
Encoding.ASCII.GetBytes("RotateChange;"), false, aToken).ConfigureAwait(false);
}

if (Math.Abs(_rotateSpeed - vi.Speed) < 0.0001)
if (Math.Abs(_rotateSpeed - vi.Speed) < 0.0001 && SentRotation)
{
return new Ok(cmdMsg.Id);
}

SentRotation = true;
_rotateSpeed = vi.Speed;

await Interface.WriteValueAsync(
Expand Down
3 changes: 2 additions & 1 deletion Buttplug/Devices/Protocols/MagicMotionProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,12 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
_vibratorSpeeds[v.Index] = v.Speed;
}

if (!changed)
if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;
byte[] data;
switch (_devInfo.Protocol)
{
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/MysteryVibeProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,13 @@ private Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg, Cance
newVibratorSpeeds[v.Index] = (byte)(v.Speed * MaxSpeed);
}

if (newVibratorSpeeds.SequenceEqual(_vibratorSpeeds))
if (newVibratorSpeeds.SequenceEqual(_vibratorSpeeds) && SentVibration)
{
return Task.FromResult(new Ok(aMsg.Id) as ButtplugMessage);
}

SentVibration = true;

_vibratorSpeeds = newVibratorSpeeds;

if (!_updateValueTimer.Enabled)
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/PicobongProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
_vibratorSpeed = v.Speed;
}

if (!changed)
if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

var speedInt = Convert.ToUInt16(_vibratorSpeed * 10);

var data = new byte[] { 0x01, speedInt > 0 ? (byte)0x01 : (byte)0xff, Convert.ToByte(speedInt) };
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/VibratissimoProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
var cmdMsg = CheckGenericMessageHandler<VibrateCmd>(aMsg, 1);
var v = cmdMsg.Speeds[0];

if (Math.Abs(v.Speed - _vibratorSpeed) < 0.001)
if (Math.Abs(v.Speed - _vibratorSpeed) < 0.001 && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

_vibratorSpeed = v.Speed;

var data = new byte[] { 0x03, 0xff };
Expand Down
8 changes: 6 additions & 2 deletions Buttplug/Devices/Protocols/VorzeSAProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ private async Task<ButtplugMessage> HandleVorzeA10CycloneCmd(ButtplugDeviceMessa
{
var cmdMsg = CheckMessageHandler<VorzeA10CycloneCmd>(aMsg);

if (_clockwise == cmdMsg.Clockwise && _speed == cmdMsg.Speed)
if (_clockwise == cmdMsg.Clockwise && _speed == cmdMsg.Speed && SentRotation)
{
return new Ok(cmdMsg.Id);
}

SentRotation = true;

_clockwise = cmdMsg.Clockwise;
_speed = cmdMsg.Speed;

Expand Down Expand Up @@ -146,11 +148,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
_speed = tmpSpeed;
}

if (!changed)
if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

await Interface.WriteValueAsync(Endpoints.Tx,
new[] { (byte)_deviceType, (byte)_commandType, (byte)_speed }, false, aToken).ConfigureAwait(false);
return new Ok(aMsg.Id);
Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/WeVibeProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
_vibratorSpeed[v.Index] = v.Speed;
}

if (!changed)
if (!changed && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

var rSpeedInt = Convert.ToUInt16(_vibratorSpeed[0] * 15);
var rSpeedExt = Convert.ToUInt16(_vibratorSpeed[_vibratorCount - 1] * 15);

Expand Down
4 changes: 3 additions & 1 deletion Buttplug/Devices/Protocols/YoucupsProtocol.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,13 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg,
var cmdMsg = CheckGenericMessageHandler<VibrateCmd>(aMsg, 1);
var v = cmdMsg.Speeds[0];

if (Math.Abs(v.Speed - _vibratorSpeed) < 0.001)
if (Math.Abs(v.Speed - _vibratorSpeed) < 0.001 && SentVibration)
{
return new Ok(cmdMsg.Id);
}

SentVibration = true;

_vibratorSpeed = v.Speed;

await Interface.WriteValueAsync(
Expand Down

0 comments on commit bff181a

Please sign in to comment.