/* ------------------------------------------------------------------------
          __
         / /_______________  ____  ___
        / __/ ___/ ___/ __ \/ __ \/ _ \
       / /_(__  ) /__/ /_/ / /_/ /  __/
       \__/____/\___/\____/ .___/\___/
                         /_/

    simon.c
    basic simon experiment

------------------------------------------------------------------------ */

#include <tscope.h>

// definitions that control which main function is used
// only one should be used, all others should be commented out

//#define TESTRND
#define REALEXP

/* ------------------------------------------------------------------------

    part 1: randomization of the trials

------------------------------------------------------------------------ */


// definitions of the number of cells and replications in the design
#define COLOR       2           // RED or GREEN
#define POSITION    2           // LEFT or RIGHT

#define CELLS       (COLOR*POSITION)
#define REP         3           // number of replications of each trial

#define NTRIALS (CELLS*REP)

// data structure containing all information about each trial
struct {
    int subj;                   // subject number
    int rmap;                   // response mapping
    int color;                  // color of the stimulus
    int position;               // position of the stimulus
    int xr;                     // expected response
    int r;                      // the response
    int corr;                   // correctness of the response
    int rt;                     // reaction time
    int re;                     // timing error
} data[NTRIALS];

#define TESTLIST    TRUE

void randomize(int subjnr)
{
    int i, tmp[NTRIALS];

    // run trials randomly or in a fixed sequence
    if (TESTLIST) {
        for (i = 0; i < NTRIALS; i++)
            tmp[i] = i % CELLS;
    } else
        ts_rlist(CELLS, REP, tmp);

    for (i = 0; i < NTRIALS; i++) {

        // fill in subject number and determine response mapping
        data[i].subj = subjnr;
        data[i].rmap = subjnr % 2;

        // determine stimulus number and category
        data[i].color = tmp[i] % COLOR;
        data[i].position = tmp[i] / COLOR;

        // determine the expected response
        // based on response mapping and stimulus category
        if (data[i].rmap == 0) {
            if (data[i].color == 0)
                data[i].xr = 2;
            else
                data[i].xr = 1;
        } else {
            if (data[i].color == 0)
                data[i].xr = 1;
            else
                data[i].xr = 2;
        }

        // initialize all other variables
        data[i].r = 0;
        data[i].corr = 0;
        data[i].rt = 0;
        data[i].re = 0;
    }
}


void writedata()
{
    // use subject number to determine output filename
    char filename[20];
    sprintf(filename, "data%d.rtd", data[0].subj);

    // open file for writing, test whether it is open
    FILE *outfile;
    outfile = fopen(filename, "a+");
    if (outfile == NULL)
        ts_fatal("error opening output file");

    int i;
    for (i = 0; i < NTRIALS; i++) {
        fprintf(outfile, "%d ", data[i].subj);
        fprintf(outfile, "%d ", data[i].rmap);
        fprintf(outfile, "%d ", data[i].color);
        fprintf(outfile, "%d ", data[i].position);
        fprintf(outfile, "%d ", data[i].xr);
        fprintf(outfile, "%d ", data[i].r);
        fprintf(outfile, "%d ", data[i].corr);
        fprintf(outfile, "%4d ", data[i].rt);
        fprintf(outfile, "%4d ", data[i].re);
        fprintf(outfile, "\n");
    }

    // close output file  
    fclose(outfile);
}

#ifdef TESTRND
int main()
{
    // make 4 output files 
    int i;
    for (i = 0; i < 4; i++) {
        randomize(i);
        writedata();
    }
    return 0;
}

END_OF_MAIN();
#endif


/* ------------------------------------------------------------------------

    part 2: trial and experiment structure

------------------------------------------------------------------------ */


// timing of the trial fields
#define FIXTM   500
#define BLANKTM 500
#define MAXTM   1500
#define FBTM    200
#define ITI     1500

// temporary timing data
struct {
    __int64 junk;               // for unused time values
    __int64 t1, e1;             // stimulus onset
    __int64 t2, e2;             // response onset
} tmp;

void trial(int i)
{
    // fixation
    ts_vsync(&tmp.junk, &tmp.junk);
    ts_printf_centre(0, 0, "+");
    ts_wait(mtt(FIXTM));

    // blank screen
    ts_vsync(&tmp.junk, &tmp.junk);
    ts_clrscr();

    // stimulus
    ts_vsync(&tmp.t1, &tmp.e1);
    if (data[i].color == 0)
        ts_fgcolor(RED);
    else
        ts_fgcolor(GREEN);

    ts_fill(ON);
    if (data[i].position == 0)
        ts_circle(-ax(.5), 0, 20);
    else
        ts_circle(ax(.5), 0, 20);
    ts_fill(OFF);
    ts_fgcolor(WHITE);

    // response
    data[i].r = ts_resp(&tmp.t2, &tmp.e2, mtt(MAXTM));
    ts_clrscr();

    // determine reaction time and timing error
    data[i].rt = ttm(tmp.t2 - tmp.t1);
    data[i].re = ttmu(tmp.e2 + tmp.e1);

    // escape program if user pressed escape
    if (data[i].r == 3)
        ts_fatal("aborted by user");

    // determine correctness of response
    if (data[i].r == data[i].xr)
        data[i].corr = 1;
    else
        data[i].corr = 0;

    // beep if response was wrong
    if (!data[i].corr)
        ts_playstream(mtt(FBTM));

    // intertrial interval              
    ts_wait(mtt(ITI));
}

#define TRIALTEST   TRUE

#ifdef REALEXP
int main()
{
    // go fullscreen if it's a real run
    if (!TRIALTEST) {
        ts_scrsize(SIZE3);
        ts_scrmode(FULLSCREEN);
    } else
        ts_scrsize(SIZE1);
    ts_init();

    // define reponse keys
    ts_defkey(M1);
    ts_defkey(M2);
    ts_defkey(KEY_ESC);         //escape-key 

    // determine subject number
    int subjnr;
    do {
        ts_clrscr();
        int y = ts_printf(-XMAX + 20, YMAX - 20, "Participant number: ");
        ts_scanf(-XMAX + 20 + y, YMAX - 20, "%d", &subjnr);
    } while (subjnr < 0 || subjnr > 20);

    // randomize trials
    randomize(subjnr);

    // give instructions to participants
    ts_clrscr();
    if (data[0].rmap == 0) {
        ts_printf_centre(0, 10,
                         "press the left mouse button if you see a red circle");
        ts_printf_centre(0, 0,
                         "press the right mouse button if you see a green circle");
    } else {
        ts_printf_centre(0, 10,
                         "press the right mouse button if you see a green circle");
        ts_printf_centre(0, 0,
                         "press the left mouse button if you see a red circle");
    }
    ts_printf_centre(0, -10, "press escape to stop");
    ts_button(XMAX - 20, -YMAX + 20);
    ts_clrscr();
    ts_wait(mtt(1000));

    // run the trials
    int i;
    for (i = 0; i < NTRIALS; i++)
        trial(i);

    // write data and exit
    writedata();
    ts_printf_centre(0, 0, "all done, thanks!");
    ts_button(XMAX - 20, -YMAX + 20);
    return 0;
}

END_OF_MAIN();
#endif


top
Persoonlijke pagina Universiteit GentTscope
Allegro | Cygwin | Gcc
© See license.html for copyright information