00001 #include <math.h>
00002 #include <stdio.h>
00003 #include <stdlib.h>
00004 #include <time.h>
00005 #include <string.h>
00006 #include <GL/gl.h>
00007 #undef APIENTRY // otherwise it gets redefined between gl.h and glut.h???
00008 #include <GL/glut.h>
00009 #include "BO/functionsConj.H"
00010
00011
00012 #include "BO/defineConj.H"
00013
00014
00015 #include "Component/ModelManager.H"
00016 #include "Psycho/PsychoDisplay.H"
00017 #include "Psycho/EyeTrackerConfigurator.H"
00018 #include "Psycho/EyeTracker.H"
00019 #include "Psycho/PsychoOpts.H"
00020 #include "Component/EventLog.H"
00021 #include "Component/ComponentOpts.H"
00022 #include "Util/MathFunctions.H"
00023 #include "Util/Types.H"
00024
00025 int type;
00026 int trial;
00027 int record = 1;
00028 int tracking = 0;
00029 int target[STIM*4];
00030 float color[16];
00031 float hor[16] = {0.25,0.25,-0.25,-0.25,
00032 -0.25,0.25,0.75,0.75,
00033 0.75,0.75,0.25,-0.25,
00034 -0.75,-0.75,-0.75,-0.75};
00035 float ver[16] = {0.25,-0.25,-0.25,0.25,
00036 0.75,0.75,0.75,0.25,
00037 -0.25,-0.75,-0.75,-0.75,
00038 -0.75,-0.25,0.25,0.75};
00039
00040
00041 double Rec_ES[100],Rec_RT[100];
00042 int place[100][16];
00043 int calib[9] = {0,0,0,0,0,0,0,0,0};
00044 int state_calib=0,flag_state=0;
00045 int input,occlude=0;
00046 int target_BO,target_Shape,target_stim,target_type,target_top,target_location;
00047 char subj[128];
00048
00049
00050
00051 time_t start_sec,start_min,last_sec,last_min;
00052 clock_t SS,ES;
00053
00054
00055 ModelManager manager("SearchConjunction");
00056
00057
00058 nub::soft_ref<EyeTrackerConfigurator>
00059 etc(new EyeTrackerConfigurator(manager));
00060
00061 nub::soft_ref<EventLog> el(new EventLog(manager));
00062
00063
00064 nub::soft_ref<EyeTracker> et;
00065
00066
00067 void Require_Record(void)
00068 {
00069 int i,j;
00070 FILE *fp1,*fp2;
00071 char File_Time[256],File_Loc[256];
00072
00073
00074
00075
00076 sprintf(File_Time,"%s_RT.data",subj);
00077 sprintf(File_Loc,"%s_Loc.data",subj);
00078
00079
00080 fp1=fopen(File_Time,"w");if(fp1==NULL)printf("error");
00081 fp2=fopen(File_Loc,"w");
00082
00083 for(i=0;i<trial;i++){
00084 fprintf(fp1,"%d\t%d\t%6.3f\t%6.3f\n",i,target[i],Rec_RT[i],Rec_ES[i]);
00085 }
00086
00087 for(i=0;i<trial;i++){
00088 for(j=0;j<16;j++){
00089 fprintf(fp2,"%d",place[i][j]);
00090 if(j==15)fprintf(fp2,"\n");
00091 else fprintf(fp2,"\t");
00092 }
00093 }
00094 fclose(fp1);
00095 fclose(fp2);
00096
00097 }
00098
00099 void idle(void)
00100 {
00101
00102 if(input == 1)input = 0;
00103
00104 }
00105
00106 void InputName(char *str)
00107 {
00108 printf("Subject Name : ");
00109 if (scanf("%s",str) != 1) LFATAL("error in scanf");
00110 printf("%s\n",subj);
00111 }
00112
00113 void CheckFormat(int argc)
00114 {
00115 if(argc!=2 && argc !=3){
00116 printf("Error\n./StimuliBO FileName\n");
00117 exit(0);
00118 }
00119 if(argc == 3)record = 0;
00120 }
00121
00122 void myReshape(GLsizei w, GLsizei h)
00123 {
00124 glClearColor(BGC, BGC, BGC, 1.0);
00125 glClear(GL_COLOR_BUFFER_BIT);
00126 glViewport(0, 0, w, h);
00127 glMatrixMode(GL_PROJECTION);
00128 glLoadIdentity();
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 if(w <= h)
00147 glOrtho( -3.25, 3.25, -1.0 * (GLfloat)h/(GLfloat)w,
00148 1.0 * (GLfloat)h/(GLfloat)w, -10.0, 10.0);
00149 else
00150 glOrtho( -3.25 * (GLfloat)h/(GLfloat)w,
00151 3.25 * (GLfloat)h/(GLfloat)w, -1.0, 1.0, -10.0, 10.0);
00152 glMatrixMode(GL_MODELVIEW);
00153 glLoadIdentity();
00154
00155 }
00156
00157 void Init(int stimuli[], int array[], float color[])
00158 {
00159 int i;
00160
00161 for(i = 0 ;i < STIM; i++){
00162 stimuli[i]=0;
00163 }
00164
00165 for(i = 0 ; i < 16; i++) array[i] = 0;
00166
00167 for(i = 0 ; i < 16; i++){
00168 color[i] = rand()%2;
00169 }
00170 }
00171
00172 void setTargetConjunction(int stimuli[],int array[],int target_trial)
00173 {
00174 struct tm *t_st;
00175 time_t timer;
00176
00177 time(&timer);
00178 t_st = localtime(&timer);
00179
00180 target_location = (rand()+t_st->tm_sec)%16;
00181
00182 if((target_trial)%2==0)target_BO = 1;
00183 else target_BO = 0;
00184
00185 target_Shape = selection(target_trial);
00186 fragStimuli(stimuli,target_trial,target_BO);
00187
00188 array[target_location] = 1;
00189 place[trial][target_location]=target_trial;
00190 }
00191
00192
00193 void display(void)
00194 {
00195
00196 int stimuli[STIM],array[16];
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208 if(flag_state == 0){
00209 if(tracking == 0){
00210 if(state_calib>=3 && state_calib <= 11){
00211 et->track(true);
00212 tracking = 1;
00213 }
00214 }
00215 Display_Calibration(state_calib,calib);
00216 if(tracking == 1){
00217 usleep(50000);
00218 et->track(false);
00219 tracking = 0;
00220 }
00221 }else{
00222 Init(stimuli,array,color);
00223 printf("trial = %d\n",trial);
00224
00225
00226 et->track(true);
00227 tracking = 1;
00228
00229 printf("target[%d] = %d\n",trial,target[trial]);
00230 target_stim = target[trial];
00231 target_type = target_stim%4;
00232 setTargetConjunction(stimuli,array,target[trial]);
00233
00234
00235
00236 Display_Conjunction(target_BO,target_Shape,target_stim,target_type,target_location,stimuli,array,place,trial,hor,ver,color);
00237
00238 SS = clock();
00239 }
00240 }
00241
00242
00243 void init(void)
00244 {
00245 glClearColor(BGC, BGC, BGC, 0.0);
00246 }
00247
00248
00249 void keyboard(unsigned char key,int x,int y)
00250 {
00251 double RT;
00252
00253
00254
00255 if(flag_state == 0){
00256 switch (key){
00257 case 'q':
00258 case 'Q':
00259 case '\033':
00260 {
00261
00262 usleep(50000);
00263 et->track(false);
00264 tracking = 0;
00265
00266
00267 manager.stop();
00268 exit(0);
00269 }
00270
00271 case ' ':
00272 {
00273 usleep(50000);
00274 et->track(false);
00275 tracking = 0;
00276 state_calib++;
00277 if(state_calib==13){
00278
00279 glClearColor(BGC, BGC, BGC, 0);
00280 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00281 Display_Blank();
00282 glutSwapBuffers();
00283 msleep(F_SLEEP*5);
00284 flag_state = 1;
00285 }
00286 }
00287 default:
00288 break;
00289 }
00290 }else{
00291 switch (key){
00292 case 'q':
00293 case 'Q':
00294 case '\033':
00295 {
00296
00297 usleep(50000);
00298 et->track(false);
00299 tracking = 0;
00300
00301
00302 manager.stop();
00303 exit(0);
00304 }
00305
00306 case ' ':
00307 {
00308 usleep(50000);
00309 et->track(false);
00310
00311
00312 tracking = 0;
00313 ES=clock();
00314 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00315 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00316 Rec_RT[trial]=RT;
00317 printf("time = %6.3f sec\n",Rec_ES[trial]);
00318 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00319 trial++;
00320
00321
00322 glClearColor(BGC, BGC, BGC, 0);
00323 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00324 glutSwapBuffers();
00325 msleep(1000);
00326 ES=clock();
00327 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00328 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00329 Rec_RT[trial]=RT;
00330 printf("time = %6.3f sec\n",Rec_ES[trial]);
00331 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00332 trial++;
00333
00334
00335 glClearColor(BGC, BGC, BGC, 0);
00336 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00337 glutSwapBuffers();
00338 msleep(1000);
00339 }
00340 default:
00341 break;
00342 }
00343
00344 if(trial == STIM*4){
00345
00346 usleep(50000);
00347 et->track(false);
00348 tracking = 0;
00349
00350
00351 manager.stop();
00352 Require_Record();
00353 exit(0);
00354 }
00355 }
00356 glutPostRedisplay();
00357 }
00358
00359 void ourInit(void)
00360 {
00361
00362
00363 glClearColor(BGC,BGC,BGC,0.0);
00364 glMatrixMode(GL_PROJECTION);
00365 glLoadIdentity();
00366
00367
00368
00369 glEnable(GL_LINE_SMOOTH);
00370 glEnable(GL_POINT_SMOOTH);
00371 glEnable(GL_POLYGON_SMOOTH);
00372
00373 glEnable(GL_BLEND);
00374 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00375
00376 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
00377
00378
00379 }
00380
00381 void myInit(int target[])
00382 {
00383 int i;
00384
00385 for(i=0;i<STIM*4;i++){
00386 if(i<STIM)target[i]=i;
00387 else if(i>=STIM && i<2*STIM)target[i]=i-STIM;
00388 else if(i>=2*STIM && i<3*STIM)target[i]=i-2*STIM;
00389 else if(i>=3*STIM && i<4*STIM)target[i]=i-3*STIM;
00390
00391 }
00392
00393 shuffle(target,STIM*4);
00394 }
00395
00396 void new_mouse(int button, int state,int x, int y)
00397 {
00398 double RT;
00399
00400 if(flag_state == 0){
00401 switch(button){
00402 case GLUT_RIGHT_BUTTON:
00403 if(state == GLUT_DOWN){
00404 state_calib++;
00405 if(state_calib==13){
00406 glClearColor(BGC,BGC,BGC,0);
00407 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00408 Display_Blank();
00409 glutSwapBuffers();
00410 msleep(F_SLEEP*5);
00411 flag_state = 1;
00412 }
00413 }
00414 break;
00415
00416 case GLUT_LEFT_BUTTON:
00417 if(state == GLUT_DOWN){
00418 state_calib++;
00419 if(state_calib==13){
00420 glClearColor(BGC,BGC,BGC,0);
00421 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00422 Display_Blank();
00423 glutSwapBuffers();
00424 msleep(F_SLEEP*5);
00425 flag_state = 1;
00426 }
00427 }
00428 break;
00429
00430 default:
00431 break;
00432 }
00433 }else{
00434 switch(button){
00435 case GLUT_RIGHT_BUTTON:
00436 if(state == GLUT_DOWN)
00437 {
00438 ES=clock();
00439 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00440 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00441 Rec_RT[trial]=RT;
00442 printf("time = %6.3f sec\n",Rec_ES[trial]);
00443 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00444 trial++;
00445
00446
00447 glClearColor(BGC, BGC, BGC, 0);
00448 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00449 glutSwapBuffers();
00450 msleep(1000);
00451 }
00452 break;
00453
00454 case GLUT_LEFT_BUTTON:
00455 if(state == GLUT_DOWN)
00456 {
00457 ES=clock();
00458 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00459 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00460 Rec_RT[trial]=RT;
00461 printf("time = %6.3f sec\n",Rec_ES[trial]);
00462 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00463 trial++;
00464
00465
00466 glClearColor(BGC, BGC, BGC, 0);
00467 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00468 glutSwapBuffers();
00469 msleep(1000);
00470 }
00471 break;
00472
00473 default:
00474 break;
00475 }
00476 }
00477 if(trial == 4){
00478 if(record == 1)Require_Record();
00479 exit(0);
00480 }
00481 glutPostRedisplay();
00482 }
00483
00484 int main(int argc, char *argv[])
00485 {
00486 const char *name = "New Stimulus";
00487
00488 MYLOGVERB = LOG_INFO;
00489
00490
00491 manager.addSubComponent(etc);
00492 manager.addSubComponent(el);
00493
00494 manager.setOptionValString(&OPT_EventLogFileName, "searchConj.log");
00495 manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00496
00497
00498 if (manager.parseCommandLine(argc, argv, "<fileName>", 1, 1) == false)
00499 return(1);
00500
00501 et = etc->getET();
00502 et->setEventLog(el);
00503
00504
00505 manager.start();
00506 el->pushEvent(std::string("===== Trial : Search Conjucntion Task ====="));
00507
00508
00509
00510
00511 glutInit(&argc, argv);
00512 strcpy(subj,argv[1]);
00513
00514
00515 myInit(target);
00516 trial = 0;
00517
00518 glClearColor(BGC,BGC,BGC,0);
00519
00520 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00521
00522 glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
00523
00524 glutInitWindowPosition(100,100);
00525 glutInitWindowSize(500,500);
00526
00527
00528
00529
00530
00531 glutCreateWindow(name);
00532
00533 glutFullScreen();
00534
00535 ourInit();
00536 glutReshapeFunc(myReshape);
00537
00538
00539 glutDisplayFunc(display);
00540 glutKeyboardFunc(keyboard);
00541 glutMouseFunc(new_mouse);
00542 glutIdleFunc(idle);
00543
00544
00545
00546
00547
00548 glutMainLoop();
00549 return 0;
00550 }