@@ -11,34 +11,37 @@ internal class LiBoBluetoothInfo : IBluetoothDeviceInfo
1111 {
1212 public enum Chrs : uint
1313 {
14- Tx1 = 0 ,
15- Tx2 ,
16- Rx ,
14+ WriteShock = 0 ,
15+ WriteVibrate ,
16+ ReadBattery ,
1717 }
1818
1919 public Guid [ ] Services { get ; } =
2020 {
2121 new Guid ( "00006000-0000-1000-8000-00805f9b34fb" ) , // Write Service
22- new Guid ( "00006050-0000-1000-8000-00805f9b34fb" ) , // Read service (battery)
2322
24- // I'm pretty sure that the 2nd one here is ignored due to a call to Services.First() in the bluetooth manager
23+ // TODO Commenting out battery service until we can handle multiple services.
24+
25+ // new Guid("00006050-0000-1000-8000-00805f9b34fb"), // Read service (battery)
2526 } ;
2627
28+ public string [ ] NamePrefixes { get ; } = { } ;
29+
2730 public string [ ] Names { get ; } =
2831 {
2932 "PiPiJing"
3033 } ;
3134
32- public Guid [ ] Characteristics { get ; } =
35+ public Dictionary < uint , Guid > Characteristics { get ; } = new Dictionary < uint , Guid > ( )
3336 {
3437 // tx1 characteristic
35- new Guid ( "00006001-0000-1000-8000-00805f9b34fb" ) , // Shock
38+ { ( uint ) Chrs . WriteShock , new Guid ( "00006001-0000-1000-8000-00805f9b34fb" ) } , // Shock
3639
3740 // tx2 characteristic
38- new Guid ( "00006002-0000-1000-8000-00805f9b34fb" ) , // VibeMode
41+ { ( uint ) Chrs . WriteVibrate , new Guid ( "00006002-0000-1000-8000-00805f9b34fb" ) } , // VibeMode
3942
4043 // rx characteristic
41- new Guid ( "00006051-0000-1000-8000-00805f9b34fb" ) , // Read for battery level
44+ { ( uint ) Chrs . ReadBattery , new Guid ( "00006051-0000-1000-8000-00805f9b34fb" ) } , // Read for battery level
4245 } ;
4346
4447 public IButtplugDevice CreateDevice ( IButtplugLogManager aLogManager ,
@@ -51,21 +54,20 @@ public IButtplugDevice CreateDevice(IButtplugLogManager aLogManager,
5154 internal class LiBo : ButtplugBluetoothDevice
5255 {
5356 private readonly uint _vibratorCount = 1 ;
54- private readonly uint _shockerCount = 1 ;
5557 private readonly double [ ] _vibratorSpeed = { 0 } ;
56- private readonly double [ ] _shockerIntensity = { 0 } ;
5758
5859 public LiBo ( IButtplugLogManager aLogManager ,
5960 IBluetoothDeviceInterface aInterface ,
6061 IBluetoothDeviceInfo aInfo )
6162 : base ( aLogManager ,
62- $ "LiBo Device ({ aInterface . Name } )",
63+ $ "LiBo ({ aInterface . Name } )",
6364 aInterface ,
6465 aInfo )
6566 {
6667 MsgFuncs . Add ( typeof ( SingleMotorVibrateCmd ) , new ButtplugDeviceWrapper ( HandleSingleMotorVibrateCmd ) ) ;
6768 MsgFuncs . Add ( typeof ( VibrateCmd ) , new ButtplugDeviceWrapper ( HandleVibrateCmd , new MessageAttributes ( ) { FeatureCount = _vibratorCount } ) ) ;
68- // Here goes a handler for Estim shocking
69+
70+ // TODO Add a handler for Estim shocking, add a battery handler.
6971 MsgFuncs . Add ( typeof ( StopDeviceCmd ) , new ButtplugDeviceWrapper ( HandleStopDeviceCmd ) ) ;
7072 }
7173
@@ -81,13 +83,7 @@ private async Task<ButtplugMessage> HandleSingleMotorVibrateCmd(ButtplugDeviceMe
8183 return BpLogger . LogErrorMsg ( aMsg . Id , Error . ErrorClass . ERROR_DEVICE , "Wrong Handler" ) ;
8284 }
8385
84- var subCmds = new List < VibrateCmd . VibrateSubcommand > ( ) ;
85- for ( var i = 0u ; i < _vibratorCount ; i ++ )
86- {
87- subCmds . Add ( new VibrateCmd . VibrateSubcommand ( i , cmdMsg . Speed ) ) ;
88- }
89-
90- return await HandleVibrateCmd ( new VibrateCmd ( cmdMsg . DeviceIndex , subCmds , cmdMsg . Id ) ) ;
86+ return await HandleVibrateCmd ( VibrateCmd . Create ( cmdMsg . DeviceIndex , cmdMsg . Id , cmdMsg . Speed , _vibratorCount ) ) ;
9187 }
9288
9389 private async Task < ButtplugMessage > HandleVibrateCmd ( ButtplugDeviceMessage aMsg )
@@ -97,7 +93,7 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
9793 return BpLogger . LogErrorMsg ( aMsg . Id , Error . ErrorClass . ERROR_DEVICE , "Wrong Handler" ) ;
9894 }
9995
100- if ( cmdMsg . Speeds . Count < 1 || cmdMsg . Speeds . Count > _vibratorCount )
96+ if ( cmdMsg . Speeds . Count != _vibratorCount )
10197 {
10298 return new Error (
10399 $ "VibrateCmd requires between 1 and { _vibratorCount } vectors for this device.",
@@ -130,77 +126,15 @@ private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
130126 return new Ok ( cmdMsg . Id ) ;
131127 }
132128
133- int mode = ( int ) Math . Ceiling ( _vibratorSpeed [ 0 ] * 3 ) ; // 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
129+ // Map a 0 - 100% value to a 0 - 3 value since 0 * x == 0 this will turn off the vibe if
130+ // speed is 0.00
131+ int mode = ( int ) Math . Ceiling ( _vibratorSpeed [ 0 ] * 3 ) ;
134132
135133 var data = new byte [ ] { Convert . ToByte ( mode ) } ;
136134
137135 return await Interface . WriteValue ( aMsg . Id ,
138- Info . Characteristics [ ( uint ) LiBoBluetoothInfo . Chrs . Tx2 ] ,
139- data ) ;
140- }
141-
142- private int getBatteryLevel ( )
143- {
144- throw new NotImplementedException ( "While most of this method is done it still needs to be adjusted/completed - see the comments" ) ;
145- int batteryLevel = Convert . ToInt32 ( 0x64 /* 100 in hex */ ) ; // Read from Info.Services[1] + Info.Characteristics[uint)LiBoBluetoothInfo.Chrs.Rx] here
146- return batteryLevel ; // As far as I can tell we currently have no way of reading values
147- }
148-
149- private async Task < ButtplugMessage > HandleShockCmd ( ButtplugDeviceMessage aMsg )
150- {
151- throw new NotImplementedException ( "While most of this method is done it still needs to be adjusted/completed - see the comments" ) ;
152- if ( ! ( aMsg is VibrateCmd cmdMsg ) ) // ShockCmd once it gets implemented
153- {
154- return BpLogger . LogErrorMsg ( aMsg . Id , Error . ErrorClass . ERROR_DEVICE , "Wrong Handler" ) ;
155- }
156-
157- if ( cmdMsg . Speeds . Count < 1 || cmdMsg . Speeds . Count > _shockerCount )
158- {
159- return new Error (
160- $ "ShockCmd requires between 1 and { _shockerCount } vectors for this device.", // Fix cmd name once settled
161- Error . ErrorClass . ERROR_DEVICE ,
162- cmdMsg . Id ) ;
163- }
164-
165- var changed = false ;
166- foreach ( var v in cmdMsg . Speeds )
167- {
168- if ( v . Index >= _shockerCount )
169- {
170- return new Error (
171- $ "Index { v . Index } is out of bounds for VibrateCmd for this device.", // Fix cmd name once settled
172- Error . ErrorClass . ERROR_DEVICE ,
173- cmdMsg . Id ) ;
174- }
175-
176- if ( ! ( Math . Abs ( v . Speed - _shockerIntensity [ v . Index ] ) > 0.001 ) )
177- {
178- continue ;
179- }
180-
181- changed = true ;
182- _shockerIntensity [ v . Index ] = v . Speed ;
183- }
184-
185- if ( ! changed )
186- {
187- return new Ok ( cmdMsg . Id ) ;
188- }
189-
190- int speed = 0 ;
191-
192- // If intensity is set to 0 skip this with 0x00 and turn the shocker off
193- if ( _shockerIntensity [ 0 ] > 0 )
194- {
195- speed = ( int ) Math . Floor ( _shockerIntensity [ 0 ] * 7 ) << 4 ; // Map a 0 - 100% value to a 0 - 6 value shift 4 to have 0xY0 where Y is speed
196- speed ++ ; // add 1 to speed to get 0xY1 where Y is speed and 1 is mode 1 (constant shock)
197- }
198-
199- var data = new byte [ ] { Convert . ToByte ( speed ) } ;
200-
201- return await Interface . WriteValue ( aMsg . Id ,
202- Info . Characteristics [ ( uint ) LiBoBluetoothInfo . Chrs . Tx1 ] ,
136+ ( uint ) LiBoBluetoothInfo . Chrs . WriteVibrate ,
203137 data ) ;
204138 }
205139 }
206- }
140+ }
0 commit comments