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.

ovpCBoxAlgorithmP300TactileVisualization.cpp 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file ovpCBoxAlgorithmP300TactileVisualization.cpp
  4. /// \brief Functions of the Class P300TactileVisualization. !!This is a modification of the P300 Speller Visualization Box!!
  5. /// \author Tobias Baumann (TH Nuernberg).
  6. /// \version 1.0.
  7. /// \date Mon Feb 04 12:43:53 2022.
  8. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>
  9. ///
  10. ///-------------------------------------------------------------------------------------------------
  11. //includes
  12. #include "ovpCBoxAlgorithmP300TactileVisualization.h"
  13. #include <tcptagging/IStimulusSender.h>
  14. #include <algorithm>
  15. #include <list>
  16. #include <string>
  17. #include <vector>
  18. namespace OpenViBE {
  19. namespace Plugins {
  20. namespace Tactilebci {
  21. static void ToggleButtonShowHideCB(GtkToggleToolButton* button, gpointer data)
  22. {
  23. if (gtk_toggle_tool_button_get_active(button)) { gtk_widget_show(GTK_WIDGET(data)); }
  24. else { gtk_widget_hide(GTK_WIDGET(data)); }
  25. }
  26. // This callback flushes all accumulated stimulations to the TCP Tagging
  27. // after the rendering has completed.
  28. static gboolean FlushCB(gpointer data)
  29. {
  30. (static_cast<CBoxAlgorithmP300TactileVisualization*>(data))->flushQueue();
  31. return false; // Only run once
  32. }
  33. bool CBoxAlgorithmP300TactileVisualization::initialize()
  34. {
  35. const Kernel::IBox& boxContext = this->getStaticBoxContext();
  36. m_mainWidgetInterface = nullptr;
  37. m_toolbarWidgetInterface = nullptr;
  38. m_flashFontDesc = nullptr;
  39. m_noFlashFontDesc = nullptr;
  40. m_targetFontDesc = nullptr;
  41. m_selectedFontDesc = nullptr;
  42. // ----------------------------------------------------------------------------------------------------------------------------------------------------------
  43. m_interfaceFilename = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
  44. m_rowStimulationBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
  45. m_columnStimulationBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
  46. m_flashBgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 3);
  47. m_flashFgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 4);
  48. m_flashFontSize = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 5);
  49. m_noFlashBgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 6);
  50. m_noFlashFgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 7);
  51. m_noFlashFontSize = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 8);
  52. m_targetBgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 9);
  53. m_targetFgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 10);
  54. m_targetFontSize = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 11);
  55. m_selectedBgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 12);
  56. m_selectedFgColor = CGdkcolorAutoCast(boxContext, this->getConfigurationManager(), 13);
  57. m_selectedFontSize = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 14);
  58. // ----------------------------------------------------------------------------------------------------------------------------------------------------------
  59. m_sequenceStimulationDecoder = &this->getAlgorithmManager().getAlgorithm(
  60. this->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StimulationDecoder));
  61. m_sequenceStimulationDecoder->initialize();
  62. m_targetStimulationDecoder = &this->getAlgorithmManager().getAlgorithm(
  63. this->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StimulationDecoder));
  64. m_targetStimulationDecoder->initialize();
  65. m_targetFlaggingStimulationEncoder = &this->getAlgorithmManager().getAlgorithm(
  66. this->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StimulationEncoder));
  67. m_targetFlaggingStimulationEncoder->initialize();
  68. m_rowSelectionStimulationDecoder = &this->getAlgorithmManager().getAlgorithm(
  69. this->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StimulationDecoder));
  70. m_rowSelectionStimulationDecoder->initialize();
  71. m_columnSelectionStimulationDecoder = &this->getAlgorithmManager().getAlgorithm(
  72. this->getAlgorithmManager().createAlgorithm(OVP_GD_ClassId_Algorithm_StimulationDecoder));
  73. m_columnSelectionStimulationDecoder->initialize();
  74. m_sequenceMemoryBuffer.initialize(
  75. m_sequenceStimulationDecoder->getInputParameter(OVP_GD_Algorithm_StimulationDecoder_InputParameterId_MemoryBufferToDecode));
  76. m_sequenceStimulationSet.initialize(
  77. m_sequenceStimulationDecoder->getOutputParameter(OVP_GD_Algorithm_StimulationDecoder_OutputParameterId_StimulationSet));
  78. m_targetMemoryBuffer.initialize(
  79. m_targetStimulationDecoder->getInputParameter(OVP_GD_Algorithm_StimulationDecoder_InputParameterId_MemoryBufferToDecode));
  80. m_targetStimulationSet.initialize(
  81. m_targetStimulationDecoder->getOutputParameter(OVP_GD_Algorithm_StimulationDecoder_OutputParameterId_StimulationSet));
  82. m_targetFlaggingStimulationSet.initialize(
  83. m_targetFlaggingStimulationEncoder->getInputParameter(OVP_GD_Algorithm_StimulationEncoder_InputParameterId_StimulationSet));
  84. m_targetFlaggingMemoryBuffer.initialize(
  85. m_targetFlaggingStimulationEncoder->getOutputParameter(OVP_GD_Algorithm_StimulationEncoder_OutputParameterId_EncodedMemoryBuffer));
  86. m_lastTime = 0;
  87. m_stimulusSender = nullptr;
  88. m_idleFuncTag = 0;
  89. m_stimuliQueue.clear();
  90. m_mainWidgetInterface = gtk_builder_new(); // glade_xml_new(m_interfaceFilename.toASCIIString(), "p300-speller-main", nullptr);
  91. if (!gtk_builder_add_from_file(m_mainWidgetInterface, m_interfaceFilename.toASCIIString(), nullptr))
  92. {
  93. this->getLogManager() << Kernel::LogLevel_ImportantWarning << "Could not load interface file [" << m_interfaceFilename << "]\n";
  94. this->getLogManager() << Kernel::LogLevel_ImportantWarning <<
  95. "The file may be missing. However, the interface files now use gtk-builder instead of glade. Did you update your files ?\n";
  96. return false;
  97. }
  98. m_toolbarWidgetInterface = gtk_builder_new(); // glade_xml_new(m_interfaceFilename.toASCIIString(), "p300-speller-toolbar", nullptr);
  99. gtk_builder_add_from_file(m_toolbarWidgetInterface, m_interfaceFilename.toASCIIString(), nullptr);
  100. m_mainWindow = GTK_WIDGET(gtk_builder_get_object(m_mainWidgetInterface, "p300-speller-main"));
  101. m_toolbarWidget = GTK_WIDGET(gtk_builder_get_object(m_toolbarWidgetInterface, "p300-speller-toolbar"));
  102. m_table = GTK_TABLE(gtk_builder_get_object(m_mainWidgetInterface, "p300-speller-table"));
  103. m_result = GTK_LABEL(gtk_builder_get_object(m_mainWidgetInterface, "label-result"));
  104. m_target = GTK_LABEL(gtk_builder_get_object(m_mainWidgetInterface, "label-target"));
  105. //TactileMenu initialization
  106. TactileMenu CBoxAlgorithmP300TactileVisualization::m_MainMenu("Ja", "Nein", "Pflege", "Hilfe", "Geräte", "Optionen", m_mainWidgetInterface);
  107. TactileMenu CBoxAlgorithmP300TactileVisualization::m_CareMenu("Ja", "Nein", "Hunger", "Lage", "Müdigkeit", "Hauptmenü", m_mainWidgetInterface);
  108. TactileMenu CBoxAlgorithmP300TactileVisualization::m_HelpMenu("Ja", "Nein", "Schmerzen", "Atemnot", "Anderes", "Hauptmenü", m_mainWidgetInterface);
  109. TactileMenu CBoxAlgorithmP300TactileVisualization::m_DevicesMenu("Ja", "Nein", "Atemgerät", "Rollstuhl", "Computer", "Hauptmenü", m_mainWidgetInterface);
  110. TactileMenu CBoxAlgorithmP300TactileVisualization::m_FunctionsMenu("Ja", "Nein", "befehl1", "befehl2", "befehl3", "Hauptmenü", m_mainWidgetInterface);
  111. m_CurrentMenu = &m_MainMenu;
  112. m_CurrentMenu->reloadui();
  113. //Grenze-------------------------------------------------------------------------------------
  114. gtk_builder_connect_signals(m_mainWidgetInterface, nullptr);
  115. gtk_builder_connect_signals(m_toolbarWidgetInterface, nullptr);
  116. g_signal_connect(gtk_builder_get_object(m_toolbarWidgetInterface, "toolbutton-show_target_text"), "toggled", G_CALLBACK(ToggleButtonShowHideCB),
  117. gtk_builder_get_object(m_mainWidgetInterface, "label-target"));
  118. g_signal_connect(gtk_builder_get_object(m_toolbarWidgetInterface, "toolbutton-show_target_text"), "toggled", G_CALLBACK(ToggleButtonShowHideCB),
  119. gtk_builder_get_object(m_mainWidgetInterface, "label-target-title"));
  120. g_signal_connect(gtk_builder_get_object(m_toolbarWidgetInterface, "toolbutton-show_result_text"), "toggled", G_CALLBACK(ToggleButtonShowHideCB),
  121. gtk_builder_get_object(m_mainWidgetInterface, "label-result"));
  122. g_signal_connect(gtk_builder_get_object(m_toolbarWidgetInterface, "toolbutton-show_result_text"), "toggled", G_CALLBACK(ToggleButtonShowHideCB),
  123. gtk_builder_get_object(m_mainWidgetInterface, "label-result-title"));
  124. m_visualizationCtx = dynamic_cast<VisualizationToolkit::IVisualizationContext*>(this->createPluginObject(
  125. OVP_ClassId_Plugin_VisualizationCtx));
  126. m_visualizationCtx->setWidget(*this, m_mainWindow);
  127. m_visualizationCtx->setToolbar(*this, m_toolbarWidget);
  128. guint nRow = 0, nCol = 0;
  129. g_object_get(m_table, "n-rows", &nRow, nullptr);
  130. g_object_get(m_table, "n-columns", &nCol, nullptr);
  131. m_nRow = nRow;
  132. m_nCol = nCol;
  133. PangoFontDescription* maxFontDesc = pango_font_description_copy(pango_context_get_font_description(gtk_widget_get_pango_context(m_mainWindow)));
  134. m_flashFontDesc = pango_font_description_copy(pango_context_get_font_description(gtk_widget_get_pango_context(m_mainWindow)));
  135. m_noFlashFontDesc = pango_font_description_copy(pango_context_get_font_description(gtk_widget_get_pango_context(m_mainWindow)));
  136. m_targetFontDesc = pango_font_description_copy(pango_context_get_font_description(gtk_widget_get_pango_context(m_mainWindow)));
  137. m_selectedFontDesc = pango_font_description_copy(pango_context_get_font_description(gtk_widget_get_pango_context(m_mainWindow)));
  138. uint64_t maxSize = 0;
  139. maxSize = std::max(maxSize, m_flashFontSize);
  140. maxSize = std::max(maxSize, m_noFlashFontSize);
  141. maxSize = std::max(maxSize, m_targetFontSize);
  142. maxSize = std::max(maxSize, m_selectedFontSize);
  143. pango_font_description_set_size(maxFontDesc, gint(maxSize * PANGO_SCALE));
  144. pango_font_description_set_size(m_flashFontDesc, gint(m_flashFontSize * PANGO_SCALE));
  145. pango_font_description_set_size(m_noFlashFontDesc, gint(m_noFlashFontSize * PANGO_SCALE));
  146. pango_font_description_set_size(m_targetFontDesc, gint(m_targetFontSize * PANGO_SCALE));
  147. pango_font_description_set_size(m_selectedFontDesc, gint(m_selectedFontSize * PANGO_SCALE));
  148. this->cacheBuildFromTable(m_table);
  149. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB, &m_noFlashBgColor);
  150. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB, &m_noFlashFgColor);
  151. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB, maxFontDesc);
  152. pango_font_description_free(maxFontDesc);
  153. m_lastTargetRow = -1;
  154. m_lastTargetCol = -1;
  155. m_targetRow = -1;
  156. m_targetCol = -1;
  157. m_selectedRow = -1;
  158. m_selectedCol = -1;
  159. m_stimulusSender = TCPTagging::CreateStimulusSender();
  160. if (!m_stimulusSender->connect("localhost", "15361"))
  161. {
  162. this->getLogManager() << Kernel::LogLevel_Warning << "Unable to connect to AS TCP Tagging, stimuli wont be forwarded.\n";
  163. }
  164. m_tableInitialized = false;
  165. return true;
  166. }
  167. bool CBoxAlgorithmP300TactileVisualization::uninitialize()
  168. {
  169. if (m_idleFuncTag)
  170. {
  171. m_stimuliQueue.clear();
  172. g_source_remove(m_idleFuncTag);
  173. m_idleFuncTag = 0;
  174. }
  175. if (m_stimulusSender)
  176. {
  177. delete m_stimulusSender;
  178. m_stimulusSender = nullptr;
  179. }
  180. if (m_selectedFontDesc)
  181. {
  182. pango_font_description_free(m_selectedFontDesc);
  183. m_selectedFontDesc = nullptr;
  184. }
  185. if (m_targetFontDesc)
  186. {
  187. pango_font_description_free(m_targetFontDesc);
  188. m_targetFontDesc = nullptr;
  189. }
  190. if (m_noFlashFontDesc)
  191. {
  192. pango_font_description_free(m_noFlashFontDesc);
  193. m_noFlashFontDesc = nullptr;
  194. }
  195. if (m_flashFontDesc)
  196. {
  197. pango_font_description_free(m_flashFontDesc);
  198. m_flashFontDesc = nullptr;
  199. }
  200. if (m_toolbarWidgetInterface)
  201. {
  202. g_object_unref(m_toolbarWidgetInterface);
  203. m_toolbarWidgetInterface = nullptr;
  204. }
  205. if (m_mainWidgetInterface)
  206. {
  207. g_object_unref(m_mainWidgetInterface);
  208. m_mainWidgetInterface = nullptr;
  209. }
  210. m_targetFlaggingStimulationSet.uninitialize();
  211. m_targetFlaggingMemoryBuffer.uninitialize();
  212. m_targetStimulationSet.uninitialize();
  213. m_targetMemoryBuffer.uninitialize();
  214. m_sequenceStimulationSet.uninitialize();
  215. m_sequenceMemoryBuffer.uninitialize();
  216. if (m_columnSelectionStimulationDecoder)
  217. {
  218. m_columnSelectionStimulationDecoder->uninitialize();
  219. this->getAlgorithmManager().releaseAlgorithm(*m_columnSelectionStimulationDecoder);
  220. m_columnSelectionStimulationDecoder = nullptr;
  221. }
  222. if (m_rowSelectionStimulationDecoder)
  223. {
  224. m_rowSelectionStimulationDecoder->uninitialize();
  225. this->getAlgorithmManager().releaseAlgorithm(*m_rowSelectionStimulationDecoder);
  226. m_rowSelectionStimulationDecoder = nullptr;
  227. }
  228. if (m_targetFlaggingStimulationEncoder)
  229. {
  230. m_targetFlaggingStimulationEncoder->uninitialize();
  231. this->getAlgorithmManager().releaseAlgorithm(*m_targetFlaggingStimulationEncoder);
  232. m_targetFlaggingStimulationEncoder = nullptr;
  233. }
  234. if (m_targetStimulationDecoder)
  235. {
  236. m_targetStimulationDecoder->uninitialize();
  237. this->getAlgorithmManager().releaseAlgorithm(*m_targetStimulationDecoder);
  238. m_targetStimulationDecoder = nullptr;
  239. }
  240. if (m_sequenceStimulationDecoder)
  241. {
  242. m_sequenceStimulationDecoder->uninitialize();
  243. this->getAlgorithmManager().releaseAlgorithm(*m_sequenceStimulationDecoder);
  244. m_sequenceStimulationDecoder = nullptr;
  245. }
  246. if (m_visualizationCtx)
  247. {
  248. this->releasePluginObject(m_visualizationCtx);
  249. m_sequenceStimulationDecoder = nullptr;
  250. }
  251. return true;
  252. }
  253. bool CBoxAlgorithmP300TactileVisualization::processInput(const size_t /*index*/)
  254. {
  255. this->getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  256. if (!m_tableInitialized)
  257. {
  258. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB, &m_noFlashBgColor);
  259. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB, &m_noFlashFgColor);
  260. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB, m_noFlashFontDesc);
  261. m_tableInitialized = true;
  262. }
  263. return true;
  264. }
  265. bool CBoxAlgorithmP300TactileVisualization::process()
  266. {
  267. Kernel::IBoxIO& boxContext = this->getDynamicBoxContext();
  268. // --- Sequence stimulations
  269. for (size_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  270. {
  271. CStimulationSet flaggingStimulationSet;
  272. m_sequenceMemoryBuffer = boxContext.getInputChunk(0, i);
  273. m_targetFlaggingStimulationSet = &flaggingStimulationSet;
  274. m_targetFlaggingMemoryBuffer = boxContext.getOutputChunk(0);
  275. m_sequenceStimulationDecoder->process();
  276. m_lastTime = boxContext.getInputChunkEndTime(0, i);
  277. if (m_sequenceStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedHeader))
  278. {
  279. m_targetFlaggingStimulationEncoder->process(OVP_GD_Algorithm_StimulationEncoder_InputTriggerId_EncodeHeader);
  280. }
  281. if (m_sequenceStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedBuffer))
  282. {
  283. IStimulationSet* stimulationSet = m_sequenceStimulationSet;
  284. for (size_t j = 0; j < stimulationSet->getStimulationCount(); ++j)
  285. {
  286. uint64_t id = stimulationSet->getStimulationIdentifier(j);
  287. bool flash = false;
  288. int row = -1;
  289. int col = -1;
  290. bool isTarget = false;
  291. if (id >= m_rowStimulationBase && id < m_rowStimulationBase + m_nRow)
  292. {
  293. row = int(id - m_rowStimulationBase);
  294. flash = true;
  295. isTarget = (row == m_lastTargetRow);
  296. }
  297. if (id >= m_columnStimulationBase && id < m_columnStimulationBase + m_nCol)
  298. {
  299. col = int(id - m_columnStimulationBase);
  300. flash = true;
  301. isTarget = (col == m_lastTargetCol);
  302. }
  303. if (id == OVTK_StimulationId_VisualStimulationStop)
  304. {
  305. this->getLogManager() << Kernel::LogLevel_Debug << "Received OVTK_StimulationId_VisualStimulationStop - resets grid\n";
  306. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB, &m_noFlashBgColor);
  307. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB, &m_noFlashFgColor);
  308. this->cacheForEach(&CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB, m_noFlashFontDesc);
  309. }
  310. if (id == OVTK_StimulationId_Reset)
  311. {
  312. gtk_label_set_text(m_target, "");
  313. gtk_label_set_text(m_result, "");
  314. }
  315. if (flash)
  316. {
  317. this->cacheForEachIf(row, col, &CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB,
  318. &CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB, &m_flashBgColor, &m_noFlashBgColor);
  319. this->cacheForEachIf(row, col, &CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB,
  320. &CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB, &m_flashFgColor, &m_noFlashFgColor);
  321. this->cacheForEachIf(row, col, &CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB,
  322. &CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB, m_flashFontDesc, m_noFlashFontDesc);
  323. // We now know if this flash corresponds to the current target or not, merge this to the outgoing stimulation stream
  324. if (isTarget)
  325. {
  326. m_stimuliQueue.push_back(OVTK_StimulationId_Target);
  327. flaggingStimulationSet.appendStimulation(OVTK_StimulationId_Target, stimulationSet->getStimulationDate(j), 0);
  328. }
  329. else
  330. {
  331. m_stimuliQueue.push_back(OVTK_StimulationId_NonTarget);
  332. flaggingStimulationSet.appendStimulation(OVTK_StimulationId_NonTarget, stimulationSet->getStimulationDate(j), 0);
  333. }
  334. }
  335. // Pass the stimulation to the server also as-is. If its a flash, it can be differentiated from a 'target' spec because
  336. // its NOT between OVTK_StimulationId_RestStart and OVTK_StimulationId_RestStop stimuli in the generated P300 timeline.
  337. m_stimuliQueue.push_back(id);
  338. }
  339. m_targetFlaggingStimulationEncoder->process(OVP_GD_Algorithm_StimulationEncoder_InputTriggerId_EncodeBuffer);
  340. }
  341. if (m_sequenceStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedEnd))
  342. {
  343. m_targetFlaggingStimulationEncoder->process(OVP_GD_Algorithm_StimulationEncoder_InputTriggerId_EncodeEnd);
  344. }
  345. boxContext.markInputAsDeprecated(0, i);
  346. boxContext.markOutputAsReadyToSend(0, boxContext.getInputChunkStartTime(0, i), boxContext.getInputChunkEndTime(0, i));
  347. }
  348. // --- Target stimulations
  349. for (size_t i = 0; i < boxContext.getInputChunkCount(1); ++i)
  350. {
  351. if (m_lastTime >= boxContext.getInputChunkStartTime(1, i))
  352. {
  353. m_targetMemoryBuffer = boxContext.getInputChunk(1, i);
  354. m_targetStimulationDecoder->process();
  355. if (m_targetStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedHeader)) { }
  356. if (m_targetStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedBuffer))
  357. {
  358. IStimulationSet* stimulationSet = m_targetStimulationSet;
  359. for (size_t j = 0; j < stimulationSet->getStimulationCount(); ++j)
  360. {
  361. uint64_t id = stimulationSet->getStimulationIdentifier(j);
  362. bool target = false;
  363. if (id >= m_rowStimulationBase && id < m_rowStimulationBase + m_nRow)
  364. {
  365. this->getLogManager() << Kernel::LogLevel_Debug << "Received Target Row " << id << "\n";
  366. m_targetRow = int(id - m_rowStimulationBase);
  367. target = true;
  368. }
  369. if (id >= m_columnStimulationBase && id < m_columnStimulationBase + m_nCol)
  370. {
  371. this->getLogManager() << Kernel::LogLevel_Debug << "Received Target Column " << id << "\n";
  372. m_targetCol = int(id - m_columnStimulationBase);
  373. target = true;
  374. }
  375. if (target && m_targetRow != -1 && m_targetCol != -1)
  376. {
  377. this->getLogManager() << Kernel::LogLevel_Debug << "Displays Target Cell\n";
  378. this->cacheForEachIf(m_targetRow, m_targetCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB,
  379. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, &m_targetBgColor, nullptr);
  380. this->cacheForEachIf(m_targetRow, m_targetCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB,
  381. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, &m_targetFgColor, nullptr);
  382. this->cacheForEachIf(m_targetRow, m_targetCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB,
  383. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, m_targetFontDesc, nullptr);
  384. std::vector<GtkWidget*> widgets;
  385. this->cacheForEachIf(m_targetRow, m_targetCol, &CBoxAlgorithmP300TactileVisualization::cacheCollectChildWidgetCB,
  386. &CBoxAlgorithmP300TactileVisualization::cacheCollectChildWidgetCB, &widgets, nullptr);
  387. // Merge the current target into the stimulation stream. It can be differentiated
  388. // from a 'flash' spec because it IS between OVTK_StimulationId_RestStart and
  389. // OVTK_StimulationId_RestStop stimulations in the P300 timeline.
  390. {
  391. m_stimuliQueue.push_back(m_targetRow + m_rowStimulationBase);
  392. m_stimuliQueue.push_back(m_targetCol + m_columnStimulationBase);
  393. }
  394. if (widgets.size() == 1)
  395. {
  396. if (GTK_IS_LABEL(widgets[0]))
  397. {
  398. std::string label;
  399. label = gtk_label_get_text(m_target);
  400. label += gtk_label_get_text(GTK_LABEL(widgets[0]));
  401. gtk_label_set_text(m_target, label.c_str());
  402. }
  403. else
  404. {
  405. this->getLogManager() << Kernel::LogLevel_Warning << "Expected label class widget... could not find a valid text to append\n";
  406. }
  407. }
  408. else
  409. {
  410. this->getLogManager() << Kernel::LogLevel_Warning << "Did not find a unique widget at row:" << size_t(m_targetRow) << " column:" <<
  411. size_t(m_targetCol) << "\n";
  412. }
  413. m_targetHistory.emplace_back(m_targetRow, m_targetCol);
  414. m_lastTargetRow = m_targetRow;
  415. m_lastTargetCol = m_targetCol;
  416. m_targetRow = -1;
  417. m_targetCol = -1;
  418. }
  419. }
  420. }
  421. if (m_targetStimulationDecoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedEnd)) { }
  422. boxContext.markInputAsDeprecated(1, i);
  423. }
  424. }
  425. // --- Selection stimulations
  426. for (size_t k = 2; k < 4; ++k)
  427. {
  428. Kernel::IAlgorithmProxy* decoder = (k == 2 ? m_rowSelectionStimulationDecoder : m_columnSelectionStimulationDecoder);
  429. Kernel::TParameterHandler<const IMemoryBuffer*> selectionMemoryBuffer(
  430. decoder->getInputParameter(OVP_GD_Algorithm_StimulationDecoder_InputParameterId_MemoryBufferToDecode));
  431. Kernel::TParameterHandler<IStimulationSet*> selectionStimulationSet(
  432. decoder->getOutputParameter(OVP_GD_Algorithm_StimulationDecoder_OutputParameterId_StimulationSet));
  433. for (size_t i = 0; i < boxContext.getInputChunkCount(k); ++i)
  434. {
  435. if (m_lastTime >= boxContext.getInputChunkStartTime(k, i))
  436. {
  437. selectionMemoryBuffer = boxContext.getInputChunk(k, i);
  438. decoder->process();
  439. if (decoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedHeader)) { }
  440. if (decoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedBuffer))
  441. {
  442. IStimulationSet* stimulationSet = selectionStimulationSet;
  443. for (size_t j = 0; j < stimulationSet->getStimulationCount(); ++j)
  444. {
  445. uint64_t id = stimulationSet->getStimulationIdentifier(j);
  446. bool selected = false;
  447. if (id >= m_rowStimulationBase && id < m_rowStimulationBase + m_nRow)
  448. {
  449. this->getLogManager() << Kernel::LogLevel_Debug << "Received Selected Row " << id << "\n";
  450. m_selectedRow = int(id - m_rowStimulationBase);
  451. selected = true;
  452. }
  453. if (id >= m_columnStimulationBase && id < m_columnStimulationBase + m_nRow)
  454. {
  455. this->getLogManager() << Kernel::LogLevel_Debug << "Received Selected Column " << id << "\n";
  456. m_selectedCol = int(id - m_columnStimulationBase);
  457. selected = true;
  458. }
  459. if (id == OVTK_StimulationId_Label_00)
  460. {
  461. if (k == 2) { m_selectedRow = -2; }
  462. if (k == 3) { m_selectedCol = -2; }
  463. selected = true;
  464. }
  465. if (selected && m_selectedRow != -1 && m_selectedCol != -1)
  466. {
  467. if (m_selectedRow >= 0 && m_selectedCol >= 0)
  468. {
  469. this->getLogManager() << Kernel::LogLevel_Debug << "Displays Selected Cell\n";
  470. this->cacheForEachIf(m_selectedRow, m_selectedCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB,
  471. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, &m_selectedBgColor, nullptr);
  472. this->cacheForEachIf(m_selectedRow, m_selectedCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB,
  473. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, &m_selectedFgColor, nullptr);
  474. this->cacheForEachIf(m_selectedRow, m_selectedCol, &CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB,
  475. &CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB, m_selectedFontDesc, nullptr);
  476. std::vector<GtkWidget*> widgets;
  477. this->cacheForEachIf(m_selectedRow, m_selectedCol, &CBoxAlgorithmP300TactileVisualization::cacheCollectChildWidgetCB,
  478. &CBoxAlgorithmP300TactileVisualization::cacheCollectChildWidgetCB, &widgets, nullptr);
  479. if (widgets.size() == 1)
  480. {
  481. if (GTK_IS_LABEL(widgets[0]))
  482. {
  483. std::string label;
  484. label = gtk_label_get_text(GTK_LABEL(widgets[0]));
  485. if (!m_targetHistory.empty())
  486. {
  487. auto it = m_targetHistory.begin();
  488. bool correct = (it->first == m_selectedRow && it->second == m_selectedCol);
  489. bool halfCorrect = (it->first == m_selectedRow || it->second == m_selectedCol);
  490. m_targetHistory.pop_front();
  491. std::string tmp;
  492. if (correct) { tmp = "<span color=\"darkgreen\">"; }
  493. else if (halfCorrect) { tmp = "<span color=\"darkorange\">"; }
  494. else { tmp = "<span color=\"darkred\">"; }
  495. label = tmp.append(label).append("</span>");
  496. }
  497. label = std::string(gtk_label_get_label(m_result)).append(label);
  498. gtk_label_set_markup(m_result, label.c_str());
  499. }
  500. else
  501. {
  502. this->getLogManager() << Kernel::LogLevel_Warning <<
  503. "Expected label class widget... could not find a valid text to append\n";
  504. }
  505. }
  506. else
  507. {
  508. this->getLogManager() << Kernel::LogLevel_Warning << "Did not find a unique widget at row : " << size_t(m_selectedRow) <<
  509. " column : " << size_t(m_selectedCol) << "\n";
  510. }
  511. }
  512. else
  513. {
  514. this->getLogManager() << Kernel::LogLevel_Trace << "Selection Rejected !\n";
  515. std::string label;
  516. label = gtk_label_get_text(m_result);
  517. label += "*";
  518. gtk_label_set_text(m_result, label.c_str());
  519. }
  520. m_selectedRow = -1;
  521. m_selectedCol = -1;
  522. }
  523. //Load new menuentries
  524. uint64_t label_id = id - m_rowStimulationBase;
  525. if(main)
  526. {
  527. set_labeltext(label_id);
  528. main = false;
  529. }
  530. else if(label_id == 5)
  531. {
  532. set_labeltext(6);
  533. main = true;
  534. }
  535. }
  536. }
  537. if (decoder->isOutputTriggerActive(OVP_GD_Algorithm_StimulationDecoder_OutputTriggerId_ReceivedEnd)) { }
  538. boxContext.markInputAsDeprecated(k, i);
  539. }
  540. }
  541. }
  542. // After any possible rendering, we flush the accumulated stimuli. The default idle func is low priority, so it should be run after rendering by gtk.
  543. if (m_idleFuncTag == 0) { m_idleFuncTag = g_idle_add(FlushCB, this); }
  544. return true;
  545. }
  546. // _________________________________________________________________________________________________________________________________________________________
  547. //
  548. void CBoxAlgorithmP300TactileVisualization::cacheBuildFromTable(GtkTable* table)
  549. {
  550. if (table)
  551. {
  552. const GdkColor white = InitGDKColor(65535, 65535, 65535, 65535);
  553. for (GList* list = table->children; list; list = list->next)
  554. {
  555. GtkTableChild* child = static_cast<GtkTableChild*>(list->data);
  556. for (size_t i = child->top_attach; i < child->bottom_attach; ++i)
  557. {
  558. for (size_t j = child->left_attach; j < child->right_attach; ++j)
  559. {
  560. widget_style_t& style = m_cache[i][j];
  561. style.widget = child->widget;
  562. style.childWidget = gtk_bin_get_child(GTK_BIN(child->widget));
  563. style.bgColor = white;
  564. style.fgColor = white;
  565. style.fontDesc = nullptr;
  566. }
  567. }
  568. }
  569. }
  570. }
  571. void CBoxAlgorithmP300TactileVisualization::cacheForEach(const cache_callback callback, void* data)
  572. {
  573. for (auto i = m_cache.begin(); i != m_cache.end(); ++i)
  574. {
  575. for (auto j = i->second.begin(); j != i->second.end(); ++j) { (this->*callback)(j->second, data); }
  576. }
  577. }
  578. void CBoxAlgorithmP300TactileVisualization::cacheForEachIf(const int iLine, const int iColumn, const cache_callback ifCB, const cache_callback elseCB,
  579. void* ifUserData, void* elseUserData)
  580. {
  581. for (auto i = m_cache.begin(); i != m_cache.end(); ++i)
  582. {
  583. for (auto j = i->second.begin(); j != i->second.end(); ++j)
  584. {
  585. const bool line = (iLine != -1);
  586. const bool column = (iColumn != -1);
  587. bool inLine = false;
  588. bool inCol = false;
  589. bool first;
  590. if (line && size_t(iLine) == i->first) { inLine = true; }
  591. if (column && size_t(iColumn) == j->first) { inCol = true; }
  592. if (line && column) { first = inLine && inCol; }
  593. else { first = inLine || inCol; }
  594. if (first) { (this->*ifCB)(j->second, ifUserData); }
  595. else { (this->*elseCB)(j->second, elseUserData); }
  596. }
  597. }
  598. }
  599. void CBoxAlgorithmP300TactileVisualization::cacheChangeNullCB(widget_style_t& /*rWidgetStyle*/, void* /*data*/) { }
  600. void CBoxAlgorithmP300TactileVisualization::cacheChangeBackgroundCB(widget_style_t& style, void* data)
  601. {
  602. GdkColor oColor = *static_cast<GdkColor*>(data);
  603. if (memcmp(&style.bgColor, &oColor, sizeof(GdkColor)) != 0)
  604. {
  605. gtk_widget_modify_bg(style.widget, GTK_STATE_NORMAL, &oColor);
  606. style.bgColor = oColor;
  607. }
  608. }
  609. void CBoxAlgorithmP300TactileVisualization::cacheChangeForegroundCB(widget_style_t& style, void* data)
  610. {
  611. GdkColor oColor = *static_cast<GdkColor*>(data);
  612. if (memcmp(&style.fgColor, &oColor, sizeof(GdkColor)) != 0)
  613. {
  614. gtk_widget_modify_fg(style.childWidget, GTK_STATE_NORMAL, &oColor);
  615. style.fgColor = oColor;
  616. }
  617. }
  618. void CBoxAlgorithmP300TactileVisualization::cacheChangeFontCB(widget_style_t& style, void* data)
  619. {
  620. auto* pFontDescription = static_cast<PangoFontDescription*>(data);
  621. if (style.fontDesc != pFontDescription)
  622. {
  623. gtk_widget_modify_font(style.childWidget, pFontDescription);
  624. style.fontDesc = pFontDescription;
  625. }
  626. }
  627. void CBoxAlgorithmP300TactileVisualization::cacheCollectWidgetCB(widget_style_t& style, void* data)
  628. {
  629. if (data) { (static_cast<std::vector<GtkWidget*>*>(data))->push_back(style.widget); }
  630. }
  631. void CBoxAlgorithmP300TactileVisualization::cacheCollectChildWidgetCB(widget_style_t& style, void* data)
  632. {
  633. if (data) { (static_cast<std::vector<GtkWidget*>*>(data))->push_back(style.childWidget); }
  634. }
  635. // Note that we don't need concurrency control here as gtk callbacks run in the main thread
  636. void CBoxAlgorithmP300TactileVisualization::flushQueue()
  637. {
  638. for (const auto& stimulation : m_stimuliQueue) { m_stimulusSender->sendStimulation(stimulation); }
  639. m_stimuliQueue.clear();
  640. // This function will be automatically removed after completion, so set to 0
  641. m_idleFuncTag = 0;
  642. }
  643. } // namespace Tactilebci
  644. } // namespace Plugins
  645. } // namespace OpenViBE