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.

ovpCBoxAlgorithmUDPStimcodeSender.cpp 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. ///-------------------------------------------------------------------------------------------------
  2. ///
  3. /// \file CBoxAlgorithmUDPStimcodeSender.cpp
  4. /// \brief Functions of Class UDP-StimCode-Sender
  5. /// \author Tobias Baumann (TH Nuernberg).
  6. /// \version 1.2.
  7. /// \date Mon Oct 04 12:43:53 2021.
  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 "ovpCBoxAlgorithmUDPStimcodeSender.h"
  13. using namespace OpenViBE;
  14. using namespace /*OpenViBE::*/Kernel;
  15. using namespace /*OpenViBE::*/Plugins;
  16. using namespace /*OpenViBE::Plugins::*/Tactilebci;
  17. bool CBoxAlgorithmUDPStimcodeSender::initialize()
  18. {
  19. m_StimDecoder.initialize(*this, 0);
  20. m_StimEncoder.initialize(*this, 0);
  21. // retrieve box settings
  22. FeatherIP = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
  23. FeatherPort = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
  24. RowStimulationBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
  25. NumberofTactilos = 6; //if this value is specified via box settings this line is not needed
  26. //NumberofTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 3); //used if this value is set in box settings
  27. // connect UDP socket
  28. boost::asio::ip::udp::endpoint Feather(boost::asio::ip::address::from_string(FeatherIP), FeatherPort);
  29. socket.connect(Feather);
  30. return true;
  31. }
  32. /*******************************************************************************/
  33. bool CBoxAlgorithmUDPStimcodeSender::uninitialize()
  34. {
  35. m_StimDecoder.uninitialize();
  36. m_StimEncoder.uninitialize();
  37. return true;
  38. }
  39. /*******************************************************************************/
  40. bool CBoxAlgorithmUDPStimcodeSender::processInput(const size_t index)
  41. {
  42. // some pre-processing code if needed...
  43. // ready to process !
  44. getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  45. return true;
  46. }
  47. /*******************************************************************************/
  48. bool CBoxAlgorithmUDPStimcodeSender::process()
  49. {
  50. // the static box context describes the box inputs, outputs, settings structures
  51. const IBox& staticBoxContext = this->getStaticBoxContext();
  52. // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
  53. IBoxIO& boxContext = this->getDynamicBoxContext();
  54. uint64_t StimulationID = 0;
  55. uint64_t ChunkStartTime = 0;
  56. uint64_t ChunkEndTime = 0;
  57. uint64_t Size = 0;
  58. const uint8_t* Buffer = nullptr;
  59. //iterate over all chunk on input 0
  60. for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  61. {
  62. // decode the chunk i
  63. m_StimDecoder.decode(i);
  64. if(m_StimDecoder.isBufferReceived())
  65. {
  66. //check received stimulations
  67. IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
  68. for(uint64_t j=0; j<StimSet->getStimulationCount(); j++)
  69. {
  70. StimulationID = StimSet->getStimulationIdentifier(j);
  71. if(StimulationID >= RowStimulationBase && StimulationID < (RowStimulationBase + NumberofTactilos))
  72. {
  73. this->getLogManager() << LogLevel_Debug << "send udp : [StimulusCode " << (StimulationID-RowStimulationBase+1) << "]\n";
  74. socket.send(boost::asio::buffer(std::to_string(StimulationID-RowStimulationBase+1)));
  75. }
  76. }
  77. }
  78. // forward input chunks
  79. boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
  80. boxContext.appendOutputChunkData(0, Buffer, Size);
  81. boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
  82. boxContext.markInputAsDeprecated(0, i);
  83. }
  84. return true;
  85. }