/* file: db.h		G. Moody	13 June 1983
			Last revised:	22 May 1997	dblib 9.7.1
DB library type, constant, structure, and function interface definitions

Copyright (C) Massachusetts Institute of Technology 1997. All rights reserved.
*/

#ifndef db_DB_H		/* avoid multiple definitions */
#define db_DB_H

/* DB library version. */
#define DB_MAJOR   9
#define DB_MINOR   7
#define DB_RELEASE 1

/* Determine what type of compiler is being used. */
#ifdef __STDC__		/* true for ANSI C compilers only */
#define db_PROTO	/* function prototypes will be needed */
#endif

#ifdef __cplusplus	/* true for some C++ compilers */
#define db_CPP
#define db_PROTO
#endif

#ifdef c_plusplus	/* true for some other C++ compilers */
#define db_CPP
#define db_PROTO
#endif

#ifdef _WINDOWS	/* true when compiling for MS Windows */
#define db_PROTO
#endif

#ifndef db_PROTO	/* should be true for K&R C compilers only */
#define db_KRC
#define signed
#endif

/* Simple data types */
typedef int	     DB_Sample;	    /* units are adus */
typedef long 	     DB_Time;	    /* units are sample intervals */
typedef long	     DB_Date;	    /* units are days */
typedef double	     DB_Frequency;  /* units are Hz (samples/second/signal) */
typedef double	     DB_Gain;	    /* units are adus per physical unit */
typedef unsigned int DB_Group;      /* signal group number */
typedef unsigned int DB_Signal;	    /* signal number */
typedef unsigned int DB_Annotator;  /* annotator number */

/* Array sizes */
#define DB_MAXANN    2	/* maximum number of input or output annotators */
#define	DB_MAXSIG   32	/* maximum number of input or output signals */
#define DB_MAXSPF    4	/* maximum number of samples per signal per frame */
#define DB_MAXRNL   11  /* maximum length of record name */
#define DB_MAXUSL   20	/* maximum length of DB_siginfo `.units' string */
#define DB_MAXDSL   60	/* maximum length of DB_siginfo `.desc' string */

/* DB_anninfo '.stat' values */
#define READ	  0	/* standard input annotation file */
#define WRITE	  1	/* standard output annotation file */
#define AHA_READ  2     /* AHA-format input annotation file */
#define AHA_WRITE 3     /* AHA-format output annotation file */

/* DB_siginfo '.fmt' values
FMT_LIST is suitable as an initializer for a static array; it lists all of
the legal values for the format field in a DB_siginfo structure.
 fmt    meaning
   0	null signal (nothing read or written)
   8	8-bit first differences
  16	16-bit 2's complement amplitudes, low byte first
  61	16-bit 2's complement amplitudes, high byte first
  80	8-bit offset binary amplitudes
 160	16-bit offset binary amplitudes
 212	2 12-bit amplitudes bit-packed in 3 bytes
 310	3 10-bit amplitudes bit-packed in 4 bytes
*/
#define FMT_LIST {0, 8, 16, 61, 80, 160, 212, 310}
#define NFMTS	  8	/* number of items in FMT_LIST */

/* Default signal specifications */
#define DEFFREQ	250.0	/* default sampling frequency (Hz) */
#define DEFGAIN	200.0	/* default value for gain (adu/physical unit) */
#define DEFRES	12	/* default value for ADC resolution (bits) */

/* getvec operating modes */
#define DB_LOWRES   	0	/* return one sample per signal per frame */
#define DB_HIGHRES	1	/* return each sample of oversampled signals,
				   duplicating samples of other signals */
/* calinfo '.caltype' values
AC_COUPLED and DC_COUPLED are used in combination with the pulse shape
definitions below to characterize calibration pulses. */
#define AC_COUPLED	0	/* AC coupled signal */
#define DC_COUPLED	1	/* DC coupled signal */
#define CAL_SQUARE	2	/* square wave pulse */
#define CAL_SINE	4	/* sine wave pulse */
#define CAL_SAWTOOTH	6	/* sawtooth pulse */
#define CAL_UNDEF	8	/* undefined pulse shape */

/* Structure definitions */
struct DB_siginfo {	/* signal information structure */
    char *fname;	/* filename of signal file */
    char *desc;		/* signal description */
    char *units;	/* physical units (mV unless otherwise specified) */
    DB_Gain gain;	/* gain (ADC units/physical unit, 0: uncalibrated) */
    DB_Sample initval; 	/* initial value (that of sample number 0) */
    DB_Group group;	/* signal group number */
    int fmt;		/* format (8, 16, etc.) */
    int spf;		/* samples per frame (>1 for oversampled signals) */
    int bsize;		/* block size (for character special files only) */
    int adcres;		/* ADC resolution in bits */
    int adczero;	/* ADC output given 0 VDC input */
    int baseline;	/* ADC output given 0 physical units input */
    long nsamp;		/* number of samples (0: unspecified) */
    int cksum;		/* 16-bit checksum of all samples */
};

struct DB_calinfo {	/* calibration information structure */
    double low;		/* low level of calibration pulse in physical units */
    double high;	/* high level of calibration pulse in physical units */
    double scale;	/* customary plotting scale (physical units per cm) */
    char *sigtype;	/* signal type */
    char *units;	/* physical units */
    int caltype;	/* calibration pulse type (see definitions above) */
};

struct DB_anninfo {	/* annotator information structure */
    char *name;		/* annotator name */
    int stat;		/* file type/access code (READ, WRITE, etc.) */
};

struct DB_ann {		/* annotation structure */
    DB_Time time;	/* annotation time, in sample intervals from
			   the beginning of the record */
    char anntyp;	/* annotation type (< ACMAX, see <ecg/ecgcodes.h> */
    signed char subtyp;	/* annotation subtype */
    signed char chan;	/* channel number */
    signed char num;	/* annotator number */
    char *aux;		/* pointer to auxiliary information */ 
};

/* Composite data types */
typedef struct DB_siginfo DB_Siginfo;
typedef struct DB_calinfo DB_Calinfo;
typedef struct DB_anninfo DB_Anninfo;
typedef struct DB_ann DB_Annotation;

/* Function types */
#ifndef _WINDOWS	/* for everything *except* MS Windows applications */
typedef char *FSTRING;
typedef DB_Date FDATE;
typedef double FDOUBLE;
typedef DB_Frequency FFREQUENCY;
typedef int FINT;
typedef long FLONGINT;
typedef DB_Sample FSAMPLE;
typedef DB_Time FSITIME;
typedef void FVOID;
#else		
#ifndef _WIN32	/* for 16-bit MS Windows applications using the DB DLL */
  /* typedefs don't work properly with _far or _pascal -- must use #defines */
#define FSTRING char _far * _pascal
#define FDATE DB_Date _far _pascal
#define FDOUBLE double _far _pascal
#define FFREQUENCY DB_Frequency _far _pascal
#define FINT int _far _pascal
#define FLONGINT long _far _pascal
#define FSAMPLE DB_Sample _far _pascal
#define FSITIME DB_Time _far _pascal
#define FVOID void _far _pascal
#else		/* for 32-bit MS Windows applications using the DB DLL */
#ifndef CALLBACK
#define CALLBACK __stdcall	/* from windef.h */
#endif
#define FSTRING __declspec (dllexport) char * CALLBACK
#define FDATE __declspec (dllexport) DB_Date CALLBACK
#define FDOUBLE __declspec (dllexport) double CALLBACK
#define FFREQUENCY __declspec (dllexport) DB_Frequency CALLBACK
#define FINT __declspec (dllexport) int CALLBACK
#define FLONGINT __declspec (dllexport) long CALLBACK
#define FSAMPLE __declspec (dllexport) DB_Sample CALLBACK
#define FSITIME __declspec (dllexport) DB_Time CALLBACK
#define FVOID __declspec (dllexport) void CALLBACK
#endif
#endif

/* Specify C linkage for C++ compilers. */
#ifdef db_CPP
extern "C" {
#endif

/* Define function prototypes for ANSI C compilers and C++ compilers */
#ifdef db_PROTO
extern FINT annopen(char *record, DB_Anninfo *aiarray, unsigned int nann);
extern FINT isigopen(char *record, DB_Siginfo *siarray, int nsig);
extern FINT osigopen(char *record, DB_Siginfo *siarray, unsigned int nsig);
extern FINT osigfopen(DB_Siginfo *siarray, unsigned int nsig);
extern FINT dbinit(char *record, DB_Anninfo *aiarray, unsigned int nann,
                  DB_Siginfo *siarray, unsigned int nsig);
extern FINT getspf(void);
extern FVOID setgvmode(int mode);
extern FINT getvec(DB_Sample *vector);
extern FINT getframe(DB_Sample *vector);
extern FINT putvec(DB_Sample *vector);
extern FINT getann(DB_Annotator a, DB_Annotation *annot);
extern FINT ungetann(DB_Annotator a, DB_Annotation *annot);
extern FINT putann(DB_Annotator a, DB_Annotation *annot);
extern FINT isigsettime(DB_Time t);
extern FINT isgsettime(DB_Group g, DB_Time t);
extern FINT iannsettime(DB_Time t);
extern FSTRING ecgstr(int annotation_code);
extern FINT strecg(char *annotation_mnemonic_string);
extern FINT setecgstr(int annotation_code, char *annotation_mnemonic_string);
extern FSTRING annstr(int annotation_code);
extern FINT strann(char *annotation_mnemonic_string);
extern FINT setannstr(int annotation_code, char *annotation_mnemonic_string);
extern FSTRING anndesc(int annotation_code);
extern FINT setanndesc(int annotation_code, char *annotation_description);
extern FVOID iannclose(DB_Annotator a);
extern FVOID oannclose(DB_Annotator a);
extern FSTRING timstr(DB_Time t);
extern FSTRING mstimstr(DB_Time t);
extern FSITIME strtim(char *time_string);
extern FSTRING datstr(DB_Date d);
extern FDATE strdat(char *date_string);
extern FINT adumuv(DB_Signal s, DB_Sample a);
extern FSAMPLE muvadu(DB_Signal s, int microvolts);
extern FDOUBLE aduphys(DB_Signal s, DB_Sample a);
extern FSAMPLE physadu(DB_Signal s, double v);
extern FINT calopen(char *calibration_filename);
extern FINT getcal(char *description, char *units, DB_Calinfo *cal);
extern FINT putcal(DB_Calinfo *cal);
extern FINT newcal(char *calibration_filename);
extern FVOID flushcal(void);
extern FSTRING getinfo(char *record);
extern FINT putinfo(char *info);
extern FINT newheader(char *record);
extern FINT setheader(char *record, DB_Siginfo *siarray, unsigned int nsig);
extern FINT setmsheader(char *record, char **seg_names, unsigned int nsegments);
extern FINT dbgetskew(DB_Signal s);
extern FVOID dbsetskew(DB_Signal s, int skew);
extern FLONGINT dbgetstart(DB_Signal s);
extern FVOID dbsetstart(DB_Signal s, long bytes);
extern FVOID dbquit(void);
extern FFREQUENCY sampfreq(char *record);
extern FINT setsampfreq(DB_Frequency sampling_frequency);
extern FFREQUENCY getcfreq(void);
extern FVOID setcfreq(DB_Frequency counter_frequency);
extern FDOUBLE getbasecount(void);
extern FVOID setbasecount(double count);
extern FINT setbasetime(char *time_string);
extern FVOID dbquiet(void);
extern FVOID dbverbose(void);
extern FSTRING dberror(void);
extern FVOID setdb(char *database_path_string);
extern FSTRING getdb(void);
extern FINT setibsize(int input_buffer_size);
extern FINT setobsize(int output_buffer_size);
extern FSTRING dbfile(char *file_type, char *record);
extern FVOID dbflush(void);
#endif

#ifdef db_CPP
}
#endif

#ifdef db_KRC	/* declare only function return types for K&R C compilers */
extern FINT annopen(), isigopen(), osigopen(), dbinit(), getspf(), getvec(),
    getframe(), putvec(), getann(), ungetann(), putann(), isigsettime(),
    isgsettime(), iannsettime(), strecg(), setecgstr(), strann(), setannstr(),
    setanndesc(), adumuv(), newheader(), setheader(), setmsheader(),
    setsampfreq(), setbasetime(), putinfo(), setibsize(), setobsize(),
    calopen(), getcal(), putcal(), newcal(), dbgetskew();
extern FLONGINT dbgetstart();
extern FSAMPLE muvadu(), physadu();
extern FSTRING ecgstr(), annstr(), anndesc(), timstr(), mstimstr(),
    datstr(), getdb(), getinfo(), dberror(), dbfile();
extern FSITIME strtim();
extern FDATE strdat();
extern FVOID setgvmode(), dbquit(), dbquiet(), dbverbose(), setdb(), dbflush(),
    setcfreq(), setbasecount(), flushcal(), dbsetskew(), dbsetstart();
extern FFREQUENCY sampfreq(), getcfreq();
extern FDOUBLE aduphys(), getbasecount();
#endif

/* Definitions for compatibility with DB applications written to use earlier
   versions of this header file.  At least some of these are incompatible with
   Solaris.  Newly-written code should not use these definitions, since they
   may be removed in future versions of the DB library. */
#if defined(__sun__) && defined(__svr4__) /* gcc detects Solaris this way */
#define SOLARIS
#elif defined(sun)
#if !defined(FILE) && !defined(_STDIO_H)
#include <stdio.h>
#endif
#if defined(_STDIO_H)		 /* Sun C and C++ detect Solaris this way */
#define SOLARIS
#endif
#endif
/* If you are using another compiler under Solaris, define SOLARIS manually. */

#ifndef SOLARIS
#define Sample DB_Sample
#define SITime DB_Time
#define Date DB_Date
#define Frequency DB_Frequency
#define Gain DB_Gain
#define Group DB_Group
#define Signal DB_Signal
#define Annotator DB_Annotator
#define MAXSIG DB_MAXSIG
#define MAXANN DB_MAXANN
#define siginfo DB_siginfo
#define calinfo DB_calinfo
#define anninfo DB_anninfo
#define ann DB_ann
#define Siginfo DB_Siginfo
#define Calinfo DB_Calinfo
#define Anninfo DB_Anninfo
#define Annotation DB_Annotation
#endif

/* Remove local preprocessor definitions. */
#ifdef db_PROTO
#undef db_PROTO
#endif

#ifdef db_CPP
#undef db_CPP
#endif

#ifdef db_KRC
#undef db_KRC
#undef signed
#endif

#endif
