TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/src/hardware/usbamp_raw_buffer.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 #ifdef WIN32
00022 
00023 #include "hardware/usbamp.h"
00024 
00025 #include <boost/bind.hpp>
00026 
00027 namespace tobiss
00028 {
00029 set<string> USBamp::serials_;
00030 bool USBamp::is_usbamp_master_(0);
00031 bool USBamp::received_enough_data_(0);
00032 
00033 USBamp* USBamp::master_device_(0);
00034 vector<USBamp*>  USBamp::slave_devices_;
00035 
00036 using boost::uint8_t;
00037 using boost::uint16_t;
00038 using boost::uint32_t;
00039 
00040 using boost::lexical_cast;
00041 using boost::bad_lexical_cast;
00042 
00043 //-----------------------------------------------------------------------------
00044 
00045 USBamp::USBamp(XMLParser& parser, ticpp::Iterator<ticpp::Element> hw)
00046   : HWThread(parser), enable_sc_(0), external_sync_(0), trigger_line_(0),
00047     sample_count_(0), error_count_(0) ,error_code_(0), expected_values_(0)
00048 {
00049   #ifdef DEBUG
00050     cout << "USBamp: Constructor" << endl;
00051   #endif
00052 
00053   #pragma comment(lib,"gUSBamp.lib")
00054 
00055   checkMandatoryHardwareTags(hw);
00056   getHandles();
00057   initFilterPtrs();
00058   setHardware(hw);
00059 
00060   expected_values_ = nr_ch_ * blocks_;
00061 
00062   driver_buffer_size_ = expected_values_ * sizeof(float) + HEADER_SIZE;
00063   driver_buffer_ = new BYTE[driver_buffer_size_];
00064 
00065   raw_buffer_.reserve(USBAMP_SAMPLE_BUFFER_MULTIPLIER *
00066                         (expected_values_ * sizeof(float)) );
00067 
00068   error_msg_ = new CHAR[USBAMP_ERROR_MSG_SIZE];
00069 
00070   data_.init(blocks_, nr_ch_, channel_types_);
00071   samples_.resize(expected_values_, 0);
00072 
00073   initUSBamp();
00074 
00075   if(external_sync_)
00076     slave_devices_.push_back(this);
00077   else
00078     master_device_ = this;
00079 
00080   cout << " * g.USBamp sucessfully initialized" << endl;
00081   cout << "    fs: " << fs_ << "Hz, nr of channels: " << nr_ch_ << ", blocksize: " << blocks_ << endl;
00082   if(!homogenous_signal_type_)
00083   {
00084     cout << "   ... NOTICE: Device is acquiring different signal types  ";
00085     cout << "--  ensure that reference and ground settings are correctly set!" << endl;
00086   }
00087 
00088   #ifdef DEBUG
00089     map<uint16_t, pair<string, uint32_t> >::iterator it(channel_info_.begin());
00090 
00091     cout << "Filter/Notch Settings:  (filter id size)" << filter_id_.size() <<endl;
00092     for(unsigned int n = 0; n < filter_id_.size(); n++)
00093     {
00094       cout << "ch: " << it->first << " -- type: "<<  bp_filters_[ filter_id_[n] ].type;
00095       cout << ", order: "  <<  bp_filters_[ filter_id_[n] ].order;
00096       cout << ", f_low: "  <<  bp_filters_[ filter_id_[n] ].fu;
00097       cout << ", f_high: " <<  bp_filters_[ filter_id_[n] ].fo;
00098       cout << "; -- notch: type: " <<  notch_filters_[ notch_id_[n] ].type;
00099       cout << ", order: "  <<  notch_filters_[ notch_id_[n] ].order;
00100       cout << ", f_low: "  <<  notch_filters_[ notch_id_[n] ].fu;
00101       cout << ", f_high: " <<  notch_filters_[ notch_id_[n] ].fo << endl;
00102       it++;
00103     }
00104 
00105     GND ground;
00106     GT_GetGround(h_, &ground);
00107 
00108     cout << "Ground settings: " << endl;
00109     cout << ground.GND1 << ", " << ground.GND2 << ", ";
00110     cout << ground.GND3 << ", " << ground.GND4 << endl;
00111 
00112     REF reference;
00113     GT_GetReference(h_, &reference);
00114 
00115     cout << "Reference settings: " << endl;
00116     cout << reference.ref1 << ", " << reference.ref2 << ", ";
00117     cout << reference.ref3 << ", " << reference.ref4 << endl;
00118 
00119     cout << "Bipolar channel settings: " << endl;
00120     cout << static_cast<uint32_t>(bipolar_channels_.Channel1) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel2) << ", ";
00121     cout << static_cast<uint32_t>(bipolar_channels_.Channel3) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel4) << ", ";
00122     cout << static_cast<uint32_t>(bipolar_channels_.Channel5) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel6) << ", ";
00123     cout << static_cast<uint32_t>(bipolar_channels_.Channel7) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel8) << endl;
00124     cout << static_cast<uint32_t>(bipolar_channels_.Channel9) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel10) << ", ";
00125     cout << static_cast<uint32_t>(bipolar_channels_.Channel11) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel12) << ", ";
00126     cout << static_cast<uint32_t>(bipolar_channels_.Channel13) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel14) << ", ";
00127     cout << static_cast<uint32_t>(bipolar_channels_.Channel15) << ", " << static_cast<uint32_t>(bipolar_channels_.Channel16) << endl;
00128 
00129     cout << "DRL channel settings: " << endl;
00130     cout << static_cast<uint32_t>(drl_channels_.Channel1) << ", " << static_cast<uint32_t>(drl_channels_.Channel2) << ", ";
00131     cout << static_cast<uint32_t>(drl_channels_.Channel3) << ", " << static_cast<uint32_t>(drl_channels_.Channel4) << ", ";
00132     cout << static_cast<uint32_t>(drl_channels_.Channel5) << ", " << static_cast<uint32_t>(drl_channels_.Channel6) << ", ";
00133     cout << static_cast<uint32_t>(drl_channels_.Channel7) << ", " << static_cast<uint32_t>(drl_channels_.Channel8) << endl;
00134     cout << static_cast<uint32_t>(drl_channels_.Channel9) << ", " << static_cast<uint32_t>(drl_channels_.Channel10) << ", ";
00135     cout << static_cast<uint32_t>(drl_channels_.Channel11) << ", " << static_cast<uint32_t>(drl_channels_.Channel12) << ", ";
00136     cout << static_cast<uint32_t>(drl_channels_.Channel13) << ", " << static_cast<uint32_t>(drl_channels_.Channel14) << ", ";
00137     cout << static_cast<uint32_t>(drl_channels_.Channel15) << ", " << static_cast<uint32_t>(drl_channels_.Channel16) << endl;
00138   #endif
00139 }
00140 
00141 //-----------------------------------------------------------------------------
00142 
00143 USBamp::~USBamp()
00144 {
00145   #ifdef DEBUG
00146     cout << "USBamp: Destructor" << endl;
00147   #endif
00148 
00149   delete[] bp_filters_;
00150   delete[] notch_filters_;
00151   delete[] driver_buffer_;
00152   delete[] error_msg_;
00153 }
00154 
00155 //-----------------------------------------------------------------------------
00156 
00157 void USBamp::setHardware(ticpp::Iterator<ticpp::Element>const& hw)
00158 {
00159   #ifdef DEBUG
00160     cout << "USBamp: setHardware" << endl;
00161   #endif
00162 
00163   ticpp::Iterator<ticpp::Element> ds(hw->FirstChildElement(cst_.hw_ds, true));
00164 
00165   setDeviceSettings(ds);
00166 
00167   ticpp::Iterator<ticpp::Element> cs(hw->FirstChildElement(cst_.hw_cs, false));
00168   if (cs != cs.end())
00169   {
00170     for(ticpp::Iterator<ticpp::Element> it(cs); ++it != it.end(); )
00171       if(it->Value() == cst_.hw_cs)
00172       {
00173         string ex_str;
00174         ex_str = "Error in "+ cst_.hardware_name +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00175         ex_str += "Multiple channel_settings found!";
00176         throw(ticpp::Exception(ex_str));
00177       }
00178       setChannelSettings(cs);
00179   }
00180 
00181   checkTriggerLineChannel();
00182 }
00183 
00184 //-----------------------------------------------------------------------------
00185 
00186 SampleBlock<double> USBamp::getSyncData()
00187 {
00188   #ifdef DEBUG
00189     cout << "USBamp: getSyncData" << endl;
00190   #endif
00191 
00192   if(!running_)
00193   {
00194     cout << "Not running!" << endl;
00195     return(data_);
00196   }
00197   boost::unique_lock<boost::shared_mutex> lock(rw_);
00198   bytes_received_ = 0;
00199 
00200     while(!received_enough_data_)
00201   {
00202 
00203   // cout << "Filling Slave Buffer ..." << endl;
00204     for(uint32_t n = 0; n < slave_devices_.size(); n++)
00205       slave_devices_[n]->callGT_GetData();
00206 
00207     callGT_GetData();
00208 
00209 
00210     received_enough_data_ = true;
00211 
00212     for(uint32_t n = 0; n < slave_devices_.size(); n++)
00213       slave_devices_[n]->fillSyncBuffer();
00214 
00215     // cout << " --> Filling MASTER Buffer ..." << endl;
00216     fillSyncBuffer();
00217   }
00218 
00219   for(uint32_t n = 0; n < slave_devices_.size(); n++)
00220     slave_devices_[n]->fillSampleBlock();
00221 
00222   fillSampleBlock();
00223 
00224   received_enough_data_ = false;
00225 
00226   lock.unlock();
00227   return(data_);
00228 }
00229 
00230 //-----------------------------------------------------------------------------
00231 
00232 SampleBlock<double> USBamp::getAsyncData()
00233 {
00234   #ifdef DEBUG
00235     cout << "USBamp: getAsyncData" << endl;
00236   #endif
00237 
00238   if(!running_)
00239   {
00240     cout << "Not running!" << endl;
00241     return(data_);
00242   }
00243 
00244 //   cout << " --> getting Slave data ..." << endl;
00245   if(external_sync_)
00246     return(data_);
00247 
00248   throw(std::runtime_error("USBamp::getAsyncData -- Async data acquisition not available for g.USBamp yet!"));
00249 
00250 //   boost::shared_lock<boost::shared_mutex> lock(rw_);
00251 //   vector<float> tmp(buffer);
00252 //   samples_available_ = false;
00253 //   lock.unlock();
00254 //   return(tmp);
00255   return(data_);
00256 }
00257 
00258 //-----------------------------------------------------------------------------
00259 
00260 void USBamp::callGT_GetData()
00261 {
00262   if(running_)
00263     if( !GT_GetData(h_, driver_buffer_, driver_buffer_size_, &ov_))
00264       throw(std::runtime_error("USBamp::getSyncData -- Error getting data!"));
00265 }
00266 
00267 //-----------------------------------------------------------------------------
00268 
00269 void USBamp::fillSyncBuffer()
00270 {
00271   if(!running_)
00272     return;
00273 
00274   // if(external_sync_)
00275     // cout << "Slave Filling sync buffer"  << flush << endl;
00276   // else
00277     // cout << "Master Filling sync buffer"  << flush << endl << endl;
00278 
00279   // if(raw_buffer_.size() >= sizeof(float)* expected_values_)
00280   // {
00281     // // fill samples into sample block (data_)
00282 
00283     // // cout << "Raw buffer already full enough --  Raw buffer size: " << raw_buffer_.size() << flush << endl;
00284 
00285     // for(unsigned int k = 0; k < expected_values_/blocks_ ; k++)
00286       // for(unsigned int j = 0; j < blocks_; j++)
00287         // samples_[ (k*blocks_) + j ] = static_cast<float>(raw_buffer_[ (k +(j* expected_values_/blocks_) )*sizeof(float) ] )   ;
00288 
00289     // // cout << "Erasing " << expected_values_ * sizeof(float) << " samples from raw buffer ..." << flush << endl;
00290     // raw_buffer_.erase(raw_buffer_.begin(), raw_buffer_.begin() + (expected_values_*sizeof(float)) );
00291 
00292     // data_.setSamples(samples_);
00293     // return;
00294 
00295   // }
00296   // else  ... get data from USBamp
00297 
00298   samples_available_ = true;
00299   // check4USBampError();
00300 
00301   timeout_ = WaitForSingleObject(data_Ev_,1000);
00302   if(timeout_ == WAIT_TIMEOUT)
00303   {
00304     cout << "Timeout!" << endl;
00305     if( !GT_ResetTransfer(h_))
00306       throw(std::runtime_error("USBamp::getSyncData -- Error resetting transfer!"));
00307     return;
00308   }
00309 
00310   GetOverlappedResult(h_, &ov_, &bytes_received_, false);
00311   sample_count_++;
00312 
00313   bytes_received_ -= HEADER_SIZE;
00314   unsigned int values = bytes_received_/sizeof(float);
00315 
00316   // cout << "Filling raw buffer ..." << flush << endl;
00317   for(unsigned int n = 0; n < (values * sizeof(float) ); n++)
00318       raw_buffer_.push_back( driver_buffer_[ HEADER_SIZE + n] );
00319 
00320   if(raw_buffer_.size() < expected_values_ * sizeof(float) )
00321   {
00322     cout << "Received not enough data  ... " << raw_buffer_.size()  << ";  expected: " << expected_values_ * sizeof(float) << flush << endl;
00323 
00324     received_enough_data_ = false;
00325   }
00326 
00327   // cout << "Filling samples  --  Raw buffer size: " << raw_buffer_.size() << flush << endl;
00328 
00329 }
00330 
00331 //-----------------------------------------------------------------------------
00332 
00333 void USBamp::fillSampleBlock()
00334 {
00335 
00336   // if(external_sync_)
00337     // cout << "Slave Filling sample block"  << flush << endl;
00338   // else
00339     // cout << "Master Filling samlpe block"  << flush << endl;
00340 
00341   for(unsigned int k = 0; k < expected_values_/blocks_ ; k++)
00342     for(unsigned int j = 0; j < blocks_; j++)
00343     {
00344       // cout << "Filling sample at " << (k*blocks_) + j << "  from  " << (k +(j* expected_values_/blocks_) )*sizeof(float) << flush << endl;
00345       samples_[ (k*blocks_) + j ] = *(reinterpret_cast<float*>(&raw_buffer_[ (k +(j* expected_values_/blocks_) )*sizeof(float) ] )  ) ;
00346     }
00347 
00348   // cout << "Erasing " << expected_values_ * sizeof(float) << " samples from raw buffer ..." << flush << endl;
00349   raw_buffer_.erase(raw_buffer_.begin(), raw_buffer_.begin() + (expected_values_ * sizeof(float)) );
00350 
00351   data_.setSamples(samples_);
00352 }
00353 
00354 
00355   // if(bytes_received_ != sizeof(float)* nr_ch_ * blocks_)
00356   // {
00357     // cout << "WARNING  -- Buffer-Size only: " << bytes_received_ << ";  -> should be: " << sizeof(float)* nr_ch_ * blocks_;
00358     // cout << endl;
00359 
00360 
00361     // cout << "  (at Sample: " << sample_count_ << ")"<< endl;
00362     // cout << "  ** WARNING: Inserted zeros instead of samples for testing!! **"<< endl;
00363     // error_count_++;
00364     // for(unsigned int n = 0; n < samples_.size(); n++)
00365       // samples_[n] = 0;
00366 
00367     // data_.setSamples(samples_);
00368     // return;
00369   // }
00370 
00371   // for(unsigned int k = 0; k < values/blocks_ ; k++)
00372     // for(unsigned int j = 0; j < blocks_; j++)
00373       // samples_[ (k*blocks_) + j ] = *(reinterpret_cast<float*>(driver_buffer_ + HEADER_SIZE + (k +(j* values/blocks_) )*sizeof(float) ));
00374 
00375 //-----------------------------------------------------------------------------
00376 
00377 void USBamp::check4USBampError()
00378 {
00379   #ifdef DEBUG
00380     cout << "USBamp: check4USBampError" << endl;
00381   #endif
00382 
00383   error_code_ = 0;
00384   CHAR* error_ptr = error_msg_;
00385 
00386   if( !GT_GetLastError(&error_code_, error_ptr))
00387     throw(std::runtime_error("USBamp::getSyncData -- Error getting last error message!"));
00388 
00389   if(error_code_)
00390     cerr << "Error code: " << error_code_ << ";  Error message: " << error_msg_ << endl;
00391 }
00392 
00393 //-----------------------------------------------------------------------------
00394 
00395 void USBamp::getHandles()
00396 {
00397   #ifdef DEBUG
00398     cout << "USBamp: getHandles" << endl;
00399   #endif
00400 
00401   if(serials_.find(m_.find(cst_.hardware_serial)->second) != serials_.end())
00402     throw(std::invalid_argument("USBamp::getHandles -- g.USBamp with serial "\
00403           +m_.find(cst_.hardware_serial)->second+" already in use!"));
00404 
00405   if(m_.find(cst_.hardware_serial)->second == "")
00406     throw(std::invalid_argument("USBamp::getHandles -- No serial number for g.USBamp given!"));
00407 
00408   serials_.insert(m_.find(cst_.hardware_serial)->second);
00409 
00410   HANDLE h_tmp;
00411   h_tmp = GT_OpenDeviceEx( const_cast<LPSTR>(  m_.find(cst_.hardware_serial)->second.c_str() ));
00412   if(h_tmp != 0)
00413   {
00414     h_ = (h_tmp);
00415 
00416     memset(&ov_, 0, sizeof(OVERLAPPED) );
00417     data_Ev_ = CreateEvent(0, false, false, 0);
00418 
00419     ov_.hEvent = data_Ev_;
00420     ov_.Offset = 0;
00421     ov_.OffsetHigh = 0;
00422     ResetEvent(data_Ev_);
00423   }
00424   else
00425     throw(std::runtime_error("USBamp::getHandles -- g.USBamp with serial "\
00426           +m_.find(cst_.hardware_serial)->second+" not connected!"));
00427 }
00428 
00429 //-----------------------------------------------------------------------------
00430 
00431 void USBamp::initFilterPtrs()
00432 {
00433   #ifdef DEBUG
00434     cout << "USBamp: initFilterPtrs" << endl;
00435   #endif
00436 
00437   if( !GT_GetNumberOfFilter(&nr_of_bp_filters_))
00438     throw(std::runtime_error("USBamp::initFilterPtrs -- Error getting number of filter!"));
00439   bp_filters_ = new FILT[nr_of_bp_filters_];
00440   if( !GT_GetFilterSpec(bp_filters_))
00441     throw(std::runtime_error("USBamp::initFilterPtrs -- Error getting filter specifications!"));
00442 
00443   if( !GT_GetNumberOfNotch(&nr_of_notch_filters_))
00444     throw(std::runtime_error("USBamp::initFilterPtrs -- Error getting number of notch!"));
00445   notch_filters_ = new FILT[nr_of_notch_filters_];
00446   if( !GT_GetNotchSpec(notch_filters_))
00447     throw(std::runtime_error("USBamp::initFilterPtrs -- Error getting notch specifications!"));
00448 }
00449 
00450 //-----------------------------------------------------------------------------
00451 
00452 void USBamp::run()
00453 {
00454   #ifdef DEBUG
00455     cout << "USBamp: run" << endl;
00456   #endif
00457 
00458   if( !GT_ResetTransfer(h_))
00459     throw(std::runtime_error("USBamp::run -- Error resetting transfer!"));
00460 
00461   ResetEvent(data_Ev_);
00462 
00463   if( !GT_Start(h_))
00464     throw(std::runtime_error("USBamp::run -- Error starting g.USBamp!"));
00465 
00466   running_ = 1;
00467   cout << " * g.USBamp sucessfully started" << endl;
00468 }
00469 
00470 //-----------------------------------------------------------------------------
00471 
00472 void USBamp::stop()
00473 {
00474   #ifdef DEBUG
00475     cout << "USBamp: stop" << endl;
00476   #endif
00477 
00478   if(!running_)
00479     return;
00480   boost::unique_lock<boost::shared_mutex> lock(rw_);
00481   running_ = 0;
00482 
00483   if(!external_sync_)
00484     for(uint32_t n = 0; n < slave_devices_.size(); n++)
00485       slave_devices_[n]->stop();
00486 
00487   if( !GT_Stop(h_))
00488     throw(std::runtime_error("USBamp::stop -- Error stopping the device!"));
00489   if( !GT_CloseDevice(&h_))
00490     throw(std::runtime_error("USBamp::stop -- Error closing the device!"));
00491 
00492   h_ = 0;
00493   serials_.erase(m_.find(cst_.hardware_serial)->second);
00494 
00495   CloseHandle(data_Ev_);
00496   lock.unlock();
00497 
00498   cout << " * USBamp "  << m_.find(cst_.hardware_serial)->second <<  " sucessfully stopped" << endl;
00499   serials_.erase(m_.find(cst_.hardware_serial)->second);
00500 }
00501 
00502 //-----------------------------------------------------------------------------
00503 
00504 void USBamp::setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father)
00505 {
00506   #ifdef DEBUG
00507     cout << "USBamp: setDeviceSettings" << endl;
00508   #endif
00509 
00510   ticpp::Iterator<ticpp::Element> elem(father->FirstChildElement(cst_.hw_fs,true));
00511     setSamplingRate(elem);
00512 
00513   elem = father->FirstChildElement(cst_.hw_channels,false);
00514   if(elem != elem.end())
00515     setDeviceChannels(elem);
00516   checkNrOfChannels();
00517 
00518   elem = father->FirstChildElement(cst_.hw_buffer,false);
00519   if(elem != elem.end())
00520     setBlocks(elem);
00521 
00522   setDefaultSettings();
00523   //---- optional ---
00524 
00525   elem = father->FirstChildElement(cst_.hw_fil,false);
00526   if(elem != elem.end())
00527     setDeviceFilterSettings(elem);
00528 
00529   elem = father->FirstChildElement(cst_.hw_notch,false);
00530   if(elem != elem.end())
00531     setDeviceNotchSettings(elem);
00532 
00533   elem = father->FirstChildElement(cst_.hw_opmode,false);
00534   if(elem != elem.end())
00535     setOperationMode(elem);
00536 
00537   elem = father->FirstChildElement(cst_.hw_sc,false);
00538   if(elem != elem.end())
00539     setShortCut(elem);
00540 
00541   elem = father->FirstChildElement(cst_.hw_trigger_line,false);
00542   if(elem != elem.end())
00543     setTriggerLine(elem);
00544 
00545   elem = father->FirstChildElement(cst_.hw_usbampmaster,false);
00546   if(elem != elem.end())
00547     setUSBampMasterOrSlave(elem);
00548 
00549   elem = father->FirstChildElement(cst_.hw_comgnd,false);
00550   if(elem != elem.end())
00551     setCommonGround(elem);
00552 
00553   elem = father->FirstChildElement(cst_.hw_comref,false);
00554   if(elem != elem.end())
00555     setCommonReference(elem);
00556 
00557   elem = father->FirstChildElement(cst_.hw_drl,false);
00558   if(elem != elem.end())
00559     setDrivenRightLeg(elem);
00560 }
00561 
00562 //---------------------------------------------------------------------------------------
00563 
00564 void USBamp::setChannelSettings(ticpp::Iterator<ticpp::Element>const &father)
00565 {
00566   #ifdef DEBUG
00567     cout << "USBamp: setChannelSettings" << endl;
00568   #endif
00569 
00570   ticpp::Iterator<ticpp::Element> elem(father->FirstChildElement(cst_.hw_sel,false));
00571   if (elem != elem.end())
00572     setChannelSelection(elem);
00573   checkNrOfChannels();
00574 
00575   elem = father->FirstChildElement(cst_.hw_fil,false);
00576   if(elem != elem.end())
00577     setChannelFilterSettings(elem);
00578 
00579   elem = father->FirstChildElement(cst_.hw_notch,false);
00580   if(elem != elem.end())
00581     setChannelNotchSettings(elem);
00582 
00583   elem = father->FirstChildElement(cst_.hw_bip,false);
00584   if(elem != elem.end())
00585     setBipolar(elem);
00586 
00587   elem = father->FirstChildElement(cst_.hw_drl,false);
00588   if(elem != elem.end())
00589     setDrivenRightLeg(elem);
00590 }
00591 
00592 //---------------------------------------------------------------------------------------
00593 
00594 void USBamp::setDefaultSettings()
00595 {
00596   #ifdef DEBUG
00597     cout << "USBamp: setDefaultSettings" << endl;
00598   #endif
00599 
00600   int id = -1;
00601   for(unsigned int n = 0; n < channel_info_.size(); n++)
00602     filter_id_.push_back(id);
00603 
00604   for(unsigned int n = 0; n < channel_info_.size(); n++)
00605     notch_id_.push_back(id);
00606 
00607   //FIXME   --  if needed, implementation of other operation modes
00608 
00609   is_usbamp_master_ = 0;
00610   enable_sc_ = 1;
00611 
00612   ground_.GND1 = 1;
00613   ground_.GND2 = 1;
00614   ground_.GND3 = 1;
00615   ground_.GND4 = 1;
00616 
00617   reference_.ref1 = 1;
00618   reference_.ref2 = 1;
00619   reference_.ref3 = 1;
00620   reference_.ref4 = 1;
00621 
00622   bipolar_channels_.Channel1 = 0;
00623   bipolar_channels_.Channel2 = 0;
00624   bipolar_channels_.Channel3 = 0;
00625   bipolar_channels_.Channel4 = 0;
00626   bipolar_channels_.Channel5 = 0;
00627   bipolar_channels_.Channel6 = 0;
00628   bipolar_channels_.Channel7 = 0;
00629   bipolar_channels_.Channel8 = 0;
00630   bipolar_channels_.Channel9 = 0;
00631   bipolar_channels_.Channel10 = 0;
00632   bipolar_channels_.Channel11 = 0;
00633   bipolar_channels_.Channel12 = 0;
00634   bipolar_channels_.Channel13 = 0;
00635   bipolar_channels_.Channel14 = 0;
00636   bipolar_channels_.Channel15 = 0;
00637   bipolar_channels_.Channel16 = 0;
00638 
00639   drl_channels_.Channel1 = 0;
00640   drl_channels_.Channel2 = 0;
00641   drl_channels_.Channel3 = 0;
00642   drl_channels_.Channel4 = 0;
00643   drl_channels_.Channel5 = 0;
00644   drl_channels_.Channel6 = 0;
00645   drl_channels_.Channel7 = 0;
00646   drl_channels_.Channel8 = 0;
00647   drl_channels_.Channel9 = 0;
00648   drl_channels_.Channel10 = 0;
00649   drl_channels_.Channel11 = 0;
00650   drl_channels_.Channel12 = 0;
00651   drl_channels_.Channel13 = 0;
00652   drl_channels_.Channel14 = 0;
00653   drl_channels_.Channel15 = 0;
00654   drl_channels_.Channel16 = 0;
00655 }
00656 
00657 //---------------------------------------------------------------------------------------
00658 
00659 void USBamp::setDeviceFilterSettings(ticpp::Iterator<ticpp::Element>const &elem)
00660 {
00661   #ifdef DEBUG
00662     cout << "USBamp: setDeviceFilterSettings" << endl;
00663   #endif
00664 
00665   checkFilterAttributes(elem);
00666 
00667   unsigned int type = 0;
00668   unsigned int order = 0;
00669   float f_low = 0;
00670   float f_high = 0;
00671 
00672   getFilterParams(elem, type, order, f_low, f_high);
00673 
00674   int id = search4FilterID(type, order, f_low, f_high);
00675   for(unsigned int n = 0; n < channel_info_.size(); n++)
00676     filter_id_[n] = id;
00677 
00678   cout << " * g.USBamp -- filter set to:" << endl;
00679   cout << "  ...  order: " << order << ", f_low: " << f_low << ", f_high: " << f_high << endl;
00680 
00681 }
00682 
00683 //---------------------------------------------------------------------------------------
00684 
00685 void USBamp::setChannelFilterSettings(ticpp::Iterator<ticpp::Element>const &father)
00686 {
00687   #ifdef DEBUG
00688     cout << "USBamp: setChannelFilterSettings" << endl;
00689   #endif
00690 
00691   ticpp::Iterator<ticpp::Element> elem;
00692   elem = father->FirstChildElement(cst_.hw_cs_ch,false);
00693 
00694   cout << " * g.USBamp -- channels specific filters set to:" << endl;
00695 
00696   for(  ; elem != elem.end(); elem++)
00697     if(elem->Value() == cst_.hw_cs_ch)
00698     {
00699       if(!elem.Get()->HasAttribute(cst_.hw_ch_nr))
00700       {
00701         string ex_str;
00702         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00703         ex_str += "Tag <"+cst_.hw_fil+"> given, but channel number ("+cst_.hw_ch_nr+") not given!";
00704         throw(ticpp::Exception(ex_str));
00705       }
00706       checkFilterAttributes(elem);
00707 
00708       uint16_t ch = 0;
00709       try{
00710         ch = lexical_cast<uint16_t>( elem.Get()->GetAttribute(cst_.hw_ch_nr) );
00711       }
00712       catch(bad_lexical_cast &)
00713       {
00714         string ex_str;
00715         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00716         ex_str += "Tag <"+ cst_.hw_fil + "> : Channel is not a number!";
00717         throw(ticpp::Exception(ex_str));
00718       }
00719       if( channel_info_.find(ch) ==  channel_info_.end() )
00720       {
00721         string ex_str;
00722         ex_str = "Error in"+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00723         ex_str += "Tag <"+ cst_.hw_fil + "> - Channel "+ lexical_cast<string>(ch) +" not set for recording!";
00724         throw(ticpp::Exception(ex_str));
00725       }
00726 
00727       unsigned int type = 0;
00728       unsigned int order = 0;
00729       float f_low = 0;
00730       float f_high = 0;
00731 
00732       getFilterParams(elem, type, order, f_low, f_high);
00733 
00734       uint16_t ch_pos = 0;
00735       map<uint16_t, pair<string, uint32_t> >::iterator it;
00736       for(it = channel_info_.begin(); it !=channel_info_.find(ch); it++)
00737         ch_pos++;
00738       filter_id_.at(ch_pos) = search4FilterID(type, order, f_low, f_high);
00739 
00740       cout << "  ... channel: " << ch << ", order: " << order << ", f_low: " << f_low << ", f_high: " << f_high << endl;
00741     }
00742     else
00743       throw(std::invalid_argument("USBamp::setChannelFilterSettings -- Tag not equal to \""+cst_.hw_cs_ch+"\"!"));
00744 }
00745 
00746 //---------------------------------------------------------------------------------------
00747 
00748 void USBamp::getFilterParams(ticpp::Iterator<ticpp::Element>const &elem,\
00749   unsigned int &type, unsigned int &order, float &f_low, float &f_high)
00750 {
00751   #ifdef DEBUG
00752     cout << "USBamp: getFilterParams" << endl;
00753   #endif
00754 
00755   type = cst_.getUSBampFilterType( elem.Get()->GetAttribute(cst_.hw_fil_type) );
00756   try
00757   {
00758     order = lexical_cast<unsigned int>( elem.Get()->GetAttribute(cst_.hw_fil_order) );
00759 
00760     f_low  = lexical_cast<float>( elem.Get()->GetAttribute(cst_.hw_fil_low) );
00761     f_high = lexical_cast<float>( elem.Get()->GetAttribute(cst_.hw_fil_high ));
00762   }
00763   catch(bad_lexical_cast &)
00764   {
00765     string ex_str;
00766     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00767     ex_str += "Tag <"+cst_.hw_fil+"> given, but order, lower or upper cutoff frequency is not a number!";
00768     throw(ticpp::Exception(ex_str));
00769   }
00770 }
00771 
00772 //---------------------------------------------------------------------------------------
00773 
00774 void USBamp::checkFilterAttributes(ticpp::Iterator<ticpp::Element>const &elem)
00775 {
00776   #ifdef DEBUG
00777     cout << "USBamp: checkFilterAttributes" << endl;
00778   #endif
00779 
00780   if(!elem.Get()->HasAttribute(cst_.hw_fil_type))
00781   {
00782     string ex_str;
00783     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00784     ex_str += "Tag <"+cst_.hw_fil+"> given, filter type ("+cst_.hw_fil_type+") not given!";
00785     throw(ticpp::Exception(ex_str));
00786   }
00787   if(!elem.Get()->HasAttribute(cst_.hw_fil_order))
00788   {
00789     string ex_str;
00790     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00791     ex_str += "Tag <"+cst_.hw_fil+"> given, filter order ("+cst_.hw_fil_order+") not given!";
00792     throw(ticpp::Exception(ex_str));
00793   }
00794   if(!elem.Get()->HasAttribute(cst_.hw_fil_low))
00795   {
00796     string ex_str;
00797     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00798     ex_str += "Tag <"+cst_.hw_fil+"> given, lower cutoff frequency ("+cst_.hw_fil_low+") not given!";
00799     throw(ticpp::Exception(ex_str));
00800   }
00801   if(!elem.Get()->HasAttribute(cst_.hw_fil_high))
00802   {
00803     string ex_str;
00804     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00805     ex_str += "Tag <"+cst_.hw_fil+"> given, upper cutoff frequency ("+cst_.hw_fil_high+") not given!";
00806     throw(ticpp::Exception(ex_str));
00807   }
00808 }
00809 
00810 //---------------------------------------------------------------------------------------
00811 
00812 int USBamp::search4FilterID(unsigned int type, unsigned int order, float f_low, float f_high)
00813 {
00814   #ifdef DEBUG
00815     cout << "USBamp: search4FilterID" << endl;
00816   #endif
00817 
00818   int id = -1;
00819 
00820   for(int n = 0; n < nr_of_bp_filters_; n++)
00821   {
00822     if(( roundD(bp_filters_[n].fs)  == fs_)      &&  ( roundD(bp_filters_[n].type) == type) && \
00823        ( roundD(bp_filters_[n].order) == order) &&  ( roundD(bp_filters_[n].fu)  == f_low) && \
00824        ( roundD(bp_filters_[n].fo)    == f_high) )
00825       id = n;
00826   }
00827 
00828   if(id < 0)
00829   {
00830     string ex_str = "USBamp::search4FilterID -- Filter settings not possible -- ";
00831     ex_str = ex_str + "Fs: "   + lexical_cast<string>(fs_)  +  ", ";
00832     ex_str = ex_str + "Type: "   + lexical_cast<string>(type)  +  ", ";
00833     ex_str = ex_str + "Order: "  + lexical_cast<string>(order) +  ", ";
00834     ex_str = ex_str + "f_low: "  + lexical_cast<string>(f_low) +  ", ";
00835     ex_str = ex_str + "f_high: " + lexical_cast<string>(f_high);
00836     throw(std::invalid_argument(ex_str));
00837   }
00838 
00839   return(id);
00840 }
00841 
00842 //---------------------------------------------------------------------------------------
00843 
00844 void USBamp::setDeviceNotchSettings(ticpp::Iterator<ticpp::Element>const &elem)
00845 {
00846   #ifdef DEBUG
00847     cout << "USBamp: setDeviceNotchSettings" << endl;
00848   #endif
00849 
00850   float f_center = 0;
00851 
00852   checkNotchAttributes(elem);
00853   getNotchParams(elem, f_center);
00854 
00855   int id = search4NotchID(f_center);
00856   for(unsigned int n = 0; n < channel_info_.size(); n++)
00857     notch_id_[n] = id;
00858 }
00859 
00860 //---------------------------------------------------------------------------------------
00861 
00862 void USBamp::setChannelNotchSettings(ticpp::Iterator<ticpp::Element>const &father)
00863 {
00864   #ifdef DEBUG
00865     cout << "USBamp: setChannelNotchSettings" << endl;
00866   #endif
00867 
00868   ticpp::Iterator<ticpp::Element> elem;
00869   elem = father->FirstChildElement(cst_.hw_cs_ch,false);
00870   for(  ; elem != elem.end(); elem++)
00871     if(elem->Value() == cst_.hw_cs_ch)
00872     {
00873       if(!elem.Get()->HasAttribute(cst_.hw_ch_nr))
00874       {
00875         string ex_str;
00876         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00877         ex_str += "Tag <"+cst_.hw_notch+"> given, but channel number ("+cst_.hw_ch_nr+") not given!";
00878         throw(ticpp::Exception(ex_str));
00879       }
00880       checkNotchAttributes(elem);
00881 
00882       uint16_t ch = 0;
00883       try{
00884         ch = lexical_cast<uint16_t>( elem.Get()->GetAttribute(cst_.hw_ch_nr) );
00885       }
00886       catch(bad_lexical_cast &)
00887       {
00888         string ex_str;
00889         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00890         ex_str += "Tag <"+ cst_.hw_notch + "> : Channel is not a number!";
00891         throw(ticpp::Exception(ex_str));
00892       }
00893       if( channel_info_.find(ch) ==  channel_info_.end() )
00894       {
00895         string ex_str;
00896         ex_str = "Error in"+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00897         ex_str += "Tag <"+ cst_.hw_notch + "> - Channel "+ lexical_cast<string>(ch) +" not set for recording!";
00898         throw(ticpp::Exception(ex_str));
00899       }
00900 
00901       float f_center = 0;
00902 
00903       getNotchParams(elem, f_center);
00904 
00905       uint16_t ch_pos = 0;
00906       map<uint16_t, pair<string, uint32_t> >::iterator it;
00907       for(it = channel_info_.begin(); it !=channel_info_.find(ch); it++)
00908         ch_pos++;
00909       notch_id_.at(ch_pos) = search4NotchID(f_center);
00910     }
00911     else
00912       throw(std::invalid_argument("USBamp::setChannelNotchSettings -- Tag not equal to \""+cst_.hw_cs_ch+"\"!"));
00913 }
00914 
00915 //---------------------------------------------------------------------------------------
00916 
00917 void USBamp::checkNotchAttributes(ticpp::Iterator<ticpp::Element>const &elem)
00918 {
00919   #ifdef DEBUG
00920     cout << "USBamp: checkNotchAttributes" << endl;
00921   #endif
00922 
00923   if(!elem.Get()->HasAttribute(cst_.hw_notch_center))
00924   {
00925     string ex_str;
00926     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00927     ex_str += "Tag <"+cst_.hw_notch+"> given, notch center frequency ("+cst_.hw_notch_center+") not given!";
00928     throw(ticpp::Exception(ex_str));
00929   }
00930 }
00931 
00932 //---------------------------------------------------------------------------------------
00933 
00934 void USBamp::getNotchParams(ticpp::Iterator<ticpp::Element>const &elem, float &f_center)
00935 {
00936   #ifdef DEBUG
00937     cout << "USBamp: getNotchParams" << endl;
00938   #endif
00939 
00940   try
00941   {
00942     f_center = lexical_cast<float>( elem.Get()->GetAttribute(cst_.hw_notch_center));
00943   }
00944   catch(bad_lexical_cast &)
00945   {
00946     string ex_str;
00947     ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
00948     ex_str += "Tag <"+cst_.hw_notch+"> given, but center frequency is not a number!";
00949     throw(ticpp::Exception(ex_str));
00950   }
00951 }
00952 
00953 //---------------------------------------------------------------------------------------
00954 
00955 int USBamp::search4NotchID(float f_center)
00956 {
00957   #ifdef DEBUG
00958     cout << "USBamp: search4NotchID" << endl;
00959   #endif
00960 
00961   int id = -1;
00962 
00963   for(int n = 0; n < nr_of_notch_filters_; n++)
00964   {
00965     if( ( roundD(notch_filters_[n].fs)  == fs_) && \
00966       ( roundD(notch_filters_[n].fu) == f_center - USBAMP_NOTCH_HALF_WIDTH ) && \
00967       ( roundD(notch_filters_[n].fo) == f_center + USBAMP_NOTCH_HALF_WIDTH ) )
00968       id = n;
00969   }
00970 
00971   if(id < 0)
00972   {
00973     string ex_str = "USBamp::search4NotchID -- Notch settings not possible -- ";
00974     ex_str = ex_str + "f_center: " + lexical_cast<string>(f_center);
00975     throw(std::invalid_argument(ex_str));
00976   }
00977 
00978   return(id);
00979 }
00980 
00981 //---------------------------------------------------------------------------------------
00982 
00983 void USBamp::setOperationMode(ticpp::Iterator<ticpp::Element>const &elem)
00984 {
00985   #ifdef DEBUG
00986     cout << "USBamp: setOperationMode" << endl;
00987   #endif
00988 
00989   //string op_mode( cst.getUSBampOpMode( elem->GetText(true) ) );
00990 
00991   //FIXME   --  if needed, implementation of other operation modes
00992   if(elem->GetText(true) != "normal")
00993     throw(std::invalid_argument("USBamp::setOperationMode -- So far only normal operation mode supported!"));
00994 
00995 }
00996 
00997 //---------------------------------------------------------------------------------------
00998 
00999 void USBamp::setShortCut(ticpp::Iterator<ticpp::Element>const &elem)
01000 {
01001   #ifdef DEBUG
01002     cout << "USBamp: setShortCut" << endl;
01003   #endif
01004 
01005   enable_sc_ = cst_.equalsOnOrOff(elem->GetText(true));
01006 }
01007 
01008 //---------------------------------------------------------------------------------------
01009 
01010 void USBamp::setTriggerLine(ticpp::Iterator<ticpp::Element>const &elem)
01011 {
01012   #ifdef DEBUG
01013     cout << "USBamp: setTriggerLine" << endl;
01014   #endif
01015 
01016   trigger_line_ = cst_.equalsOnOrOff(elem->GetText(true));
01017 }
01018 
01019 //---------------------------------------------------------------------------------------
01020 
01021 void USBamp::setUSBampMasterOrSlave(ticpp::Iterator<ticpp::Element>const &elem)
01022 {
01023   #ifdef DEBUG
01024     cout << "USBamp: setUSBampMasterOrSlave" << endl;
01025   #endif
01026 
01027   external_sync_ = !(cst_.equalsYesOrNo(elem->GetText(true)));
01028 
01029   if(is_usbamp_master_ && !external_sync_)
01030     throw(std::runtime_error("USBamp::setUSBampMasterOrSlave -- Only one USBamp master allowed!"));
01031 
01032   is_usbamp_master_ = true;
01033 }
01034 
01035 //---------------------------------------------------------------------------------------
01036 
01037 void USBamp::setCommonGround(ticpp::Iterator<ticpp::Element>const &father)
01038 {
01039   #ifdef DEBUG
01040     cout << "USBamp: setCommonGround" << endl;
01041   #endif
01042 
01043   ticpp::Iterator<ticpp::Element> elem;
01044   vector<uint8_t> v(USBAMP_NR_OF_CHANNEL_GROUPS,1);
01045   elem = father->FirstChildElement(false);
01046 
01047   for(  ; elem != elem.end(); elem++)
01048     if(elem->Value() == cst_.hw_gnd)
01049     {
01050       if(!elem.Get()->HasAttribute(cst_.hw_gnd_block))
01051       {
01052         string ex_str;
01053         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01054         ex_str += "Tag <"+cst_.hw_comgnd+"> given, but block identifier ("+cst_.hw_gnd_block+") not given!";
01055         throw(ticpp::Exception(ex_str));
01056       }
01057       if(!elem.Get()->HasAttribute(cst_.hw_gnd_value))
01058       {
01059         string ex_str;
01060         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01061         ex_str += "Tag <"+cst_.hw_comgnd+"> given, but block value ("+cst_.hw_gnd_value+") not given!";
01062         throw(ticpp::Exception(ex_str));
01063       }
01064 
01065       int gnd = cst_.getUSBampBlockNr( elem.Get()->GetAttribute(cst_.hw_gnd_block) );
01066       bool b = cst_.equalsYesOrNo( elem.Get()->GetAttribute(cst_.hw_gnd_value) );
01067 
01068       v.at(gnd) = b;
01069     }
01070     else
01071       throw(std::invalid_argument("USBamp::setCommonGround -- Tag not equal to \""+cst_.hw_gnd+"\"!"));
01072 
01073   //FIXME   --  hardcoded values
01074   ground_.GND1 = v.at(0);
01075   ground_.GND2 = v.at(1);
01076   ground_.GND3 = v.at(2);
01077   ground_.GND4 = v.at(3);
01078 }
01079 
01080 //---------------------------------------------------------------------------------------
01081 
01082 void USBamp::setCommonReference(ticpp::Iterator<ticpp::Element>const &father)
01083 {
01084   #ifdef DEBUG
01085     cout << "USBamp: setCommonReference" << endl;
01086   #endif
01087 
01088   ticpp::Iterator<ticpp::Element> elem;
01089   vector<uint8_t> v(USBAMP_NR_OF_CHANNEL_GROUPS,1);
01090   elem = father->FirstChildElement(false);
01091 
01092   for(  ; elem != elem.end(); elem++)
01093     if(elem->Value() == cst_.hw_cr)
01094     {
01095       if(!elem.Get()->HasAttribute(cst_.hw_cr_block))
01096       {
01097         string ex_str;
01098         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01099         ex_str += "Tag <"+cst_.hw_comref+"> given, but block identifier ("+cst_.hw_cr_block+") not given!";
01100         throw(ticpp::Exception(ex_str));
01101       }
01102       if(!elem.Get()->HasAttribute(cst_.hw_cr_value))
01103       {
01104         string ex_str;
01105         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01106         ex_str += "Tag <"+cst_.hw_comref+"> given, but block value ("+cst_.hw_cr_value+") not given!";
01107         throw(ticpp::Exception(ex_str));
01108       }
01109 
01110       int ref = cst_.getUSBampBlockNr( elem.Get()->GetAttribute(cst_.hw_cr_block) );
01111       bool b = cst_.equalsYesOrNo( elem.Get()->GetAttribute(cst_.hw_cr_value) );
01112 
01113       v.at(ref) = b;
01114     }
01115     else
01116       throw(std::invalid_argument("USBamp::setCommonReference -- Tag not equal to \""+cst_.hw_cr+"\"!"));
01117 
01118   //FIXME   --  hardcoded values
01119   reference_.ref1 = v.at(0);
01120   reference_.ref2 = v.at(1);
01121   reference_.ref3 = v.at(2);
01122   reference_.ref4 = v.at(3);
01123 
01124 }
01125 
01126 //---------------------------------------------------------------------------------------
01127 
01128 void USBamp::setBipolar(ticpp::Iterator<ticpp::Element>const &father)
01129 {
01130   #ifdef DEBUG
01131     cout << "USBamp: setBipolar" << endl;
01132   #endif
01133 
01134   ticpp::Iterator<ticpp::Element> elem;
01135   vector<uint8_t> v(USBAMP_MAX_NR_OF_CHANNELS,0);
01136   elem = father->FirstChildElement(cst_.hw_cs_ch,false);
01137 
01138   for(  ; elem != elem.end(); elem++)
01139     if(elem->Value() == cst_.hw_cs_ch)
01140     {
01141       if(!elem.Get()->HasAttribute(cst_.hw_ch_nr))
01142       {
01143         string ex_str;
01144         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01145         ex_str += "Tag <"+cst_.hw_bip+"> given, but channel number ("+cst_.hw_ch_nr+") not given!";
01146         throw(ticpp::Exception(ex_str));
01147       }
01148       if(!elem.Get()->HasAttribute(cst_.hw_bip_with))
01149       {
01150         string ex_str;
01151         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01152         ex_str += "Tag <"+cst_.hw_bip+"> given, but bipolar combination ("+cst_.hw_drl_value+") not given!";
01153         throw(ticpp::Exception(ex_str));
01154       }
01155 
01156       uint16_t with = 0;
01157       uint16_t ch   = 0;
01158 
01159       try{
01160         with = lexical_cast<uint16_t>( elem.Get()->GetAttribute(cst_.hw_bip_with) );
01161         ch   = lexical_cast<uint16_t>( elem.Get()->GetAttribute(cst_.hw_ch_nr) );
01162       }
01163       catch(bad_lexical_cast &)
01164       {
01165         string ex_str;
01166         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01167         ex_str += "Tag <"+ cst_.hw_drl + "> : Channel is not a number!";
01168         throw(ticpp::Exception(ex_str));
01169       }
01170       if( (channel_info_.find(ch) ==  channel_info_.end()) || (channel_info_.find(with) ==  channel_info_.end()) )
01171       {
01172         string ex_str;
01173         ex_str = "Error in"+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01174         ex_str += "Tag <"+ cst_.hw_bip + "> - Channel "+ lexical_cast<string>(ch) +" not set for recording!";
01175         throw(ticpp::Exception(ex_str));
01176       }
01177 
01178       // FIXME  --  check channels, if in range ( USBAMP_MAX_NR... )
01179 
01180       v.at(ch) = with;
01181     }
01182     else
01183       throw(std::invalid_argument("USBamp::setBipolar -- Tag not equal to \""+cst_.hw_cs_ch+"\"!"));
01184 
01185   //FIXME   --  hardcoded values
01186   bipolar_channels_.Channel1 = v.at(0);
01187   bipolar_channels_.Channel2 = v.at(1);
01188   bipolar_channels_.Channel3 = v.at(2);
01189   bipolar_channels_.Channel4 = v.at(3);
01190   bipolar_channels_.Channel5 = v.at(4);
01191   bipolar_channels_.Channel6 = v.at(5);
01192   bipolar_channels_.Channel7 = v.at(6);
01193   bipolar_channels_.Channel8 = v.at(7);
01194   bipolar_channels_.Channel9 = v.at(8);
01195   bipolar_channels_.Channel10 = v.at(9);
01196   bipolar_channels_.Channel11 = v.at(10);
01197   bipolar_channels_.Channel12 = v.at(11);
01198   bipolar_channels_.Channel13 = v.at(12);
01199   bipolar_channels_.Channel14 = v.at(13);
01200   bipolar_channels_.Channel15 = v.at(14);
01201   bipolar_channels_.Channel16 = v.at(15);
01202 }
01203 
01204 //---------------------------------------------------------------------------------------
01205 
01206 void USBamp::setDrivenRightLeg(ticpp::Iterator<ticpp::Element>const &father)
01207 {
01208   #ifdef DEBUG
01209     cout << "USBamp: setDrivenRightLeg" << endl;
01210   #endif
01211 
01212   ticpp::Iterator<ticpp::Element> elem;
01213   elem = father->FirstChildElement(cst_.hw_cs_ch,false);
01214   if(elem != elem.end())
01215   {
01216     setIndividualDrivenRightLeg(elem);
01217     return;
01218   }
01219 
01220   if(cst_.equalsYesOrNo(father->GetText(true)))
01221   {
01222     drl_channels_.Channel1 = 1;
01223     drl_channels_.Channel2 = 1;
01224     drl_channels_.Channel3 = 1;
01225     drl_channels_.Channel4 = 1;
01226     drl_channels_.Channel5 = 1;
01227     drl_channels_.Channel6 = 1;
01228     drl_channels_.Channel7 = 1;
01229     drl_channels_.Channel8 = 1;
01230     drl_channels_.Channel9 = 1;
01231     drl_channels_.Channel10 = 1;
01232     drl_channels_.Channel11 = 1;
01233     drl_channels_.Channel12 = 1;
01234     drl_channels_.Channel13 = 1;
01235     drl_channels_.Channel14 = 1;
01236     drl_channels_.Channel15 = 1;
01237     drl_channels_.Channel16 = 1;
01238   }
01239 }
01240 
01241 //---------------------------------------------------------------------------------------
01242 
01243 void USBamp::setIndividualDrivenRightLeg(ticpp::Iterator<ticpp::Element> &elem)
01244 {
01245   #ifdef DEBUG
01246     cout << "USBamp: setIndividualDrivenRightLeg" << endl;
01247   #endif
01248 
01249   vector<uint8_t> v(USBAMP_MAX_NR_OF_CHANNELS,0);
01250 
01251   for(  ; elem != elem.end(); elem++)
01252     if(elem->Value() == cst_.hw_cs_ch)
01253     {
01254       if(!elem.Get()->HasAttribute(cst_.hw_ch_nr))
01255       {
01256         string ex_str;
01257         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01258         ex_str += "Tag <"+cst_.hw_drl+"> given, but channel number ("+cst_.hw_ch_nr+") not given!";
01259         throw(ticpp::Exception(ex_str));
01260       }
01261       if(!elem.Get()->HasAttribute(cst_.hw_drl_value))
01262       {
01263         string ex_str;
01264         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01265         ex_str += "Tag <"+cst_.hw_drl+"> given, but DRL value ("+cst_.hw_drl_value+") not given!";
01266         throw(ticpp::Exception(ex_str));
01267       }
01268 
01269       bool b = cst_.equalsOnOrOff( elem.Get()->GetAttribute(cst_.hw_drl_value) );
01270       uint16_t ch   = 0;
01271 
01272       try{
01273         ch = lexical_cast<uint16_t>( elem.Get()->GetAttribute(cst_.hw_ch_nr) );
01274       }
01275       catch(bad_lexical_cast &)
01276       {
01277         string ex_str;
01278         ex_str = "Error in "+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01279         ex_str += "Tag <"+ cst_.hw_drl + "> : Channel is not a number!";
01280         throw(ticpp::Exception(ex_str));
01281       }
01282       if( channel_info_.find(ch) ==  channel_info_.end() )
01283       {
01284         string ex_str;
01285         ex_str = "Error in"+ cst_.hardware +" - " + m_.find(cst_.hardware_name)->second + " -- ";
01286         ex_str += "Tag <"+ cst_.hw_drl + "> - Channel "+ lexical_cast<string>(ch) +" not set for recording!";
01287         throw(ticpp::Exception(ex_str));
01288       }
01289 
01290       v.at(ch-1) = b;
01291     }
01292     else
01293       throw(std::invalid_argument("USBamp::setIndividualDrivenRightLeg -- Tag not equal to \""+cst_.hw_cs_ch+"\"!"));
01294 
01295     //FIXME   --  hardcoded values
01296     drl_channels_.Channel1 = v.at(0);
01297     drl_channels_.Channel2 = v.at(1);
01298     drl_channels_.Channel3 = v.at(2);
01299     drl_channels_.Channel4 = v.at(3);
01300     drl_channels_.Channel5 = v.at(4);
01301     drl_channels_.Channel6 = v.at(5);
01302     drl_channels_.Channel7 = v.at(6);
01303     drl_channels_.Channel8 = v.at(7);
01304     drl_channels_.Channel9 = v.at(8);
01305     drl_channels_.Channel10 = v.at(9);
01306     drl_channels_.Channel11 = v.at(10);
01307     drl_channels_.Channel12 = v.at(11);
01308     drl_channels_.Channel13 = v.at(12);
01309     drl_channels_.Channel14 = v.at(13);
01310     drl_channels_.Channel15 = v.at(14);
01311     drl_channels_.Channel16 = v.at(15);
01312 }
01313 
01314 //---------------------------------------------------------------------------------------
01315 
01316 void USBamp::checkNrOfChannels()
01317 {
01318   #ifdef DEBUG
01319     cout << "USBamp: checkNrOfChannels" << endl;
01320   #endif
01321 
01322   if(nr_ch_ > USBAMP_MAX_NR_OF_CHANNELS )
01323     throw(std::invalid_argument("Too many channels defined -- maximum nr of channels: "\
01324                                 +lexical_cast<string>(USBAMP_MAX_NR_OF_CHANNELS)) );
01325 
01326   map<uint16_t, pair<string, uint32_t> >::iterator it(channel_info_.begin());
01327 
01328   for(  ; it != channel_info_.end(); it++ )
01329     if(it->first >= USBAMP_MAX_NR_OF_CHANNELS )
01330       throw(std::invalid_argument("Channel number too high -- maximum nr of channels: "\
01331       +lexical_cast<string>(USBAMP_MAX_NR_OF_CHANNELS)+ " -- (2 digital channels not supported yet!)" ) );
01332 }
01333 
01334 //---------------------------------------------------------------------------------------
01335 
01336 void USBamp::setUSBampChannels()
01337 {
01338   #ifdef DEBUG
01339     cout << "USBamp: setUSBampChannels" << endl;
01340   #endif
01341 
01342   vector<uint16_t> channels;
01343   map<uint16_t, pair<string, uint32_t> >::iterator it(channel_info_.begin());
01344   map<uint16_t, pair<string, uint32_t> >::iterator stop(channel_info_.end());
01345 
01346   if(trigger_line_)
01347     stop--;
01348 
01349 //   if(it->first <= USBAMP_MAX_NR_OF_ANALOG_CHANNELS )
01350 
01351   for(  ; it != stop; it++)
01352       channels.push_back(it->first);
01353 
01354   UCHAR* uc_channels = new UCHAR[channels.size()];
01355   for(unsigned int n = 0; n < channels.size(); n++)
01356     uc_channels[n] = channels[n];
01357 
01358   if( !GT_SetChannels(h_, uc_channels, channels.size()))
01359     throw(std::runtime_error("USBamp::setUSBampChannels -- Error setting channels!"));
01360 
01361   delete[] uc_channels;
01362 }
01363 
01364 //---------------------------------------------------------------------------------------
01365 
01366 void USBamp::setUSBampFilter()
01367 {
01368   #ifdef DEBUG
01369     cout << "USBamp: setUSBampFilter" << endl;
01370   #endif
01371 
01372   bool check = 1;
01373   unsigned int count = 0;
01374   map<uint16_t, pair<string, uint32_t> >::iterator it;
01375   map<uint16_t, pair<string, uint32_t> >::iterator stop(channel_info_.end());
01376 
01377   if(trigger_line_)
01378     stop--;
01379 
01380   for( it=channel_info_.begin() ; it != stop; it++)
01381   {
01382     if(check)
01383     {
01384       if(filter_id_[count] > 0)
01385         check = GT_SetBandPass(h_, (*it).first, filter_id_[count]);
01386       else
01387         cout << "Filter for channel " << (*it).first << " NOT set!" << endl;
01388 
01389       count++;
01390     }
01391   }
01392 
01393   if(!check)
01394     throw(std::runtime_error("USBamp::setUSBampFilter -- Error setting filter!"));
01395 
01396 }
01397 
01398 //---------------------------------------------------------------------------------------
01399 
01400 void USBamp::setUSBampNotch()
01401 {
01402   #ifdef DEBUG
01403     cout << "USBamp: setUSBampNotch" << endl;
01404   #endif
01405 
01406   bool check = 1;
01407   unsigned int count = 0;
01408   map<uint16_t, pair<string, uint32_t> >::iterator it;
01409   map<uint16_t, pair<string, uint32_t> >::iterator stop(channel_info_.end());
01410 
01411   if(trigger_line_)
01412     stop--;
01413 
01414   for( it=channel_info_.begin() ; it != stop; it++)
01415   {
01416     if(check)
01417     {
01418       if(notch_id_[count] > 0)
01419         check = GT_SetNotch(h_, (*it).first, notch_id_[count]);
01420       else
01421         cout << "Notch for channel " << (*it).first << " NOT set!" << endl;
01422       count++;
01423     }
01424   }
01425 
01426   if(!check)
01427     throw(std::runtime_error("USBamp::setUSBampNotch -- Error setting notch!"));
01428 
01429 }
01430 
01431 //---------------------------------------------------------------------------------------
01432 
01433 void USBamp::checkTriggerLineChannel()
01434 {
01435   #ifdef DEBUG
01436     cout << "USBamp: checkTriggerLineChannel" << endl;
01437   #endif
01438 
01439 //   if(channel_info_.find(USBAMP_TRIGGER_LINE_CHANNEL) != channel_info_.end())
01440 
01441   if(trigger_line_)
01442   {
01443     channel_info_[channel_info_.size() + 1] = make_pair(cst_.hw_trigger_line, SIG_USER_1);
01444     nr_ch_ += 1;
01445     homogenous_signal_type_ = 0;
01446     setChannelTypes();
01447   }
01448 }
01449 
01450 //---------------------------------------------------------------------------------------
01451 
01452 void USBamp::initUSBamp()
01453 {
01454   #ifdef DEBUG
01455     cout << "USBamp: initUSBamp" << endl;
01456   #endif
01457 
01458   if( !GT_SetMode(h_, M_NORMAL) )
01459     throw(std::runtime_error("USBamp::initUSBamp -- Error setting mode!"));
01460 
01461   if( !GT_SetSampleRate(h_, fs_))
01462     throw(std::runtime_error("USBamp::initUSBamp -- Error setting sampling rate!"));
01463 
01464   if( !GT_SetBufferSize(h_, blocks_))
01465     throw(std::runtime_error("USBamp::initUSBamp -- Error setting buffer size!"));
01466 
01467   setUSBampChannels();
01468 
01469   if( !GT_SetSlave(h_, external_sync_) )
01470     throw(std::runtime_error("USBamp::initUSBamp -- Error setting synchronization mode (master or slave)!"));
01471 
01472   if( !GT_EnableTriggerLine(h_, trigger_line_))
01473     throw(std::runtime_error("USBamp::initUSBamp -- Error setting trigger line!"));
01474 
01475   if( !GT_EnableSC(h_, enable_sc_) )
01476     throw(std::runtime_error("USBamp::initUSBamp -- Error setting shortcut!"));
01477 
01478   if( !GT_SetBipolar(h_, bipolar_channels_) )
01479     throw(std::runtime_error("USBamp::initUSBamp -- Error setting bipolar channels!"));
01480 
01481   if( !GT_SetReference(h_, reference_) )
01482     throw(std::runtime_error("USBamp::initUSBamp -- Error setting reference!"));
01483 
01484   if( !GT_SetGround(h_, ground_) )
01485     throw(std::runtime_error("USBamp::initUSBamp -- Error setting ground!"));
01486 
01487   if( !GT_SetDRLChannel(h_, drl_channels_) )
01488     throw(std::runtime_error("USBamp::initUSBamp -- Error setting driven right leg channels!"));
01489 
01490   setUSBampFilter();
01491   setUSBampNotch();
01492 }
01493 
01494 //-----------------------------------------------------------------------------
01495 
01496 } // Namespace tobiss
01497 
01498 #endif // WIN32
 All Data Structures Files Functions Variables