|
| 1 | +usingSystem; |
| 2 | +usingSystem.Linq; |
| 3 | +usingSystem.Threading; |
| 4 | +usingSystem.Threading.Tasks; |
| 5 | +usingButtplug.Core.Logging; |
| 6 | +usingButtplug.Core.Messages; |
| 7 | + |
| 8 | +namespaceButtplug.Devices.Protocols |
| 9 | +{ |
| 10 | +internalclassMotorbunnyProtocol:ButtplugDeviceProtocol |
| 11 | +{ |
| 12 | +privatedouble_vibratorSpeed; |
| 13 | + |
| 14 | +publicMotorbunnyProtocol(IButtplugLogManageraLogManager, |
| 15 | +IButtplugDeviceImplaInterface) |
| 16 | +:base(aLogManager, |
| 17 | +"Motorbunny", |
| 18 | +aInterface) |
| 19 | +{ |
| 20 | +AddMessageHandler<SingleMotorVibrateCmd>(HandleSingleMotorVibrateCmd); |
| 21 | +AddMessageHandler<VibrateCmd>(HandleVibrateCmd,newMessageAttributes(){FeatureCount=1}); |
| 22 | +AddMessageHandler<StopDeviceCmd>(HandleStopDeviceCmd); |
| 23 | +} |
| 24 | + |
| 25 | +privateasyncTask<ButtplugMessage>HandleStopDeviceCmd(ButtplugDeviceMessageaMsg,CancellationTokenaToken) |
| 26 | +{ |
| 27 | +BpLogger.Debug("Stopping Device "+Name); |
| 28 | +returnawaitHandleSingleMotorVibrateCmd(newSingleMotorVibrateCmd(aMsg.DeviceIndex,0,aMsg.Id),aToken).ConfigureAwait(false); |
| 29 | +} |
| 30 | + |
| 31 | +privateasyncTask<ButtplugMessage>HandleSingleMotorVibrateCmd(ButtplugDeviceMessageaMsg,CancellationTokenaToken) |
| 32 | +{ |
| 33 | +varcmdMsg=CheckMessageHandler<SingleMotorVibrateCmd>(aMsg); |
| 34 | + |
| 35 | +returnawaitHandleVibrateCmd(VibrateCmd.Create(cmdMsg.DeviceIndex,cmdMsg.Id,cmdMsg.Speed,1),aToken).ConfigureAwait(false); |
| 36 | +} |
| 37 | + |
| 38 | +privateasyncTask<ButtplugMessage>HandleVibrateCmd(ButtplugDeviceMessageaMsg,CancellationTokenaToken) |
| 39 | +{ |
| 40 | +varcmdMsg=CheckGenericMessageHandler<VibrateCmd>(aMsg,1); |
| 41 | +varv=cmdMsg.Speeds[0]; |
| 42 | + |
| 43 | +if(Math.Abs(v.Speed-_vibratorSpeed)<0.001&&SentVibration) |
| 44 | +{ |
| 45 | +returnnewOk(cmdMsg.Id); |
| 46 | +} |
| 47 | + |
| 48 | +SentVibration=true; |
| 49 | + |
| 50 | +// If speed is zero, send the magic stop packet, otherwise *weird* things happen. |
| 51 | +if((cmdMsg.Speeds[0].Speed*255)<5) |
| 52 | +{ |
| 53 | +awaitInterface.WriteValueAsync(newbyte[]{0xf0,0x00,0x00,0x00,0x00,0xec},aToken).ConfigureAwait(false); |
| 54 | +returnnewOk(aMsg.Id); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +varcmdData=newbyte[]{0xff}; |
| 59 | + |
| 60 | +for(vari=0;i<7;++i) |
| 61 | +{ |
| 62 | +cmdData=cmdData.Concat(newbyte[]{(byte)(cmdMsg.Speeds[0].Speed*255),0x14}).ToArray(); |
| 63 | +} |
| 64 | + |
| 65 | +bytecrc=0; |
| 66 | + |
| 67 | +// Skip the first byte for the CRC |
| 68 | +for(varj=1;j<cmdData.Length;++j) |
| 69 | +{ |
| 70 | +crc=(byte)(cmdData[j]+crc); |
| 71 | +} |
| 72 | + |
| 73 | +cmdData=cmdData.Concat(newbyte[]{crc,0xec}).ToArray(); |
| 74 | + |
| 75 | +awaitInterface.WriteValueAsync(cmdData,aToken).ConfigureAwait(false); |
| 76 | +returnnewOk(aMsg.Id); |
| 77 | +} |
| 78 | +} |
| 79 | +} |
0 commit comments