TOBI SignalServer  0.1
/home/breidi/Dropbox/signalserver/include/hardware/dataq_wrapper.h
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 
00038 #ifndef DATAQ_WRAPPER_H
00039 #define DATAQ_WRAPPER_H
00040 
00041 #include <windows.h>
00042 #include <exception>
00043 #include <string>
00044 #include <iostream>
00045 
00046 #include "extern/include/dataq/hdrdef32.h"
00047 namespace tobiss
00048 {
00049 
00050 #ifndef ACHN
00051 #define ACHN 29
00052 #endif
00053 
00054 typedef struct tagCODASHDR
00055 {
00056    unsigned short header[2];
00057    struct hdrcmn hdrc;
00058    struct chinfo cary[ACHN];
00059    unsigned short vbflags;
00060 } CODASHDR;
00061 
00062 typedef struct di_anin_struct {
00063    unsigned short chan; /* input channel. 0 to 15 */
00064    unsigned short diff; /* input config. 0 = single end, 1 = diff. */
00065    unsigned short gain; /* gain code. 0 to 3 */
00066    unsigned short unipolar; /* unipolar or bipolar. 0 = BI, 1 = UN */
00067 } DI_ANIN_STRUCT;
00068 
00069 typedef struct di_info_struct {
00070    unsigned short port; /* device port address */
00071    unsigned short buf_in_chn; /* device input channel */
00072    unsigned short buf_out_chn; /* device output channel */
00073    unsigned short sft_lvl; /* software interrupt level */
00074    unsigned short hrd_lvl; /* hardware interrupt level */
00075    short *buf_in_ptr; /* input buffer pointer */
00076    unsigned short buf_in_size; /* input buffer size in words */
00077    short *buf_out_ptr; /* output buffer pointer */
00078    unsigned short buf_out_size; /* output buffer size in words */
00079    char tsr_version[20]; /* TSR version & hardware model */
00080    char dsp_version[20]; /* DSP program version */
00081    char sdk_version[20]; /* SDK library version */
00082    unsigned long serial_no; /* PCB serial number */
00083    unsigned long last_cal; /* last calibration time in sec since 1-1-1970 */
00084    char board_id[10]; /* PCB model name */
00085    char pgh_pgl;  /* type of PGA 0 = pgh, 1 = pgl */
00086    char hrdwr_rev; /* ASCII char REV letter */
00087    char pal0_rev; /* ASCII char REV of PAL0 */
00088    char pal1_rev; /* ASCII char REV of PAL1 */
00089 } DI_INFO_STRUCT;
00090 
00091 typedef struct di_info_struct32 {
00092    unsigned short port; /* device port address */
00093    unsigned short buf_in_chn; /* device input channel */
00094    unsigned short buf_out_chn; /* device output channel */
00095    unsigned short sft_lvl; /* software interrupt level */
00096    unsigned short hrd_lvl; /* hardware interrupt level */
00097    unsigned short padding; /* for 4-byte alignment of following */
00098    short *buf_in_ptr; /* input buffer pointer */
00099    unsigned buf_in_size; /* input buffer size in words */
00100    short *buf_out_ptr; /* output buffer pointer */
00101    unsigned buf_out_size; /* output buffer size in words */
00102    char tsr_version[20]; /* TSR version & hardware model */
00103    char dsp_version[20]; /* DSP program version */
00104    char sdk_version[20]; /* SDK library version */
00105    unsigned long serial_no; /* PCB serial number */
00106    unsigned long last_cal; /* last calibration time in sec since 1-1-1970 */
00107    char board_id[10]; /* PCB model name */
00108    char pgh_pgl;  /* type of PGA 0 = pgh, 1 = pgl */
00109    char hrdwr_rev; /* ASCII char REV letter */
00110    char pal0_rev; /* ASCII char REV of PAL0 */
00111    char pal1_rev; /* ASCII char REV of PAL1 */
00112 } DI_INFO_STRUCT32;
00113 
00114 typedef struct di_mode_struct {
00115    unsigned short mode:4; /* mode 0 = off, 1 = triggered analog, 2 = digital */
00116    unsigned short hystx:4; /* hysteresis index for DI-500 */
00117    unsigned short scnx:8; /* scanlist index of DI-500 trig chan, 0 for first chan */
00118    unsigned short trig_level; /* trigger level */
00119    unsigned short trig_slope; /* trigger slope 0 = +, 1 = - */
00120    unsigned short trig_pre; /* pre trigger number fo samples before trigger */
00121    unsigned short trig_post; /* post trigger length of scan before trigger occurs */
00122 } DI_MODE_STRUCT;
00123 
00124 typedef struct di_inlist_struct {
00125    unsigned short chan; /* input channel. 0 to 15 */
00126    unsigned short diff; /* input config. 0 = single ended, 1 = diff */
00127    unsigned short gain; /* gain. 0 to 3 */
00128    unsigned short unipolar; /* unipolar or bipolar, 0 = BI, 1= UN */
00129    unsigned short dig_out_enable; /* 1 = digital output enable */
00130    unsigned short dig_out; /* D0 to D4 = data */
00131    unsigned short ave; /* sample averaging, 0 = off, 1 = on */
00132    unsigned short counter; /* scan position counter */
00133 } DI_INLIST_STRUCT;
00134 
00135 typedef struct di_outlist_struct {
00136    unsigned short unipolar; /* unipolar or bipolar. 0 = BI, 1 = UN */
00137    unsigned short digital; /* digital output from buffer = 1, analog output = 0 */
00138    unsigned short dig_out_enable; /* 1 = digital out enable with analog */
00139    unsigned short dig_data; /* D0 to D7 = data for output */
00140    unsigned short counter; /* scan position counter */
00141 } DI_OUTLIST_STRUCT;
00142 
00143 
00144 typedef int (PASCAL *FPDIINLIST)(struct di_inlist_struct *);
00145 typedef int (PASCAL *FPDIBURSTRATE)(unsigned);
00146 typedef unsigned (PASCAL *FPDIMODE)(struct di_mode_struct *);
00147 typedef int (PASCAL *FPDICLOSE)(void);
00148 typedef int (PASCAL *FPDIINFO)(struct di_info_struct *);
00149 typedef int (PASCAL *FPDIBUFFERFREE)(unsigned);
00150 typedef short* (PASCAL *FPDIBUFFERALLOC)(unsigned,unsigned);
00151 typedef int (PASCAL *FPDISTARTSCAN)(void);
00152 typedef int (PASCAL *FPDIOUTLIST)(struct di_outlist_struct *);
00153 typedef int (PASCAL *FPDILISTLENGTH)(unsigned,unsigned);
00154 typedef int (PASCAL *FPDISTOPSCAN)(void);
00155 typedef int (PASCAL *FPDICOPYMUX)(short *);
00156 typedef unsigned (PASCAL *FPDISETDATAMODE)(unsigned);
00157 typedef int (PASCAL *FPDIOPEN)(unsigned);
00158 typedef unsigned (PASCAL *FPDIBUFFERSTATUS)(unsigned);
00159 typedef unsigned (PASCAL *FPDIDIGIN)(void);
00160 typedef unsigned (PASCAL *FPDIDIGOUT)(unsigned);
00161 typedef int (PASCAL *FPDIANIN)(struct di_anin_struct *);
00162 typedef int (PASCAL *FPDIANOUT)(unsigned,unsigned);
00163 typedef int (PASCAL *FPDISTRERR)(unsigned,char *);
00164 typedef int (PASCAL *FPDICOM)(unsigned,unsigned);
00165 typedef double (PASCAL *FPDIMAXIMUMRATE)(double);
00166 typedef double (PASCAL *FPDISAMPLERATE)(double, int *, int *);
00167 
00168 typedef unsigned (PASCAL *FPDIBUFFERSIZE)(void);
00169 typedef int (PASCAL *FPDICOPYARRAY)(unsigned, short*, unsigned);
00170 typedef int (PASCAL *FPDICOPYBUFFER)(unsigned, short*, unsigned);
00171 typedef unsigned (PASCAL *FPDICOPYHEADER)(unsigned, CODASHDR*, unsigned, unsigned);
00172 typedef CODASHDR* (PASCAL *FPDIGETACQHEADER)(void);
00173 //typedef int (PASCAL *FPDISETPORTMODE)(unsigned);
00174 typedef unsigned (PASCAL *FPDISTATUSREAD)(short*, unsigned);
00175 
00176 //---------------------------------
00178 class DataQWrapper
00179 {
00180  public:
00181   DataQWrapper (std::string dll_filename)
00182    : dll_handle_ (NULL), dll_filename_(dll_filename)
00183   {
00184 
00185       size_t origsize = strlen(dll_filename.c_str() ) + 1;
00186       const size_t newsize = 128;
00187       size_t convertedChars = 0;
00188       wchar_t wcstring[newsize];
00189       mbstowcs_s(&convertedChars, wcstring, origsize, dll_filename.c_str(), _TRUNCATE);
00190 
00191       //dll_handle_ = LoadLibrary( (LPCWSTR)wcstring );
00192       dll_handle_ = LoadLibrary( dll_filename_.c_str( ) );
00193       if (dll_handle_ == NULL)
00194         throw std::runtime_error("Can't load " + dll_filename_ + "!");
00195 
00196       setupDLLFunction (fndi_inlist_ptr_,"di_inlist");
00197       setupDLLFunction (fndi_burst_rate_ptr_,"di_burst_rate");
00198       setupDLLFunction (fndi_mode_ptr_,"di_mode@4");
00199       setupDLLFunction (fndi_close_ptr_,"di_close");
00200       setupDLLFunction (fndi_info_ptr_,"di_info");
00201       setupDLLFunction (fndi_buffer_free_ptr_,"di_buffer_free");
00202       setupDLLFunction (fndi_buffer_alloc_ptr_,"di_buffer_alloc");
00203       setupDLLFunction (fndi_start_scan_ptr_,"di_start_scan");
00204       setupDLLFunction (fndi_outlist_ptr_,"di_outlist");
00205       setupDLLFunction (fndi_list_length_ptr_,"di_list_length");
00206       setupDLLFunction (fndi_stop_scan_ptr_,"di_stop_scan");
00207       setupDLLFunction (fndi_copy_mux_ptr_,"di_copy_mux");
00208       setupDLLFunction (fndi_set_data_mode_ptr_,"di_set_data_mode");
00209       setupDLLFunction (fndi_open_ptr_,"di_open");
00210       setupDLLFunction (fndi_buffer_status_ptr_,"di_buffer_status");
00211       setupDLLFunction (fndi_digin_ptr_,"di_digin");
00212       setupDLLFunction (fndi_digout_ptr_,"di_digout");
00213       setupDLLFunction (fndi_anin_ptr_,"di_anin");
00214       setupDLLFunction (fndi_anout_ptr_,"di_anout");
00215       setupDLLFunction (fndi_strerr_ptr_,"di_strerr");
00216       setupDLLFunction (fndi_com_ptr_,"di_com");
00217       setupDLLFunction (fndi_maximum_rate_ptr_,"di_maximum_rate");
00218       setupDLLFunction (fndi_sample_rate_ptr_,"di_sample_rate");
00219 
00220       setupDLLFunction (fndi_buffer_size_ptr_, "di_buffer_size");
00221       setupDLLFunction (fndi_copy_array_ptr_, "di_copy_array");
00222       setupDLLFunction (fndi_copy_buffer_ptr_, "di_copy_buffer");
00223       setupDLLFunction (fndi_copy_header_ptr_, "di_copy_header");
00224       setupDLLFunction (fndi_get_acq_header_ptr_, "di_get_acq_header");
00225       //setupDLLFunction (fndi_set_port_mode_ptr_, "di_set_port_mode");
00226       setupDLLFunction (fndi_status_read_ptr_, "di_status_read");
00227 
00228     }
00229 
00230     ~DataQWrapper ()
00231       { FreeLibrary (dll_handle_); }
00232 
00233     int di_inlist(struct di_inlist_struct *input_list)
00234       { return fndi_inlist_ptr_(input_list); }
00235 
00236     int di_burst_rate(unsigned rate)
00237       { return fndi_burst_rate_ptr_(rate); }
00238 
00239     unsigned di_mode(struct di_mode_struct *mode)
00240       { return fndi_mode_ptr_(mode); }
00241 
00242     int di_close(void)
00243       { return fndi_close_ptr_(); }
00244 
00245     int di_info(struct di_info_struct *info)
00246       { return fndi_info_ptr_(info); }
00247 
00248     int di_buffer_free(unsigned chn)
00249       { return fndi_buffer_free_ptr_(chn); }
00250 
00251     short* di_buffer_alloc(unsigned chn,unsigned size)
00252       { return fndi_buffer_alloc_ptr_(chn, size); }
00253 
00254     int di_start_scan(void)
00255       { return fndi_start_scan_ptr_(); }
00256 
00257     int di_outlist(struct di_outlist_struct *output_list)
00258       { return fndi_outlist_ptr_(output_list); }
00259 
00260     int di_list_length(unsigned in_length, unsigned out_length)
00261       { return fndi_list_length_ptr_(in_length, out_length); }
00262 
00263     int di_stop_scan(void)
00264       { return fndi_stop_scan_ptr_(); }
00265 
00266     int di_copy_mux(short *dest)
00267       { return fndi_copy_mux_ptr_(dest); }
00268 
00269     unsigned di_set_data_mode(unsigned datamode)
00270       { return fndi_set_data_mode_ptr_(datamode); }
00271 
00272     int di_open(unsigned devno)
00273       { return fndi_open_ptr_(devno); }
00274 
00275     unsigned di_buffer_status(unsigned chn)
00276       { return fndi_buffer_status_ptr_(chn); }
00277 
00278     unsigned di_digin(void)
00279       { return fndi_digin_ptr_(); }
00280 
00281     unsigned di_digout(unsigned i)
00282       { return fndi_digout_ptr_(i); }
00283 
00284     int di_anin(struct di_anin_struct *analog_input)
00285       { return fndi_anin_ptr_(analog_input); }
00286 
00287     int di_anout(unsigned dac_data, unsigned range)
00288       { return fndi_anout_ptr_(dac_data, range); }
00289 
00290     int di_strerr(unsigned err_code,char *err_str)
00291       { return fndi_strerr_ptr_(err_code, err_str); }
00292 
00293     int di_com(unsigned di_command, unsigned di_data)
00294       { return fndi_com_ptr_(di_command, di_data); }
00295 
00296     double di_maximum_rate(double max_burst_rate)
00297       { return fndi_maximum_rate_ptr_(max_burst_rate); }
00298 
00299     double di_sample_rate(double desired_rate, int *oversample, int *burst_div)
00300       { return fndi_sample_rate_ptr_(desired_rate, oversample, burst_div); }
00301 
00302     unsigned di_buffer_size(void)
00303       { return fndi_buffer_size_ptr_(); }
00304 
00305     int di_copy_array(unsigned buf_index, short *source, unsigned word_count)
00306       { return fndi_copy_array_ptr_(buf_index, source, word_count); }
00307 
00308     int di_copy_buffer(unsigned buf_index, short *dest,  unsigned word_count)
00309       { return fndi_copy_buffer_ptr_(buf_index, dest, word_count); }
00310 
00311     unsigned di_copy_header(unsigned hdr_index,  CODASHDR *dest,
00312                             unsigned byte_count, unsigned clear_bits)
00313       { return fndi_copy_header_ptr_(hdr_index, dest, byte_count, clear_bits); }
00314 
00315     CODASHDR* di_get_acq_header(void)
00316       { return fndi_get_acq_header_ptr_(); }
00317 
00318     //int di_set_port_mode(unsigned mode)
00319     //  { return fndi_set_port_mode_ptr_(mode); }
00320 
00321     unsigned di_status_read(short *dest, unsigned num_scans)
00322       { return fndi_status_read_ptr_(dest, num_scans); }
00323 
00324 
00325  private:
00326   DataQWrapper () { }
00327   DataQWrapper (const DataQWrapper &) { }
00328 
00329   template<typename T> void setupDLLFunction (T& pointer, std::string const& name)
00330   {
00331    pointer = (T)GetProcAddress (dll_handle_, name.c_str());
00332    if (pointer == NULL)
00333     throw std::runtime_error (std::string ( dll_filename_ + " -- missing function: ").append (name));
00334   }
00335 
00336  private:
00337   HINSTANCE         dll_handle_;
00338   std::string     dll_filename_;
00339 
00340   FPDIINLIST     fndi_inlist_ptr_;
00341   FPDIBURSTRATE    fndi_burst_rate_ptr_;
00342   FPDIMODE       fndi_mode_ptr_;
00343   FPDICLOSE       fndi_close_ptr_;
00344   FPDIINFO       fndi_info_ptr_;
00345   FPDIBUFFERFREE  fndi_buffer_free_ptr_;
00346   FPDIBUFFERALLOC  fndi_buffer_alloc_ptr_;
00347   FPDISTARTSCAN    fndi_start_scan_ptr_;
00348   FPDIOUTLIST     fndi_outlist_ptr_;
00349   FPDILISTLENGTH  fndi_list_length_ptr_;
00350   FPDISTOPSCAN    fndi_stop_scan_ptr_;
00351   FPDICOPYMUX     fndi_copy_mux_ptr_;
00352   FPDISETDATAMODE  fndi_set_data_mode_ptr_;
00353   FPDIOPEN       fndi_open_ptr_;
00354   FPDIBUFFERSTATUS fndi_buffer_status_ptr_;
00355   FPDIDIGIN       fndi_digin_ptr_;
00356   FPDIDIGOUT     fndi_digout_ptr_;
00357   FPDIANIN          fndi_anin_ptr_;
00358   FPDIANOUT       fndi_anout_ptr_;
00359   FPDISTRERR     fndi_strerr_ptr_;
00360   FPDICOM        fndi_com_ptr_;
00361   FPDIMAXIMUMRATE  fndi_maximum_rate_ptr_;
00362   FPDISAMPLERATE  fndi_sample_rate_ptr_;
00363 
00364     // functions taken from http://www.dataq.com/sdk/
00365     FPDIBUFFERSIZE    fndi_buffer_size_ptr_;
00366     FPDICOPYARRAY     fndi_copy_array_ptr_;
00367     FPDICOPYBUFFER    fndi_copy_buffer_ptr_;
00368     FPDICOPYHEADER    fndi_copy_header_ptr_;
00369     FPDIGETACQHEADER  fndi_get_acq_header_ptr_;
00370     //FPDISETPORTMODE   fndi_set_port_mode_ptr_;
00371     FPDISTATUSREAD    fndi_status_read_ptr_;
00372 
00373 };
00374 
00375 } // namespace tobiss
00376 
00377 #endif // DATAQ
 All Data Structures Files Functions Variables