Sending video frame accurate triggers using Pixel Mode
This demo code tests Pixel Mode triggers using a psychopy window. Pass/fail reported in command window.
For more details on Pixel Mode, see Sending Triggers with Pixel Mode.
from psychopy import visual, core
from pypixxlib import _libdpx as dp
def drawPixelModeTrigger(win, pixelValue):
#takes a pixel colour and draws it as a single pixel in the top left corner of the window
#window must cover top left of screen to work
#interpolate must be set to FALSE before color is set
#call this just before flip to ensure pixel is drawn over other stimuli
topLeftCorner = [-win.size[0]/2, win.size[1]/2]
line = visual.Line(
win=win,
units = 'pix',
start=topLeftCorner,
end=[topLeftCorner[0]+1, topLeftCorner[1]],
interpolate = False,
colorSpace = 'rgb255',
lineColor = pixelValue)
line.draw()
def RGB2Trigger(color):
#helper function determines expected trigger from a given RGB 255 colour value
#return triggerVal
return int( (color[2]<<16)+(color[1]<<8)+color[0] )
def Trigger2RGB(trigger):
#helper function determines pixel mode RGB 255 colour value based on 24-bit trigger (in decimal, base 10)
# return [red, green, blue]
return [ trigger%256, (trigger>>8)%256, (trigger>>16)%256]
##
#START
#Initialize connection and set up some default parameters.
dp.DPxOpen()
dp.DPxEnableDoutPixelMode()
dp.DPxWriteRegCache()
win = visual.Window(
screen = 0, # change here to 1 to display on second screen!
monitor = None,
fullscr=True,
pos = [0,0],
color='black',
units = "pix"
)
testvals = [0, 64, 128, 191, 255]
failcount =0
print('\nStarting Pixel Mode Test\n\nTest#\tRGB225 Color\t Expected Dout Returned Dout')
for k in range(0,len(testvals)):
for i in range(0,3):
color =[0,0,0]
color[i] = testvals[k]
drawPixelModeTrigger(win, color)
win.flip()
core.wait(0.2)
dp.DPxUpdateRegCache()
value = dp.DPxGetDoutValue()
expectedValue = RGB2Trigger(color)
if value == expectedValue:
outcome = 'PASS'
else:
outcome = 'FAIL'
failcount +=1
# print('Color is: ', color, 'Expected value: ', expectedValue, ' , returned: ', value, ' ', outcome)
print('%d\t[% 4d,% 4d,% 4d]\t% 9d\t% 9d' % (i+3*k+1,color[0],color[1],color[2],expectedValue,value), ' ', outcome)
print('Tests run: %d, % d failed.\nPixel Mode Test Complete\n\n' % (len(testvals)*3, failcount) )
win.close()
dp.DPxDisableDoutPixelMode()
dp.DPxWriteRegCache()
dp.DPxClose()