TobiiEyeTracker.cs 3.24 KB
Newer Older
1 2 3 4 5 6 7 8 9
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tobii.Research;

namespace TrackerBridge
{
10
    public class TobiiEyeTracker : IBridgeTracker
11 12 13
    {
        public event ConnectionEventHandler ConnectionEvent;
        public event ConnectionEventHandler ConnectionTimeout;
14 15
        public event GazeDataHandler GazeDataAvailable {
            add {
16
                gazeDataProcessor.GazeDataProcessed += value;
17 18
            }
            remove {
19
                gazeDataProcessor.GazeDataProcessed -= value;
20 21
            }
        }
22

23
        private readonly GazeDataProcessor gazeDataProcessor;
24
        private IEyeTracker eyeTracker = null;
25 26

        public Boolean IsConnected { get; private set; } = false;
27 28 29 30
        public Single OutputFrequency {
            get => gazeDataProcessor.OutputFrequency;
            set => gazeDataProcessor.OutputFrequency = value;
        }
31
        public String SerialNumber { get; private set; } = null;
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
        public Single TrackingFrequency {
            get {
                if (eyeTracker != null)
                {
                    return eyeTracker.GetGazeOutputFrequency();
                }
                else
                {
                    return 0f;
                }
            }
            set {
                if (eyeTracker != null && eyeTracker.GetAllGazeOutputFrequencies().Contains(value))
                {
                    eyeTracker.SetGazeOutputFrequency(value);
                }
            }
        }
50 51 52

        public TobiiEyeTracker()
        {
53
            gazeDataProcessor = new GazeDataProcessor();
54 55 56
        }
        public TobiiEyeTracker(String serialNumber) : this()
        {
57 58
            SerialNumber = serialNumber;
        }
59

60 61 62 63
        public void Connect()
        {
            Task task = new Task(() => InitiateConnection());
            task.Start();
64 65 66 67 68 69 70 71 72
        }
        private void InitiateConnection()
        {
            DateTime time = DateTime.Now;
            while (eyeTracker == null)
            {
                EyeTrackerCollection eyeTrackers = EyeTrackingOperations.FindAllEyeTrackers();
                if (eyeTrackers.Count > 0)
                {
73
                    if (SerialNumber == null)
74 75 76 77 78 79 80 81
                    {
                        eyeTracker = eyeTrackers[0];
                        break;
                    }
                    else
                    {
                        foreach (IEyeTracker t in eyeTrackers)
                        {
82
                            if (SerialNumber == t.SerialNumber)
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
                            {
                                eyeTracker = t;
                                break;
                            }
                        }
                    }
                }

                if (eyeTracker != null)
                {
                    break;
                }

                if ((DateTime.Now - time) > (new TimeSpan(0, 0, 30)))
                {
                    ConnectionTimeout?.Invoke(this);
                }
            }
101
            eyeTracker.GazeDataReceived += gazeDataProcessor.Process;
102
            SerialNumber = eyeTracker.SerialNumber;
103
            TrackingFrequency = 600f;
104 105 106 107
            ConnectionEvent?.Invoke(this);
        }
    }
}