11using System ;
2+ using System . Collections . Generic ;
23using System . IO . Ports ;
34using System . Text ;
45using System . Threading . Tasks ;
56using System . Timers ;
67using Buttplug . Core ;
78using Buttplug . Core . Messages ;
9+ using Buttplug . Server . Util ;
810using static Buttplug . Server . Managers . ETSerialManager . ET312Protocol ;
911
1012namespace Buttplug . Server . Managers . ETSerialManager
@@ -69,8 +71,9 @@ public ET312Device(SerialPort port, IButtplugLogManager aLogManager, string name
6971 }
7072
7173 // We're now ready to receive events
72- MsgFuncs . Add ( typeof ( FleshlightLaunchFW12Cmd ) , HandleFleshlightLaunchFW12Cmd ) ;
73- MsgFuncs . Add ( typeof ( StopDeviceCmd ) , HandleStopDeviceCmd ) ;
74+ MsgFuncs . Add ( typeof ( FleshlightLaunchFW12Cmd ) , new ButtplugDeviceWrapper ( HandleFleshlightLaunchFW12Cmd ) ) ;
75+ MsgFuncs . Add ( typeof ( LinearCmd ) , new ButtplugDeviceWrapper ( HandleLinearCmd , new MessageAttributes ( ) { FeatureCount = 1 } ) ) ;
76+ MsgFuncs . Add ( typeof ( StopDeviceCmd ) , new ButtplugDeviceWrapper ( HandleStopDeviceCmd ) ) ;
7477
7578 // Start update timer
7679 _updateInterval = 20 ; // <- Change this value to adjust box update frequency in ms
@@ -209,12 +212,41 @@ private async Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aM
209212 }
210213 }
211214
215+ private async Task < ButtplugMessage > HandleLinearCmd ( ButtplugDeviceMessage aMsg )
216+ {
217+ var cmdMsg = aMsg as LinearCmd ;
218+ if ( cmdMsg is null )
219+ {
220+ return BpLogger . LogErrorMsg ( aMsg . Id , Error . ErrorClass . ERROR_DEVICE , "Wrong Handler" ) ;
221+ }
222+
223+ foreach ( var v in cmdMsg . Vectors )
224+ {
225+ if ( v . Index != 0 )
226+ {
227+ continue ;
228+ }
229+
230+ return await HandleFleshlightLaunchFW12Cmd ( new FleshlightLaunchFW12Cmd ( cmdMsg . DeviceIndex ,
231+ Convert . ToUInt32 ( FleshlightHelper . GetSpeed ( Math . Abs ( ( _position / 100 ) - v . Position ) , v . Duration ) * 99 ) ,
232+ Convert . ToUInt32 ( v . Position * 99 ) , cmdMsg . Id ) ) ;
233+ }
234+
235+ return new Ok ( aMsg . Id ) ;
236+ }
237+
212238 private async Task < ButtplugMessage > HandleFleshlightLaunchFW12Cmd ( ButtplugDeviceMessage aMsg )
213239 {
240+ var cmdMsg = aMsg as FleshlightLaunchFW12Cmd ;
241+ if ( cmdMsg is null )
242+ {
243+ return BpLogger . LogErrorMsg ( aMsg . Id , Error . ErrorClass . ERROR_DEVICE , "Wrong Handler" ) ;
244+ }
245+
214246 lock ( _movementLock )
215247 {
216- _speed = ( aMsg as FleshlightLaunchFW12Cmd ) . Speed ;
217- _position = ( aMsg as FleshlightLaunchFW12Cmd ) . Position ;
248+ _speed = ( Convert . ToDouble ( cmdMsg . Speed ) / 99 ) * 100 ;
249+ _position = ( Convert . ToDouble ( cmdMsg . Position ) / 99 ) * 100 ;
218250
219251 _position = _position < 0 ? 0 : _position ;
220252 _position = _position > 100 ? 100 : _position ;
@@ -223,9 +255,8 @@ private async Task<ButtplugMessage> HandleFleshlightLaunchFW12Cmd(ButtplugDevice
223255
224256 // This is @funjack's algorithm for converting Fleshlight Launch
225257 // commands into absolute distance (percent) / duration (millisecond) values
226- double distance = Math . Abs ( _position - _currentPosition ) ;
227- double mil = Math . Pow ( _speed / 25000 , - 0.95 ) ;
228- double duration = mil / ( 90 / distance ) ;
258+ var distance = Math . Abs ( _position - _currentPosition ) ;
259+ var duration = FleshlightHelper . GetDuration ( distance / 100 , _speed / 100 ) ;
229260
230261 // We convert those into "position" increments for our OnUpdate() timer event.
231262 _increment = 1.5 * ( distance / ( duration / _updateInterval ) ) ;
0 commit comments