aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/TobiiGlassesPro2/TobiiData.py
blob: 8e0a8b3c9c1c440d1b61bb40ff4c668ada205fef (plain)
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
#!/usr/bin/env python

import threading
import time

from argaze.TobiiGlassesPro2 import TobiiController

class TobiiDataThread(threading.Thread):
    """Handle data reception in a separate thread."""

    def __init__(self, controller: TobiiController.TobiiController):
        """Initialise thread super class and prepare data reception."""
        
        threading.Thread.__init__(self)

        self.stop_event = threading.Event()
        self.read_lock = threading.Lock()

        self.controller = controller

        self.fps = self.controller.get_et_freq()
        self.sleep = 1./self.fps

        self.__ac_buffer = []
        self.__gy_buffer = []
        self.__gp_buffer = []
        self.__pts_buffer = []

        self.__start_ts = 0

    def __del__(self):
        pass

    def __get_ac(self, data):

        ac_value = data['mems']['ac']['ac']
        ac_ts = data['mems']['ac']['ts']
        ac_data = {
            'TIMESTAMP': ac_ts,
            'TIME': (ac_ts - self.__start_ts) / 1000000.,
            'X': ac_value[0],
            'Y': ac_value[1],
            'Z': ac_value[2]
        }

        return ac_data

    def __get_gy(self, data):

        gy_value = data['mems']['gy']['gy']
        gy_ts = data['mems']['gy']['ts']
        gy_data = {
            'TIMESTAMP': gy_ts,
            'TIME': (gy_ts - self.__start_ts) / 1000000.,
            'X': gy_value[0],
            'Y': gy_value[1],
            'Z': gy_value[2]
        }

        return gy_data

    def __get_gp(self, data):

        gp_value = data['gp']['gp']
        gp_ts = data['gp']['ts']
        gp_data = {
            'TIMESTAMP': gp_ts,
            'TIME': (gp_ts - self.__start_ts) / 1000000.,
            'X': gp_value[0],
            'Y': gp_value[1]
        }

        return gp_data

    def __get_pts(self, data):

        pts_value = data['pts']['pts']
        pts_ts = data['pts']['ts']
        pts_data = {
            'TIMESTAMP': pts_ts,
            'TIME': (pts_ts - self.__start_ts) / 1000000.,
            'PTS': pts_value
        }

        return pts_data

    def run(self):
        """Data reception function."""

        while not self.stop_event.isSet():

            time.sleep(self.sleep)

            self.read_lock.acquire()

            data = self.controller.get_data()

            # store only timestamped datas
            if 'pts' in data:

                pts_data = data['pts']

                if 'pts' in pts_data:

                    ac_ts = data['mems']['ac']['ts']
                    gy_ts = data['mems']['gy']['ts']
                    gp_ts = data['gp']['ts']
                    pts_ts = pts_data['ts']

                    # get start timestamp
                    if self.__start_ts == 0:

                        # ignore -1 timestamp
                        valid_ts = []
                        for ts in [ac_ts, gy_ts, gp_ts, pts_ts]:
                            if ts > 0:
                                valid_ts.append(ts)

                        self.__start_ts = min(valid_ts)
                        #print(f'Tobii Data Frame: __start_ts = {self.__start_ts}')

                    #print(f'Tobii Data Frame: ac_ts = {ac_ts}, gy_ts = {gy_ts}, gp_ts = {gp_ts}, pts_ts = {pts_ts}')

                    # ignore -1 timestamp and filter repetitions

                    if ac_ts != -1:
                        if len(self.__ac_buffer) == 0:
                            self.__ac_buffer.append(self.__get_ac(data))
                        elif ac_ts != self.__ac_buffer[-1]['TIMESTAMP']:
                            self.__ac_buffer.append(self.__get_ac(data))

                    if gy_ts != -1:
                        if len(self.__gy_buffer) == 0:
                            self.__gy_buffer.append(self.__get_gy(data))
                        elif gy_ts != self.__gy_buffer[-1]['TIMESTAMP']:
                            self.__gy_buffer.append(self.__get_gy(data))

                    if gp_ts != -1:
                        if len(self.__gp_buffer) == 0:
                            self.__gp_buffer.append(self.__get_gp(data))
                        elif gp_ts != self.__gp_buffer[-1]['TIMESTAMP']:
                            self.__gp_buffer.append(self.__get_gp(data))

                    if pts_ts != -1:
                        if len(self.__pts_buffer) == 0:
                            self.__pts_buffer.append(self.__get_pts(data))
                        elif pts_ts != self.__pts_buffer[-1]['TIMESTAMP']:
                            self.__pts_buffer.append(self.__get_pts(data))

            self.read_lock.release()

    def read_accelerometer_data(self, timestamp: int = -1):
        """Get accelerometer data at a given timestamp.
        **Returns:** accelerometer dictionary
        ``` 
        {
            'TIMESTAMP': int,
            'TIME': int,
            'X': float,
            'Y': float,
            'Z': float
        }
        ``` 
        """

        if len(self.__ac_buffer):

            self.read_lock.acquire()
                
            # TODO : find closest timestamp data
            ac_data = self.__ac_buffer[-1].copy()

            self.read_lock.release()

            return ac_data

        else:

            return {}

    def read_accelerometer_buffer(self):
        """Get accelerometer data buffer.
        **Returns:** accelerometer dictionary array"""

        self.read_lock.acquire()
            
        ac_buffer = self.__ac_buffer.copy()

        self.read_lock.release()

        return ac_buffer

    def read_gyroscope_data(self, timestamp: int = -1):
        """Get gyroscope data at a given timestamp.
        **Returns:** gyroscope dictionary
        ``` 
        {
            'TIMESTAMP': int,
            'TIME': int,
            'X': float,
            'Y': float,
            'Z': float
        }
        ``` 
        """

        if len(self.__gy_buffer):

            self.read_lock.acquire()
                
            # TODO : find closest timestamp data
            gy_data = self.__gy_buffer[-1].copy()

            self.read_lock.release()

            return gy_data

        else:

            return {}

    def read_gyroscope_buffer(self):
        """Get gyroscope data buffer.
        **Returns:** gyroscope dictionary array"""

        self.read_lock.acquire()
            
        gy_buffer = self.__gy_buffer.copy()

        self.read_lock.release()

        return gy_buffer

    def read_gaze_data(self, timestamp: int = -1):
        """Get gaze data at a given timestamp.
        **Returns:** gaze dictionary
        ``` 
        {
            'TIMESTAMP': int,
            'TIME': int,
            'X': float,
            'Y': float
        }
        ``` 
        """

        if len(self.__gp_buffer):

            self.read_lock.acquire()
                
            # TODO : find closest timestamp data
            gp_data = self.__gp_buffer[-1].copy()

            self.read_lock.release()

            return gp_data

        else:

            return {}

    def read_gaze_buffer(self):
        """Get gaze data buffer.
        **Returns:** gaze dictionary array"""

        self.read_lock.acquire()
            
        gp_buffer = self.__gp_buffer.copy()

        self.read_lock.release()

        return gp_buffer

    def read_pts_data(self, timestamp: int = -1):
        """Get Presentation Time Stamp (pts) data at a given timestamp.
        **Returns:** pts dictionary
        ``` 
        {
            'TIMESTAMP': int,
            'TIME': int,
            'PTS': int
        }
        ``` 
        """

        if len(self.__pts_buffer):

            self.read_lock.acquire()
                
            # TODO : find closest timestamp data
            pts_data = self.__pts_buffer[-1].copy()

            self.read_lock.release()

            return pts_data

        else:

            return {}

    def read_pts_buffer(self):
        """Get Presentation Time Stamp (pts) data buffer.
        **Returns:** pts dictionary array"""

        self.read_lock.acquire()
            
        pts_buffer = self.__pts_buffer.copy()

        self.read_lock.release()

        return pts_buffer

    def stop(self):
        """Stop data reception definitively."""

        self.stop_event.set()
        threading.Thread.join(self)