FakeTracker.cs 1.99 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
using System;
using System.Drawing;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

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

22
        private readonly GazeDataProcessor gazeDataProcessor;
23
        private System.Timers.Timer timer;
24 25
        private Single trackerFrequency;

26 27 28 29 30 31 32 33 34 35
        public Single OutputFrequency {
            get => gazeDataProcessor.OutputFrequency;
            set => gazeDataProcessor.OutputFrequency = value;
        }
        public String SerialNumber => "Fake";
        public Single TrackingFrequency {
            get {
                return trackerFrequency;
            }
            set {
36 37
                trackerFrequency = value;
                TimerSetup();
38 39
            }
        }
40 41 42 43

        public FakeTracker(Single frequency)
        {
            trackerFrequency = frequency;
44
            gazeDataProcessor = new GazeDataProcessor();
45 46
        }

47 48
        public void Connect()
        {
49
            TimerSetup();
50 51
            ConnectionEvent?.Invoke(this);
        }
52 53

        private void TimerSetup()
54
        {
55 56 57
            timer?.Stop();
            timer?.Dispose();
            timer = new System.Timers.Timer(1000.0 / trackerFrequency);
58 59 60 61
            timer.Elapsed += (Object sender, ElapsedEventArgs e) =>
            {
                Point mousePosition = Control.MousePosition;
                GazeData data = new GazeData(mousePosition.X, mousePosition.Y, mousePosition.X, mousePosition.Y, 0, 0);
62
                gazeDataProcessor.Process(this, data);
63 64 65 66 67 68
            };
            timer.AutoReset = true;
            timer.Start();
        }
    }
}