How to use coroutines with inline scripts ?
I am trying to use coroutines with an inline script. I read the documentation (https://osdoc.cogsci.nl/3.2/manual/structure/coroutines/) but I really don't get how to do it.
What I need to do is the following :
I am running my experiment in MRI, and I need to get the triggers sent by the MRI to write them in a file, and change what is shown to the participant accordingly.
While I wait for these triggers, my participant is able to answer using a response box. While trying the experiment in real conditions, it happened that the participant responded close to a trigger, so some triggers were missed.
For these reasons, I really need to have a script running in the background, waiting for keys. What I tried so far is the following :
I have a function my_coroutine() that I define at the beginning of my experiment, in the prepare tab. I want to get keys in this function and set global variables accordingly.
key_pressed = None trigger_pressed = None key_pressed_time = 0 def my_coroutine(): global key_pressed global trigger_pressed global key_pressed_time yield while True: key, end_time = phase_1_keyboard.get_key(timeout=None) # If I got a trigger if key in trigger_keys: trigger_pressed = key # If I got a response else: key_pressed = key key_pressed_time = end_time
This generator is then called in a coroutines object in which I put the other script I want to run along with my coroutine.
In my inline script Display_target_1 that is triggered at each trial, I show a canvas, check if a trigger or a response key was pressed and do stuff accordingly.
canvas_trial.show() while count_trigger < 3: if trigger_pressed: trigger_pressed = None list_to_write = [str(x) for x in (1, block+1, self.get("run_task1")+1, trial+1, count_trigger+1, end_time, exp.get("Condition"))] trigger_files.write("\t".join(list_to_write) + "\n") count_trigger += 1 if key_pressed: key = key_pressed key_pressed = None if not response_given: var.Response_task1 = key var.Response_time_task1 = key_pressed_time - var.Begin_canvas_show canvas_trial['lure'].visible = False canvas_trial['target'].visible = False response_given = True
But when I launch my experiment, it seems I can't get past the initialize yield of my coroutine. Do you know what I am missing ?
Thanks for your help.