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.

ovpCBoxAlgorithmTactiloController.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file ovpCBoxAlgorithmTactiloController.cpp
  4. /// \brief Functions of the Box Tactilo Controller.
  5. /// \author Tobias Baumann (TH-Nürnberg).
  6. /// \version 1.0.
  7. /// \date Mon Feb 21 14:59:56 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 "ovpCBoxAlgorithmTactiloController.h"
  13. using namespace OpenViBE;
  14. using namespace /*OpenViBE::*/Kernel;
  15. using namespace /*OpenViBE::*/Plugins;
  16. using namespace /*OpenViBE::Plugins::*/Tactilebci;
  17. /*******************************************************************************/
  18. bool CBoxAlgorithmTactiloController::initialize()
  19. {
  20. m_StimDecoder.initialize(*this, 0);
  21. m_StimEncoder.initialize(*this, 0);
  22. //get Box Settings
  23. m_PortName = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
  24. m_RowBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
  25. m_nTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
  26. //set m_nTactilos to 2 if lower than 2
  27. if(m_nTactilos < 2)
  28. {
  29. m_nTactilos = 2;
  30. }
  31. //set m_nTactilos to MAX if greater than MAX_TACTILOS
  32. if(m_nTactilos > MAX_TACTILOS)
  33. {
  34. m_nTactilos = MAX_TACTILOS;
  35. }
  36. //open serial port and set baudrate to 115200
  37. m_Port.open(m_PortName.toASCIIString());
  38. m_Port.set_option(boost::asio::serial_port::baud_rate(115200));
  39. if(m_Port.is_open())
  40. {
  41. this->getLogManager() << LogLevel_Info << "Connected to Serial Port: " << m_PortName << "\n";
  42. m_Port.write_some(boost::asio::buffer("b"));
  43. }
  44. return true;
  45. }
  46. /*******************************************************************************/
  47. bool CBoxAlgorithmTactiloController::uninitialize()
  48. {
  49. this->getLogManager() << LogLevel_Info << "Disconnect from Serial Port: " << m_PortName << "\n";
  50. m_Port.write_some(boost::asio::buffer("e"));
  51. m_Port.close();
  52. m_StimDecoder.uninitialize();
  53. m_StimEncoder.uninitialize();
  54. return true;
  55. }
  56. /*******************************************************************************/
  57. bool CBoxAlgorithmTactiloController::processInput(const size_t index)
  58. {
  59. // some pre-processing code if needed...
  60. // ready to process !
  61. getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  62. return true;
  63. }
  64. /*******************************************************************************/
  65. bool CBoxAlgorithmTactiloController::process()
  66. {
  67. // the static box context describes the box inputs, outputs, settings structures
  68. const IBox& staticBoxContext = this->getStaticBoxContext();
  69. // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
  70. IBoxIO& boxContext = this->getDynamicBoxContext();
  71. uint64_t ChunkStartTime = 0;
  72. uint64_t ChunkEndTime = 0;
  73. uint64_t Size = 0;
  74. const uint8_t* Buffer = nullptr;
  75. //iterate over all chunk on input 0
  76. for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  77. {
  78. // decode the chunk i
  79. m_StimDecoder.decode(i);
  80. if(m_StimDecoder.isBufferReceived())
  81. {
  82. //check received stimulations
  83. CStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
  84. for(uint64_t j=0; j<StimSet->size(); j++)
  85. {
  86. uint64_t StimulationID = StimSet->getId(j);
  87. if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + m_nTactilos))
  88. {
  89. m_currTactiloID = StimulationID-m_RowBase;
  90. }
  91. if(StimulationID == OVTK_StimulationId_VisualStimulationStart)
  92. {
  93. this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " ON\n";
  94. boost::asio::async_write(m_Port, boost::asio::buffer(std::to_string(m_currTactiloID), 1), boost::bind(&CBoxAlgorithmTactiloController::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  95. }
  96. if(StimulationID == OVTK_StimulationId_VisualStimulationStop)
  97. {
  98. this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " OFF\n";
  99. boost::asio::async_write(m_Port, boost::asio::buffer(std::to_string(m_currTactiloID), 1), boost::bind(&CBoxAlgorithmTactiloController::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
  100. }
  101. }
  102. }
  103. // forward input chunks
  104. boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
  105. boxContext.appendOutputChunkData(0, Buffer, Size);
  106. boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
  107. boxContext.markInputAsDeprecated(0, i);
  108. }
  109. return true;
  110. }
  111. /*******************************************************************************/
  112. void CBoxAlgorithmTactiloController::handler(const boost::system::error_code& error, std::size_t bytes_transferred)
  113. {
  114. if(error)
  115. {
  116. this->getLogManager() << LogLevel_Warning << error.message() << "\n";
  117. }
  118. }