private async Task <ButtplugMessage> HandleLinearCmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckMessageHandler <LinearCmd>(aMsg);

            if (cmdMsg.Vectors.Count > 1)
            {
                throw new ButtplugDeviceException(
                          "LinearCmd requires 1 vector for this device.",
                          cmdMsg.Id);
            }

            var command = new byte[11];

            // Vector commands start with 0x02;
            command[0] = 0x02;
            // Since we're simulating linear movement here, we'll move both belts in the same direction.
            command[1] = (byte)RealTouchComponents.U;
            // If we're moving backward, add 0x80 to our direction component.
            if (cmdMsg.Vectors[0].Position < _currentPosition)
            {
                command[1] += 0x80;
            }

            var speed = FleshlightHelper.GetSpeed(Math.Abs(cmdMsg.Vectors[0].Position - _currentPosition),
                                                  cmdMsg.Vectors[0].Duration);

            _currentPosition = cmdMsg.Vectors[0].Position;

            // This is our speed, or "magnitude" in CDK-speak. Right now, we'll
            // use the position differential like the fleshlight. Not super
            // thrilled with this idea, but blame everyone who decided the
            // Fleshlight Launch was the only f*****g toy to ever exist.
            //
            // Also, on my RealTouches, if the speed goes below about 40, things
            // basically stop moving. Make sure our speed doesn't dip below that.
            const double speedMin = 40;

            command[2] = (byte)(speedMin + ((0xff - speedMin) * speed));
            Debug.WriteLine((byte)(0xff * speed));

            // This is our duration, in milliseconds, 16-bit, LE across the two bytes.
            command[3] = (byte)(cmdMsg.Vectors[0].Duration & 0xff);
            command[4] = (byte)((cmdMsg.Vectors[0].Duration & 0xff00) >> 0x8);

            // In envelope, Same magnitude/duration as last 3 bytes.
            // g[5] = inMagnitude
            // g[6] = inDuration & 0xff
            // g[7] = (inDuration & 0xff00) >> 0x8

            // Out envelope, Same magnitude/duration as last 3 bytes.
            // g[9] = outMagnitude
            // g[9] = outDuration & 0xff
            // g[10] = (outDuration & 0xff00) >> 0x8

            var cmd = GetCommandArray(command);
            await Interface.WriteValueAsync(cmd, aToken);

            return(new Ok(aMsg.Id));
        }
Example #2
0
        private Task <ButtplugMessage> HandleFleshlightLaunchFW12Cmd([NotNull] ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckMessageHandler <FleshlightLaunchFW12Cmd>(aMsg);

            var pos = Convert.ToDouble(cmdMsg.Position) / 99.0;
            var dur = Convert.ToUInt32(FleshlightHelper.GetDuration(Math.Abs((1 - pos) - _currentPosition), cmdMsg.Speed / 99.0));

            return(HandleLinearCmd(LinearCmd.Create(cmdMsg.DeviceIndex, cmdMsg.Id, dur, pos, 1), aToken));
        }
        private async Task <ButtplugMessage> HandleFleshlightLaunchFW12Cmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckMessageHandler <FleshlightLaunchFW12Cmd>(aMsg);

            // We'll need to figure out the duration of the Fleshlight move, in
            // order to translate to a LinearCmd message.

            var position = (cmdMsg.Position / 99.0);

            var distance = Math.Abs(_currentPosition - position);

            var duration = FleshlightHelper.GetDuration(distance, (cmdMsg.Speed / 99.0));
            var vectors  = new List <LinearCmd.VectorSubcommand>();

            vectors.Add(new LinearCmd.VectorSubcommand(0, (uint)duration, position));
            var msg = new LinearCmd(aMsg.DeviceIndex, vectors, aMsg.Id);

            return(await HandleLinearCmd(msg, aToken));
        }
Example #4
0
        private Task <ButtplugMessage> HandleFleshlightLaunchCmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckMessageHandler <FleshlightLaunchFW12Cmd>(aMsg);

            _speed    = (Convert.ToDouble(cmdMsg.Speed) / 99) * 100;
            _position = (Convert.ToDouble(cmdMsg.Position) / 99) * 100;

            _position = _position < 0 ? 0 : _position;
            _position = _position > 100 ? 100 : _position;
            _speed    = _speed < 20 ? 20 : _speed;
            _speed    = _speed > 100 ? 100 : _speed;

            // This is @funjack's algorithm for converting Fleshlight Launch commands into absolute
            // distance (percent) / duration (millisecond) values
            var distance = Math.Abs(_position - _currentPosition);
            var duration = FleshlightHelper.GetDuration(distance / 100, _speed / 100);

            // We convert those into "position" increments for our OnUpdate() timer event.
            _increment = 1.5 * (distance / (duration / _updateInterval));

            return(Task.FromResult <ButtplugMessage>(new Ok(aMsg.Id)));
        }
        private async Task <ButtplugMessage> HandleLinearCmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckGenericMessageHandler <LinearCmd>(aMsg, 1);
            var v      = cmdMsg.Vectors[0];

            // For now, generate speeds that are similar to the fleshlight
            // launch. The scale is actually far different, but this is better
            // than nothing.
            return(await HandleFleshlightLaunchFW12Cmd(new FleshlightLaunchFW12Cmd(cmdMsg.DeviceIndex,
                                                                                   Convert.ToUInt32(FleshlightHelper.GetSpeed(Math.Abs(_lastPosition - v.Position), v.Duration) * 99),
                                                                                   Convert.ToUInt32(v.Position * 99), cmdMsg.Id), aToken).ConfigureAwait(false));
        }
        private async Task <ButtplugMessage> HandleLinearCmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckGenericMessageHandler <LinearCmd>(aMsg, 1);
            var v      = cmdMsg.Vectors[0];

            return(await HandleFleshlightLaunchFW12Cmd(new FleshlightLaunchFW12Cmd(cmdMsg.DeviceIndex,
                                                                                   Convert.ToUInt32(FleshlightHelper.GetSpeed(Math.Abs(_lastPosition - v.Position), v.Duration) * 99),
                                                                                   Convert.ToUInt32(v.Position * 99), cmdMsg.Id), aToken).ConfigureAwait(false));
        }
Example #7
0
        private async Task <ButtplugMessage> HandleLinearCmd(ButtplugDeviceMessage aMsg, CancellationToken aToken)
        {
            var cmdMsg = CheckMessageHandler <LinearCmd>(aMsg);

            if (cmdMsg.Vectors.Count != 1)
            {
                throw new ButtplugDeviceException(BpLogger,
                                                  "LinearCmd requires 1 vector for this device.",
                                                  cmdMsg.Id);
            }

            foreach (var v in cmdMsg.Vectors)
            {
                if (v.Index != 0)
                {
                    throw new ButtplugDeviceException(BpLogger,
                                                      $"Index {v.Index} is out of bounds for LinearCmd for this device.",
                                                      cmdMsg.Id);
                }

                return(await HandleFleshlightLaunchCmd(new FleshlightLaunchFW12Cmd(cmdMsg.DeviceIndex,
                                                                                   Convert.ToUInt32(FleshlightHelper.GetSpeed(Math.Abs((_position / 100) - v.Position), v.Duration) * 99),
                                                                                   Convert.ToUInt32(v.Position * 99), cmdMsg.Id), aToken).ConfigureAwait(false));
            }

            return(new Ok(aMsg.Id));
        }