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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #include <string>
  14. using namespace OpenViBE;
  15. using namespace /*OpenViBE::*/Kernel;
  16. using namespace /*OpenViBE::*/Plugins;
  17. using namespace /*OpenViBE::Plugins::*/Tactilebci;
  18. /*******************************************************************************/
  19. bool CBoxAlgorithmTactiloController::initialize()
  20. {
  21. m_StimDecoder.initialize(*this, 0);
  22. m_StimEncoder.initialize(*this, 0);
  23. //get Box Settings
  24. m_PortName = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
  25. m_RowBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
  26. m_StartStimulation = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
  27. m_StopStimulation = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 3);
  28. m_NumberofTactilos = 6; //if this value is specified via box settings this line is not needed
  29. //NumberofTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 3); //used if this value is set in box settings
  30. //open serial port and set baudrate to 115200
  31. m_Port.open(m_PortName.toASCIIString());
  32. m_Port.set_option(boost::asio::serial_port::baud_rate(115200));
  33. if(m_Port.is_open())
  34. {
  35. this->getLogManager() << LogLevel_Info << "Connected to Serial Port: " << m_PortName << "\n";
  36. m_Port.write_some(boost::asio::buffer("0"));
  37. }
  38. return true;
  39. }
  40. /*******************************************************************************/
  41. bool CBoxAlgorithmTactiloController::uninitialize()
  42. {
  43. this->getLogManager() << LogLevel_Info << "Close Serial Port: " << m_PortName << "\n";
  44. m_Port.write_some(boost::asio::buffer("0"));
  45. m_Port.close();
  46. m_StimDecoder.uninitialize();
  47. m_StimEncoder.uninitialize();
  48. return true;
  49. }
  50. /*******************************************************************************/
  51. bool CBoxAlgorithmTactiloController::processInput(const size_t index)
  52. {
  53. // some pre-processing code if needed...
  54. // ready to process !
  55. getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  56. return true;
  57. }
  58. /*******************************************************************************/
  59. bool CBoxAlgorithmTactiloController::process()
  60. {
  61. // the static box context describes the box inputs, outputs, settings structures
  62. const IBox& staticBoxContext = this->getStaticBoxContext();
  63. // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
  64. IBoxIO& boxContext = this->getDynamicBoxContext();
  65. uint64_t TactiloNr = 0;
  66. uint64_t StimulationID = 0;
  67. uint64_t ChunkStartTime = 0;
  68. uint64_t ChunkEndTime = 0;
  69. uint64_t Size = 0;
  70. const uint8_t* Buffer = nullptr;
  71. //iterate over all chunk on input 0
  72. for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  73. {
  74. // decode the chunk i
  75. m_StimDecoder.decode(i);
  76. if(m_StimDecoder.isBufferReceived())
  77. {
  78. //check received stimulations
  79. IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
  80. for(uint64_t j=0; j<StimSet->getStimulationCount(); j++)
  81. {
  82. StimulationID = StimSet->getStimulationIdentifier(j);
  83. if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + m_NumberofTactilos))
  84. {
  85. TactiloNr = StimulationID-m_RowBase+1;
  86. }
  87. if(StimulationID == m_StartStimulation || StimulationID == m_StopStimulation)
  88. {
  89. this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << TactiloNr << " ON\n";
  90. m_Port.write_some(boost::asio::buffer(std::to_string(TactiloNr)));
  91. }
  92. if(StimulationID == m_StopStimulation)
  93. {
  94. this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << TactiloNr << " OFF\n";
  95. m_Port.write_some(boost::asio::buffer(std::to_string(TactiloNr)));
  96. }
  97. }
  98. }
  99. // forward input chunks
  100. boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
  101. boxContext.appendOutputChunkData(0, Buffer, Size);
  102. boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
  103. boxContext.markInputAsDeprecated(0, i);
  104. }
  105. return true;
  106. }
  107. /*******************************************************************************/