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 #include <GL/glu.h>
00008 #include <SDL/SDL.h>
00009 #include "BO/functionsTest.H"
00010
00011
00012 #include "BO/defineConj.H"
00013
00014
00015 #include "Component/ModelManager.H"
00016 #include "Image/Image.H"
00017 #include "Psycho/PsychoDisplay.H"
00018 #include "Psycho/EyeTrackerConfigurator.H"
00019 #include "Psycho/EyeTracker.H"
00020 #include "Psycho/PsychoOpts.H"
00021 #include "Component/EventLog.H"
00022 #include "Component/ComponentOpts.H"
00023 #include "Util/MathFunctions.H"
00024 #include "Util/Types.H"
00025 #include "rutz/time.h"
00026
00027 int type;
00028 int trial;
00029 int record = 1;
00030 int tracking = 0;
00031 int target[STIM*4];
00032 float color[16];
00033 float hor[16] = {0.25,0.25,-0.25,-0.25,
00034 -0.25,0.25,0.75,0.75,
00035 0.75,0.75,0.25,-0.25,
00036 -0.75,-0.75,-0.75,-0.75};
00037 float ver[16] = {0.25,-0.25,-0.25,0.25,
00038 0.75,0.75,0.75,0.25,
00039 -0.25,-0.75,-0.75,-0.75,
00040 -0.75,-0.25,0.25,0.75};
00041
00042
00043 double Rec_ES[100],Rec_RT[100];
00044 int place[100][16];
00045 int calib[9] = {0,0,0,0,0,0,0,0,0};
00046 int state_calib=0,flag_state=0;
00047 int input,occlude=0;
00048 int target_BO,target_Shape,target_stim,target_type,target_top,target_location;
00049 char subj[128];
00050
00051
00052
00053 time_t start_sec,start_min,last_sec,last_min;
00054 clock_t SS,ES;
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 int Require_Result(void)
00073 {
00074 int i,j;
00075 FILE *fp1,*fp2;
00076 char File_Time[128],File_Loc[128];
00077
00078
00079 sprintf(File_Time,"%s_RT.data",subj);
00080 sprintf(File_Loc,"%s_Loc.data",subj);
00081 printf("test1\n");
00082
00083 fp1=fopen(File_Time,"w");
00084 fp2=fopen(File_Loc,"w");
00085 printf("test2\n");
00086 for(i=0;i<trial;i++){
00087 fprintf(fp1,"%d\t%d\t%6.3f\t%6.3f\n",i,target[i],Rec_RT[i],Rec_ES[i]);
00088 }
00089 printf("test3\n");
00090 for(i=0;i<trial;i++){
00091 for(j=0;j<16;j++){
00092 fprintf(fp2,"%d",place[i][j]);
00093 if(j==15)fprintf(fp2,"\n");
00094 else fprintf(fp2,"\t");
00095 }
00096 }
00097 printf("test4\n");
00098 fclose(fp1);
00099 fclose(fp2);
00100 printf("test5\n");
00101 return 0;
00102 }
00103
00104 void quit_tutorial(int code)
00105 {
00106 SDL_Quit();
00107 exit(code);
00108 }
00109
00110 void idle(void)
00111 {
00112
00113 if(input == 1)input = 0;
00114
00115 }
00116
00117 void InputName(char *str)
00118 {
00119 printf("Subject Name : ");
00120 scanf("%s",str);
00121 printf("%s\n",subj);
00122 }
00123
00124 void CheckFormat(int argc)
00125 {
00126 if(argc!=2 && argc !=3){
00127 printf("Error\n./StimuliBO FileName\n");
00128 exit(0);
00129 }
00130 if(argc == 3)record = 0;
00131 }
00132
00133 void myReshape(GLsizei w, GLsizei h)
00134 {
00135 glClearColor(BGC, BGC, BGC, 1.0);
00136 glClear(GL_COLOR_BUFFER_BIT);
00137 glViewport(0, 0, w, h);
00138 glMatrixMode(GL_PROJECTION);
00139 glLoadIdentity();
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 if(w <= h)
00158 glOrtho( -3.25, 3.25, -1.0 * (GLfloat)h/(GLfloat)w,
00159 1.0 * (GLfloat)h/(GLfloat)w, -10.0, 10.0);
00160 else
00161 glOrtho( -3.25 * (GLfloat)h/(GLfloat)w,
00162 3.25 * (GLfloat)h/(GLfloat)w, -1.0, 1.0, -10.0, 10.0);
00163 glMatrixMode(GL_MODELVIEW);
00164 glLoadIdentity();
00165
00166 }
00167
00168 void Init(int stimuli[], int array[], float color[])
00169 {
00170 int i;
00171
00172 for(i = 0 ;i < STIM; i++){
00173 stimuli[i]=0;
00174 }
00175
00176 for(i = 0 ; i < 16; i++) array[i] = 0;
00177
00178 for(i = 0 ; i < 16; i++){
00179 color[i] = rand()%2;
00180 }
00181 }
00182
00183 void setTargetConjunction(int stimuli[],int array[],int target_trial)
00184 {
00185 struct tm *t_st;
00186 time_t timer;
00187
00188 time(&timer);
00189 t_st = localtime(&timer);
00190
00191 target_location = (rand()+t_st->tm_sec)%16;
00192
00193 if((target_trial)%2==0)target_BO = 1;
00194 else target_BO = 0;
00195
00196 target_Shape = selection(target_trial);
00197 fragStimuli(stimuli,target_trial,target_BO);
00198
00199 array[target_location] = 1;
00200 place[trial][target_location]=target_trial;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277 void display(void)
00278 {
00279
00280 int stimuli[STIM],array[16];
00281
00282 Init(stimuli,array,color);
00283
00284 printf("target[%d] = %d\n",trial,target[trial]);
00285 target_stim = target[trial];
00286 target_type = target_stim%4;
00287 setTargetConjunction(stimuli,array,target[trial]);
00288
00289
00290
00291 Display_Conjunction(target_BO,target_Shape,target_stim,target_type,target_location,stimuli,array,place,trial,hor,ver,color);
00292
00293 SS = clock();
00294
00295 }
00296
00297
00298 void init(void)
00299 {
00300 glClearColor(BGC, BGC, BGC, 0.0);
00301 }
00302
00303
00304 void keyboard2(SDL_keysym* keysym)
00305 {
00306 double RT;
00307 printf("checking et (keyboard)\n");
00308
00309 if(flag_state == 0){
00310 switch (keysym->sym){
00311 case SDLK_ESCAPE:
00312 {
00313
00314
00315
00316
00317
00318
00319
00320 quit_tutorial(0);
00321 }
00322
00323 case SDLK_SPACE:
00324 {
00325 state_calib++;
00326 if(state_calib==13){
00327 printf("calibration is over\n");
00328 glClearColor(BGC, BGC, BGC, 0);
00329 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00330 Display_Blank();
00331 SDL_GL_SwapBuffers();
00332 msleep(F_SLEEP*5);
00333 flag_state = 1;
00334 }
00335 }
00336 default:
00337 break;
00338 }
00339 }else{
00340 switch (keysym->sym){
00341 case SDLK_ESCAPE:
00342 {
00343 printf("before stop\n");
00344
00345
00346 tracking = 0;
00347
00348
00349
00350 quit_tutorial(0);
00351 }
00352
00353 case SDLK_SPACE:
00354 {
00355 ES=clock();
00356 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00357 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00358 Rec_RT[trial]=RT;
00359 printf("time = %6.3f sec\n",Rec_ES[trial]);
00360 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00361 trial++;
00362
00363
00364 glClearColor(BGC, BGC, BGC, 0);
00365 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00366 SDL_GL_SwapBuffers();
00367 msleep(1000);
00368 }
00369 default:
00370 break;
00371 }
00372
00373 if(trial == STIM*4){
00374 if(record == 1)Require_Result();
00375 printf("before stop\n");
00376
00377
00378 tracking = 0;
00379
00380
00381
00382 quit_tutorial(0);
00383 }
00384 }
00385
00386
00387
00388
00389
00390 tracking = 0;
00391
00392 SDL_GL_SwapBuffers();
00393
00394 }
00395
00396 void keyboard(int key)
00397 {
00398 double RT;
00399 printf("checking et (keyboard)\n");
00400
00401
00402 switch (key){
00403 case 'q':
00404 case 'Q':
00405 case '\033':
00406 {
00407 quit_tutorial(0);
00408 }
00409
00410 case ' ':
00411 {
00412 ES=clock();
00413 RT=(double)(ES-SS)/CLOCKS_PER_SEC;
00414 Rec_ES[trial]=(double)ES/CLOCKS_PER_SEC;
00415 Rec_RT[trial]=RT;
00416 printf("time = %6.3f sec\n",Rec_ES[trial]);
00417 printf("Reaction time R = %6.3f sec\n",Rec_RT[trial]);
00418 trial++;
00419
00420
00421 glClearColor(BGC, BGC, BGC, 0);
00422 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00423 SDL_GL_SwapBuffers();
00424 msleep(1000);
00425 }
00426 default:
00427 break;
00428 }
00429
00430
00431
00432
00433
00434
00435
00436 }
00437
00438 void process_events(void)
00439 {
00440 SDL_Event event;
00441
00442 while(SDL_PollEvent(&event)){
00443 switch(event.type){
00444 case SDL_KEYDOWN:
00445 keyboard2(&event.key.keysym);
00446 break;
00447 case SDL_QUIT:
00448 quit_tutorial(0);
00449 break;
00450 }
00451 }
00452 }
00453
00454 void ourInit(void)
00455 {
00456
00457
00458 glClearColor(BGC,BGC,BGC,0.0);
00459 glMatrixMode(GL_PROJECTION);
00460 glLoadIdentity();
00461
00462
00463
00464 glEnable(GL_LINE_SMOOTH);
00465 glEnable(GL_POINT_SMOOTH);
00466 glEnable(GL_POLYGON_SMOOTH);
00467
00468 glEnable(GL_BLEND);
00469 glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
00470
00471 glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
00472
00473
00474 }
00475
00476 void myInit(int target[])
00477 {
00478 int i;
00479
00480 for(i=0;i<STIM*4;i++){
00481 if(i<STIM)target[i]=i;
00482 else if(i>=STIM && i<2*STIM)target[i]=i-STIM;
00483 else if(i>=2*STIM && i<3*STIM)target[i]=i-2*STIM;
00484 else if(i>=3*STIM && i<4*STIM)target[i]=i-3*STIM;
00485
00486 }
00487
00488 shuffle(target,STIM*4);
00489 }
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579 int main(int argc, char *argv[])
00580 {
00581 const SDL_VideoInfo* info = NULL;
00582 int c;
00583 int width = 0;
00584 int height = 0;
00585 int bpp = 0;
00586 uint32 flag = 0;
00587
00588 MYLOGVERB = LOG_INFO;
00589
00590
00591 ModelManager manager("Psycho SearchConjunction");
00592
00593 nub::soft_ref<EventLog> el(new EventLog(manager));
00594 manager.addSubComponent(el);
00595
00596 nub::soft_ref<EyeTrackerConfigurator>
00597 etc(new EyeTrackerConfigurator(manager));
00598 manager.addSubComponent(etc);
00599
00600 nub::soft_ref<PsychoDisplay> d(new PsychoDisplay(manager));
00601 manager.addSubComponent(d);
00602
00603 if(SDL_Init(SDL_INIT_VIDEO)<0)quit_tutorial(1);
00604
00605 info = SDL_GetVideoInfo();
00606 width = 500;
00607 height = 500;
00608 bpp = info->vfmt->BitsPerPixel;
00609
00610 manager.setOptionValString(&OPT_EventLogFileName, "searchConj.log");
00611 manager.setOptionValString(&OPT_EyeTrackerType, "ISCAN");
00612
00613
00614 if (manager.parseCommandLine(argc, argv, "<fileName>", 1, 1) == false)
00615 return(1);
00616
00617 nub::soft_ref<EyeTracker> et = etc->getET();
00618 d->setEyeTracker(et);
00619 d->setEventLog(el);
00620 et->setEventLog(el);
00621
00622
00623 if (etc->getModelParamString("EyeTrackerType").compare("EL") == 0)
00624 d->setModelParamVal("SDLslaveMode", true);
00625
00626
00627
00628 CheckFormat(argc);
00629
00630
00631
00632
00633
00634
00635
00636
00637 manager.start();
00638 el->pushEvent(std::string("===== Trial : Search Conjucntion Task ====="));
00639 et->calibrate(d);
00640
00641 d->clearScreen();
00642 d->clearScreen();
00643 d->displayText("Experiment start for space");
00644 d->waitForKey();
00645 d->clearScreen();
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655 flag =
00656 SDL_OPENGL |
00657 SDL_FULLSCREEN;
00658 strcpy(subj,argv[1]);
00659 printf("%s\n",subj);
00660 myInit(target);
00661 trial = 0;
00662
00663 if(SDL_SetVideoMode(width,height,32,flag) == 0)quit_tutorial(1);
00664
00665
00666
00667
00668 ourInit();
00669
00670 while(1){
00671
00672 et->track(true);
00673
00674 display();
00675 c = d->waitForKey();
00676
00677
00678 keyboard(c);
00679
00680 usleep(50000);
00681 et->track(false);
00682 if(trial == 4)break;
00683 }
00684
00685
00686 if(record == 1)Require_Result();
00687 printf("finish record\n");
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699 d->clearScreen();
00700 d->displayText("Experiment complete. Thank you!");
00701 d->waitForKey();
00702
00703
00704 manager.stop();
00705
00706
00707 return 0;
00708 }