#!/usr/bin/env python
#rotate flashing wedge
from psychopy import core, visual, event, gui

from numpy import *

myMonitorName = 'PeterMonitor'

myRadialCycles=4.0
myAngularCycles=16.0
cycleDuration = 36.0 #secs for full cycle
nCycles = 8

globalClock = core.Clock()
win = visual.Window([1024,768], allowGUI=False, units='deg', monitor=myMonitorName, fullscr=True, waitBlanking=True)

#fixation - large x for albinism
fixNVertices2 = [[-1,-1],[1,1]]
fixNVertices1 = [[-1,1],[1,-1]]

largeFixn1 = visual.ShapeStim(win, units ='norm',
                 lineColor='red',
                 lineWidth=5.0, #in pixels
                 fillColor=None, #beware, with convex shapes fill colors don't work
                 vertices=fixNVertices1,#choose something from the above or make your own
                 closeShape=False,#do you want the final vertex to complete a loop with 1st?
                 pos= [0.0,0.0], #the anchor (rotaion and vertices are position with respect to this)
                 interpolate=True,
                 opacity=1.0,
                 autoLog=False)#this stim changes too much for autologging to be useful

largeFixn2 = visual.ShapeStim(win, units ='norm',
                 lineColor='red',
                 lineWidth=5.0, #in pixels
                 fillColor=None, #beware, with convex shapes fill colors don't work
                 vertices=fixNVertices2,#choose something from the above or make your own
                 closeShape=False,#do you want the final vertex to complete a loop with 1st?
                 pos= [0,0], #the anchor (rotaion and vertices are position with respect to this)
                 interpolate=True,
                 opacity=1.0,
                 autoLog=False)#this stim changes too much for autologging to be useful



#make two wedges (in opposite contrast) and alternate them for flashing
wedge1 = visual.RadialStim(win, tex='sqrXsqr', color=1,size=30, 
     radialCycles=myRadialCycles, angularCycles=myAngularCycles, interpolate=False,
    autoLog=False, ori=360.0/64,angularRes=128,texRes=360,mask=[1,1,1,0,0,0,0,0])

wedge2 = visual.RadialStim(win, tex='sqrXsqr', color=-1,size=30, 
    radialCycles=myRadialCycles, angularCycles=myAngularCycles, interpolate=False,
    autoLog=False, ori=360.0/64,angularRes=128,texRes=360,mask=[1,1,1,0,0,0,0,0])


# there is an edge effect on the outer edge of the stimulus 
maskFirstCycle = visual.RadialStim(win, tex='sqrXsqr', color=0, size=32, 
    radialCycles=0, angularCycles=0, interpolate=True, visibleWedge=[0, 360],
    autoLog=False, ori=0, angularRes=360, mask=[0,0,0,0,0,0,0,1])

#mask a hemifield as required
maskHemiR = visual.RadialStim(win, tex='sqrXsqr', color=0,size=31, 
    radialCycles=0, angularCycles=0, interpolate=False, visibleWedge=[180, 360],
    autoLog=False, ori=0, angularRes=360)

maskHemiL = visual.RadialStim(win, tex='sqrXsqr', color=0,size=31, 
    radialCycles=0, angularCycles=0, interpolate=False, visibleWedge=[0,180],
    autoLog=False, ori=0, angularRes=360)

# l or r masked?
hemiMask = maskHemiR

stepTimes = linspace(0, cycleDuration, myRadialCycles*2+1)
stepTimes = stepTimes[1:]
0
#mask vectors used to create the expenading ring effect
myMaskVectors = [[1,1,1,0,0,0,0,0],
                                [0,1,1,1,0,0,0,0],
                                [0,0,1,1,1,0,0,0],
                                [0,0,0,1,1,1,0,0],
                                [0,0,0,0,1,1,1,0],
                                [0,0,0,0,0,1,1,1],
                                [1,0,0,0,0,0,1,1],
                                [1,1,0,0,0,0,0,1]]


#wait for button press to start
import time
myGo = 0

while myGo == 0:
    for keys in event.getKeys():
        if keys in ['space']:
            myGo = 1
        else:
            time.sleep(0.01)

largeFixn1.draw()
largeFixn2.draw()
win.flip()
#dummy vols
time.sleep(12)


t=0
globalClock.reset()

for i in range(nCycles):
    cycleClock = globalClock.getTime()
    print cycleClock
    for j in range(len(stepTimes)):
        wedge1.setMask(myMaskVectors[j])
        wedge2.setMask(myMaskVectors[j])
        
        while globalClock.getTime()-cycleClock < stepTimes[j]:
            
            for i in range(6):
                wedge1.draw()
                if j == 0: #first cycle
                    maskFirstCycle.draw()
                hemiMask.draw()
                largeFixn1.draw()
                largeFixn2.draw()
                win.flip()
                
            for i in range(6):
                wedge2.draw()
                if j == 0: #first cycle
                    maskFirstCycle.draw()
                hemiMask.draw()
                largeFixn1.draw()
                largeFixn2.draw()
                win.flip()

        #handle key presses each frame
        for key in event.getKeys():
            if key in ['escape','q']:
                win.close()
                core.quit()

print globalClock.getTime()
win.close()

