TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/usbamp_raw_buffer.h
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 
00030 #ifdef WIN32
00031 
00032 #ifndef USBAMP_H
00033 #define USBAMP_H
00034 
00035 #include <boost/cstdint.hpp>
00036 
00037 #include <vector>
00038 #include <set>
00039 
00040 #include <boost/thread/condition.hpp>  // for mutex and cond. variables
00041 #include <boost/thread/shared_mutex.hpp>
00042 #include <windows.h>
00043 
00044 #include "extern/include/g.usbamp/gUSBamp.h"
00045 //#pragma comment(lib,"gUSBamp.lib")
00046 
00047 #include "hw_thread.h"
00048 
00049 namespace tobiss
00050 {
00051 
00052 static const unsigned int USBAMP_MAX_NR_OF_DEVICES    = 16;
00053 static const unsigned int USBAMP_MAX_NR_OF_CHANNELS   = 17;
00054 static const unsigned int USBAMP_MAX_NR_OF_ANALOG_CHANNELS   = 16;
00055 static const unsigned int USBAMP_NR_OF_CHANNEL_GROUPS = 4;
00056 static const unsigned int USBAMP_NOTCH_HALF_WIDTH = 2;   // to one side  ...  e.g.  f_center = 50 Hz -->  48/52 Hz
00057 static const unsigned int USBAMP_ERROR_MSG_SIZE = 256;
00058 static const unsigned int DIGITS_TO_ROUND = 2;
00059 static const unsigned int USBAMP_SAMPLE_BUFFER_MULTIPLIER = 8;
00060 
00061 //-----------------------------------------------------------------------------
00062 
00075 class USBamp : public HWThread
00076 {
00077   public:
00078 //   USBamp(boost::asio::io_service& io, const HWConfig& config)
00079 //   : HWThread(config.sampling_rate, config.nr_channels, config.blocks)
00080 //   {
00081 //     ch = new UCHAR [USBAMP_MAX_NR_OF_CHANNELS * sizeof(UCHAR)];
00082 //     initUSBamp();
00083 //   }
00084 
00085   USBamp(XMLParser& parser, ticpp::Iterator<ticpp::Element> hw);
00086 
00087   virtual ~USBamp();
00088 
00089   virtual SampleBlock<double> getSyncData();
00090 
00091   virtual SampleBlock<double> getAsyncData();
00092 
00096   virtual void run();
00097 
00101   virtual void stop();
00102 
00103 //-----------------------------------------------
00104 
00105   private:
00106 
00107     void setHardware(ticpp::Iterator<ticpp::Element>const &hw);
00108 
00114     virtual void setDeviceSettings(ticpp::Iterator<ticpp::Element>const &father);
00120     virtual void setChannelSettings(ticpp::Iterator<ticpp::Element>const &father);
00121 
00122     void setDefaultSettings();
00123     void setDeviceFilterSettings(ticpp::Iterator<ticpp::Element>const &elem);
00124     void setChannelFilterSettings(ticpp::Iterator<ticpp::Element>const &father);
00125 
00126     void setDeviceNotchSettings(ticpp::Iterator<ticpp::Element>const &elem);
00127     void setChannelNotchSettings(ticpp::Iterator<ticpp::Element>const &father);
00128 
00129     void setOperationMode(ticpp::Iterator<ticpp::Element>const &elem);
00130     void setShortCut(ticpp::Iterator<ticpp::Element>const &elem);
00131     void setTriggerLine(ticpp::Iterator<ticpp::Element>const &elem);
00132     void setUSBampMasterOrSlave(ticpp::Iterator<ticpp::Element>const &elem);
00133     void setCommonGround(ticpp::Iterator<ticpp::Element>const &father);
00134     void setCommonReference(ticpp::Iterator<ticpp::Element>const &father);
00135     void setBipolar(ticpp::Iterator<ticpp::Element>const &father);
00136     void setDrivenRightLeg(ticpp::Iterator<ticpp::Element>const &father);
00137     void setIndividualDrivenRightLeg(ticpp::Iterator<ticpp::Element> &elem);
00138 
00139     void checkFilterAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00140     void getFilterParams(ticpp::Iterator<ticpp::Element>const &elem,\
00141       unsigned int &type, unsigned int &order, float &f_low, float &f_high);
00142     void checkNotchAttributes(ticpp::Iterator<ticpp::Element>const &elem);
00143     void getNotchParams(ticpp::Iterator<ticpp::Element>const &elem, float &f_center);
00144 
00145     void checkNrOfChannels();
00146     void checkTriggerLineChannel();
00147 
00148     void getHandles();
00149     void check4USBampError();
00150     void initFilterPtrs();
00151     void setUSBampChannels();
00152     int search4FilterID(unsigned int type, unsigned int order, float f_low, float f_high);
00153     int search4NotchID(float f_center);
00154     void setUSBampFilter();
00155     void setUSBampNotch();
00156 
00157     void initUSBamp();
00158 
00159     void callGT_GetData();
00160     void fillSyncBuffer();
00161     void fillSampleBlock();
00162 
00163     inline double roundD(double number, int digits = DIGITS_TO_ROUND)
00164     {
00165       return floor(number * pow(10., digits) + .5) / pow(10., digits);
00166     }
00167 
00168   //-----------------------------------------------
00169 
00170   private:
00171     static std::set<static std::string> serials_;
00172     static bool is_usbamp_master_;
00173 
00174     static USBamp*         master_device_;
00175     static std::vector<USBamp*>   slave_devices_;
00176 
00177     bool enable_sc_;
00178     bool external_sync_;
00179     bool trigger_line_;
00180     boost::uint64_t sample_count_;
00181 
00182     boost::uint64_t error_count_;
00183     WORD error_code_;
00184     CHAR* error_msg_;
00185 
00186     BYTE* driver_buffer_;
00187     DWORD driver_buffer_size_;
00188     DWORD bytes_received_;
00189     DWORD timeout_;
00190 
00191     std::vector<BYTE> raw_buffer_;
00192     boost::uint32_t expected_values_;
00193     static bool received_enough_data_;
00194 
00195     HANDLE  h_;
00196     HANDLE  data_Ev_;
00197     OVERLAPPED ov_;
00198 
00199     std::vector<double> samples_;
00200     std::vector<boost::uint16_t> channels_;
00201 
00202 
00203     int nr_of_bp_filters_;
00204     FILT* bp_filters_;
00205 
00206     int nr_of_notch_filters_;
00207     FILT* notch_filters_;
00208 
00209     std::vector<boost::int16_t> filter_id_;
00210     std::vector<boost::int16_t> notch_id_;
00211     GND ground_;
00212     REF reference_;
00213     CHANNEL bipolar_channels_;
00214     CHANNEL drl_channels_;
00215     std::string mode_;
00216 
00217     Constants cst_;
00218 };
00219 
00220 } // Namespace tobiss
00221 
00222 #endif // USBAMP_H
00223 
00224 #endif // WIN32
00225 //-----------------------------------------------------------------------------
 All Data Structures Files Functions Variables