Sending EEG triggers in the Builder with custom code

OS (e.g. Win10): Ubuntu
PsychoPy version (e.g. 2024.2.4 Py 3.8): Psychopy 2024.2.4 Python 3.10.11
Standard Standalone Installation? (y/n) If not then what?: y
Do you want it to also run online? (y/n) n
What are you trying to achieve?:

I have a go/no-go task that sends EEG triggers via parallel port, and I need precise timing for ERPs. I built the task in Builder, but the Parallel Out component doesn’t fit my use case because:

  1. I run the task on both Linux and Windows and want to set the port address based on the OS.

  2. On Linux, the port is only recognised if I leave the address field blank (instead of specifying its address /dev/parport0)

So I want to send triggers from custom code, with a 10ms pulse, synced to when the digit is presented on the screen.

What did you try to make it work?:

I looked at the code generated by the Parallel Out component and tried to replicate its behaviour by adding this custom code to the digit presentation routine:

Each Frame

if p_port.status == NOT_STARTED and digit.status == STARTED:
p_port.status = STARTED
win.callOnFlip(p_port.setData, int(1))

if tThisFlipGlobal > p_port.tStartRefresh + 0.01 - frameTolerance:
p_port.status = FINISHED
win.callOnFlip(p_port.setData, int(0))

End Routine

if p_port.status == STARTED:
win.callOnFlip(p_port.setData, int(0))

Would this work well in terms of timing and duration, or is there something else that the Parallel Out component handles that I should keep in mind?
Or overall, any better suggestion for sending triggers via custom code?

Link to the most relevant existing thread you have found: Trigger delays are highly variable and imprecise - #3 by IRIO

Depends on the refresh rate of your monitor. Remember, this code runs EACH FRAME, so if you have 60 or 120hz, the timing will differ. So if you need to send a EEG pulse every 10ms, that is not going to be doable, or even once 10ms after stimuli onset. (60hz ~= 16.6ms, 120hz = 8.3ms)

The EEG labs I work with will have 2 computers in this setup, so one can handle the EEG triggering, and one handles the psychopy experiment. And they talk with each other about when stuff happens. With one computer, you would need an additional program, or trigger box, to get the “Trial has started” msg from psychopy, and then the intermediary will know to trigger ~9ms after receiving that message.

Issac