#!/usr/bin/env python

# Display a moving sinusoidal grating

from VisionEgg.Core import *
from VisionEgg.Gratings import *
from VisionEgg.AppHelper import *

# Define a controller function to move the grating
def phase_as_function_of_time(t):
    temporal_frequency_hz = 5.0
    return temporal_frequency_hz*t*360.0

# Define a second controller to turn off grating between presentations
def on_during_experiment(t):
    if t < 0.0:
        return 0 # off between stimuli
    else:
        return 1 # on during stimulus presentation
    
# Initialize OpenGL graphics screen.
screen = get_default_screen()

# Use viewport with pixel coordinate system for projection
viewport = Viewport(screen=screen)

# Calculate where the lower left corner of the grating must be to be centered
grating_size = 256
grating_left   = (screen.size[0]/2) - (grating_size/2)
grating_bottom = (screen.size[1]/2) - (grating_size/2)

# Create the instance SinGrating with appropriate parameters
stimulus = SinGrating2D(lowerleft   = ( grating_left , grating_bottom ),
                        size        = ( grating_size , grating_size ),
                        wavelength  = grating_size / 10.0, # 10 cycles over this size
                        orientation = 90.0 )

# Add the stimulus to the viewport
viewport.add_stimulus(stimulus)

# Create an instance of the Presentation class
p = Presentation(duration=(5.0,'seconds'),viewports=[viewport])

# Register the controller functions
p.add_realtime_time_controller(stimulus,'phase', phase_as_function_of_time)
p.add_transitional_controller(stimulus,'on', on_during_experiment)

# Go!
p.go()

@@ -156,548 +156,217 @@
  *%0A%0A
-# Define a controller function to move the grating%0Adef phase_as_function_of_time(t):%0A    temporal_frequency_hz = 5.0%0A    return temporal_frequency_hz*t*360.0%0A%0A# Define a second controller to turn off grating between presentations%0Adef on_during_experiment(t):%0A    if t %3C 0.0:%0A        return 0 # off between stimuli%0A    else:%0A        return 1 # on during stimulus presentation%0A    %0A# Initialize OpenGL graphics screen.%0Ascreen = get_default_screen()%0A%0A# Use viewport with pixel coordinate system for projection%0Aviewport = Viewport(screen=screen
+temporal_frequency_hz = 5.0%0Aphase_controller = EvalStringController(%0A    during_go_eval_string=%22%25f*t*360.0%22%25(temporal_frequency_hz,)%0A    )%0A    %0A# Initialize OpenGL graphics screen.%0Ascreen = get_default_screen(
 )%0A%0A#
@@ -915,15 +915,71 @@
 %0A%0A# 
-Add 
+Create a viewport (with default pixel coordinate system)%0A# wi
 th
-e
  sti
@@ -987,24 +987,17 @@
 ulus
- to the 
+%0A
 viewport
 %0Avie
@@ -996,39 +996,53 @@
 port
-%0Av
+ = V
 iewport
-.add_
+( screen=screen, 
 stimul
-us(
+i=%5B
 stimulus
 )%0A%0A#
@@ -1037,16 +1037,18 @@
 stimulus
+%5D 
 )%0A%0A# Cre
@@ -1200,22 +1200,8 @@
 add_
-realtime_time_
 cont
@@ -1235,94 +1235,18 @@
 ase_
-as_function_of_time)%0Ap.add_transitional_controller(stimulus,'on', on_during_experiment
+controller
 )%0A%0A#

Update to use new Controller class.