You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

p300-speller-stimulator.lua 4.4KB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. -- This Lua script sends Stimulations to the speller visualization box for a p300-visual-speller
  2. --
  3. -- Author : Tobias Baumann
  4. -- Date : 2021-12-06
  5. -- Revised: 2021-19-11
  6. --This function lets the box sleep until a fixed moment
  7. function wait_until(box, time)
  8. while box:get_current_time() < time do
  9. box:sleep()
  10. end
  11. end
  12. --This function lets the box wait for a fixed duration
  13. function wait_for(box, duration)
  14. wait_until(box, box:get_current_time() + duration)
  15. end
  16. --this function checks, wether value already is an element of the given stim_matrix
  17. function is_element(matrix, value)
  18. for i = 1, #matrix do
  19. if #matrix == 0 then
  20. return(false)
  21. elseif value == matrix[i] then
  22. return(true)
  23. end
  24. end
  25. return(false)
  26. end
  27. --this function creates a sequence of stimulations by shuffeling the values of the given stim_matrix
  28. function create_sequence(matrix)
  29. math.randomseed(os.time())
  30. local stim_matrix = {}
  31. local stim_code = 0
  32. local i = 1
  33. while i <= #matrix do
  34. stim_code = matrix[math.random(1,#matrix)]
  35. if is_element(stim_matrix, stim_code) == false then
  36. stim_matrix[i] = stim_code
  37. i = i + 1
  38. end
  39. end
  40. return(stim_matrix)
  41. end
  42. -- this function is called when the box is initialized
  43. function initialize(box)
  44. --load stimulation codes
  45. dofile(box:get_config("${Path_Data}") .. "/plugins/stimulation/lua-stimulator-stim-codes.lua")
  46. --load box settings
  47. row_base = _G[box:get_setting(2)]
  48. col_base = _G[box:get_setting(3)]
  49. matrix_size = box:get_setting(4)
  50. n_repetitions = box:get_setting(5)
  51. flash_duration = box:get_setting(7)
  52. noflash_duration = box:get_setting(8)
  53. inter_repetition_delay = box:get_setting(9)
  54. inter_trial_delay = box:get_setting(10)
  55. send_toggle = _G[box:get_setting(11)]
  56. time_to_send = box:get_setting(13)
  57. if box:get_setting(12) == 'true' then
  58. free_spelling = true
  59. n_trials = 1
  60. else
  61. free_spelling = false
  62. n_trials = box:get_setting(6)
  63. end
  64. --Lua variables
  65. experiment_end = false
  66. send = false
  67. row_stimcodes = {}
  68. col_stimcodes = {}
  69. for x = 1, matrix_size do
  70. row_stimcodes[x] = row_base + x - 1
  71. end
  72. for x = 1, matrix_size do
  73. col_stimcodes[x] = col_base + x - 1
  74. end
  75. end
  76. -- this function is called when the box is uninitialized
  77. function uninitialize(box)
  78. end
  79. -- this function is called once by the box
  80. function process(box)
  81. while box:keep_processing() do
  82. if send and not experiment_end then
  83. for trial = 1, n_trials do
  84. if not send then
  85. break
  86. end
  87. box:send_stimulation(1, OVTK_StimulationId_RestStart, box:get_current_time() , 0)
  88. wait_for(box, inter_trial_delay)
  89. box:send_stimulation(1, OVTK_StimulationId_RestStop, box:get_current_time() , 0)
  90. box:send_stimulation(1, OVTK_StimulationId_TrialStart ,box:get_current_time() , 0)
  91. for segment = 1, n_repetitions do
  92. row_stimcodes = create_sequence(row_stimcodes)
  93. col_stimcodes = create_sequence(col_stimcodes)
  94. box:send_stimulation(1, OVTK_StimulationId_SegmentStart ,box:get_current_time() , 0)
  95. for i = 1, matrix_size do
  96. box:send_stimulation(1, row_stimcodes[i] ,box:get_current_time() , 0)
  97. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  98. wait_for(box, flash_duration)
  99. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  100. wait_for(box, noflash_duration)
  101. box:send_stimulation(1, col_stimcodes[i] ,box:get_current_time() , 0)
  102. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  103. wait_for(box, flash_duration)
  104. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  105. wait_for(box, noflash_duration)
  106. end
  107. box:send_stimulation(1, OVTK_StimulationId_SegmentStop ,box:get_current_time() , 0)
  108. wait_for(box, inter_repetition_delay)
  109. end
  110. box:send_stimulation(1, OVTK_StimulationId_TrialStop ,box:get_current_time() , 0)
  111. end
  112. if not free_spelling then
  113. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  114. send = false
  115. experiment_end = true
  116. end
  117. else if not send and not experiment_end then
  118. wait_for(box, time_to_send)
  119. send = true
  120. box:send_stimulation(1, OVTK_StimulationId_ExperimentStart ,box:get_current_time() , 0)
  121. box:remove_stimulation(1, 1)
  122. end
  123. end
  124. -- releases cpu
  125. box:sleep()
  126. end
  127. end