Skip to content

Commit 66488d5

Browse files
blackspherefollowerqdot
authored andcommitted
Updating the Simulator to support the generic commands
This is particularly useful for guaging the difference between the FleshlightLaunch raw command and the new LinearCmd.
1 parent e8f2c86 commit 66488d5

8 files changed

Lines changed: 257 additions & 96 deletions

File tree

Buttplug.Apps.DeviceSimulatorGUI/Buttplug.Apps.DeviceSimulatorGUI.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@
5454
<Reference Include="System.Xaml">
5555
<RequiredTargetFramework>4.0</RequiredTargetFramework>
5656
</Reference>
57+
<Reference Include="TAlex.WPF.Controls, Version=2.0.1.0, Culture=neutral, processorArchitecture=MSIL">
58+
<HintPath>..\packages\TAlex.WPF.Controls.2.0.1.0\lib\net40\TAlex.WPF.Controls.dll</HintPath>
59+
</Reference>
5760
<Reference Include="WindowsBase" />
5861
</ItemGroup>
5962
<ItemGroup>
@@ -69,6 +72,9 @@
6972
<Generator>MSBuild:Compile</Generator>
7073
<SubType>Designer</SubType>
7174
</Page>
75+
<Compile Include="..\Buttplug.Server\Util\FleshlightHelper.cs">
76+
<Link>FleshlightHelper.cs</Link>
77+
</Compile>
7278
<Compile Include="App.xaml.cs">
7379
<DependentUpon>App.xaml</DependentUpon>
7480
<SubType>Code</SubType>

Buttplug.Apps.DeviceSimulatorGUI/DeviceSimulator.xaml

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,52 @@
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
6-
mc:Ignorable="d"
6+
xmlns:devicesimulatorgui="clr-namespace:Buttplug.Apps.DeviceSimulatorGUI"
7+
xmlns:talex="http://schemas.talex-soft.com/2010/xaml/presentation"
8+
mc:Ignorable="d"
79
d:DesignHeight="400" d:DesignWidth="600">
8-
<Grid Background="#FFE5E5E5">
9-
<Grid.RowDefinitions>
10-
<RowDefinition Height="auto"/>
11-
<RowDefinition Height="auto"/>
12-
<RowDefinition Height="auto"/>
13-
<RowDefinition Height="auto"/>
14-
<RowDefinition Height="auto"/>
15-
<RowDefinition Height="auto"/>
16-
<RowDefinition Height="auto"/>
17-
<RowDefinition Height="auto"/>
18-
<RowDefinition Height="auto"/>
19-
</Grid.RowDefinitions>
20-
<Grid.ColumnDefinitions>
21-
<ColumnDefinition Width="auto"/>
22-
<ColumnDefinition Width="*"/>
23-
<ColumnDefinition Width="auto"/>
24-
</Grid.ColumnDefinitions>
25-
26-
<Label Content="DeviceName" Grid.Column="0" Grid.Row="0"/>
27-
<TextBox Name="DeviceName" Grid.Column="1" Grid.Row="0"/>
28-
29-
<Label Content="DeviceId" Grid.Column="0" Grid.Row="1"/>
30-
<TextBox Name="DeviceId" Grid.Column="1" Grid.Row="1"/>
10+
<ScrollViewer VerticalScrollBarVisibility="Auto">
11+
<Grid Background="#FFE5E5E5">
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="auto"/>
14+
<RowDefinition Height="auto"/>
15+
<RowDefinition Height="auto"/>
16+
<RowDefinition Height="auto"/>
17+
<RowDefinition Height="auto"/>
18+
<RowDefinition Height="auto"/>
19+
<RowDefinition Height="auto"/>
20+
<RowDefinition Height="auto"/>
21+
<RowDefinition Height="auto"/>
22+
</Grid.RowDefinitions>
23+
<Grid.ColumnDefinitions>
24+
<ColumnDefinition Width="auto"/>
25+
<ColumnDefinition Width="*"/>
26+
<ColumnDefinition Width="auto"/>
27+
</Grid.ColumnDefinitions>
3128

32-
<CheckBox Name="DeviceHasLinear" Content="Linear" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/>
33-
<Label Content="Linear Position" Grid.Column="0" Grid.Row="3"/>
34-
<ProgressBar Name="LinearPosition" Grid.Column="1" Grid.Row="3"/>
29+
<Label Content="DeviceName" Grid.Column="0" Grid.Row="0"/>
30+
<TextBox Name="DeviceName" Grid.Column="1" Grid.Row="0"/>
3531

36-
<CheckBox Name="DeviceHasVibrator" Content="Vibrator" Grid.Column="0" Grid.Row="4" Grid.ColumnSpan="2"/>
37-
<Label Content="Vibrator Speed" Grid.Column="0" Grid.Row="5"/>
38-
<ProgressBar Name="VibratorSpeed" Grid.Column="1" Grid.Row="5"/>
32+
<Label Content="DeviceId" Grid.Column="0" Grid.Row="1"/>
33+
<TextBox Name="DeviceId" Grid.Column="1" Grid.Row="1"/>
3934

40-
<CheckBox Name="DeviceHasRotator" Content="Rotator" Grid.Column="0" Grid.Row="6" Grid.ColumnSpan="2"/>
41-
<Label Content="Rotatot Speed" Grid.Column="0" Grid.Row="7"/>
42-
<ProgressBar Name="RotatorSpeed" Grid.Column="1" Grid.Row="7"/>
35+
<CheckBox Name="DeviceHasLinear" Content="Linear" Grid.Column="0" Grid.Row="2" Grid.ColumnSpan="2"/>
36+
<Label Content="Linear Position" Grid.Column="0" Grid.Row="3"/>
37+
<ProgressBar Name="LinearPosition" Grid.Column="1" Grid.Row="3"/>
4338

44-
</Grid>
39+
<Label Content="Vibrators" Grid.Column="0" Grid.Row="4"/>
40+
<talex:NumericUpDown Name="DeviceHasVibrator" Grid.Column="1" Grid.Row="4" Width="50px" HorizontalAlignment="Left" ValueChanged="DeviceHasVibrator_ValueChanged"/>
41+
<Grid Name="Vibrators" Grid.Column="0" Grid.Row="5" Grid.ColumnSpan="2" Margin="5">
42+
<Grid.ColumnDefinitions>
43+
<ColumnDefinition Width="auto"/>
44+
<ColumnDefinition Width="*"/>
45+
</Grid.ColumnDefinitions>
46+
</Grid>
47+
48+
<CheckBox Name="DeviceHasRotator" Content="Rotator" Grid.Column="0" Grid.Row="6" Grid.ColumnSpan="2"/>
49+
<Label Content="Rotatot Speed" Grid.Column="0" Grid.Row="7"/>
50+
<ProgressBar Name="RotatorSpeed" Grid.Column="1" Grid.Row="7"/>
51+
52+
</Grid>
53+
</ScrollViewer>
4554
</UserControl>

Buttplug.Apps.DeviceSimulatorGUI/DeviceSimulator.xaml.cs

Lines changed: 103 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System;
1+
using Buttplug.Server.Util;
2+
using System;
3+
using System.Collections.Generic;
24
using System.Threading;
35
using System.Threading.Tasks;
46
using System.Windows;
@@ -19,22 +21,23 @@ public partial class DeviceSimulator
1921

2022
public bool HasLinear;
2123

22-
public bool HasVibrator;
24+
public uint VibratorCount = 0;
2325

2426
public bool HasRotator;
2527

26-
internal uint LinearTargetPosition;
28+
internal double LinearTargetPosition;
2729

2830
internal double LinearCurrentPosition;
2931

3032
internal long LinearCurrentTime;
3133

32-
internal uint LinearSpeed;
34+
internal double LinearSpeed;
3335

3436
private Task _posThread;
3537

3638
private CancellationTokenSource _tokenSource;
37-
private double _linearPosMax;
39+
40+
private Dictionary<uint, VibratorRow> _vibrators = new Dictionary<uint, VibratorRow>();
3841

3942
public DeviceSimulator(int aTabId)
4043
{
@@ -44,12 +47,11 @@ public DeviceSimulator(int aTabId)
4447
Name = Id = DeviceId.Text = DeviceName.Text;
4548
DeviceId.TextChanged += IdChangedEventHandler;
4649
DeviceName.TextChanged += NameChangedEventHandler;
47-
HasRotator = HasVibrator = HasLinear = false;
50+
HasRotator = HasLinear = false;
4851
DeviceHasLinear.Checked += LinearCheckedEventHandler;
49-
DeviceHasVibrator.Checked += VibratorCheckedEventHandler;
5052
DeviceHasRotator.Checked += RotatorCheckedEventHandler;
5153

52-
_linearPosMax = LinearPosition.Maximum;
54+
LinearCurrentPosition = 0;
5355

5456
_tokenSource = new CancellationTokenSource();
5557
_posThread = new Task(() => { posUpdater(_tokenSource.Token); }, _tokenSource.Token, TaskCreationOptions.LongRunning);
@@ -64,7 +66,7 @@ private void posUpdater(CancellationToken aCancellationToken)
6466
var now = DateTime.Now.Ticks;
6567
if (LinearCurrentPosition != LinearTargetPosition)
6668
{
67-
var diff = distance(now - LinearCurrentTime, LinearSpeed) * _linearPosMax;
69+
var diff = FleshlightHelper.GetDistance(Convert.ToUInt32(new TimeSpan(now - LinearCurrentTime).TotalMilliseconds), LinearSpeed);
6870
var diff2 = LinearTargetPosition - LinearCurrentPosition;
6971
if (diff2 < 0)
7072
{
@@ -80,21 +82,15 @@ private void posUpdater(CancellationToken aCancellationToken)
8082

8183
Dispatcher.Invoke(() =>
8284
{
83-
LinearPosition.Value = LinearCurrentPosition;
85+
LinearPosition.Value = LinearCurrentPosition * LinearPosition.Maximum;
8486
});
8587
}
88+
8689
LinearCurrentTime = now;
8790
Thread.Sleep(10);
8891
}
8992
}
9093

91-
private double distance(long time, double speed)
92-
{
93-
var mil = Math.Pow(speed / 25000, -0.95);
94-
var diff = mil - (Convert.ToDouble(time) / 1e6);
95-
return 90 - ((diff / mil) * 90);
96-
}
97-
9894
private void IdChangedEventHandler(object o, TextChangedEventArgs args)
9995
{
10096
Id = DeviceId.Text;
@@ -110,14 +106,100 @@ private void LinearCheckedEventHandler(object o, RoutedEventArgs args)
110106
HasLinear = DeviceHasLinear.IsChecked.GetValueOrDefault(false);
111107
}
112108

113-
private void VibratorCheckedEventHandler(object o, RoutedEventArgs args)
109+
private void RotatorCheckedEventHandler(object o, RoutedEventArgs args)
114110
{
115-
HasVibrator = DeviceHasVibrator.IsChecked.GetValueOrDefault(false);
111+
HasRotator = DeviceHasRotator.IsChecked.GetValueOrDefault(false);
116112
}
117113

118-
private void RotatorCheckedEventHandler(object o, RoutedEventArgs args)
114+
private class VibratorRow
119115
{
120-
HasRotator = DeviceHasRotator.IsChecked.GetValueOrDefault(false);
116+
public Label RowLabel;
117+
public ProgressBar RowProgress;
118+
119+
public VibratorRow(int vId)
120+
{
121+
RowLabel = new Label();
122+
RowLabel.Name = "VibratorLabel" + vId;
123+
RowLabel.Content = "Vibrator" + vId + " Speed:";
124+
RowProgress = new ProgressBar();
125+
RowProgress.Name = "VibratorSpeed" + vId;
126+
RowProgress.Maximum = 1;
127+
128+
Grid.SetRow(RowLabel, vId);
129+
Grid.SetColumn(RowLabel, 0);
130+
Grid.SetRow(RowProgress, vId);
131+
Grid.SetColumn(RowProgress, 1);
132+
}
133+
}
134+
135+
private void DeviceHasVibrator_ValueChanged(object sender, RoutedPropertyChangedEventArgs<decimal> e)
136+
{
137+
// Lock
138+
DeviceHasVibrator.IsEnabled = false;
139+
140+
while (e.NewValue > VibratorCount)
141+
{
142+
// Add a vibe row
143+
var vId = VibratorCount++;
144+
var rowDef = new RowDefinition();
145+
rowDef.Height = GridLength.Auto;
146+
Vibrators.RowDefinitions.Add(rowDef);
147+
148+
var row = new VibratorRow((int)vId);
149+
Vibrators.Children.Add(row.RowLabel);
150+
Vibrators.Children.Add(row.RowProgress);
151+
_vibrators.Add(vId, row);
152+
}
153+
154+
while (e.NewValue < VibratorCount)
155+
{
156+
// Remove a vibe row
157+
var vId = --VibratorCount;
158+
159+
if (_vibrators.TryGetValue(vId, out var row))
160+
{
161+
Vibrators.Children.Remove(row.RowLabel);
162+
Vibrators.Children.Remove(row.RowProgress);
163+
}
164+
165+
Vibrators.RowDefinitions.RemoveAt((int)vId);
166+
_vibrators.Remove(vId);
167+
}
168+
169+
// Unlock
170+
DeviceHasVibrator.IsEnabled = true;
171+
}
172+
173+
public void StopDevice()
174+
{
175+
Dispatcher.Invoke(() =>
176+
{
177+
foreach (var j in _vibrators)
178+
{
179+
j.Value.RowProgress.Value = 0;
180+
}
181+
182+
if (DeviceHasRotator.IsChecked.GetValueOrDefault(false))
183+
{
184+
RotatorSpeed.Value = 0;
185+
}
186+
187+
if (DeviceHasLinear.IsChecked.GetValueOrDefault(false))
188+
{
189+
// Complicated stuff: position stays the same
190+
}
191+
});
192+
}
193+
194+
public void Vibrate(uint aId, double aSpeed)
195+
{
196+
Dispatcher.Invoke(() =>
197+
{
198+
if (_vibrators.TryGetValue(aId, out var row))
199+
{
200+
row.RowProgress.Value = Math.Min(aSpeed, 1) * row.RowProgress.Maximum;
201+
}
202+
});
121203
}
122204
}
123205
}

Buttplug.Apps.DeviceSimulatorGUI/MainWindow.xaml.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Windows.Controls;
1010
using System.Windows.Media;
1111
using Buttplug.DeviceSimulator.PipeMessages;
12+
using Buttplug.Server.Util;
1213

1314
namespace Buttplug.Apps.DeviceSimulatorGUI
1415
{
@@ -113,6 +114,7 @@ private void pipeReader(CancellationToken aCancellationToken)
113114
}
114115
}
115116

117+
Console.Out.WriteLine(msg);
116118
var parsed = _parser.Deserialize(msg);
117119
if (parsed == null)
118120
{
@@ -128,7 +130,7 @@ private void pipeReader(CancellationToken aCancellationToken)
128130
devAdded.Name = dev.Name;
129131
devAdded.Id = dev.Id;
130132
devAdded.HasLinear = dev.HasLinear;
131-
devAdded.HasVibrator = dev.HasVibrator;
133+
devAdded.VibratorCount = dev.VibratorCount;
132134
devAdded.HasRotator = dev.HasRotator;
133135

134136
_msgQueue.Enqueue(devAdded);
@@ -148,21 +150,7 @@ private void pipeReader(CancellationToken aCancellationToken)
148150
return;
149151
}
150152

151-
Dispatcher.Invoke(() =>
152-
{
153-
if (dev.DeviceHasVibrator.IsChecked.GetValueOrDefault(false))
154-
{
155-
dev.VibratorSpeed.Value = 0;
156-
}
157-
if (dev.DeviceHasRotator.IsChecked.GetValueOrDefault(false))
158-
{
159-
dev.RotatorSpeed.Value = 0;
160-
}
161-
if (dev.DeviceHasLinear.IsChecked.GetValueOrDefault(false))
162-
{
163-
// Complicated stuff: position stays the same
164-
}
165-
});
153+
dev.StopDevice();
166154
}
167155

168156
break;
@@ -175,18 +163,29 @@ private void pipeReader(CancellationToken aCancellationToken)
175163
return;
176164
}
177165

178-
if (dev.HasVibrator)
166+
dev.Vibrate(v.VibratorId, v.Speed);
167+
}
168+
169+
break;
170+
171+
case Linear l:
172+
foreach (DeviceSimulator dev in tabs.Values)
173+
{
174+
if (dev == null || dev.Id != l.Id)
179175
{
180-
Dispatcher.Invoke(() =>
181-
{
182-
dev.VibratorSpeed.Value = Math.Min(v.Speed, 1) * dev.VibratorSpeed.Maximum;
183-
});
176+
return;
177+
}
178+
179+
if (dev.HasLinear)
180+
{
181+
dev.LinearTargetPosition = l.Position;
182+
dev.LinearSpeed = l.Speed;
184183
}
185184
}
186185

187186
break;
188187

189-
case Linear l:
188+
case Linear2 l:
190189
foreach (DeviceSimulator dev in tabs.Values)
191190
{
192191
if (dev == null || dev.Id != l.Id)
@@ -196,8 +195,9 @@ private void pipeReader(CancellationToken aCancellationToken)
196195

197196
if (dev.HasLinear)
198197
{
199-
dev.LinearTargetPosition = Math.Min(l.Position, 99);
200-
dev.LinearSpeed = Math.Min(l.Speed, 99);
198+
double dist = Math.Abs(dev.LinearTargetPosition - l.Position);
199+
dev.LinearSpeed = FleshlightHelper.GetSpeed(dist, l.Duration);
200+
dev.LinearTargetPosition = l.Position;
201201
}
202202
}
203203

0 commit comments

Comments
 (0)