Browse Source

Dateien hochladen nach „src/UDPStimCodeSender“

master
Tobias Baumann 2 years ago
parent
commit
0df3ef9057

+ 103
- 0
src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.cpp View File

///-------------------------------------------------------------------------------------------------
///
/// \file CBoxAlgorithmUDPStimcodeSender.cpp
/// \brief Functions of Class UDP-StimCode-Sender
/// \author Tobias Baumann (TH Nuernberg).
/// \version 1.2.
/// \date Mon Oct 04 12:43:53 2021.
/// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
///
///-------------------------------------------------------------------------------------------------
//includes
#include "ovpCBoxAlgorithmUDPStimcodeSender.h"
using namespace OpenViBE;
using namespace /*OpenViBE::*/Kernel;
using namespace /*OpenViBE::*/Plugins;
using namespace /*OpenViBE::Plugins::*/Tactilebci;
bool CBoxAlgorithmUDPStimcodeSender::initialize()
{
m_StimDecoder.initialize(*this, 0);
m_StimEncoder.initialize(*this, 0);
// retrieve box settings
FeatherIP = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 0);
FeatherPort = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 1);
RowStimulationBase = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 2);
NumberofTactilos = 6; //if this value is specified via box settings this line is not needed
//NumberofTactilos = FSettingValueAutoCast(*this->getBoxAlgorithmContext(), 3); //used if this value is set in box settings
// connect UDP socket
boost::asio::ip::udp::endpoint Feather(boost::asio::ip::address::from_string(FeatherIP), FeatherPort);
socket.connect(Feather);
return true;
}
/*******************************************************************************/
bool CBoxAlgorithmUDPStimcodeSender::uninitialize()
{
m_StimDecoder.uninitialize();
m_StimEncoder.uninitialize();
return true;
}
/*******************************************************************************/
bool CBoxAlgorithmUDPStimcodeSender::processInput(const size_t index)
{
// some pre-processing code if needed...
// ready to process !
getBoxAlgorithmContext()->markAlgorithmAsReadyToProcess();
return true;
}
/*******************************************************************************/
bool CBoxAlgorithmUDPStimcodeSender::process()
{
// the static box context describes the box inputs, outputs, settings structures
const IBox& staticBoxContext = this->getStaticBoxContext();
// the dynamic box context describes the current state of the box inputs and outputs (i.e. the chunks)
IBoxIO& boxContext = this->getDynamicBoxContext();
uint64_t StimulationID = 0;
uint64_t ChunkStartTime = 0;
uint64_t ChunkEndTime = 0;
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)
{
// decode the chunk i
m_StimDecoder.decode(i);
if(m_StimDecoder.isBufferReceived())
{
//check received stimulations
IStimulationSet* StimSet = m_StimDecoder.getOutputStimulationSet();
for(uint64_t j=0; j<StimSet->getStimulationCount(); j++)
{
StimulationID = StimSet->getStimulationIdentifier(j);
if(StimulationID >= RowStimulationBase && StimulationID < (RowStimulationBase + NumberofTactilos))
{
this->getLogManager() << LogLevel_Info << "send udp : [StimulusCode " << (StimulationID-RowStimulationBase+1) << "]\n";
socket.send(boost::asio::buffer(std::to_string(StimulationID-RowStimulationBase+1)));
}
}
}
// forward input chunks
boxContext.getInputChunk(0, i, ChunkStartTime, ChunkEndTime, Size, Buffer);
boxContext.appendOutputChunkData(0, Buffer, Size);
boxContext.markOutputAsReadyToSend(0, ChunkStartTime, ChunkEndTime);
boxContext.markInputAsDeprecated(0, i);
}
return true;
}

+ 155
- 0
src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.h View File

///-------------------------------------------------------------------------------------------------
///
/// \file CBoxAlgorithmUDPStimcodeSender.h
/// \brief Classes of the Box UDPStimcodeSender.
/// \author Tobias Baumann (TH Nuernberg).
/// \version 1.1.
/// \date Mon Oct 04 12:43:53 2021.
/// \copyright <a href="https://choosealicense.com/licenses/agpl-3.0/">GNU Affero General Public License v3.0</a>.
///
///-------------------------------------------------------------------------------------------------
//includes
#pragma once
//You may have to change this path to match your folder organisation
#include "../ovp_defines.h"
#include <openvibe/ov_all.h>
#include <toolkit/ovtk_all.h>
#include <boost/asio.hpp>
namespace OpenViBE
{
namespace Plugins
{
namespace Tactilebci
{
/// <summary> The class CBoxAlgorithmUDPStimcodeSender describes the box UDPStimcodeSender. </summary>
class CBoxAlgorithmUDPStimcodeSender final : virtual public Toolkit::TBoxAlgorithm<IBoxAlgorithm>
{
public:
void release() override { delete this; }
bool initialize() override;
bool uninitialize() override;
bool processInput(const size_t index) override;
bool process() override;
// 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 >
_IsDerivedFromClass_Final_(Toolkit::TBoxAlgorithm<IBoxAlgorithm>, OVP_ClassId_BoxAlgorithm_UDPStimcodeSender)
protected:
// Input decoder:
Toolkit::TStimulationDecoder<CBoxAlgorithmUDPStimcodeSender> m_StimDecoder;
// Output decoder:
Toolkit::TStimulationEncoder<CBoxAlgorithmUDPStimcodeSender> m_StimEncoder;
private:
// Box setting variables
CString FeatherIP;
uint64_t FeatherPort;
uint64_t RowStimulationBase;
uint64_t NumberofTactilos;
// Socket
boost::asio::io_service io_service;
boost::asio::ip::udp::socket socket{io_service};
};
// If you need to implement a box Listener, here is a sekeleton for you.
// Use only the callbacks you need.
// For example, if your box has a variable number of input, but all of them must be stimulation inputs.
// The following listener callback will ensure that any newly added input is stimulations :
/*
bool onInputAdded(Kernel::IBox& box, const size_t index) override
{
box.setInputType(index, OV_TypeId_Stimulations);
};
*/
/*
// The box listener can be used to call specific callbacks whenever the box structure changes : input added, name changed, etc.
// Please uncomment below the callbacks you want to use.
/// <summary> Listener of the box UDPStimcodeSender. </summary>
class CBoxAlgorithmUDPStimcodeSenderListener final : public Toolkit::TBoxListener<IBoxListener>
{
public:
//bool onInitialized(Kernel::IBox& box) override { return true; };
//bool onNameChanged(Kernel::IBox& box) override { return true; };
//bool onInputConnected(Kernel::IBox& box, const size_t index) override { return true; };
//bool onInputDisconnected(Kernel::IBox& box, const size_t index) override { return true; };
//bool onInputAdded(Kernel::IBox& box, const size_t index) override { return true; };
//bool onInputRemoved(Kernel::IBox& box, const size_t index) override { return true; };
//bool onInputTypeChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onInputNameChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onOutputConnected(Kernel::IBox& box, const size_t index) override { return true; };
//bool onOutputDisconnected(Kernel::IBox& box, const size_t index) { return true; };
//bool onOutputAdded(Kernel::IBox& box, const size_t index) override { return true; };
//bool onOutputRemoved(Kernel::IBox& box, const size_t index) override { return true; };
//bool onOutputTypeChanged(Kernel::IBox& box, const size_t index) override override { return true; };
//bool onOutputNameChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingAdded(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingRemoved(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingTypeChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingNameChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingDefaultValueChanged(Kernel::IBox& box, const size_t index) override { return true; };
//bool onSettingValueChanged(Kernel::IBox& box, const size_t index) override { return true; };
_IsDerivedFromClass_Final_(Toolkit::TBoxListener<IBoxListener>, CIdentifier::undefined())
};
*/
/// <summary> Descriptor of the box UDPStimcodeSender. </summary>
class CBoxAlgorithmUDPStimcodeSenderDesc final : virtual public IBoxAlgorithmDesc
{
public:
void release() override { }
CString getName() const override { return CString("UDPStimcodeSender"); }
CString getAuthorName() const override { return CString("Tobias Baumann"); }
CString getAuthorCompanyName() const override { return CString("TH Nuernberg"); }
CString getShortDescription() const override { return CString("Sends Stimulationcodes via UDP"); }
CString getDetailedDescription() const override { return CString("Sends the received Stimulationcodes the Adafruit Feather, using UDP. The Input Stimulation will be passed to the Output unchanged."); }
CString getCategory() const override { return CString("TactileBCI"); }
CString getVersion() const override { return CString("1.1"); }
CString getStockItemName() const override { return CString("gtk-network"); }
CIdentifier getCreatedClass() const override { return OVP_ClassId_BoxAlgorithm_UDPStimcodeSender; }
IPluginObject* create() override { return new CBoxAlgorithmUDPStimcodeSender; }
/*
IBoxListener* createBoxListener() const override { return new CBoxAlgorithmUDPStimcodeSenderListener; }
void releaseBoxListener(IBoxListener* listener) const override { delete listener; }
*/
bool getBoxPrototype(Kernel::IBoxProto& prototype) const override
{
prototype.addInput("StimcodeIn",OV_TypeId_Stimulations);
//prototype.addFlag(Kernel::BoxFlag_CanModifyInput);
//prototype.addFlag(Kernel::BoxFlag_CanAddInput);
prototype.addOutput("StimcodeOut",OV_TypeId_Stimulations);
//prototype.addFlag(Kernel::BoxFlag_CanModifyOutput);
//prototype.addFlag(Kernel::BoxFlag_CanAddOutput);
prototype.addSetting("FeatherIP",OV_TypeId_String,"192.168.4.1");
prototype.addSetting("FeatherPort",OV_TypeId_Integer,"8888");
prototype.addSetting("RowStimulationBase",OV_TypeId_Stimulation,"OVTK_StimulationId_Label_01");
//prototype.addSetting("Number of Tactilos",OV_TypeId_Integer,"6"); //used to make this setting accessable in the box settings
prototype.addFlag(Kernel::BoxFlag_CanModifySetting);
//prototype.addFlag(Kernel::BoxFlag_CanAddSetting);
prototype.addFlag(OV_AttributeId_Box_FlagIsUnstable);
return true;
}
_IsDerivedFromClass_Final_(IBoxAlgorithmDesc, OVP_ClassId_BoxAlgorithm_UDPStimcodeSenderDesc)
};
} // namespace Tactilebci
} // namespace Plugins
} // namespace OpenViBE

Loading…
Cancel
Save