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.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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-21-07
  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. send = false
  66. row_stimcodes = {}
  67. col_stimcodes = {}
  68. for x = 1, matrix_size do
  69. row_stimcodes[x] = row_base + x - 1
  70. end
  71. for x = 1, matrix_size do
  72. col_stimcodes[x] = col_base + x - 1
  73. end
  74. end
  75. -- this function is called when the box is uninitialized
  76. function uninitialize(box)
  77. end
  78. -- this function is called once by the box
  79. function process(box)
  80. while box:keep_processing() do
  81. if send then
  82. for stimulation = 1, box:get_stimulation_count(1) do
  83. stimulation_id, stimulation_time, stimulation_duration = box:get_stimulation(1, 1)
  84. if stimulation_id == send_toggle then
  85. send = false
  86. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  87. box:remove_stimulation(1, 1)
  88. end
  89. end
  90. for trial = 1, n_trials do
  91. if not send then
  92. break
  93. end
  94. box:send_stimulation(1, OVTK_StimulationId_RestStart, box:get_current_time() , 0)
  95. wait_for(box, inter_trial_delay)
  96. box:send_stimulation(1, OVTK_StimulationId_RestStop, box:get_current_time() , 0)
  97. box:send_stimulation(1, OVTK_StimulationId_TrialStart ,box:get_current_time() , 0)
  98. for segment = 1, n_repetitions do
  99. row_stimcodes = create_sequence(row_stimcodes)
  100. col_stimcodes = create_sequence(col_stimcodes)
  101. box:send_stimulation(1, OVTK_StimulationId_SegmentStart ,box:get_current_time() , 0)
  102. for i = 1, matrix_size do
  103. box:send_stimulation(1, row_stimcodes[i] ,box:get_current_time() , 0)
  104. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  105. wait_for(box, flash_duration)
  106. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  107. wait_for(box, noflash_duration)
  108. box:send_stimulation(1, col_stimcodes[i] ,box:get_current_time() , 0)
  109. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStart ,box:get_current_time() , 0)
  110. wait_for(box, flash_duration)
  111. box:send_stimulation(1, OVTK_StimulationId_VisualStimulationStop ,box:get_current_time() , 0)
  112. wait_for(box, noflash_duration)
  113. end
  114. box:send_stimulation(1, OVTK_StimulationId_SegmentStop ,box:get_current_time() , 0)
  115. wait_for(box, inter_repetition_delay)
  116. end
  117. box:send_stimulation(1, OVTK_StimulationId_TrialStop ,box:get_current_time() , 0)
  118. end
  119. if not free_spelling then
  120. box:send_stimulation(1, OVTK_StimulationId_ExperimentStop ,box:get_current_time() , 0)
  121. send = false
  122. end
  123. else
  124. for stimulation = 1, box:get_stimulation_count(1) do
  125. stimulation_id, stimulation_time, stimulation_duration = box:get_stimulation(1, 1)
  126. if stimulation_id == send_toggle then
  127. send = true
  128. wait_for(box, time_to_send)
  129. box:send_stimulation(1, OVTK_StimulationId_ExperimentStart ,box:get_current_time() , 0)
  130. box:remove_stimulation(1, 1)
  131. end
  132. end
  133. end
  134. -- releases cpu
  135. box:sleep()
  136. end
  137. end