PEBL 2.2
Psychology Experiment Building Language - Cross-platform psychological experiment development system
PEyeTracker.cpp
Go to the documentation of this file.
1//* -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- */
3// Name: src/devices/PEyeTracker.cpp
4// Purpose: Class for handling gazelib eyetracker device
5// (TheEyeTribe eyetracker)
6// Author: Shane T. Mueller, Ph.D.
7// Copyright: (c) 2016 Shane T. Mueller <smueller@obereed.net>
8// License: GPL 2
9//
10//
11//
12// This file is part of the PEBL project.
13//
14// PEBL is free software; you can redistribute it and/or modify
15// it under the terms of the GNU General Public License as published by
16// the Free Software Foundation; either version 2 of the License, or
17// (at your option) any later version.
18//
19// PEBL is distributed in the hope that it will be useful,
20// but WITHOUT ANY WARRANTY; without even the implied warranty of
21// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22// GNU General Public License for more details.
23//
24// You should have received a copy of the GNU General Public License
25// along with PEBL; if not, write to the Free Software
26// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28//
29// This class is targeted to gazelib developed by theeyetribe, a $100 eyetracker.
30
31#ifdef PEBL_GAZELIB
32
33#include "PEyeTracker.h"
34#include "../objects/PCustomObject.h"
35#include "../base/PComplexData.h"
36#include "../base/Variant.h"
37#include "../libs/PEBLEnvironment.h"
38
39//#include "../../../libs/tet-cpp-client-master/include/libs/gazeapi.h"
40
41// --- MyGaze implementation
42PEyeTracker::PEyeTracker()
43{
44 // Connect to the server in push mode on the default TCP port (6555)
45
46 if( !m_api.connect( false ) )
47 {
48 //warning or error should be flagged here.
49
50 }
51
52 // m_api.set_push(true);
53}
54
55PEyeTracker::~PEyeTracker()
56{
57 //m_api.remove_listener( *this );
58 m_api.disconnect();
59}
60
61gtl::GazeData * PEyeTracker::GetGazeFrame()
62{
63 //This blocks until it gets the next gaze frame.
64 gtl::GazeData *gd = new gtl::GazeData;
65 m_api.get_frame(*gd);
66
67//GazeData contains:
68//enum tracking state
69//.time=timestamp
70//.fix ==is fixated
71//.state = 32 bit state integer???
72//.raw raw gaze coordinates in pixels
73//.avg smoothed coordinates in pixels
74//.lefteye data for left eye
75//.righteye data for right eye
76
77
78 return gd;
79}
80
81
82//This doesn't need to be a method.
83//This creates a custom object containing the infromation in gd
84 Variant ConvertGazeData(const gtl::GazeData& gd)
85{
86
87 PCustomObject * po = new PCustomObject("gazedata");
88
89 po->SetProperty("TIME",gd.time);
90// po->SetProperty("TIMESTAMP",gd.timestamp);
91 po->SetProperty("FIX",gd.fix);
92 po->SetProperty("STATE",gd.state);
93 po->SetProperty("RAWX",gd.raw.x);
94 po->SetProperty("RAWY",gd.raw.y);
95 po->SetProperty("AVGX",gd.avg.x);
96 po->SetProperty("AVGY",gd.avg.y);
97
98 po->SetProperty("LEFTEYERAWX",gd.lefteye.raw.x);
99 po->SetProperty("LEFTEYERAWY",gd.lefteye.raw.y);
100 po->SetProperty("LEFTEYEAVGX",gd.lefteye.avg.x);
101 po->SetProperty("LEFTEYEAVGY",gd.lefteye.avg.y);
102 po->SetProperty("LEFTEYEPSIZE", gd.lefteye.psize);
103 po->SetProperty("LEFTEYECENTERX",gd.lefteye.pcenter.x);
104 po->SetProperty("LEFTEYECENTERY",gd.lefteye.pcenter.y);
105
106 po->SetProperty("RIGHTEYERAWX",gd.righteye.raw.x);
107 po->SetProperty("RIGHTEYERAWY",gd.righteye.raw.y);
108 po->SetProperty("RIGHTEYEAVGX",gd.righteye.avg.x);
109 po->SetProperty("RIGHTEYEAVGY",gd.righteye.avg.y);
110 po->SetProperty("RIGHTEYEPSIZE", gd.righteye.psize);
111 po->SetProperty("RIGHTEYECENTERX",gd.righteye.pcenter.x);
112 po->SetProperty("RIGHTEYECENTERY",gd.righteye.pcenter.y);
113
114
115
117 PComplexData * pcd = new PComplexData(tmpObject);
118 Variant tmp = Variant(pcd);
119 delete pcd;
120 pcd=NULL;
121 return tmp;
122}
123
124
125
126bool PEyeTracker::SetProperty(std::string name, Variant v)
127{
128 //We won't do any validation here. Maybe in the future
129 //some validation cuold be done here; allowing the user
130 //to specify things like value type, or a subset of values.
131
132 mProperties[name]=v;
133 return true;
134}
135
136Variant PEyeTracker::GetProperty(std::string name)const
137{
138 return PEBLObjectBase::GetProperty(name);
139}
140
141
142 ObjectValidationError PEyeTracker::ValidateProperty(std::string name, Variant v)const
143{
144 return ValidateProperty(name);
145}
146
147ObjectValidationError PEyeTracker::ValidateProperty(std::string name)const
148{
149 cout << "Validating eyetracking property\n";
151 if(ove == OVE_VALID)
152 return ove;
153 else
155
156}
157
158
159
160
161
162// --- MyGaze implementation
163PEBLGaze::PEBLGaze(std::string functionname)
164{
165 mFunctionName = functionname;
166 // Connect to the server in push mode on the default TCP port (6555)
167 if( m_api.connect( true ) )
168 {
169 // Enable GazeData notifications
170 m_api.add_listener( *this );
171 }
172}
173
174PEBLGaze::~PEBLGaze()
175{
176 m_api.remove_listener( *this );
177 m_api.disconnect();
178}
179
180void PEBLGaze::on_gaze_data( gtl::GazeData const & gaze_data )
181{
182 if( gaze_data.state & gtl::GazeData::GD_STATE_TRACKING_GAZE )
183 {
184
185
186 if(false)
187 {
188 //Very fast recording.
189 std::cout <<"automatic:" << gaze_data.time << std::endl;
190
191 } else {
192 Variant gd = ConvertGazeData(gaze_data);
193
194
195 //set the arguments
196 PList * vlist = new PList;
197 vlist->PushBack(gd);
198
200 PComplexData * pcd = new PComplexData(tmplist1);
201 Variant argvlist = Variant(pcd);
202 delete pcd;
203 pcd=NULL;
204
205
206 Variant fname = mFunctionName;
207
208 //we need to create a 'varlist' tree, which
209 //is a set of opnode(varlist) nodes with the variable on
210 // the left and another opnode on the right.
211
212
213
214 PList * arglist = new PList;
215 arglist->PushBack(fname);
216 arglist->PushBack(argvlist);
217
219 pcd = new PComplexData(tmplist);
220 Variant tmp = Variant(pcd);
221 delete pcd;
222
223 //Calls a function with the specified parameters
225 }
226
227 }
228}
229
230
231
232
233
234#endif
235
236
#define NULL
Definition BinReloc.cpp:317
ObjectValidationError
Definition PEBLObject.h:37
@ OVE_INVALID_PROPERTY_NAME
Definition PEBLObject.h:40
@ OVE_VALID
Definition PEBLObject.h:39
This class simply represent an abstract text-based object.
virtual bool SetProperty(std::string, Variant v)
virtual ObjectValidationError ValidateProperty(std::string, Variant v) const
Variant GetProperty(std::string) const
Definition PList.h:45
void PushBack(const Variant &v)
Definition PList.cpp:149
Variant CallFunction(Variant v)