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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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_input0Decoder.initialize(*this, 0);
  22. m_output0Encoder.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. //open serial port
  29. m_Port.open(m_PortName.toASCIIString());
  30. if(m_Port.is_open())
  31. {
  32. this->getLogManager() << LogLevel_Info << "Connected to Serial Port: " << m_PortName << "\n";
  33. }
  34. return true;
  35. }
  36. /*******************************************************************************/
  37. bool CBoxAlgorithmTactiloController::uninitialize()
  38. {
  39. m_input0Decoder.uninitialize();
  40. m_output0Encoder.uninitialize();
  41. m_Port.close();
  42. return true;
  43. }
  44. /*******************************************************************************/
  45. bool CBoxAlgorithmTactiloController::processInput(const size_t index)
  46. {
  47. // some pre-processing code if needed...
  48. // ready to process !
  49. getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
  50. return true;
  51. }
  52. /*******************************************************************************/
  53. bool CBoxAlgorithmTactiloController::process()
  54. {
  55. // the static box context describes the box inputs, outputs, settings structures
  56. const IBox& staticBoxContext = this->getStaticBoxContext();
  57. // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
  58. IBoxIO& boxContext = this->getDynamicBoxContext();
  59. uint64_t TactiloNr = 0;
  60. uint64_t StimulationID = 0;
  61. uint64_t ChunkStartTime = 0;
  62. uint64_t ChunkEndTime = 0;
  63. uint64_t Size = 0;
  64. const uint8_t* Buffer = nullptr;
  65. //iterate over all chunk on input 0
  66. for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
  67. {
  68. // decode the chunk i
  69. m_StimDecoder.decode(i);
  70. if(m_StimDecoder.isBufferReceived())
  71. {
  72. //check received stimulations
  73. IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
  74. for(uint64_t j=0; j<StimSet->getStimulationCount(); j++)
  75. {
  76. StimulationID = StimSet->getStimulationIdentifier(j);
  77. if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + NumberofTactilos))
  78. {
  79. this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << (StimulationID-m_RowBase+1) << "\n";
  80. TactiloNr = StimulationID-m_RowBase+1;
  81. }
  82. if(StimulationID == m_StartStimulation || StimulationID == m_StopStimulation)
  83. {
  84. this->getLogManager() << LogLevel_Debug << "Toggle Tactilo Nr. " << (StimulationID-m_RowBase+1) << "\n";
  85. boost::asio::write::async_write(m_Port, boost::asio::buffer(std::to_string(TactiloNr));
  86. }
  87. }
  88. }
  89. // forward input chunks
  90. boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
  91. boxContext.appendOutputChunkData(0, Buffer, Size);
  92. boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
  93. boxContext.markInputAsDeprecated(0, i);
  94. }
  95. return true;
  96. }
  97. /*******************************************************************************/