# For the Octopus 900
# Check if pupil centre is within 10 pixels of (160,140)
checkFixationOK <- function(ret) return(sqrt((ret$pupilX - 160)^2 + (ret$pupilY - 140)^2) < 10)
# Return a list of opi stim objects (list of class opiStaticStimulus) for each level (dB) in
# p[5:length(p)]. Each stim has responseWindow BETWEEN_FLASH_TIME, except the last which has
# rwin. This one assumes p is on old Octopus 900 dB scale (0dB == 4000 cd/m^2).
makeStim <- function(p, rwin) {
BETWEEN_FLASH_TIME <- 750 # ms
res <- NULL
for(i in 5:length(p)) {
s <- list(x=p[1], y=p[2], level=dbTocd(p[i],4000/pi), size=0.43, duration=200,
responseWindow=ifelse(i < length(p), BETWEEN_FLASH_TIME, rwin),
checkFixationOK=NULL)
class(s) <- "opiStaticStimulus"
res <- c(res, list(s))
}
return(res)
}
################################################################
# Read in a key press 'z' is correct==1, 'm' otherwise
# correct is either 1 or 2, whichever is the correct interval
#
# Return list(seen={TRUE|FALSE}, time=time, err=NULL))
# seen is TRUE if correct key pressed
################################################################
if (FALSE) {
if (length(dir(".", "getKeyPress.py")) < 1)
stop('Python script getKeyPress.py missing?')
}
keyHandler <- function(correct, ret) {
return(list(seen=TRUE, time=0, err=NULL))
ONE <- "b'z'"
TWO <- "b'm'"
time <- Sys.time()
key <- 'q'
while (key != ONE && key != TWO) {
a <- system('python getKeyPress.py', intern=TRUE)
key <- a # substr(a, nchar(a), nchar(a))
print(paste('Key pressed: ',key,'from',a))
if (key == "b'8'")
stop('Key 8 pressed')
}
time <- Sys.time() - time
if ((key == ONE && correct == 1) || (key == TWO && correct == 2))
return(list(seen=TRUE, time=time, err=NULL))
else
return(list(seen=FALSE, time=time, err=NULL))
}
################################################################
# Read in return value from opipresent with F310 controller.
# First param is correct, next is 1 for left button, 2 for right button
# Left button (LB) is correct for interval 1, RB for interval 2
# correct is either 1 or 2, whichever is the correct interval
#
# Return list(seen={TRUE|FALSE}, time=time, err=NULL))
# seen is TRUE if correct key pressed
################################################################
F310Handler <- function(correct, opiResult) {
z <- opiResult$seen == correct
opiResult$seen <- z
return(opiResult)
}
################################################################
# 2 example beep_function
################################################################
if (FALSE) {
require(beepr)
myBeep <- function(type='None') {
if (type == 'correct') {
beepr::beep(2) # coin noise
Sys.sleep(0.5)
}
if (type == 'incorrect') {
beepr::beep(1) # system("rundll32 user32.dll,MessageBeep -1") # system beep
#Sys.sleep(0.0)
}
}
require(audio)
myBeep <- function(type="None") {
if (type == 'correct') {
wait(audio::play(sin(1:10000/10)))
}
if (type == 'incorrect') {
wait(audio::play(sin(1:10000/20)))
}
}
}
################################################################
# An example stim_print function
################################################################
if (FALSE) {
stim_print <- function(s, ret) {
sprintf("%4.1f %2.0f",cdTodb(s$level,10000/pi), ret$seen)
}
}
Run the code above in your browser using DataLab