From 0df3ef90572360fd21bb588c06abd3c01fc92c63 Mon Sep 17 00:00:00 2001 From: Tobias Baumann Date: Thu, 10 Feb 2022 17:57:37 +0000 Subject: [PATCH] =?UTF-8?q?Dateien=20hochladen=20nach=20=E2=80=9Esrc/UDPSt?= =?UTF-8?q?imCodeSender=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ovpCBoxAlgorithmUDPStimcodeSender.cpp | 103 ++++++++++++ .../ovpCBoxAlgorithmUDPStimcodeSender.h | 155 ++++++++++++++++++ 2 files changed, 258 insertions(+) create mode 100644 src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.cpp create mode 100644 src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.h diff --git a/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.cpp b/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.cpp new file mode 100644 index 0000000..b33e89f --- /dev/null +++ b/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.cpp @@ -0,0 +1,103 @@ +///------------------------------------------------------------------------------------------------- +/// +/// \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 GNU Affero General Public License v3.0. +/// +///------------------------------------------------------------------------------------------------- + +//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; jgetStimulationCount(); 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; +} diff --git a/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.h b/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.h new file mode 100644 index 0000000..f6aa74d --- /dev/null +++ b/src/UDPStimCodeSender/ovpCBoxAlgorithmUDPStimcodeSender.h @@ -0,0 +1,155 @@ +///------------------------------------------------------------------------------------------------- +/// +/// \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 GNU Affero General Public License v3.0. +/// +///------------------------------------------------------------------------------------------------- + +//includes +#pragma once +//You may have to change this path to match your folder organisation +#include "../ovp_defines.h" + +#include +#include +#include + + +namespace OpenViBE +{ + namespace Plugins + { + namespace Tactilebci + { + /// The class CBoxAlgorithmUDPStimcodeSender describes the box UDPStimcodeSender. + class CBoxAlgorithmUDPStimcodeSender final : virtual public Toolkit::TBoxAlgorithm + { + 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, OVP_ClassId_BoxAlgorithm_UDPStimcodeSender) + + protected: + // Input decoder: + Toolkit::TStimulationDecoder m_StimDecoder; + // Output decoder: + Toolkit::TStimulationEncoder 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. + /// Listener of the box UDPStimcodeSender. + class CBoxAlgorithmUDPStimcodeSenderListener final : public Toolkit::TBoxListener + { + 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, CIdentifier::undefined()) + }; + */ + + /// Descriptor of the box UDPStimcodeSender. + 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