Attribute VB_Name = "episound"
Public achar(), bchar(), cchar(), dchar(), outputfile, playlist
Public control_time!, start_time!, RespDur, InstrDur, delay!, endcount, response_time!, StopOnDur!, StopOffDur!, prem_resp!, GoDur!, run_err!, OnCtrlDur!
Declare Function GetTickCount Lib "User" () As Long
Public Declare Function timeGetTime Lib "winmm.dll" () As Long
Public Declare Function showcursor Lib "user32.dll" (ByVal bShow As Long) As Long

Sub stoptaska()
'serena maja 25.1.01

' declare for the dispchk4button()
Dim response_code
Dim r_c_val
Dim response_dir
Dim response_time
Dim start_time
Dim end_time

image_number = 0
tot_stop_correct = 0
tot_stop = 0
image_show_dur = 1000
prem_time = 200
StopOnDur = 300
start_dly = 250
running_dly = 0

StopTask.Picture1(1).Picture = LoadPicture("c:\vb98\mri\images\stop\arrow1.bmp")
StopTask.Picture1(2).Picture = LoadPicture("c:\vb98\mri\images\stop\arrow2.bmp")
StopTask.Picture1(3).Picture = LoadPicture("c:\vb98\mri\images\stop\arrow3.bmp")
StopTask.Picture1(4).Picture = LoadPicture("c:\vb98\mri\images\stop\arrow4.bmp")
StopTask.Picture1(5).Picture = LoadPicture("c:\vb98\mri\images\stop\arrow5.bmp")
StopTask.Picture1(1).Visible = False
StopTask.Picture1(2).Visible = False
StopTask.Picture1(3).Visible = False
StopTask.Picture1(4).Visible = False
StopTask.Picture1(5).Visible = False


loop_start:
image_number = image_number + 1
response_dir = 0
If (next_response = 0) Then
   response_code = 0
Else
   response_code = "premature"
   response_dir = next_dir
End If
next_response = 0
correct = 0

response_time = 0
start_time = timeGetTime()
total_image_dur = bchar(image_number)

Select Case achar(image_number)
  Case 1, 2
    display_period = image_show_dur
    periods = 2
  Case 3, 4, 5, 6
    display_period = start_dly
    periods = 3
End Select
Select Case achar(image_number)
  Case 1, 3, 5
    StopTask.Picture1(1).Visible = True
  Case 2, 4, 6
    StopTask.Picture1(2).Visible = True
End Select

display1:
end_time = (start_time + display_period)
Call Sdispcheck4button(response_code, 1, response_dir, response_time, start_time, end_time, 0, response_code)
 
If (periods = 2) Then
   GoTo display3
End If

display2:
StopTask.Picture1(1).Visible = False
StopTask.Picture1(2).Visible = False
StopTask.Picture1(5).Visible = True
end_time = (start_time + display_period + StopOnDur)
Call Sdispcheck4button(response_code, 2, response_dir, response_time, start_time, end_time, 0, response_code)
 
display3:
StopTask.Picture1(1).Visible = False
StopTask.Picture1(2).Visible = False
StopTask.Picture1(5).Visible = False
end_time = (start_time + total_image_dur - prem_time)
Call Sdispcheck4button(response_code, 3, response_dir, response_time, start_time, end_time, 0, response_code)

display4:
end_time = (start_time + total_image_dur)
If (response_code = 0) Then
  Call Sdispcheck4button(response_code, 4, response_dir, response_time, start_time, end_time, 0, 0)
Else
  Call Sdispcheck4button(next_response, 5, next_dir, duff, start_time, end_time, 0, 0)
End If

after_display:
math:

If (response_dir = 0) Then
  response_dir = "no response"
End If

RT = response_time / 1000
If (response_code = "premature") Then
   RT = "premature"
End If

Select Case achar(image_number)
  Case 1
    If Not (response_code = 0) Then
      If (response_dir = 4) Then correct = 1
    End If
    Print #2, image_number, achar(image_number), response_dir, correct, RT
  Case 2
    If Not (response_code = 0) Then
      If (response_dir = 2) Then correct = 1
    End If
    Print #2, image_number, achar(image_number), response_dir, correct, RT
  Case 3, 4, 5, 6
    If (response_code = 0) Then
       correct = 1
       tot_stop_correct = tot_stop_correct + 1
    End If
    tot_stop = tot_stop + 1
    running_dly = running_dly + start_dly

    If ((tot_stop_correct / tot_stop) > 0.5) Then
       If (start_dly < 900) Then
       start_dly = start_dly + 50
       End If
    ElseIf ((tot_stop_correct / tot_stop) < 0.5) Then
       If (start_dly > 50) Then
       start_dly = start_dly - 50
       End If
    End If

    Print #2, image_number, achar(image_number), response_dir, correct, RT
    Print #2, "Stop signal ---->      "; (tot_stop_correct); " out of "; tot_stop; " correct ("; ((tot_stop_correct / tot_stop) * 100); "%) next stop signal delay is: "; start_dly; "ms"
End Select

If (image_number = endcount) Then GoTo endlog

GoTo loop_start

endlog:
ShowMouse
Print #2, "The final overall percentage of inhibition was "; (tot_stop_correct / tot_stop) * 100; "%"
Print #2, "average stop signal delay was: "; (running_dly / image_number); ""
Close #2
End
End Sub


Sub load_stoptaska()
Open playlist For Input As #1
increm = 1
Do While Not EOF(1)

ReDim Preserve achar(increm), bchar(increm), cchar(increm)
Input #1, achar(increm), bchar(increm), cchar(increm)
increm = increm + 1
Loop
endcount = increm - 1
Close #1
Close #2
Open outputfile For Output As #2
Print #2, "Trial", "Arrow", "Response", "correct", "RT"
increm = 1
End Sub



Public Function dispcheck4button(ByRef response_code, ByVal r_c_val, ByRef response_dir, ByRef response_time, ByVal start_time, ByVal end_time, exit_, ignore_) As Integer
'response time is output with ref. to start_time
'serena maja 25.1.01

Do While timeGetTime() < (end_time)
  If (response_code = 0) Then
    If (FixedStopTask.Joystk1.Button1 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 1
    ElseIf (FixedStopTask.Joystk1.Button2 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 2
    ElseIf (FixedStopTask.Joystk1.Button3 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 3
    ElseIf (FixedStopTask.Joystk1.Button4 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 4
    End If
  End If
DoEvents
Loop


End Function



Public Function switchbutton(ByRef response_code, ByVal r_c_val, ByRef response_dir, ByRef response_time, ByVal start_time, ByVal end_time, exit_, ignore_) As Integer
'response time is output with ref. to start_time
'serena maja 25.1.01

Do While timeGetTime() < (end_time)
  If (ignore_ = 0) Then
    If (Form6.Joystk1.Button1 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 1
      If (exit_ = True) Then Exit Function
    ElseIf (Form6.Joystk1.Button2 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 2
      If (exit_ = True) Then Exit Function
    ElseIf (Form6.Joystk1.Button3 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 3
      If (exit_ = True) Then Exit Function
    ElseIf (Form6.Joystk1.Button4 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 4
      If (exit_ = True) Then Exit Function
    End If
  End If
DoEvents
Loop


End Function




Public Function Sdispcheck4button(ByRef response_code, ByVal r_c_val, ByRef response_dir, ByRef response_time, ByVal start_time, ByVal end_time, exit_, ignore_) As Integer
'response time is output with ref. to start_time
'serena maja 25.1.01

Do While timeGetTime() < (end_time)
  If (ignore_ = 0) Then
    
    If (StopTask.Joystk1.Button1 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 1
      If (exit_ = True) Then Exit Function
    ElseIf (StopTask.Joystk1.Button2 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 2
      If (exit_ = True) Then Exit Function
    ElseIf (StopTask.Joystk1.Button3 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 3
      If (exit_ = True) Then Exit Function
    ElseIf (StopTask.Joystk1.Button4 = True) Then
      response_time = timeGetTime() - start_time
      response_code = r_c_val
      response_dir = 4
      If (exit_ = True) Then Exit Function
    End If
  End If
DoEvents
Loop


End Function










