Dateien hochladen nach „src/TactiloController“

This commit is contained in:
Tobias Baumann 2022-05-23 16:31:36 +00:00
parent 70d5a8043d
commit 1840fc3e99
2 changed files with 151 additions and 139 deletions

View File

@ -1,138 +1,146 @@
///------------------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------------------
/// ///
/// \file ovpCBoxAlgorithmTactiloController.cpp /// \file ovpCBoxAlgorithmTactiloController.cpp
/// \brief Functions of the Box Tactilo Controller. /// \brief Functions of the Box Tactilo Controller.
/// \author Tobias Baumann (TH-Nürnberg). /// \author Tobias Baumann (TH-Nürnberg).
/// \version 1.0. /// \version 1.0.
/// \date Mon Feb 21 14:59:56 2022. /// \date Mon Feb 21 14:59:56 2022.
/// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>. /// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
/// ///
///------------------------------------------------------------------------------------------------- ///-------------------------------------------------------------------------------------------------
//includes //includes
#include "ovpCBoxAlgorithmTactiloController.h" #include "ovpCBoxAlgorithmTactiloController.h"
#include <string>
using namespace OpenViBE;
using namespace OpenViBE; using namespace /*OpenViBE::*/Kernel;
using namespace /*OpenViBE::*/Kernel; using namespace /*OpenViBE::*/Plugins;
using namespace /*OpenViBE::*/Plugins; using namespace /*OpenViBE::Plugins::*/Tactilebci;
using namespace /*OpenViBE::Plugins::*/Tactilebci;
/*******************************************************************************/
/*******************************************************************************/
bool CBoxAlgorithmTactiloController::initialize()
bool CBoxAlgorithmTactiloController::initialize() {
{ m_StimDecoder.initialize(*this, 0);
m_StimDecoder.initialize(*this, 0); m_StimEncoder.initialize(*this, 0);
m_StimEncoder.initialize(*this, 0);
//get Box Settings
//get Box Settings m_PortName = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
m_PortName = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0); m_RowBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
m_RowBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1); m_nTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
m_nTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
//set m_nTactilos to 2 if lower than 2
//set m_nTactilos to 2 if lower than 2 if(m_nTactilos < 2)
if(m_nTactilos < 2) {
{ m_nTactilos = 2;
m_nTactilos = 2; }
} //set m_nTactilos to MAX if greater than MAX_TACTILOS
//set m_nTactilos to MAX if greater than MAX_TACTILOS if(m_nTactilos > MAX_TACTILOS)
if(m_nTactilos > MAX_TACTILOS) {
{ m_nTactilos = MAX_TACTILOS;
m_nTactilos = MAX_TACTILOS; }
}
//open serial port and set baudrate to 115200
//open serial port and set baudrate to 115200 m_Port.open(m_PortName.toASCIIString());
m_Port.open(m_PortName.toASCIIString()); m_Port.set_option(boost::asio::serial_port::baud_rate(115200));
m_Port.set_option(boost::asio::serial_port::baud_rate(115200));
if(m_Port.is_open())
if(m_Port.is_open()) {
{ this->getLogManager() << LogLevel_Info << "Connected to Serial Port: " << m_PortName << "\n";
this->getLogManager() << LogLevel_Info << "Connected to Serial Port: " << m_PortName << "\n"; m_Port.write_some(boost::asio::buffer("b"));
m_Port.write_some(boost::asio::buffer("b")); }
}
return true;
return true; }
}
/*******************************************************************************/
/*******************************************************************************/
bool CBoxAlgorithmTactiloController::uninitialize()
bool CBoxAlgorithmTactiloController::uninitialize() {
{ this->getLogManager() << LogLevel_Info << "Disconnect from Serial Port: " << m_PortName << "\n";
this->getLogManager() << LogLevel_Info << "Disconnect from Serial Port: " << m_PortName << "\n"; m_Port.write_some(boost::asio::buffer("e"));
m_Port.write_some(boost::asio::buffer("e")); m_Port.close();
m_Port.close();
m_StimDecoder.uninitialize();
m_StimDecoder.uninitialize(); m_StimEncoder.uninitialize();
m_StimEncoder.uninitialize();
return true;
return true; }
}
/*******************************************************************************/
/*******************************************************************************/
bool CBoxAlgorithmTactiloController::processInput(const size_t index)
bool CBoxAlgorithmTactiloController::processInput(const size_t index) {
{ // some pre-processing code if needed...
// some pre-processing code if needed...
// ready to process !
// ready to process ! getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
return true;
return true; }
}
/*******************************************************************************/
/*******************************************************************************/ bool CBoxAlgorithmTactiloController::process()
bool CBoxAlgorithmTactiloController::process() {
{
// the static box context describes the box inputs, outputs, settings structures
// the static box context describes the box inputs, outputs, settings structures const IBox& staticBoxContext = this->getStaticBoxContext();
const IBox& staticBoxContext = this->getStaticBoxContext(); // the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
// the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks) IBoxIO& boxContext = this->getDynamicBoxContext();
IBoxIO& boxContext = this->getDynamicBoxContext();
uint64_t ChunkStartTime = 0;
uint64_t StimulationID = 0; uint64_t ChunkEndTime = 0;
uint64_t ChunkStartTime = 0; uint64_t Size = 0;
uint64_t ChunkEndTime = 0; const uint8_t* Buffer = nullptr;
uint64_t Size = 0;
const uint8_t* Buffer = nullptr; //iterate over all chunk on input 0
for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i)
//iterate over all chunk on input 0 {
for (uint64_t i = 0; i < boxContext.getInputChunkCount(0); ++i) // decode the chunk i
{ m_StimDecoder.decode(i);
// decode the chunk i
m_StimDecoder.decode(i); if(m_StimDecoder.isBufferReceived())
{
if(m_StimDecoder.isBufferReceived()) //check received stimulations
{ CStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
//check received stimulations for(uint64_t j=0; j<StimSet->size(); j++)
IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet(); {
for(uint64_t j=0; j<StimSet->getStimulationCount(); j++) uint64_t StimulationID = StimSet->getId(j);
{
StimulationID = StimSet->getStimulationIdentifier(j); if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + m_nTactilos))
{
if(StimulationID >= m_RowBase && StimulationID < (m_RowBase + m_nTactilos)) m_currTactiloNr = StimulationID-m_RowBase+1;
{ }
m_currTactiloNr = StimulationID-m_RowBase+1; if(StimulationID == OVTK_StimulationId_VisualStimulationStart)
} {
if(StimulationID == OVTK_StimulationId_VisualStimulationStart) this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " ON\n";
{ boost::asio::async_write(m_Port, boost::asio::buffer(std::to_string(m_currTactiloNr-1), 1), boost::bind(&CBoxAlgorithmTactiloController::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " ON\n"; }
m_Port.write_some(boost::asio::buffer(std::to_string(m_currTactiloNr-1))); if(StimulationID == OVTK_StimulationId_VisualStimulationStop)
} {
if(StimulationID == OVTK_StimulationId_VisualStimulationStop) this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " OFF\n";
{ boost::asio::async_write(m_Port, boost::asio::buffer(std::to_string(m_currTactiloNr-1), 1), boost::bind(&CBoxAlgorithmTactiloController::handler, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
this->getLogManager() << LogLevel_Debug << "Tactilo Nr. " << m_currTactiloNr << " OFF\n"; }
m_Port.write_some(boost::asio::buffer(std::to_string(m_currTactiloNr-1))); }
} }
}
} // forward input chunks
// forward input chunks boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer); boxContext.appendOutputChunkData(0, Buffer, Size);
boxContext.appendOutputChunkData(0, Buffer, Size); boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime); boxContext.markInputAsDeprecated(0, i);
boxContext.markInputAsDeprecated(0, i);
} }
return true; return true;
} }
/*******************************************************************************/ /*******************************************************************************/
void CBoxAlgorithmTactiloController::handler(const boost::system::error_code& error, std::size_t bytes_transferred)
{
if(error)
{
this->getLogManager() << LogLevel_Warning << error.message() << "\n";
}
}

View File

@ -17,6 +17,7 @@
#include <openvibe/ov_all.h> #include <openvibe/ov_all.h>
#include <toolkit/ovtk_all.h> #include <toolkit/ovtk_all.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <string> #include <string>
namespace OpenViBE namespace OpenViBE
@ -35,7 +36,10 @@ namespace OpenViBE
bool uninitialize() override; bool uninitialize() override;
bool processInput(const size_t index) override; bool processInput(const size_t index) override;
bool process() override; bool process() override;
// write handler
void handler(const boost::system::error_code& error, std::size_t bytes_transferred);
// As we do with any class in openvibe, we use the macro below to associate this box to an unique identifier. // As we do with any class in openvibe, we use the macro below to associate this box to an unique identifier.
// The inheritance information is also made available, as we provide the superclass Toolkit::TBoxAlgorithm < IBoxAlgorithm > // The inheritance information is also made available, as we provide the superclass Toolkit::TBoxAlgorithm < IBoxAlgorithm >
_IsDerivedFromClass_Final_(Toolkit::TBoxAlgorithm<IBoxAlgorithm>, OVP_ClassId_BoxAlgorithm_TactiloController) _IsDerivedFromClass_Final_(Toolkit::TBoxAlgorithm<IBoxAlgorithm>, OVP_ClassId_BoxAlgorithm_TactiloController)