TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/nirscout.cpp
00001 /*
00002     This file is part of the TOBI signal server.
00003 
00004     The TOBI signal server is free software: you can redistribute it and/or modify
00005     it under the terms of the GNU General Public License as published by
00006     the Free Software Foundation, either version 3 of the License, or
00007     (at your option) any later version.
00008 
00009     The TOBI signal server is distributed in the hope that it will be useful,
00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012     GNU General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
00016 
00017     Copyright 2010 Christian Breitwieser
00018     Contact: c.breitwieser@tugraz.at
00019 */
00020 
00021 #include <iostream>
00022 
00023 #include "hardware/nirscout.h"
00024 
00025 namespace tobiss
00026 {
00027 
00028 using std::string;
00029 using std::vector;
00030 using std::map;
00031 using std::pair;
00032 using std::make_pair;
00033 
00034 using std::cout;
00035 using std::cerr;
00036 using std::endl;
00037 
00038 const HWThreadBuilderTemplateRegistratorWithoutIOService<NIRScout> NIRScout::factory_registrator_ ("NIRScout");
00039 
00040 static const int NIRSCOUT_TIMEOUT = 1000;
00041 static const int STR_BUFFER_SIZE = 1024;
00042 static const int CHUNK_SIZE = 1;
00043 
00044 //-----------------------------------------------------------------------------
00045 
00046 NIRScout::NIRScout(ticpp::Iterator<ticpp::Element> hw)
00047 {
00048  versionStruct version;
00049 
00050  tsdk_util_getAPIVersion(&version);
00051  cout << "NIRScout Version: " << version.Major << "."<< version.Minor << ".";
00052  cout << version.Fix << "."<< version.Build << endl;
00053 
00054   string_buffer_ = new char[STR_BUFFER_SIZE];
00055   for(unsigned int n = 0; n < STR_BUFFER_SIZE; n++)
00056     string_buffer_[n] = 0;
00057 
00058   timestamps_  = new double[CHUNK_SIZE];
00059   for(unsigned int n = 0; n < CHUNK_SIZE; n++)
00060     timestamps_[n] = 0;
00061 
00062   timing_bytes_ = new char[CHUNK_SIZE];
00063   for(unsigned int n = 0; n < CHUNK_SIZE; n++)
00064     timing_bytes_[n] = 0;
00065 
00066   error_code_ = tsdk_initialize();
00067   //error_code_ = tsdk_connect(target_ip_.c_str(), port_, NIRSCOUT_TIMEOUT);
00068   error_code_ = tsdk_getChannels(&sources_, &detectors_, &wavelengths_);
00069 
00070   unsigned int status_flags;
00071   double sample_rate;
00072   error_code_ = tsdk_getStatus( &status_flags, &sample_rate);
00073 
00074   if(sample_rate)
00075     fs_ = sample_rate;
00076 
00077   // TODO:   blocks_   ==  frame_size ???
00078 
00079   for(unsigned int i=0; i < sources; i++)
00080   {
00081     error_code_ = tsdk_getName(sources, i, string_buffer_, STR_BUFFER_SIZE);
00082     std::cout << string_buffer_ << std::endl;
00083   }
00084 
00085   for(unsigned int i=0; i < detectors; i++)
00086   {
00087     error_code_ = tsdk_getName(detectors, i, string_buffer_, STR_BUFFER_SIZE);
00088     std::cout << string_buffer_ << std::endl;
00089   }
00090 
00091   for(unsigned int i=0; i < wavelengths; i++)
00092   {
00093     error_code_ = tsdk_getName(wavelengths, i, string_buffer_, STR_BUFFER_SIZE);
00094     std::cout << string_buffer_ << std::endl;
00095   }
00096 }
00097 
00098 //-----------------------------------------------------------------------------
00099 
00100 NIRScout::~NIRScout()
00101 {
00102   if(string_buffer_)
00103     delete[] string_buffer_;
00104 
00105   if(raw_data_)
00106     delete[] raw_data_;
00107 
00108   if(timestamps_)
00109     delete[] timestamps_;
00110 
00111   if(timing_bytes_)
00112     delete[] timing_bytes_;
00113 }
00114 
00115 //-----------------------------------------------------------------------------
00116 
00117 SampleBlock<double> NIRScout::getSyncData()
00118 {
00119   error_code_ = tsdk_getNFrames(CHUNK_SIZE, NIRSCOUT_TIMEOUT, &frame_count_,
00120                                 timestamps_, timing_bytes_, raw_data_, &buffer_size_);
00121 
00122   if(frame_count_)
00123   {
00124     tsdk_util_getTimeString(timestamps_[0], string_buffer_, STR_BUFFER_SIZE);
00125 
00126     int frames_avail = 0;
00127     error_code_ = tsdk_getFramesAvail(&frames_avail);
00128   }
00129   else
00130   {
00131     unsigned int status_flags;
00132     double sample_rate;
00133     error_code_ = tsdk_getStatus( &status_flags, &sample_rate);
00134     if(!(status_flags & FLAGS_CONNECTED))
00135     {
00136       cerr << "Connection lost!" << endl;
00137     }
00138   }
00139 
00140   return(data_);
00141 }
00142 
00143 //-----------------------------------------------------------------------------
00144 
00145 SampleBlock<double> NIRScout::getAsyncData()
00146 {
00147   return(data_);
00148 }
00149 
00150 //-----------------------------------------------------------------------------
00151 
00152 void NIRScout::run()
00153 {
00154   int framesize = 0;
00155   error_code_ = tsdk_start(NULL, NULL, NULL, 0, 0, 0, 10, &framesize);
00156 
00157   raw_data_ = new float[framesize];
00158   for(int n = 0; n < framesize; n++)
00159     raw_data_[n] = 0;
00160 
00161   buffer_size_ = framesize;
00162 
00163 
00164 }
00165 
00166 //-----------------------------------------------------------------------------
00167 void NIRScout::stop()
00168 {
00169   error_code_ = tsdk_stop();
00170   error_code_ = tsdk_disconnect();
00171   error_code_ = tsdk_close();
00172 }
00173 
00174 //-----------------------------------------------------------------------------
00175 
00176 void NIRScout::setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father)
00177 {
00178 
00179 }
00180 
00181 //-----------------------------------------------------------------------------
00182 
00183 void NIRScout::setChannelSettings(ticpp::Iterator<ticpp::Element>const &father)
00184 {
00185 
00186 }
00187 
00188 //-----------------------------------------------------------------------------
00189 
00190 void NIRScout::setHardware(ticpp::Iterator<ticpp::Element>const &hw)
00191 {
00192 
00193 }
00194 
00195 //-----------------------------------------------------------------------------
00196 
00197 
00198 } // tobiss
00199 
 All Data Structures Files Functions Variables