#######################################################################
############################   INFO   #################################
#######################################################################
/*
	Synaesthetic Simon Go/NoGo
 
	based on Simon Go/NoGo Chmielewski WX and Beste C (2017)

	Synaesthesia Modulationh
	Witold Chmielewski & Marie-Luise Schreiter
	 TU Dresden / University Clinics, Germany

 Codes
 
 		synaesthetic color trials 
   11		letter "A" (in the synaesthetically associated color) on the left side (left press / key 1 required)
   12		letter "A" (in the synaesthetically associated color) on the right side (left press / key 1 required)
   13		letter "E" (in the synaesthetically associated color) on the left side (right press / key 2 required)
   14		letter "E" (in the synaesthetically associated color) on the right side (right press / key 2 required)

  111		letter "A" (in the synaesthetically associated color) on the left side (Nogo / no response required)
  112		letter "A" (in the synaesthetically associated color) on the right side (Nogo / no response required)
  113		letter "E" (in the synaesthetically associated color) on the left side (Nogo / no response required)
  114		letter "E" (in the synaesthetically associated color) on the right side (Nogo / no response required)


 		non-synaesthetic color (/synaesthetic color for other letter) trials 
   21		letter "A" (synaesthetically associated color for T) on the left side (left press / key 1 required)
   22		letter "A" (synaesthetically associated color for T) on the right side (left press / key 1 required)
   23		letter "E" (synaesthetically associated color for S) on the left side (right press / key 2 required)
   24		letter "E" (synaesthetically associated color for S) on the right side (right press / key 2 required)

  121		letter "A" (synaesthetically associated color for T) on the left side (Nogo / no response required)
  122		letter "A" (synaesthetically associated color for T) on the right side (Nogo / no response required)
  123		letter "E" (synaesthetically associated color for S) on the left side (Nogo / no response required)
  124		letter "E" (synaesthetically associated color for S) on the right side (Nogo / no response required)

 		

 		Other codes
 90		(THIS WAS ERASED AS IT COULD BE INTERPRETED AS AN INDIDATOR FOR GO TRIALS) speed warning ("Schneller!"); will be given in case no response occurs within 500 msec after presentation of stimuli
 91 		instructions standard trials
 92 		instructions cross trials: hand condition
 94 		pause
 95 		continue
 96		excercise
 97		experiment
 99 		end */





#######################################################################
############################   HEADER   ###############################
#######################################################################

#scenario parameter
default_background_color = 0, 0, 0;					#should be black
default_text_color= 255, 255, 255;					#should be white
default_font_size=20;
default_font="arial";
active_buttons= 3;										#needs to be selected on the response pannel of the presentation window
button_codes = 1,2,3;									#define: 1=left key/pannel, 2=right key/pannel, 3=SHAM
response_matching=simple_matching;

#DEACTIVATE IN ORDER TO NOT SEND TRIGGERS
#EEG parameters 
write_codes=true;                     
pulse_width=5;
response_port_output = true;						



#######################################################################
############################   SDL part   #############################
#######################################################################

default_formatted_text = true;

#scenario
begin;


###INSTRUCTIONS

#commands & infos

text { caption="Experiment"; font_size=40;}experiment_text;
picture {text experiment_text; x=0;y=0;} pic_experiment;

text { caption="Pause"; font_size=40;}pause_text;
picture {text pause_text; x=0;y=0;} pic_pause;

text { caption="Bitte drücken Sie eine Taste, um fortzufahren!"; font_size=30;}continue_text;
picture {text continue_text; x=0;y=0;} pic_continue;

text { caption="Ende"; font_size=40;}end_text;
picture {text end_text; x=0; y=0;} pic_end;

#task explanation
text { caption=
"Bitte legen Sie nun den LINKEN ZEIGEFINGER auf die LINKE TASTE und den rechten Zeigefinger
auf die rechte Taste, so dass Ihre Arme parallel zueinander sind.

Wenn der gezeigte Bustabe ein NORMALES A ist, drücken Sie bitte möglichst schnell die LINKE Taste.
Ist der Buchstabe hingegen ein NORMALES E, drücken Sie bitte die RECHTE Taste.

Ist der Buchstabe <i>kursiv</i> und <b>fett</b> (<b><i>A</i></b> / <b><i>E</i></b>), drücken Sie bitte keine der Tasten und warten den nächsten Buchstaben ab.

Drücken Sie bitte einen Knopf, um fortzufahren.";
font_size=20; }instructionsstd2;
picture {text instructionsstd2; x=0;y=0; } pic_instructions_par;

#box
line_graphic {
   polygon_coordinates = -25, 25, 25, 25, 25, -25, -25, -25;
line_width = 2;
}box_both;

#fixation cross
text {caption="+";}fixationcross; 
picture {text fixationcross; x=0;y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} fixation_cross;

text { caption="Schneller";font_size=30; font_color=0,0,0;}speed_up_text; # the speed-up signal was made invisible in order to not indicate whether a Go or NoGo trial is presented

picture {box {height=1; width=1; color=255,255,255; }; x=0;y=0;} placeholder;


###STIMULI

# synaesthetic targets PLEASE ADAPT THE COLORS FOR THE SYNAESTHETICALLY ASSOCIATED LETTERS
text {caption="A"; font_size=30; font_color=0,255,0;}A_syn_Go;							# use color for a
text {caption="<b><i>A</i></b>"; font_size=30; font_color=0,255,0;}A_syn_NoGo;	# use color for a
text {caption="E"; font_size=30; font_color=255,0,0;}E_syn_Go;							# use color for e
text {caption="<b><i>E</i></b>"; font_size=30; font_color=255,0,0;}E_syn_NoGo;	# use color for e

# non-synaesthetic targets PLEASE ADAPT THE COLORS FOR THE NON_SYNAESTHETICALLY ASSOCIATED LETTERS
text {caption="A"; font_size=30; font_color=255,236,4;}A_nonsyn_Go; 						# use color for t
text {caption="<b><i>A</i></b>"; font_size=30; font_color=255,236,4;}A_nonsyn_NoGo; # use color for t
text {caption="E"; font_size=30; font_color=0,255,255;}E_nonsyn_Go; 						# use color for s
text {caption="<b><i>E</i></b>"; font_size=30; font_color=0,255,255;}E_nonsyn_NoGo;	# use color for s


# noise 
line_graphic {
   coordinates = -9, 9, 9, 9;
   coordinates = -9, 0, 9, 0;
   coordinates = -9, -9, 9, -9;
line_width = 2;
}noise_both;

picture {text fixationcross; x=0;y=0; text A_syn_Go; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_syn_Go_L;
picture {text fixationcross; x=0;y=0; text A_syn_Go; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_syn_Go_R;
picture {text fixationcross; x=0;y=0; text E_syn_Go; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_syn_Go_L;
picture {text fixationcross; x=0;y=0; text E_syn_Go; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_syn_Go_R;
picture {text fixationcross; x=0;y=0; text A_syn_NoGo; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_syn_NoGo_L;
picture {text fixationcross; x=0;y=0; text A_syn_NoGo; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_syn_NoGo_R;
picture {text fixationcross; x=0;y=0; text E_syn_NoGo; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_syn_NoGo_L;
picture {text fixationcross; x=0;y=0; text E_syn_NoGo; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_syn_NoGo_R;

picture {text fixationcross; x=0;y=0; text A_nonsyn_Go; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_nonsyn_Go_L;
picture {text fixationcross; x=0;y=0; text A_nonsyn_Go; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_nonsyn_Go_R;
picture {text fixationcross; x=0;y=0; text E_nonsyn_Go; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_nonsyn_Go_L;
picture {text fixationcross; x=0;y=0; text E_nonsyn_Go; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_nonsyn_Go_R;
picture {text fixationcross; x=0;y=0; text A_nonsyn_NoGo; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_nonsyn_NoGo_L;
picture {text fixationcross; x=0;y=0; text A_nonsyn_NoGo; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} A_nonsyn_NoGo_R;
picture {text fixationcross; x=0;y=0; text E_nonsyn_NoGo; x=-65; y=0; line_graphic noise_both; x=65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_nonsyn_NoGo_L;
picture {text fixationcross; x=0;y=0; text E_nonsyn_NoGo; x=65; y=0; line_graphic noise_both; x=-65; y=0;line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0;} E_nonsyn_NoGo_R;

picture {text fixationcross; x=0;y=0; line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0; text speed_up_text; x=0;y=100;}speed_up; # not visible, if intended to present change color at text (line 131)
picture {text fixationcross; x=0;y=0; line_graphic box_both; x=-65; y=0; line_graphic box_both; x=65; y=0; }NO_speed_up;


#GENERATE TRIALS

#instructions & continue sign
trial { 
trial_duration = forever;	
trial_type = first_response;	
stimulus_event {
picture pic_instructions_par;
time=0;	
code="91";
port_code=91;
}forever_event;	
}forever_trial;	

#experiment / pause / end sign
trial{
all_responses=false;
trial_duration=1000;
stimulus_event {
picture pic_experiment;
time=0;
duration=next_picture;
code="97";
port_code=97;
}fixed_event;
}fixed_trial;

#Main trial
trial {
trial_type = first_response;
trial_duration=1400;
stimulus_event {
picture A_syn_Go_L; 		#adjust in PCL
time=0;
target_button = 1;		#adjust in PCL
duration=200;
code="93";					#adjust in PCL
port_code=93;				#adjust in PCL
} target_event;
stimulus_event{
picture fixation_cross; 
time=200;
duration=next_picture;
};
stimulus_event{
picture speed_up;       # not visible in the experiment change color in line (line 131) in order to present
time=500;
duration=next_picture;
}speed_event;
}main_trial;

#response-stimulus intervall trial
trial{
all_responses=false; 
trial_duration=1700;   # adjust in PCL part
stimulus_event{
picture fixation_cross; 
time=0;
duration=next_picture;
};
}rsi_intervall_trial;


#######################################################################
############################   PCL part   #############################
#######################################################################


#pcl
begin_pcl;

# variables for quick adjustments
int blocks=7;											#enter any even number
int trialsperblock=160;								#enter any number which can be divided by forty
int number_of_syn_GOs=(trialsperblock/80)*7;
int number_of_nonsyn_GOs=(trialsperblock/80)*7;	
int number_of_syn_NOGOs=(trialsperblock/80)*3;
int number_of_nonsyn_NOGOs=(trialsperblock/80)*3;	
int number_of_syns=(trialsperblock/2);	
int totaltrials=blocks*trialsperblock;			#do not adjust this parameter

# fill experiment array with stimuli
array <int> conditions[trialsperblock];
loop
   int i=1
   until i>trialsperblock
   begin
      if 																					i<(1*number_of_syn_GOs)+1 				  						then conditions[i]=1; #syn_GOs
			elseif 	i>(1*number_of_syn_GOs) 									&& i<(2*number_of_syn_GOs)+1										then conditions[i]=2;
			elseif 	i>(2*number_of_syn_GOs) 									&& i<(3*number_of_syn_GOs)+1										then conditions[i]=3;
			elseif 	i>(3*number_of_syn_GOs) 									&& i<(4*number_of_syn_GOs)+1										then conditions[i]=4;
			elseif 	i>(4*number_of_syn_GOs)										&& i<(4*number_of_syn_GOs)+(1*number_of_syn_NOGOs)+1		then conditions[i]=5; #syn_NOGOs
			elseif 	i>(4*number_of_syn_GOs)+(1*number_of_syn_NOGOs) 	&& i<(4*number_of_syn_GOs)+(2*number_of_syn_NOGOs)+1		then conditions[i]=6;
			elseif 	i>(4*number_of_syn_GOs)+(2*number_of_syn_NOGOs) 	&& i<(4*number_of_syn_GOs)+(3*number_of_syn_NOGOs)+1		then conditions[i]=7;
			elseif 	i>(4*number_of_syn_GOs)+(3*number_of_syn_NOGOs) 	&& i<(4*number_of_syn_GOs)+(4*number_of_syn_NOGOs)+1		then conditions[i]=8;

		   elseif 	i>(number_of_syns)																		&&	i<(number_of_syns)+(1*number_of_nonsyn_GOs)+1 				  							then conditions[i]=11; #nonsyn_GOs
			elseif 	i>(number_of_syns)+(1*number_of_nonsyn_GOs) 										&& i<(number_of_syns)+(2*number_of_nonsyn_GOs)+1											then conditions[i]=12;
			elseif 	i>(number_of_syns)+(2*number_of_nonsyn_GOs) 										&& i<(number_of_syns)+(3*number_of_nonsyn_GOs)+1											then conditions[i]=13;
			elseif 	i>(number_of_syns)+(3*number_of_nonsyn_GOs) 										&& i<(number_of_syns)+(4*number_of_nonsyn_GOs)+1											then conditions[i]=14;
			elseif 	i>(number_of_syns)+(4*number_of_nonsyn_GOs)										&& i<(number_of_syns)+(4*number_of_nonsyn_GOs)+(1*number_of_nonsyn_NOGOs)+1		then conditions[i]=15; #nonsyn_NOGOs
			elseif 	i>(number_of_syns)+(4*number_of_nonsyn_GOs)+(1*number_of_nonsyn_NOGOs) 	&& i<(number_of_syns)+(4*number_of_nonsyn_GOs)+(2*number_of_nonsyn_NOGOs)+1		then conditions[i]=16;
			elseif 	i>(number_of_syns)+(4*number_of_nonsyn_GOs)+(2*number_of_nonsyn_NOGOs) 	&& i<(number_of_syns)+(4*number_of_nonsyn_GOs)+(3*number_of_nonsyn_NOGOs)+1		then conditions[i]=17;
			elseif 	i>(number_of_syns)+(4*number_of_nonsyn_GOs)+(3*number_of_nonsyn_NOGOs) 	&& i<(number_of_syns)+(4*number_of_nonsyn_GOs)+(4*number_of_nonsyn_NOGOs)+1		then conditions[i]=18;
		
		end;
	i=i+1;
end;

#needed for subroutines
int z=1;
int j=1;

# arrays for the output files
array <int> mytrial [totaltrials];		#array for the trial number
mytrial.fill( 1, totaltrials, 1, 1);
array <string> mytype [totaltrials];	#array for the condition
array <int> myblock [totaltrials];		#array for the block
array <int> mycode [totaltrials];	#array for the codes of events
array <int> myhits[totaltrials];			#array for the type (hit: 0/1)
array <int> myincorrects[totaltrials];	#array for the type (incorrect: 0/1)
array <int> mymisses[totaltrials];		#array for the type (miss: 0/1)
array <int> myspeedup[totaltrials];		#array for speed up signs (0/1)
array <int> rtime [totaltrials];			#array for the reaction times

#create additional output files
output_file relevantdata_ede = new output_file;
relevantdata_ede.open("key-results_ede_Nogo_"+logfile.subject());
relevantdata_ede.print("code\thits\tincorrects\tmisses\tRT\n");
relevantdata_ede.close();

output_file relevantdata_simon = new output_file;
relevantdata_simon.open("key-results_simon_Nogo_Synaesthesia_"+logfile.subject());
relevantdata_simon.print("\nnumber-of-blocks\t");
relevantdata_simon.print(blocks);
relevantdata_simon.print("\ntrials-per-block\t");
relevantdata_simon.print(trialsperblock);
relevantdata_simon.print("\n\ntrial_number\tblock\tcondition\tcode\thits\tincorrects\tmisses\tspeedup_sign\tRT\n");
relevantdata_simon.close();


###Subroutines

sub
	sub_block
begin
	
	forever_trial.present();
	rsi_intervall_trial.present(); 
	
	loop
		j=1
		until j>trialsperblock
		begin
		
			#adjust stimuli
			if 			conditions[j]==1 	then	target_event.set_stimulus(A_syn_Go_L);
				elseif 	conditions[j]==2	then	target_event.set_stimulus(A_syn_Go_R);
				elseif 	conditions[j]==3	then	target_event.set_stimulus(E_syn_Go_L);
				elseif 	conditions[j]==4	then	target_event.set_stimulus(E_syn_Go_R);
				elseif 	conditions[j]==5 	then	target_event.set_stimulus(A_syn_NoGo_L);
				elseif 	conditions[j]==6	then	target_event.set_stimulus(A_syn_NoGo_R);
				elseif 	conditions[j]==7	then	target_event.set_stimulus(E_syn_NoGo_L);
				elseif 	conditions[j]==8	then	target_event.set_stimulus(E_syn_NoGo_R);
			
				elseif 	conditions[j]==11 then	target_event.set_stimulus(A_nonsyn_Go_L);
				elseif 	conditions[j]==12	then	target_event.set_stimulus(A_nonsyn_Go_R);
				elseif 	conditions[j]==13	then	target_event.set_stimulus(E_nonsyn_Go_L);
				elseif 	conditions[j]==14	then	target_event.set_stimulus(E_nonsyn_Go_R);
				elseif 	conditions[j]==15 then	target_event.set_stimulus(A_nonsyn_NoGo_L);
				elseif 	conditions[j]==16	then	target_event.set_stimulus(A_nonsyn_NoGo_R);
				elseif 	conditions[j]==17	then	target_event.set_stimulus(E_nonsyn_NoGo_L);
				elseif 	conditions[j]==18	then	target_event.set_stimulus(E_nonsyn_NoGo_R);
			end;
				
			#adjust codes
			if 			conditions[j]< 5 								then	mycode[j]=conditions[j]+10;
																						speed_event.set_stimulus(speed_up);
				elseif	conditions[j]> 4 	&& conditions[j]<9 	then	mycode[j]=conditions[j]+106;
																						speed_event.set_stimulus(NO_speed_up);
				elseif	conditions[j]>10 	&& conditions[j]<15 	then	mycode[j]=conditions[j]+10;
																						speed_event.set_stimulus(speed_up);
				elseif	conditions[j]>14 								then	mycode[j]=conditions[j]+106;
																						speed_event.set_stimulus(NO_speed_up);
																						
																						
			end;
			target_event.set_event_code(string(mycode[j]));
			target_event.set_port_code(mycode[j]);
			
			#adjust buttons
			if 			conditions[j]==1 || conditions[j]==2 || conditions[j]==11 || conditions[j]==12		then	target_event.set_target_button(1); # Go left button
				elseif	conditions[j]==3 || conditions[j]==4 || conditions[j]==13 || conditions[j]==14		then	target_event.set_target_button(2); # Go right button
				elseif	conditions[j]==5 || conditions[j]==6 || conditions[j]== 7 || conditions[j]== 8 || conditions[j]==15 || conditions[j]==16 || conditions[j]== 17 || conditions[j]== 18 								
																																			then	target_event.set_target_button(3); # NoGo sham button used in order to generate NoGo trials
			end;
		
			#present trial
			main_trial.present();
			
			#obtain relevant data 
			myhits[j]=response_manager.hits();
			myincorrects[j]=response_manager.incorrects();
			mymisses[j]=response_manager.misses();
			stimulus_data laststim = stimulus_manager.last_stimulus_data();
			rtime[j]= laststim.reaction_time();

			#present RSI
			rsi_intervall_trial.set_duration(random(850,1050));
			rsi_intervall_trial.present(); 
		j=j+1; 
	end
end;


sub
	sub_keyfiles
begin
	loop
		j=1
		until j>trialsperblock
		begin
		mytype[j]="parallel_hands";
		myblock[j]=z;	
		j=j+1; 
	end;
	
	loop
		j=1
		until j>trialsperblock
		begin
			if rtime[j]>500 then	myspeedup[j]=1;
			else						myspeedup[j]=0;
		end;
		j=j+1; 
	end;

	relevantdata_simon.open_append("key-results_simon_Nogo_Synaesthesia_"+logfile.subject());
		loop
			int i=1
			until i>trialsperblock
			begin
			relevantdata_simon.print(mytrial[i+((z-1)*trialsperblock)]);			# trial number
			relevantdata_simon.print( "\t" );				# new column ( "\t" );
			relevantdata_simon.print(myblock[i]);			# block
			relevantdata_simon.print( "\t" );		
			relevantdata_simon.print(mytype[i]);			# condition
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(mycode[i]);			# code
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(myhits[i]);			# hits
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(myincorrects[i]);	# incorrects
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(mymisses[i]);			# misses
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(myspeedup[i]);		# speed up signs
			relevantdata_simon.print( "\t" );				
			relevantdata_simon.print(rtime[i]);				# RT
			relevantdata_simon.print( "\n" );				# end
		i=i+1;
		end;
	  relevantdata_simon.close();

	#print file for Edmund/vincentization
	relevantdata_ede.open_append("key-results_ede_Nogo_"+logfile.subject());
			loop
				int i=1
				until i>trialsperblock
				begin
				relevantdata_ede.print(mycode[i]);	# code
				relevantdata_ede.print( "\t" );		# new column ( "\t" );
				relevantdata_ede.print(myhits[i]);	# type
				relevantdata_ede.print( "\t" );
				relevantdata_ede.print(myincorrects[i]);	# type
				relevantdata_ede.print( "\t" );
				relevantdata_ede.print(mymisses[i]);	# type
				relevantdata_ede.print( "\t" );
				relevantdata_ede.print(rtime[i]);		#rt
				relevantdata_ede.print( "\n" );			#end
			i=i+1;
			end;
		relevantdata_ede.close()
end;

sub
	sub_inbetween
begin
	if z==blocks then	fixed_event.set_stimulus(pic_end);
							fixed_event.set_duration(1000);
							fixed_event.set_event_code("99");
							fixed_event.set_port_code(99);
							fixed_trial.present();
				else		fixed_event.set_stimulus(pic_pause);
							fixed_event.set_duration(5000);
							fixed_event.set_event_code("94");
							fixed_event.set_port_code(94);
							forever_event.set_stimulus(pic_continue);
							forever_event.set_event_code("95");
							forever_event.set_port_code(95);
							fixed_trial.present();
							forever_trial.present();
	end
end;


###RUN EXPERIMENT

fixed_trial.present();

loop
	 z=1
	until z>blocks
	begin
		conditions.shuffle();
		sub_block();
		sub_keyfiles();
		sub_inbetween();
	z=z+1; 
end;