-
Notifications
You must be signed in to change notification settings - Fork 1
/
TrackerController.cs
115 lines (100 loc) · 2.98 KB
/
TrackerController.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tobii.Gaze.Core;
using System.Diagnostics;
using System.Threading;
namespace GazeMonitor
{
class TrackerController
{
public IEyeTracker tracker;
private Thread eventLoop;
public bool trackingStarted = false;
public void Initialize()
{
Uri url = new EyeTrackerCoreLibrary().GetConnectedEyeTracker();
if (url == null)
{
throw new ApplicationException("No eye tracker found, check cable!");
}
else
{
tracker = new EyeTracker(url);
eventLoop = CreateAndRunEventLoopThread(tracker);
tracker.Connect();
}
}
private Thread CreateAndRunEventLoopThread(IEyeTracker tracker)
{
var thread = new Thread(() =>
{
try
{
tracker.RunEventLoop();
}
catch (EyeTrackerException ex)
{
Debug.WriteLine("An error occurred in the eye tracker event loop: " + ex.Message);
}
Debug.WriteLine("Leaving the event loop.");
});
thread.Start();
return thread;
}
public void Dispose()
{
if (tracker != null)
{
tracker.Disconnect();
if (eventLoop != null)
{
tracker.BreakEventLoop();
eventLoop.Join();
}
tracker.Dispose();
}
}
public void StopTracking()
{
try
{
tracker.StopTrackingAsync(TrackingStopped);
}
catch (EyeTrackerException e)
{
Debug.WriteLine(e.ErrorCode);
}
}
private void TrackingStopped(ErrorCode errorCode)
{
Debug.WriteLine("Tracking Stopped: " + errorCode.ToString());
trackingStarted = false;
}
public void StartTracking()
{
tracker.StartTrackingAsync(TrackingStarted);
}
private void TrackingStarted(ErrorCode errorCode)
{
Debug.WriteLine("Tracking Started: " + errorCode.ToString());
trackingStarted = true;
}
public void SetCalibration(byte[] data)
{
Calibration calibration = new Calibration(data);
tracker.SetCalibrationAsync(calibration, CalibrationSet);
}
private void CalibrationSet(ErrorCode errorCode)
{
Debug.WriteLine("Calibration Set: " + errorCode.ToString());
}
public byte[] GetCalibration()
{
Calibration calibration = tracker.GetCalibration();
return calibration.GetData();
}
}
}