Skip to content

Commit 9b56195

Browse files
blackspherefollowerqdot
authored andcommitted
Adding indivdual vibe crontrol support Lovense Edge
1 parent 9f52b02 commit 9b56195

1 file changed

Lines changed: 41 additions & 27 deletions

File tree

Buttplug.Server/Bluetooth/Devices/Lovense.cs

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public IButtplugDevice CreateDevice(IButtplugLogManager aLogManager,
213213

214214
internal class Lovense : ButtplugBluetoothDevice
215215
{
216-
private static Dictionary<string, string> friendlyNames = new Dictionary<string, string>()
216+
private static readonly Dictionary<string, string> FriendlyNames = new Dictionary<string, string>()
217217
{
218218
{ "LVS-A011", "Nora" },
219219
{ "LVS-C011", "Nora" },
@@ -229,22 +229,28 @@ internal class Lovense : ButtplugBluetoothDevice
229229
};
230230

231231
private bool _clockwise = true;
232-
private double _rotateSpeed = 0;
233-
private double _vibratorSpeed = 0;
232+
private double _rotateSpeed;
233+
private uint _vibratorCount = 1;
234+
private double[] _vibratorSpeeds = { 0, 0 };
234235

235236
public Lovense(IButtplugLogManager aLogManager,
236237
IBluetoothDeviceInterface aInterface,
237238
IBluetoothDeviceInfo aInfo)
238239
: base(aLogManager,
239-
$"Lovense Device ({friendlyNames[aInterface.Name]})",
240+
$"Lovense Device ({FriendlyNames[aInterface.Name]})",
240241
aInterface,
241242
aInfo)
242243
{
244+
if (FriendlyNames[aInterface.Name] == "Edge")
245+
{
246+
_vibratorCount++;
247+
}
248+
243249
MsgFuncs.Add(typeof(SingleMotorVibrateCmd), new ButtplugDeviceWrapper(HandleSingleMotorVibrateCmd));
244-
MsgFuncs.Add(typeof(VibrateCmd), new ButtplugDeviceWrapper(HandleVibrateCmd, new MessageAttributes() { FeatureCount = 1 }));
250+
MsgFuncs.Add(typeof(VibrateCmd), new ButtplugDeviceWrapper(HandleVibrateCmd, new MessageAttributes() { FeatureCount = _vibratorCount }));
245251
MsgFuncs.Add(typeof(StopDeviceCmd), new ButtplugDeviceWrapper(HandleStopDeviceCmd));
246252

247-
if (friendlyNames[aInterface.Name] == "Nora")
253+
if (FriendlyNames[aInterface.Name] == "Nora")
248254
{
249255
MsgFuncs.Add(typeof(RotateCmd), new ButtplugDeviceWrapper(HandleRotateCmd, new MessageAttributes() { FeatureCount = 1 }));
250256
}
@@ -254,7 +260,7 @@ private async Task<ButtplugMessage> HandleStopDeviceCmd(ButtplugDeviceMessage aM
254260
{
255261
BpLogger.Debug("Stopping Device " + Name);
256262

257-
if (friendlyNames[Interface.Name] == "Nora")
263+
if (FriendlyNames[Interface.Name] == "Nora")
258264
{
259265
await Interface.WriteValue(aMsg.Id,
260266
Info.Characteristics[(uint)LovenseRev1BluetoothInfo.Chrs.Tx],
@@ -266,61 +272,69 @@ await Interface.WriteValue(aMsg.Id,
266272

267273
private async Task<ButtplugMessage> HandleSingleMotorVibrateCmd(ButtplugDeviceMessage aMsg)
268274
{
269-
var cmdMsg = aMsg as SingleMotorVibrateCmd;
270-
if (cmdMsg is null)
275+
if (!(aMsg is SingleMotorVibrateCmd cmdMsg))
271276
{
272277
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
273278
}
274279

275-
return await HandleVibrateCmd(new VibrateCmd(cmdMsg.DeviceIndex,
276-
new List<VibrateCmd.VibrateSubcommand>() { new VibrateCmd.VibrateSubcommand(0, cmdMsg.Speed) },
277-
cmdMsg.Id));
280+
var speeds = new List<VibrateCmd.VibrateSubcommand>();
281+
for (uint i = 0; i < _vibratorCount; i++)
282+
{
283+
speeds.Add(new VibrateCmd.VibrateSubcommand(i, cmdMsg.Speed));
284+
}
285+
286+
return await HandleVibrateCmd(new VibrateCmd(cmdMsg.DeviceIndex, speeds, cmdMsg.Id));
278287
}
279288

280289
private async Task<ButtplugMessage> HandleVibrateCmd(ButtplugDeviceMessage aMsg)
281290
{
282-
var cmdMsg = aMsg as VibrateCmd;
283-
if (cmdMsg is null)
291+
if (!(aMsg is VibrateCmd cmdMsg))
284292
{
285293
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
286294
}
287295

288-
if (cmdMsg.Speeds.Count != 1)
296+
if (cmdMsg.Speeds.Count == 0 || cmdMsg.Speeds.Count > _vibratorCount)
289297
{
290298
return new Error(
291-
"VibrateCmd requires 1 vector for this device.",
299+
_vibratorCount == 1 ? "VibrateCmd requires 1 vector for this device." :
300+
$"VibrateCmd requires between 1 and {_vibratorCount} vectors for this device.",
292301
Error.ErrorClass.ERROR_DEVICE,
293302
cmdMsg.Id);
294303
}
295304

296305
foreach (var v in cmdMsg.Speeds)
297306
{
298-
if (v.Index != 0)
307+
if (v.Index >= _vibratorCount)
299308
{
300309
return new Error(
301310
$"Index {v.Index} is out of bounds for VibrateCmd for this device.",
302311
Error.ErrorClass.ERROR_DEVICE,
303312
cmdMsg.Id);
304313
}
305314

306-
if (v.Speed == _vibratorSpeed)
315+
if (Math.Abs(v.Speed - _vibratorSpeeds[v.Index]) < 0.0001)
307316
{
308-
return new Ok(cmdMsg.Id);
317+
continue;
309318
}
310319

311-
_vibratorSpeed = v.Speed;
320+
_vibratorSpeeds[v.Index] = v.Speed;
321+
var vId = _vibratorCount == 1 ? string.Empty : string.Empty + (v.Index + 1);
322+
var res = await Interface.WriteValue(aMsg.Id,
323+
Info.Characteristics[(uint)LovenseRev1BluetoothInfo.Chrs.Tx],
324+
Encoding.ASCII.GetBytes($"Vibrate{vId}:{(int)(_vibratorSpeeds[v.Index] * 20)};"));
325+
326+
if ((res as Ok) == null)
327+
{
328+
return res;
329+
}
312330
}
313331

314-
// While there are 3 lovense revs right now, all of the characteristic arrays are the same.
315-
return await Interface.WriteValue(aMsg.Id,
316-
Info.Characteristics[(uint)LovenseRev1BluetoothInfo.Chrs.Tx],
317-
Encoding.ASCII.GetBytes($"Vibrate:{(int)(_vibratorSpeed * 20)};"));
332+
return new Ok(cmdMsg.Id);
318333
}
319334

320335
private async Task<ButtplugMessage> HandleRotateCmd(ButtplugDeviceMessage aMsg)
321336
{
322-
var cmdMsg = aMsg as RotateCmd;
323-
if (cmdMsg is null)
337+
if (!(aMsg is RotateCmd cmdMsg))
324338
{
325339
return BpLogger.LogErrorMsg(aMsg.Id, Error.ErrorClass.ERROR_DEVICE, "Wrong Handler");
326340
}
@@ -346,7 +360,7 @@ private async Task<ButtplugMessage> HandleRotateCmd(ButtplugDeviceMessage aMsg)
346360
cmdMsg.Id);
347361
}
348362

349-
speedChange = _rotateSpeed != vi.Speed;
363+
speedChange = Math.Abs(_rotateSpeed - vi.Speed) > 0.0001;
350364
_rotateSpeed = vi.Speed;
351365
dirChange = _clockwise != vi.Clockwise;
352366
}

0 commit comments

Comments
 (0)