TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/eeg_simulator.h
Go to the documentation of this file.
00001 /*
00002     This file is part of the TOBI SignalServer.
00003 
00004     Commercial Usage
00005     Licensees holding valid Graz University of Technology Commercial
00006     licenses may use this file in accordance with the Graz University
00007     of Technology Commercial License Agreement provided with the
00008     Software or, alternatively, in accordance with the terms contained in
00009     a written agreement between you and Graz University of Technology.
00010 
00011     --------------------------------------------------
00012 
00013     GNU General Public License Usage
00014     Alternatively, this file may be used under the terms of the GNU
00015     General Public License version 3.0 as published by the Free Software
00016     Foundation and appearing in the file gpl.txt included in the
00017     packaging of this file.  Please review the following information to
00018     ensure the GNU General Public License version 3.0 requirements will be
00019     met: http://www.gnu.org/copyleft/gpl.html.
00020 
00021     In case of GNU General Public License Usage ,the TOBI SignalServer
00022     is distributed in the hope that it will be useful,
00023     but WITHOUT ANY WARRANTY; without even the implied warranty of
00024     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00025     GNU General Public License for more details.
00026 
00027     You should have received a copy of the GNU General Public License
00028     along with the TOBI SignalServer. If not, see <http://www.gnu.org/licenses/>.
00029 
00030     Copyright 2010 Graz University of Technology
00031     Contact: SignalServer@tobi-project.org
00032 */
00033 
00039 #ifndef EEG_SIMULATOR_H
00040 #define EEG_SIMULATOR_H
00041 
00042 #include "hardware/hw_thread.h"
00043 #include "hardware/artificial_signal_source.h"
00044 #include "hardware/hw_thread_builder.h"
00045 #include "hardware/eeg_sim_msg_parser.h"
00046 
00047 #include <boost/random/mersenne_twister.hpp>
00048 #include <boost/random/normal_distribution.hpp>
00049 #include <boost/random/variate_generator.hpp>
00050 
00051 #include <map>
00052 
00053 namespace tobiss
00054 {
00055 
00056 //-----------------------------------------------------------------------------
00057 
00075 class EEGSimulator : public ArtificialSignalSource
00076 {
00077   public:
00078 
00079 //  static const unsigned int MESSAGE_VERSION = 1;
00080 
00084     EEGSimulator(boost::asio::io_service& io,
00085                  ticpp::Iterator<ticpp::Element> hw);
00089     virtual ~EEGSimulator();
00090 
00091 //------------------------------------------
00092 
00093   private:
00094 //    /**
00095 //    * @struct EEGConfig
00096 //    * @brief Simple struct holding parameters to configure the EEG signal.
00097 //    */
00098 //    struct EEGConfig
00099 //    {
00100 //      public:
00101 //        double scaling_;
00102 //        double offset_;
00103 //    };
00104 
00105 //    /**
00106 //    * @struct SineConfig
00107 //    * @brief Simple struct holding parameters to configure the sine waves.
00108 //    */
00109 //    struct SineConfig
00110 //    {
00111 //      public:
00112 //        double freq_;
00113 //        double amplitude_;
00114 //        double phase_;
00115 //    };
00116 
00117 //    enum MessageType
00118 //    {
00119 //      Invalid,
00120 //      GetConfig,
00121 //      Config
00122 //    };
00123 
00127     virtual void generateSignal();
00128 
00134     void setHardware(ticpp::Iterator<ticpp::Element>const &hw);
00140     virtual void setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father);
00146     virtual void setChannelSettings(ticpp::Iterator<ticpp::Element>const &father);
00147 
00151     void setPort(ticpp::Iterator<ticpp::Element>const &elem);
00152 
00156     void setDeviceEEGConfig(ticpp::Iterator<ticpp::Element>const &elem);
00160     void setDeviceSineConfig(ticpp::Iterator<ticpp::Element>const &elem);
00161 
00165     void setChannelEEGConfig(ticpp::Iterator<ticpp::Element>const &father);
00169     void setChannelSineConfig(ticpp::Iterator<ticpp::Element>const &father);
00170 
00175     void checkEEGConfigAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00180     void checkSineConfigAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00181 
00186     EEGSimMsgParser::EEGConfig getEEGConfig(ticpp::Iterator<ticpp::Element>const &elem);
00191     EEGSimMsgParser::SineConfig getSineConfig(ticpp::Iterator<ticpp::Element>const &elem);
00192 
00197     void handleAsyncRead(const boost::system::error_code& ec, std::size_t bytes_transferred );
00198 
00203     void acceptHandler(const boost::system::error_code& error);
00204 
00205 //    void checkMessage();
00206 //    MessageType getMessageType();
00207 //    void parseConfigMessage(std::map<boost::uint16_t, EEGConfig>& eeg,
00208 //                      std::multimap<boost::uint16_t, SineConfig>& sine);
00209 
00210     void updateEEGConfig(std::map<boost::uint16_t, EEGSimMsgParser::EEGConfig>& eeg);
00211     void updateSineConfig(std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig>& sine);
00212 
00213 //    std::string buildConfigString();
00214     void setSineConfigInMultimap(boost::uint16_t ch, EEGSimMsgParser::SineConfig config);
00215 
00216 //------------------------------------------
00217 
00218   private:
00219     static const HWThreadBuilderTemplateRegistrator<EEGSimulator> factory_registrator_;
00220     boost::mt19937 twister_;
00221     boost::normal_distribution<> eeg_dist_;
00222     boost::variate_generator<boost::mt19937&, boost::normal_distribution<> > eeg_gen_;
00223 
00224     boost::asio::ip::tcp::acceptor acceptor_;
00225     boost::asio::ip::tcp::socket socket_;
00226     boost::uint32_t port_;
00227     std::string     peer_ip_;
00228     bool connected_;
00229 
00230 
00231     boost::asio::streambuf message_buffer_;
00232     std::string str_buffer_;
00233 
00234 //    std::map<std::string, MessageType>            msg_types_map_;
00235 
00236     std::map<boost::uint16_t, EEGSimMsgParser::EEGConfig>          eeg_config_;       
00237     std::multimap<boost::uint16_t, EEGSimMsgParser::SineConfig>    sine_configs_;     
00238 
00239     EEGSimMsgParser                               parser_;
00240 
00241     //--------------------------------------
00242     // Constants:
00243 
00244     static const std::string xml_eeg_sim_port_;
00245     static const std::string xml_eeg_config_;
00246     static const std::string xml_sine_config_;
00247     static const std::string xml_scaling_;
00248     static const std::string xml_offset_;
00249     static const std::string xml_frequ_;
00250     static const std::string xml_amplitude_;
00251     static const std::string xml_phase_;
00252 
00253 };
00254 
00255 }  // tobiss
00256 
00257 
00258 
00259 
00260 #endif // EEG_SIMULATOR_H
 All Data Structures Files Functions Variables