TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/artificial_signal_source.cpp
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 */
00037 #include "hardware/artificial_signal_source.h"
00038 
00039 #include <math.h>
00040 
00041 #include <boost/lexical_cast.hpp>
00042 #include <boost/bind.hpp>
00043 
00044 namespace tobiss
00045 {
00046 using boost::uint8_t;
00047 using boost::uint16_t;
00048 using boost::uint32_t;
00049 
00050 using boost::lexical_cast;
00051 using boost::bad_lexical_cast;
00052 
00053 using std::map;
00054 using std::vector;
00055 using std::string;
00056 using std::cout;
00057 using std::endl;
00058 
00059 //-----------------------------------------------------------------------------
00060 
00061 ArtificialSignalSource::ArtificialSignalSource(boost::asio::io_service& io, ticpp::Iterator<ticpp::Element> hw)
00062 : acquiring_(0), io_(io), current_block_(0), td_(0)
00063 {
00064   #ifdef DEBUG
00065     cout << "ArtificialSignalSource: Constructor" << endl;
00066   #endif
00067 }
00068 
00069 //-----------------------------------------------------------------------------
00070 
00071 ArtificialSignalSource::~ArtificialSignalSource()
00072 {
00073   delete t_;
00074 }
00075 
00076 //-----------------------------------------------------------------------------
00077 
00078 void ArtificialSignalSource::init()
00079 {
00080 
00081   step_ = 1/static_cast<float>(fs_);
00082   cycle_dur_ = 1/static_cast<float>(fs_);
00083   boost::posix_time::microseconds period(1000000/fs_);
00084   td_ += period;
00085 
00086   buffer_.init(blocks_ , nr_ch_ , channel_types_);
00087   data_.init(blocks_ , nr_ch_ , channel_types_);
00088 
00089   samples_.resize(nr_ch_ ,0);
00090   t_ = new boost::asio::deadline_timer(io_, td_);
00091 
00092 //  cout << " * ArtificialSignalSource sucessfully initialized" << endl;
00093 //  cout << "    fs: " << fs_ << "Hz, nr of channels: " << nr_ch_  << ", blocksize: " << blocks_  << endl;
00094 
00095 }
00096 
00097 //-----------------------------------------------------------------------------
00098 
00099 void ArtificialSignalSource::run()
00100 {
00101   #ifdef DEBUG
00102     cout << "ArtificialSignalSource: run" << endl;
00103   #endif
00104 
00105   std::cout << BOOST_CURRENT_FUNCTION << std::endl << std::flush;
00106 
00107   running_ = 1;
00108   generateSignal();
00109 }
00110 
00111 
00112 //-----------------------------------------------------------------------------
00113 
00114 void ArtificialSignalSource::stop()
00115 {
00116   #ifdef DEBUG
00117     cout << "ArtificialSignalSource: stop" << endl;
00118   #endif
00119 
00120   std::cout << BOOST_CURRENT_FUNCTION << std::endl << std::flush;
00121 
00122   running_ = 0;
00123   cond_.notify_all();
00124 }
00125 
00126 //-----------------------------------------------------------------------------
00127 
00128 SampleBlock<double> ArtificialSignalSource::getSyncData()
00129 {
00130   #ifdef DEBUG
00131     cout << "ArtificialSignalSource: getSyncData" << endl;
00132   #endif
00133 
00134   if(!acquiring_)
00135     acquiring_ = 1;
00136 
00137   boost::unique_lock<boost::mutex> syn(sync_mut_);
00138   while(!samples_available_ && running_)
00139     cond_.wait(syn);
00140   boost::shared_lock<boost::shared_mutex> lock(rw_);
00141   samples_available_ = false;
00142   lock.unlock();
00143   cond_.notify_all();
00144   syn.unlock();
00145   return(data_);
00146 }
00147 
00148 //-----------------------------------------------------------------------------
00149 
00150 SampleBlock<double> ArtificialSignalSource::getAsyncData()
00151 {
00152   #ifdef DEBUG
00153     cout << "ArtificialSignalSource: getAsyncData" << endl;
00154   #endif
00155   boost::shared_lock<boost::shared_mutex> lock(rw_);
00156   samples_available_ = false;
00157   lock.unlock();
00158   return(data_);
00159 }
00160 
00161 //---------------------------------------------------------------------------------------
00162 
00163 } // Namespace tobiss
 All Data Structures Files Functions Variables