TobiiEyeTracker.cs 3.19 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tobii.Research;

namespace TrackerBridge
{
    public class TobiiEyeTracker
    {
        public delegate void ConnectionEventHandler(TobiiEyeTracker sender);
        public delegate void GazeDataHandler(TobiiEyeTracker sender, GazeData data);
        public event ConnectionEventHandler ConnectionEvent;
        public event ConnectionEventHandler ConnectionTimeout;
        public event GazeDataHandler GazeDataAvailable;

        public Boolean IsConnected { get; private set; }
19
        public String SerialNumber { get => serialNumber; }
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

        private IEyeTracker eyeTracker = null;
        private String serialNumber = null;
        private GazeDataProcessor gazeDataProcessor;

        public TobiiEyeTracker()
        {
            gazeDataProcessor = new GazeDataProcessor();
        }

        public TobiiEyeTracker(String serialNumber) : this()
        {
            this.serialNumber = serialNumber;
        }

        public float GetTrackingFrequency()
        {
            if (eyeTracker != null)
            {
                return eyeTracker.GetGazeOutputFrequency();
            }
            else
            {
                throw new NullReferenceException();
            }
        }
        public void SetTrackingFrequency(float value)
        {
            if (eyeTracker != null)
            {
                eyeTracker.SetGazeOutputFrequency(value);
            }
        }

        public void Connect()
        {
            Task task = new Task(() => InitiateConnection());
            task.Start();
        }

        private void InitiateConnection()
        {
            DateTime time = DateTime.Now;
            while (eyeTracker == null)
            {
                EyeTrackerCollection eyeTrackers = EyeTrackingOperations.FindAllEyeTrackers();
                if (eyeTrackers.Count > 0)
                {
                    if (serialNumber == null)
                    {
                        eyeTracker = eyeTrackers[0];
                        break;
                    }
                    else
                    {
                        foreach (IEyeTracker t in eyeTrackers)
                        {
                            if (serialNumber == t.SerialNumber)
                            {
                                eyeTracker = t;
                                break;
                            }
                        }
                    }
                }

                if (eyeTracker != null)
                {
                    break;
                }

                if ((DateTime.Now - time) > (new TimeSpan(0, 0, 30)))
                {
                    ConnectionTimeout?.Invoke(this);
                }
            }
            eyeTracker.GazeDataReceived += OnGazeDataReceived;
97
            serialNumber = eyeTracker.SerialNumber;
98 99 100 101 102 103 104 105 106 107 108
            SetTrackingFrequency(600f);
            ConnectionEvent?.Invoke(this);
        }

        private void OnGazeDataReceived(object sender, GazeDataEventArgs e)
        {
            GazeData gazeData = gazeDataProcessor.Extract(e);
            GazeDataAvailable?.Invoke(this, gazeData);
        }
    }
}