-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainForm.cs
391 lines (341 loc) · 13.3 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows.Forms;
using Tobii.Eyetracking.Sdk;
using Tobii.Eyetracking.Sdk.Exceptions;
using Tobii.Eyetracking.Sdk.Time;
namespace SDK_Basic_Eyetracking_Sample
{
public partial class MainForm : Form
{
private readonly EyetrackerBrowser _trackerBrowser;
private readonly Clock _clock;
private IEyetracker _connectedTracker;
private SyncManager _syncManager;
private string _connectionName;
private bool _isTracking;
public MainForm()
{
InitializeComponent();
_clock = new Clock();
_trackerBrowser = new EyetrackerBrowser();
_trackerBrowser.EyetrackerFound += EyetrackerFound;
_trackerBrowser.EyetrackerUpdated += EyetrackerUpdated;
_trackerBrowser.EyetrackerRemoved += EyetrackerRemoved;
}
private void EyetrackerFound(object sender, EyetrackerInfoEventArgs e)
{
// When an eyetracker is found on the network we add it to the listview
var trackerItem = CreateTrackerListViewItem(e.EyetrackerInfo);
_trackerList.Items.Add(trackerItem);
UpdateUIElements();
}
private void EyetrackerRemoved(object sender, EyetrackerInfoEventArgs e)
{
// When an eyetracker disappears from the network we remove it from the listview
_trackerList.Items.RemoveByKey(e.EyetrackerInfo.ProductId);
UpdateUIElements();
}
private void EyetrackerUpdated(object sender, EyetrackerInfoEventArgs e)
{
// When an eyetracker is updated we simply create a new
// listviewitem and replace the old one
int index = _trackerList.Items.IndexOfKey(e.EyetrackerInfo.ProductId);
if(index >= 0)
{
_trackerList.Items[index] = CreateTrackerListViewItem(e.EyetrackerInfo);
}
}
private static ListViewItem CreateTrackerListViewItem(EyetrackerInfo info)
{
var trackerItem = new ListViewItem(info.ProductId);
trackerItem.Name = info.ProductId;
var sb = new StringBuilder();
sb.AppendLine("Model: " + info.Model);
sb.AppendLine("Status: " + info.Status);
sb.AppendLine("Generation: " + info.Generation);
sb.AppendLine("Product Id: " + info.ProductId);
sb.AppendLine("Given Name: " + info.GivenName);
sb.AppendLine("Firmware Version: " + info.Version);
trackerItem.ToolTipText = sb.ToString();
trackerItem.Tag = info;
return trackerItem;
}
private ListViewItem GetSelectedItem()
{
if (_trackerList.SelectedItems.Count == 1)
{
return _trackerList.SelectedItems[0];
}
return null;
}
private void UpdateUIElements()
{
var selectedItemInfo = GetSelectedItem();
if(selectedItemInfo != null)
{
_trackerInfoLabel.Text = selectedItemInfo.ToolTipText;
_connectButton.Enabled = true;
}
else
{
_trackerInfoLabel.ResetText();
_connectButton.Enabled = false;
}
if(_connectedTracker != null)
{
_connectionStatusLabel.Text = "Connected to " + _connectionName;
_trackButton.Enabled = true;
_calibrateButton.Enabled = true;
_loadCalibrationMenuItem.Enabled = true;
_saveCalibrationMenuItem.Enabled = true;
_viewCalibrationMenuItem.Enabled = true;
_framerateMenuItem.Enabled = true;
}
else
{
_connectionStatusLabel.Text = "Disconnected";
_trackButton.Enabled = false;
_calibrateButton.Enabled = false;
_loadCalibrationMenuItem.Enabled = false;
_saveCalibrationMenuItem.Enabled = false;
_viewCalibrationMenuItem.Enabled = false;
_framerateMenuItem.Enabled = false;
}
if(_isTracking)
{
_trackButton.Text = "Stop Tracking";
_trackStatus.Enabled = true;
}
else
{
_trackButton.Text = "Start Tracking";
_trackStatus.Enabled = false;
}
}
private void MainForm_Load(object sender, EventArgs e)
{
// Start browsing for eyetrackers on the network
_trackerBrowser.Start();
UpdateUIElements();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Shutdown browser service
_trackerBrowser.Stop();
// Cleanup connections
DisconnectTracker();
}
private void _trackerList_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateUIElements();
}
private void _connectButton_Click(object sender, EventArgs e)
{
// Disconnect existing connection
DisconnectTracker();
// Create new connection
var selectedItem = GetSelectedItem();
if(selectedItem != null)
{
var info = (EyetrackerInfo) selectedItem.Tag;
ConnectToTracker(info);
}
UpdateUIElements();
}
private void ConnectToTracker(EyetrackerInfo info)
{
try
{
_connectedTracker = EyetrackerFactory.CreateEyetracker(info);
_connectedTracker.ConnectionError += HandleConnectionError;
_connectionName = info.ProductId;
_syncManager = new SyncManager(_clock, info);
_connectedTracker.GazeDataReceived += _connectedTracker_GazeDataReceived;
_connectedTracker.FramerateChanged += _connectedTracker_FramerateChanged;
}
catch (EyetrackerException ee)
{
if(ee.ErrorCode == 0x20000402)
{
MessageBox.Show("Failed to upgrade protocol. " +
"This probably means that the firmware needs" +
" to be upgraded to a version that supports the new sdk.","Upgrade Failed",MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Eyetracker responded with error " + ee, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
DisconnectTracker();
}
catch(Exception)
{
MessageBox.Show("Could not connect to eyetracker.","Connection Failed",MessageBoxButtons.OK,MessageBoxIcon.Error);
DisconnectTracker();
}
UpdateUIElements();
}
private void _connectedTracker_GazeDataReceived(object sender, GazeDataEventArgs e)
{
// Send the gaze data to the track status control.
GazeDataItem gd = e.GazeDataItem;
_trackStatus.OnGazeData(gd);
if (_syncManager.SyncState.StateFlag == SyncStateFlag.Synchronized)
{
Int64 convertedTime = _syncManager.RemoteToLocal(gd.TimeStamp);
Int64 localTime = _clock.GetTime();
}
else
{
Console.WriteLine("Warning. Sync state is " + _syncManager.SyncState.StateFlag);
}
}
private static void _connectedTracker_FramerateChanged(object sender, FramerateChangedEventArgs e)
{
Console.WriteLine("Framerate changed " + e.Framerate);
}
private void HandleConnectionError(object sender, ConnectionErrorEventArgs e)
{
// If the connection goes down we dispose
// the IAsyncEyetracker instance. This will release
// all resources held by the connection
DisconnectTracker();
UpdateUIElements();
}
private void DisconnectTracker()
{
if(_connectedTracker != null)
{
_connectedTracker.GazeDataReceived -= _connectedTracker_GazeDataReceived;
_connectedTracker.Dispose();
_connectedTracker = null;
_connectionName = string.Empty;
_isTracking = false;
_syncManager.Dispose();
}
}
private void _trackButton_Click(object sender, EventArgs e)
{
if(_isTracking)
{
// Unsubscribe from gaze data stream
_connectedTracker.StopTracking();
_isTracking = false;
}
else
{
// Start subscribing to gaze data stream
_connectedTracker.StartTracking();
_isTracking = true;
}
UpdateUIElements();
}
private void _calibrateButton_Click(object sender, EventArgs e)
{
var runner = new CalibrationRunner();
try
{
// Start a new calibration procedure
var result = runner.RunCalibration(_connectedTracker);
// Show a calibration plot if everything went OK
if (result != null)
{
var resultForm = new CalibrationResultForm();
resultForm.SetPlotData(result);
resultForm.ShowDialog();
}
else
{
MessageBox.Show("Not enough data to create a calibration (or calibration aborted).");
}
}
catch(EyetrackerException ee)
{
MessageBox.Show("Failed to calibrate. Got exception " + ee,
"Calibration Failed",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void _saveCalibrationMenuItem_Click(object sender, EventArgs e)
{
if(_saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
var calibration = _connectedTracker.GetCalibration();
using(var stream = _saveFileDialog.OpenFile())
using(var writer = new BinaryWriter(stream))
{
writer.Write(calibration.RawData);
}
}
catch (EyetrackerException ee)
{
MessageBox.Show("Failed to get calibration data. Got exception " + ee,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
private void _viewCalibrationMenuItem_Click(object sender, EventArgs e)
{
try
{
var calibration =_connectedTracker.GetCalibration();
var resultForm = new CalibrationResultForm();
resultForm.SetPlotData(calibration);
resultForm.ShowDialog();
}
catch(EyetrackerException ee)
{
MessageBox.Show("Failed to get calibration data. Got exception " + ee,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void _loadCalibrationMenuItem_Click(object sender, EventArgs e)
{
try
{
if (_openFileDialog.ShowDialog() == DialogResult.OK)
{
using (var stream = _openFileDialog.OpenFile())
using (var reader = new BinaryReader(stream))
{
byte[] data = reader.ReadBytes((int)stream.Length);
Calibration calibration = new Calibration(data);
_connectedTracker.SetCalibration(calibration);
}
}
}
catch (EyetrackerException ee)
{
MessageBox.Show("Failed to load calibration data. Got exception " + ee,
"Error",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
private void _framerateMenuItem_Click(object sender, EventArgs e)
{
var framerate = _connectedTracker.GetFramerate();
var availableFramerates = _connectedTracker.EnumerateFramerates();
int fpsIndex = availableFramerates.IndexOf(framerate);
FramerateDialog fpsDialog = new FramerateDialog(availableFramerates,fpsIndex);
if(fpsDialog.ShowDialog() == DialogResult.OK)
{
_connectedTracker.SetFramerate(fpsDialog.CurrentFramerate);
}
}
}
}