// ###################################################################### void EyeTrackerEyeLink::recalibrate(nub::soft_ref<PsychoDisplay> d,int repeats) { #ifndef HAVE_EYELINK LFATAL("Proprietary EyeLink developer API not installed"); #else //create SDL_Surface of a image that replaces calibration point if(!d->getModelParamString("PsychoDisplayFixationIcon").empty()){ Image< PixRGB<byte> > fixicon = Raster::ReadRGB(d->getModelParamString("PsychoDisplayFixationIcon")); SDL_Surface *img = d->makeBlittableSurface(fixicon, false, PixRGB<byte>(target_background_color.r, target_background_color.g, target_background_color.b)); set_cal_target_surface(img); } do_drift_correct(d->getWidth()/2, d->getHeight()/2, 1, 1); // Get calibration result char message[256]; eyelink_cal_message(message); eyemsg_printf(message); // set offline mode to ensure trackign set_offline_mode(); #endif }
int gaze_control_trial(UINT32 time_limit) { UINT32 trial_start; /* trial start time (for timeout) */ UINT32 drawing_time; /* retrace-to-draw delay*/ int button; /* the button pressed (0 if timeout) */ int error; /* trial result code*/ ALLF_DATA evt; /* buffer to hold sample and event data*/ int eye_used = -1; /* indicates which eye's data to display*/ int i; SDL_Surface *gbm; /* The bitmap containing the stimulus display*/ /* This supplies the title at the bottom of the eyetracker display */ eyecmd_printf("record_status_message 'GAZE CONTROL TRIAL' "); /* Always send a TRIALID message before starting to record. It should contain trial condition data required for analysis. */ eyemsg_printf("TRIALID CONTROL"); /* TRIAL_VAR_DATA message is recorded for EyeLink Data Viewer analysis It specifies the list of trial variables value for the trial This must be specified within the scope of an individual trial (i.e., after "TRIALID" and before "TRIAL_RESULT") */ eyemsg_printf("!V TRIAL_VAR_DATA GAZECTRL"); /* IMGLOAD command is recorded for EyeLink Data Viewer analysis It displays a default image on the overlay mode of the trial viewer screen. Writes the image filename + path info */ eyemsg_printf("!V IMGLOAD FILL images/grid.png"); /* IAREA command is recorded for EyeLink Data Viewer analysis It creates a set of interest areas by reading the segment files Writes segmentation filename + path info */ eyemsg_printf("!V IAREA FILE segments/grid.ias"); /* Before recording, we place reference graphics on the EyeLink display*/ set_offline_mode(); /* Must be offline to draw to EyeLink screen*/ gbm = draw_grid_to_bitmap_segment("grid.ias", "segments", 1);;/* Draw bitmap and EyeLink reference graphics*/ segment_source = gbm; /* Save bitmap and transfer to the tracker pc. Since it takes a long to save the bitmap to the file, the value of sv_options should be set as SV_NOREPLACE to save time */ bitmap_save_and_backdrop(gbm, 0, 0, 0, 0, "grid.png", "images", SV_NOREPLACE, 0, 0, (UINT16)(BX_MAXCONTRAST|((eyelink_get_tracker_version(NULL)>=2)?0:BX_GRAYSCALE))); /* DO PRE-TRIAL DRIFT CORRECTION */ /* We repeat if ESC key pressed to do setup. */ while(1) { /* Check link often so we can exit if tracker stopped*/ if(!eyelink_is_connected()) return ABORT_EXPT; /* We let do_drift_correct() draw target in this example*/ /* 3rd argument would be 0 if we already drew the display*/ error = do_drift_correct((INT16)(SCRWIDTH/2), (INT16)(SCRHEIGHT/2), 1, 1); /* repeat if ESC was pressed to access Setup menu */ if(error!=27) break; } clear_full_screen_window(target_background_color); /* make sure display is blank*/ /* Configure EyeLink to send fixation updates every 50 msec*/ eyecmd_printf("link_event_filter = LEFT,RIGHT,FIXUPDATE"); eyecmd_printf("fixation_update_interval = 50"); eyecmd_printf("fixation_update_accumulate = 50"); init_regions(); /* Initialize regions for this display*/ /* Start data recording to EDF file, BEFORE DISPLAYING STIMULUS You should always start recording 50-100 msec before required otherwise you may lose a few msec of data */ error = start_recording(1,1,0,1); /* send events only through link*/ if(error != 0) /* ERROR: couldn't start recording*/ { SDL_FreeSurface(gbm); /* Be sure to delete bitmap before exiting!*/ return error; /* Return the error code*/ } /* record for 100 msec before displaying stimulus */ begin_realtime_mode(100); /* Windows 2000/XP: no interruptions from now on*/ /* DISPLAY OUR IMAGE TO SUBJECT by copying bitmap to display Because of faster drawing speeds and good refresh locking, we now place the stimulus onset message just after display refresh and before drawing the stimulus. This is accurate and will allow drawing a new stimulus each display refresh. However, we do NOT send the message after the retrace--this may take too long instead, we add a number to the message that represents the delay from the event to the message in msec */ /* COPY BITMAP to display*/ SDL_BlitSurface(gbm, NULL, window,NULL); Flip(window); drawing_time = current_msec(); /* time of retrace*/ trial_start = drawing_time; SDL_BlitSurface(gbm, NULL, window,NULL); /* delay from retrace (time to draw)*/ drawing_time = current_msec()-drawing_time; /* message for RT recording in analysis */ eyemsg_printf("%d DISPLAY ON", drawing_time); /* message marks zero-plot time for EDFVIEW */ eyemsg_printf("SYNCTIME %d", drawing_time); /* Print a title for the trial (for demo only)*/ get_new_font("Times Roman", 24, 1); graphic_printf(window,target_foreground_color, NONE, SCRWIDTH/2, 24, "Gaze Control Trial"); if(!eyelink_wait_for_block_start(100, 0, 1)) /* wait for link event data*/ { end_trial(); alert_printf("ERROR: No link events received!"); return TRIAL_ERROR; } eye_used = eyelink_eye_available(); /* determine which eye(s) are available*/ switch(eye_used) /* select eye, add annotation to EDF file */ { case RIGHT_EYE: eyemsg_printf("EYE_USED 1 RIGHT"); break; case BINOCULAR: /* both eye's data present: use left eye only*/ eye_used = LEFT_EYE; case LEFT_EYE: eyemsg_printf("EYE_USED 0 LEFT"); break; } /* Now get ready for trial loop*/ eyelink_flush_keybuttons(0); /* reset keys and buttons from tracker */ /* we don't use getkey() especially in a time-critical trial as Windows may interrupt us and cause an unpredicatable delay so we would use buttons or tracker keys only Trial loop: till timeout or response -- added code for processing FIXUPDATE events */ while(1) { /* First, check if recording aborted */ if((error=check_recording())!=0) return error; /* Check if trial time limit expired*/ if(current_time() > trial_start+time_limit) { eyemsg_printf("TIMEOUT"); /* message to log the timeout */ end_trial(); /* local function to stop recording*/ button = 0; /* trial result message is 0 if timeout */ break; /* exit trial loop*/ } if(break_pressed()) /* check for program termination or ALT-F4 or CTRL-C keys*/ { end_trial(); /* local function to stop recording*/ return ABORT_EXPT; /* return this code to terminate experiment*/ } /* check for local ESC key to abort trial (useful in debugging) */ if(escape_pressed()) { end_trial(); /* local function to stop recording*/ return SKIP_TRIAL; /* return this code if trial terminated*/ } /* BUTTON RESPONSE TEST */ /* Check for eye-tracker buttons pressed /* This is the preferred way to get response data or end trials */ button = eyelink_last_button_press(NULL); if(button!=0) /* button number, or 0 if none pressed*/ { /* message to log the button press*/ eyemsg_printf("ENDBUTTON %d", button); /* local function to stop recording*/ end_trial(); break; /* exit trial loop*/ } /* GET FIXUPDATE EVENTS, PROCESS*/ i = eyelink_get_next_data(NULL); /* Check for data from link*/ if(i == FIXUPDATE) /* only process FIXUPDATE events*/ { /* get a copy of the FIXUPDATE event */ eyelink_get_float_data(&evt); /* only process if it's from the desired eye?*/ if(evt.fe.eye == eye_used) { /* get average position and duration of the update */ process_fixupdate((int)(evt.fe.gavx), (int)(evt.fe.gavy),/* Process event */ evt.fe.entime-evt.fe.sttime); } } }/* end of loop*/ /* report response result: 0=timeout, else button number*/ eyemsg_printf("TRIAL_RESULT %d", button); /* Call this at the end of the trial, to handle special conditions*/ SDL_FreeSurface(gbm); segment_source = NULL; return check_record_exit(); }
// Trial using bitmap copying // Caller must create and destroy the bitmap int bitmap_recording_trial(HBITMAP gbm, UINT32 time_limit) { UINT32 trial_start; // trial start time (for timeout) UINT32 drawing_time; // retrace-to-draw delay int button; // the button pressed (0 if timeout) int error; // trial result code // NOTE: TRIALID AND TITLE MUST HAVE BEEN SET BEFORE DRIFT CORRECTION! // FAILURE TO INCLUDE THESE MAY CAUSE INCOMPATIBILITIES WITH ANALYSIS SOFTWARE! // DO PRE-TRIAL DRIFT CORRECTION // We repeat if ESC key pressed to do setup. while(1) { // Check link often so we can exit if tracker stopped if(!eyelink_is_connected()) return ABORT_EXPT; // We let do_drift_correct() draw target in this example // 3rd argument would be 0 if we already drew the fixation target error = do_drift_correct((INT16)(SCRWIDTH/2), (INT16)(SCRHEIGHT/2), 1, 1); // repeat if ESC was pressed to access Setup menu if(error!=27) break; } clear_full_screen_window(target_background_color); // make sure display is blank // Start data recording to EDF file, BEFORE DISPLAYING STIMULUS // You should always start recording 50-100 msec before required // otherwise you may lose a few msec of data error = start_recording(1,1,0,0); // record samples and events to EDF file only if(error != 0) return error; // ERROR: couldn't start recording // record for 100 msec before displaying stimulus begin_realtime_mode(100); // Windows 2000/XP: no interruptions till display start marked // DISPLAY OUR IMAGE TO SUBJECT by copying bitmap to display // Because of faster drawing speeds and good refresh locking, // we now place the stimulus onset message just after display refresh // and before drawing the stimulus. This is accurate and will allow // drawing a new stimulus each display refresh. // However, we do NOT send the message after the retrace--this may take too long // instead, we add a number to the message that represents the delay // from the event to the message in msec wait_for_video_refresh(); // synchronize before drawing so all seen at once drawing_time = current_msec(); // time of retrace trial_start = drawing_time; // record the display onset time display_bitmap(full_screen_window, gbm, 0, 0); // COPY BITMAP to display wait_for_drawing(full_screen_window); // wait till bitmap copy finished drawing_time = current_msec()-drawing_time; // delay from retrace (time to draw) eyemsg_printf("%d DISPLAY ON", drawing_time); // message for RT recording in analysis eyemsg_printf("SYNCTIME %d", drawing_time); // message marks zero-plot time for EDFVIEW // we would stay in realtime mode if timing is critical // for example, if a dynamic (changing) stimulus was used // or if display duration accuracy of 1 video refresh. was needed end_realtime_mode(); // we don't care as much about time now, allow keyboard to work // Now get ready for trial loop eyelink_flush_keybuttons(0); // reset keys and buttons from tracker // we don't use getkey() especially in a time-critical trial // as Windows may interrupt us and cause an unpredicatable delay // so we would use buttons or tracker keys only // Trial loop: till timeout or response while(1) { // First, check if recording aborted if((error=check_recording())!=0) return error; // Check if trial time limit expired if(current_time() > trial_start+time_limit) { eyemsg_printf("TIMEOUT"); // message to log the timeout end_trial(); // local function to stop recording button = 0; // trial result message is 0 if timeout break; // exit trial loop } if(break_pressed()) // check for program termination or ALT-F4 or CTRL-C keys { end_trial(); // local function to stop recording return ABORT_EXPT; // return this code to terminate experiment } if(escape_pressed()) // check for local ESC key to abort trial (useful in debugging) { end_trial(); // local function to stop recording return SKIP_TRIAL; // return this code if trial terminated } /* BUTTON RESPONSE TEST */ // Check for eye-tracker buttons pressed // This is the preferred way to get response data or end trials button = eyelink_last_button_press(NULL); if(button!=0) // button number, or 0 if none pressed { eyemsg_printf("ENDBUTTON %d", button); // message to log the button press end_trial(); // local function to stop recording break; // exit trial loop } } // END OF RECORDING LOOP end_realtime_mode(); // safety cleanup code while(getkey()); // dump any accumulated key presses // report response result: 0=timeout, else button number eyemsg_printf("TRIAL_RESULT %d", button); // Call this at the end of the trial, to handle special conditions return check_record_exit(); }
// Run gaze-contingent window trial // <fgbm> is bitmap to display within window // <bgbm> is bitmap to display outside window // <wwidth, wheight> is size of window in pixels // <mask> flags whether to treat window as a mask // <time_limit> is the maximum time the stimuli are displayed int gc_window_trial(HBITMAP fgbm, HBITMAP bgbm, int wwidth, int wheight, int mask, UINT32 time_limit) { UINT32 trial_start=0; // trial start time (for timeout) UINT32 drawing_time; // retrace-to-draw delay int button; // the button pressed (0 if timeout) int error; // trial result code ALLF_DATA evt; // buffer to hold sample and event data int first_display = 1; // used to determine first drawing of display int eye_used = 0; // indicates which eye's data to display float x, y; // gaze position // NOTE: TRIALID AND TITLE MUST HAVE BEEN SET BEFORE DRIFT CORRECTION! // FAILURE TO INCLUDE THESE MAY CAUSE INCOMPATIBILITIES WITH ANALYSIS SOFTWARE! // Set size and type of gaze-contingent window RECT display_rect; display_rect.top = dispinfo.top; display_rect.bottom = dispinfo.bottom; display_rect.left = dispinfo.left; display_rect.right = dispinfo.right; initialize_gc_window(wwidth, wheight, fgbm, bgbm, full_screen_window, display_rect, mask, SCRWIDTH/300); // sets 0.1 degree deadband // DO PRE-TRIAL DRIFT CORRECTION // We repeat if ESC key pressed to do setup. while(1) { // Check link often so we can exit if tracker stopped if(!eyelink_is_connected()) return ABORT_EXPT; // We let do_drift_correct() draw target in this example // 3rd argument would be 0 if we already drew the display error = do_drift_correct((INT16)(SCRWIDTH/2), (INT16)(SCRHEIGHT/2), 1, 1); // repeat if ESC was pressed to access Setup menu if(error!=27) break; } clear_full_screen_window(target_background_color); // make sure display is blank // Start data recording to EDF file, BEFORE DISPLAYING STIMULUS // You should always start recording 50-100 msec before required // otherwise you may lose a few msec of data // tell start_recording() to send link data error = start_recording(1,1,1,1); // record with link data enabled if(error != 0) return error; // ERROR: couldn't start recording // record for 100 msec before displaying stimulus begin_realtime_mode(100); // Windows 2000/XP: no interruptions from now on // DONT DISPLAY OUR IMAGES TO SUBJECT until we have first gaze postion! if(!eyelink_wait_for_block_start(100, 1, 0)) // wait for link sample data { end_trial(); alert_printf("ERROR: No link samples received!"); return TRIAL_ERROR; } eye_used = eyelink_eye_available(); // determine which eye(s) are available switch(eye_used) // select eye, add annotation to EDF file { case RIGHT_EYE: eyemsg_printf("EYE_USED 1 RIGHT"); break; case BINOCULAR: // both eye's data present: use left eye only eye_used = LEFT_EYE; case LEFT_EYE: eyemsg_printf("EYE_USED 0 LEFT"); break; } // Now get ready for trial loop eyelink_flush_keybuttons(0); // reset keys and buttons from tracker // we don't use getkey() especially in a time-critical trial // as Windows may interrupt us and cause an unpredicatable delay // so we would use buttons or tracker keys only // Trial loop: till timeout or response -- added code for reading samples and moving cursor while(1) { // First, check if recording aborted if((error=check_recording())!=0) return error; // Check if trial time limit expired if(current_time() > trial_start+time_limit && trial_start!=0) { eyemsg_printf("TIMEOUT"); // message to log the timeout end_trial(); // local function to stop recording button = 0; // trial result message is 0 if timeout break; // exit trial loop } if(break_pressed()) // check for program termination or ALT-F4 or CTRL-C keys { end_trial(); // local function to stop recording return ABORT_EXPT; // return this code to terminate experiment } if(escape_pressed()) // check for local ESC key to abort trial (useful in debugging) { end_trial(); // local function to stop recording return SKIP_TRIAL; // return this code if trial terminated } /* BUTTON RESPONSE TEST */ // Check for eye-tracker buttons pressed // This is the preferred way to get response data or end trials button = eyelink_last_button_press(NULL); if(button!=0) // button number, or 0 if none pressed { eyemsg_printf("ENDBUTTON %d", button); // message to log the button press end_trial(); // local function to stop recording break; // exit trial loop } // NEW CODE FOR GAZE CONTINGENT WINDOW if(eyelink_newest_float_sample(NULL)>0) // check for new sample update { eyelink_newest_float_sample(&evt); // get the sample x = evt.fs.gx[eye_used]; // yes: get gaze position from sample y = evt.fs.gy[eye_used]; if(x!=MISSING_DATA && y!=MISSING_DATA && evt.fs.pa[eye_used]>0) // make sure pupil is present { if(first_display) // mark display start AFTER first drawing of window { wait_for_video_refresh(); // synchronize before drawing so all seen at once drawing_time = current_msec(); // time of retrace trial_start = drawing_time; // record the display onset time } redraw_gc_window((int)x, (int)y); // move window if visible if(first_display) // mark display start AFTER first drawing of window { first_display = 0; drawing_time = current_msec() - drawing_time; // delay from retrace eyemsg_printf("%d DISPLAY ON", drawing_time); // message for RT recording in analysis eyemsg_printf("SYNCTIME %d", drawing_time); // message marks zero-plot time for EDFVIEW } } else { // Don't move window during blink // To hide window, use: redraw_gc_window(MISSING, MISSING); } } } // END OF RECORDING LOOP end_realtime_mode(); // safety cleanup code while(getkey()); // dump any accumulated key presses // report response result: 0=timeout, else button number eyemsg_printf("TRIAL_RESULT %d", button); // Call this at the end of the trial, to handle special conditions return check_record_exit(); }