TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/usbamp.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 
00043 #ifndef USBAMP_H
00044 #define USBAMP_H
00045 
00046 #include <boost/cstdint.hpp>
00047 
00048 #include <vector>
00049 #include <set>
00050 
00051 #include <boost/thread/condition.hpp>  // for mutex and cond. variables
00052 #include <boost/thread/shared_mutex.hpp>
00053 #include <windows.h>
00054 
00055 #include "gtec_usbamp_wrapper.h"
00056 #include "hw_thread.h"
00057 #include "hw_thread_builder.h"
00058 
00059 namespace tobiss
00060 {
00061 
00062 static const unsigned int DIGITS_TO_ROUND = 2;
00063 
00064 //-----------------------------------------------------------------------------
00065 
00087 class USBamp : public HWThread
00088 {
00089   public:
00093   USBamp(ticpp::Iterator<ticpp::Element> hw);
00094 
00098   virtual ~USBamp();
00099 
00104   virtual SampleBlock<double> getSyncData();
00105 
00110   virtual SampleBlock<double> getAsyncData();
00111 
00115   virtual void run();
00116 
00120   virtual void stop();
00121 
00122 //-----------------------------------------------
00123 
00124   private:
00125 
00126     void setHardware(ticpp::Iterator<ticpp::Element>const &hw);
00127 
00133     virtual void setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father);
00139     virtual void setChannelSettings(ticpp::Iterator<ticpp::Element>const &father);
00140 
00141     void setDefaultSettings();
00142     void setDeviceFilterSettings(ticpp::Iterator<ticpp::Element>const &elem);
00143     void setChannelFilterSettings(ticpp::Iterator<ticpp::Element>const &father);
00144 
00145     void setDeviceNotchSettings(ticpp::Iterator<ticpp::Element>const &elem);
00146     void setChannelNotchSettings(ticpp::Iterator<ticpp::Element>const &father);
00147 
00148     void setOperationMode(ticpp::Iterator<ticpp::Element>const &elem);
00149     void setShortCut(ticpp::Iterator<ticpp::Element>const &elem);
00150     void setTriggerLine(ticpp::Iterator<ticpp::Element>const &elem);
00151     void setUSBampMasterOrSlave(ticpp::Iterator<ticpp::Element>const &elem);
00152     void setCommonGround(ticpp::Iterator<ticpp::Element>const &father);
00153     void setCommonReference(ticpp::Iterator<ticpp::Element>const &father);
00154     void setBipolar(ticpp::Iterator<ticpp::Element>const &father);
00155     void setDrivenRightLeg(ticpp::Iterator<ticpp::Element>const &father);
00156     void setIndividualDrivenRightLeg(ticpp::Iterator<ticpp::Element> &elem);
00157 
00158     void checkFilterAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00159     void getFilterParams(ticpp::Iterator<ticpp::Element>const &elem,\
00160       unsigned int &type, unsigned int &order, double &f_low, double &f_high);
00161     void checkNotchAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00162     void getNotchParams(ticpp::Iterator<ticpp::Element>const &elem, float &f_center);
00163 
00164     void checkNrOfChannels();
00165     void adjustSettingsToChannelSelection();
00166     void checkTriggerLineChannel();
00167 
00168     void getHandles();
00169     void check4USBampError();
00170     void initFilterPtrs();
00171     void setUSBampChannels();
00172     int search4FilterID(unsigned int type, unsigned int order, double f_low, double f_high);
00173     int search4NotchID(float f_center);
00174     void printPossibleBandPassFilters();
00175     void printPossibleNotchFilters();
00176   void printAvailableAmps();
00177     void setUSBampFilter();
00178     void setUSBampNotch();
00179 
00180     void initUSBamp();
00181 
00182     void callGT_GetData();
00183     void callGT_ResetTransfer();
00184     void callGT_Start();
00185 
00186     void fillSyncBuffer();
00187     void fillSampleBlock();
00188 
00189     int getUSBampFilterType(const std::string& s);
00190     std::string getUSBampFilterName(double n);
00191     std::string getUSBampOpMode(const std::string& s);
00192     int getUSBampBlockNr(const std::string& s);
00193 
00194     inline double roundD(double number, int digits = DIGITS_TO_ROUND)
00195     {
00196       return floor(number * pow(10., digits) + .5) / pow(10., digits);
00197     }
00198 
00199   //-----------------------------------------------
00200 
00201   private:
00202     static std::set<static std::string> serials_;
00203     static bool is_usbamp_master_;
00204     std::string serial_;
00205 
00206     static USBamp*         master_device_;
00207     static std::vector<USBamp*>   slave_devices_;
00208 
00209     bool enable_sc_;
00210     bool external_sync_;
00211     bool trigger_line_;
00212     boost::uint32_t  trigger_line_sig_type_;
00213 
00214     boost::uint64_t sample_count_;
00215 
00216     boost::uint64_t error_count_;
00217     WORD error_code_;
00218     CHAR* error_msg_;
00219 
00220     std::vector<BYTE*> driver_buffer_;
00221     DWORD driver_buffer_size_;
00222     std::vector<DWORD> bytes_received_;
00223     DWORD timeout_;
00224 
00225     boost::uint32_t expected_values_;
00226 
00227     boost::uint32_t first_run_;
00228     boost::uint32_t current_overlapped_;
00229 
00230     HANDLE  h_;
00231     std::vector<HANDLE>  data_Ev_;
00232     std::vector<OVERLAPPED> ov_;
00233 
00234     std::vector<double> samples_;
00235     std::vector<boost::uint16_t> channels_;
00236 
00237 
00238     int nr_of_bp_filters_;
00239     FILT* bp_filters_;
00240 
00241     int nr_of_notch_filters_;
00242     FILT* notch_filters_;
00243 
00244     std::vector<boost::int16_t> filter_id_;
00245     std::vector<boost::int16_t> notch_id_;
00246     GND ground_;
00247     REF reference_;
00248     CHANNEL bipolar_channels_;
00249     CHANNEL drl_channels_;
00250     std::string mode_;
00251 
00252     GTECUSBampWrapper usb_amp_;
00253 
00254     std::map<std::string, std::string> m_; 
00255 
00256     //-----------------------------------------------
00257     // Constants
00258     static const HWThreadBuilderTemplateRegistratorWithoutIOService<USBamp> FACTORY_REGISTRATOR_;
00259 
00260     static const std::string hw_filter_;   
00261     static const std::string hw_filter_type_;   
00262     static const std::string hw_filter_order_;   
00263     static const std::string hw_filter_low_;   
00264     static const std::string hw_filter_high_;   
00265 
00266     static const std::string hw_notch_;   
00267     static const std::string hw_notch_center_;    
00268 
00269 
00270     static const std::string hw_opmode_;       
00271     static const std::string hw_sc_;            
00272     static const std::string hw_trigger_line_;            
00273     static const std::string hw_usbampmaster_;   
00274     static const std::string hw_comgnd_;        
00275     static const std::string hw_gnd_;          
00276     static const std::string hw_gnd_block_;   
00277     static const std::string hw_gnd_value_;   
00278 
00279     static const std::string hw_comref_;   
00280     static const std::string hw_cr_;       
00281     static const std::string hw_cr_block_;   
00282     static const std::string hw_cr_value_;   
00283 
00284     static const std::string hw_bipolar_;        
00285     static const std::string hw_bipolar_with_;   
00286     static const std::string hw_drl_;        
00287     static const std::string hw_drl_value_;   
00288 
00292     std::map<std::string, unsigned int> usbamp_filter_types_;
00293 
00297     std::map<std::string, std::string> usbamp_opModes_;
00298 
00302     std::map<std::string, unsigned int> usbamp_block_names_;
00303 
00304 };
00305 
00306 } // Namespace tobiss
00307 
00308 #endif // USBAMP_H
00309 
00310 //-----------------------------------------------------------------------------
All Data Structures Files Functions Variables