/**************************************************************\
*	SPLAT Program                   Version 1.00		     *        *
*   For CompuScope 8500 boards and Microsoft Windows 95/98	 *
*	   												         *
*     09/09/1999							Jun				 *
\**************************************************************/

#include "whichdrv.h"
#include <time.h>
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>	/* for pow */
#include "gage_drv.h"
#include "gage_low.h"
#include "eeprom.h"
#include "structs.h"
#include "gsdlldem.h"
#include <direct.h>
#include "splatisa.h"
#include <sys/types.h>
#include <sys/timeb.h>
#include <commctrl.h>



#include "resource.h"

#define USE_MEM_READ			0x0001
#define USE_TRIGGER_VIEW		0x0002
#define USE_TRIGGER_VIEW_2		0x0003
#define USE_TRIGGER_VIEW_3		0x0004
#define USE_TRANSFER_BUFFER		0x0005
#define USE_TRANSFER_BUFFER_2	0x0006
#define USE_TRANSFER_BUFFER_3	0x0007
#define USE_REAL_TIME			0x0008

/****************************\
*                           *
*	GLOBAL DEFINITIONS.		*
*	===================		*
*                           *
\***************************/


int32	trigger_address[GAGE_B_L_MAX_CARDS];
int32	starting_address[GAGE_B_L_MAX_CARDS];
int32	ending_address[GAGE_B_L_MAX_CARDS];
int32	current_address[GAGE_B_L_MAX_CARDS];
int32	channel_ram_size = 0L;
uInt16	board_type = GAGE_ASSUME_NOTHING;
int32	board_memory = 0L;
double	sample_resolution = 128.0;    /*   12-bit = 2048.0 and 8-bit = 128.0.      */
int16	sample_offset = 0;            /*   12-bit = 1      and 8-bit = 0.          */
int16	CapturingData = FALSE;
int16	ContinuousMode = FALSE;
uInt16	wTimerInterval = 100;   	  /*   Number of milliseconds between timer events.  */
int16	number_of_timer_events = 0;
int16	multiple_record_avail = 0;
int cont_run = 1;
int k=0;
int nxx=5;
int     j;
long		location;  
int32		trigger;	/* trigger address  */

FILE *stream;
uInt8 *chan_a_8 = NULL;
int32		points;		/* number of points */
uInt32		offset_a;
char filename[20];
char filepath[30];
char line[100];
char linezip[100];
char lineunzip[100];
char directoryname[20];
char  buffer[200];
char tmpbuf1[128];
char tmpbuf2[128];


#define	TRIG_TIMEOUT   100000
#define	BUSY_TIMEOUT    10

HWND	hDisplayWnd;
UINT	wTimerID;
HPEN	hBlackPen;
HPEN    hTriggPen;
uInt8  hTriggPenStr[10];
HPEN	hZeroPen;
HPEN	hDotPen;
HBRUSH	hWhiteBrush;

uInt8 AdjustFlag=0;
int8    str[256];       /*  Miscellaneous uses.  */
uInt16	boards_in_system;

uInt8 *rt_packeddata=0;
uInt32 rt_packeddataLength=0;
uInt8 *rt_buffer;
uInt8 *rt_buffer_1M;

uInt32 trigger_trace=0;
uInt32 adjusted_depth=0;

int32	ptm_pre_samples=0, ptm_post_samples=0, ptm_start=0, ptm_max_group=0;
int32	ptm_max_pre = 0 , ptm_max_post = 0, view_start = 0, ptm_group;
int16	ptm_channel;
int16	buildup_signal = 0;

uInt32 acgtimes = 0;

// add new globals for time keeping to measure the speed of transfer
uInt32 time1, time2, time3, tmp1;


/*-------------------------------------------------------------------------*/

#define BLF    260

/*-------------------------------------------------------------------------*/

int8					board_loc_file[BLF];
uInt16					gage_board_location[GAGE_B_L_BUFFER_SIZE];
gage_driver_info_type   board_info;
uInt16					zoom_x = 1, zoom_x2 = 1;
uInt16					scrn_x;

uInt32	TVT3_offset[GAGE_B_L_MAX_CARDS]={0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L};
uInt32	TVT3_offsetB[GAGE_B_L_MAX_CARDS]={0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L,0L};
int16	*TVT2_ptr[GAGE_B_L_MAX_CARDS];
int16	*TVT2_ptrB[GAGE_B_L_MAX_CARDS];

uInt8	far *TVTBufferPtr[GAGE_B_L_MAX_CARDS];

uInt8	far *a_buffer[GAGE_B_L_MAX_CARDS];
uInt8	far *b_buffer[GAGE_B_L_MAX_CARDS];
int16	far *a_buffer16[GAGE_B_L_MAX_CARDS];
int16	far *b_buffer16[GAGE_B_L_MAX_CARDS];

int32	bsize; // size of dedicated buffer for tvt2 and real-time mode

int16	transfer_type = USE_TRANSFER_BUFFER_3;
int16	xfer_time = 0;
HWND 	SRateDlg = 0;
HWND	IRangeDlg = 0;
HWND	TriggerDlg = 0;
HWND	AboutDlg = 0;
HWND	PmtDlg   = 0;

int16	external_clock_in_use = 0;
float	external_clock_rate = 10000000.0F;
int16	use_multiple_record = 0;
int16	multiple_record_available;

int32	current_board_type;

LRESULT CALLBACK SRateDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK IRangeDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK TriggerDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK AboutDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK PmtDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);


BOOL CheckMessageQueue (void);
/*-------------------------------------------------------------------------*/
/*
 *  CheckMessageQueue retrieves and dispatches any messages waiting in
 *  the application's message queue. WM_QUIT messages are NOT removed from
 *  the queue.
 *
 *  Input parameters:
 *    None
 *
 *  Returns:
 *    TRUE = Message queue empty; okay to continue
 *    FALSE = WM_QUIT waiting in queue
 *-------------------------------------------------------------------------*/
 
BOOL CheckMessageQueue (void)
{
    MSG msg;

    while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE)) {
        if (msg.message == WM_QUIT)
            return FALSE;
        else {
            GetMessage (&msg, NULL, 0, 0);
            TranslateMessage (&msg);
            DispatchMessage (&msg);
        }
    }
    return TRUE;
}

/*-------------------------------------------------------------------------*/

void	SetDefaultBoardLocation (uInt16 seg, uInt16 ind)
{
	int16             i;

	gage_set_records ((uInt16 far *)gage_board_location, 0, seg, ind, 0, 0);
	for (i = 1 ; i < GAGE_B_L_MAX_CARDS ; i++)
		gage_set_records ((uInt16 far *)gage_board_location, i, 0, 0, 0, 0);

}       /*      End of SetDefaultBoardLocation ().  */


/*-------------------------------------------------------------------------*/

int8	*BoardTypeSizeToText (unsigned short board_type, int32 max_memory)
{
	static char     str[13];
	
	if (board_type == 0)
		return ("ABSENT");
	if (board_type == GAGE_ASSUME_CS3200)
		lstrcpy (str, "3200");
	else if (board_type == 	GAGE_ASSUME_CS1610)
		lstrcpy (str, "1602");
	else if (board_type & (GAGE_ASSUME_CSLITE | GAGE_ASSUME_CSLITE15))
		lstrcpy (str, "LITE");
	else if (board_type & GAGE_ASSUME_CS250)
		lstrcpy (str, "250");
	else if (board_type & GAGE_ASSUME_CS25018)
		lstrcpy (str, "250v8");
	else if (board_type & GAGE_ASSUME_CS225)
		lstrcpy (str, "225");
	else if (board_type & GAGE_ASSUME_CS220)
		lstrcpy (str, "220");
	else if (board_type & GAGE_ASSUME_CS2125)
		lstrcpy (str, "2125");
	else if (board_type & GAGE_ASSUME_CS265)
		lstrcpy (str, "265");
	else if (board_type & GAGE_ASSUME_CS1016)
		lstrcpy (str, "1016");
	else if (board_type == GAGE_ASSUME_CS8500)
		lstrcpy (str, "8500");
	else if (board_type == GAGE_ASSUME_CS12100)
		if (board_info.ee_options & EEPROM_OPTIONS_ALTERNATE_CONFIG )
			lstrcpy (str, "1250");
		else if ((board_info.ee_options & EEPROM_OPTIONS_NORMAL_OSC) == 0)
			lstrcpy (str, "1210");
		else
			lstrcpy (str, "12100");
	else if (board_type & GAGE_ASSUME_CS512)
		if (board_type & GAGE_ASSUME_CSPCI)
			lstrcpy (str, "512PCI");
		else
			lstrcpy (str, "512");
	else if (board_type & GAGE_ASSUME_CS1012)
		if (board_type & GAGE_ASSUME_CSPCI)
			lstrcpy (str, "1012PCI");
		else
			lstrcpy (str, "1012");
	else if (board_type & GAGE_ASSUME_CS6012)
		if (board_type & GAGE_ASSUME_CSPCI)
			lstrcpy (str, "6012PCI");
		else
			lstrcpy (str, "6012");
	else if (board_type & GAGE_ASSUME_CS6012)
		if (board_type & GAGE_ASSUME_CSPCI)
			lstrcpy (str, "6012PCI");
		else
			lstrcpy (str, "6012");
	else if (board_type & GAGE_ASSUME_CS8012)
		if (board_type & GAGE_ASSUME_CSPCI)
			lstrcpy (str, "8012PCI");
		else
			lstrcpy (str, "8012");
	if (max_memory >= 1048576L)
		wsprintf (str + lstrlen (str), " %ldM", max_memory / 1048576L);
	else
		wsprintf (str + lstrlen (str), " %ldK", max_memory / 1024L);
	return (str);
	
}       /*  End of BoardTypeSizeToText ().  */

/*-------------------------------------------------------------------------*/

int	InitBoard (HWND hWnd)
{

	int16 				       	ret;
	uInt16						major_version, minor_version, board_support;
	int32						gdi_size, jj;
	gage_driver_info_type far 	*gdi;

    
	for (jj=0;jj<GAGE_B_L_MAX_CARDS; jj++)
	{
        a_buffer[jj] = NULL;
		b_buffer[jj] = NULL;
	}


	board_loc_file[0] = '\0';
	ret = gage_get_config_filename ((LPSTR)(board_loc_file));
	if ((ret = gage_read_config_file (board_loc_file, (uInt16 far *)(gage_board_location))) < 0)  {
#if 0
		wsprintf (str, "%s not found!", (LPSTR)board_loc_file);
		MessageBox(hWnd, str, "Error", MB_OK);
#endif
		SetDefaultBoardLocation (0xffff, 0xffff);
	}
	
	if (gage_driver_initialize((uInt16 far *)gage_board_location, GAGE_MEMORY_SIZE_TEST) <=0 ) 
	{
		if (gage_driver_initialize((uInt16 far *)gage_board_location, GAGE_MEMORY_SIZE_TEST) <=0 ) 
		{
			wsprintf (str, "No CompuScope boards found!  Error code = %02x", (int16)(gage_board_location[GAGE_B_L_STATUS_START + 1]));
			MessageBox(hWnd, str, "Error", MB_OK);
			return (1);
		}
	}

    gage_get_driver_info ((gage_driver_info_type far *)&board_info);
		
	boards_in_system = gage_get_boards_found();
	board_type = board_info.board_type;
	board_memory = board_info.max_memory;
	wsprintf (str, "CompuScope %s board found!", (LPSTR)(BoardTypeSizeToText (board_type, board_memory)));
	MessageBox(hWnd, str, "Success", MB_OK | MB_ICONINFORMATION);
	
	if (board_info.sample_bits == 12) {
		sample_resolution = 2048.0;
		sample_offset = 1;
	}else if (board_info.sample_bits == 16) {
		sample_resolution = 32768.0;
		sample_offset = 1;
	}else{
		sample_resolution = 128.0;
		sample_offset = 0;
	}

	board.srindex =	best_sample_rate (board_info.board_type, board.opmode, 0);


	for (jj=0; jj < boards_in_system; jj++)
	{
		if (a_buffer[jj] == NULL) {
			gage_select_board((int16)(jj+1));
			bsize = 0x00100000;
			a_buffer[jj] = VirtualAlloc(
                                NULL,	// address of region to reserve or commit  
                                (DWORD)(80+board.depth * ((board_info.sample_bits >= 12) + 1)),	// size of region 
                                MEM_COMMIT,	// type of allocation 
                                PAGE_READWRITE 	// type of access protection 
								);
			TVTBufferPtr[jj] = a_buffer[jj]; 
	}

	a_buffer16[jj] = (int16 far *)a_buffer[jj];

	if (b_buffer[jj] == NULL) {
		b_buffer[jj] = VirtualAlloc(
                                NULL,	// address of region to reserve or commit  
                                (DWORD)(80+board.depth * ((board_info.sample_bits >= 12) + 1)),	// size of region 
								MEM_COMMIT,	// type of allocation 
                                PAGE_READWRITE 	// type of access protection 
				        );
	}
	b_buffer16[jj] = (int16 far *)b_buffer[jj];

    if((a_buffer[jj] == NULL) || (b_buffer[jj] ==NULL))
       exit(1);
	
	}

	gage_get_driver_info_structure ((uInt16 far *)(&major_version), (uInt16 far *)(&minor_version), (uInt16 far *)(&board_support),
									(gage_driver_info_type far * far *)(&gdi), (int32 far *)(&gdi_size));

    gage_get_driver_info ((gage_driver_info_type far *)gdi);
	wsprintf (str, "CompuScope Driver Version %d.%02d%c.\n", major_version, minor_version & 0xff, (char)((minor_version >> 8) & 0xff));
    MessageBox (hWnd, str, "DLL version", MB_OK);

	current_board_type = B_T_DOUBLE_UP_BITS (board_info.board_type);
	update_sr_table_for_osc ((uInt16)0, (uInt16)current_board_type, (uInt32)board_info.ee_options);

	gage_initialize_start_capture (1);
	if (gage_select_board(1)!=1)  {
		MessageBox(hWnd, "Error while selecting board!", "Error", MB_OK);
		return (2);
	}

	multiple_record_avail = gage_detect_multiple_record();

    update_sr_table_for_osc (board_info.board_version, board_info.board_type, board_info.ee_options);
	update_range_table_for_version (board_info.board_version, board_info.board_type, board_info.ee_options); 
	
	/* Set opmode for the first time the window is drawn */
	if (board_info.board_type & GAGE_ASSUME_CS512)
		board.opmode = GAGE_DUAL_CHAN;
	else if ((board_info.board_type == GAGE_ASSUME_CS1016))
		board.opmode = GAGE_SINGLE_CHAN;
	else if ((board_info.board_type == GAGE_ASSUME_CS1610))
		board.opmode = GAGE_DUAL_CHAN;
	else if ((board_info.board_type == GAGE_ASSUME_CS8500) || (board_info.board_type == GAGE_ASSUME_CS3200))

	{
		board.opmode = GAGE_SINGLE_CHAN;
		if (board_info.board_type == GAGE_ASSUME_CS3200)
			board.source = GAGE_EXTERNAL;
	}
	else
		board.opmode = GAGE_DUAL_CHAN;

	return (0);


}       /*  End of InitBoard ().  */


/*-------------------------------------------------------------------------*/

void	prepare_for_capture (int16 boards_in_system, boarddef *board)
/*
	Preparing the CompuScope MASTER/SLAVE cards for data capture.
	Initialize the hardware with the new values.
*/
{

	int16	i, ret;
	int16	external_clock_in_use = 0;

		
	if (external_clock_in_use)  {
		board->srindex = SRTI_EXTERNAL;
		gage_set_ext_clock_variables (board->opmode, (uInt16)-1, (float)external_clock_rate);
		sample_rate_table[SRTI_EXTERNAL].sr_calc = 1000000000.0F / external_clock_rate;
		if (sample_rate_table[SRTI_EXTERNAL].sr_text != NULL)
			free (sample_rate_table[SRTI_EXTERNAL].sr_text);
		sample_rate_table[SRTI_EXTERNAL].sr_text = strdup (value_and_units_to_text ((double)(external_clock_rate), "Hz") + 1);	/*	Add 1 to skip the '+' sign.	*/
	}

	gage_get_driver_info (&board_info);

	if (board_info.board_type & GAGE_ASSUME_CS512)	
		/* If CS512, force opmode to dual channel, single is invalid for 512. */
    	board->opmode = GAGE_DUAL_CHAN;
	
	if (board_info.board_type == GAGE_ASSUME_CS1016) {
		/* If CS1016, force opmode to dual channel and range to +/- 1 volt.	*/
		board->opmode = GAGE_SINGLE_CHAN;
		board->range_a = board->range_b = GAGE_PM_1_V;
		board->source = GAGE_EXTERNAL;
		board->level = 192;
		board->range_e = GAGE_PM_5_V;
	}

	if (board_info.board_type == GAGE_ASSUME_CS8500)
		board->opmode = GAGE_SINGLE_CHAN;


	if (board_info.board_type == GAGE_ASSUME_CS3200) {
		board->opmode = GAGE_SINGLE_CHAN;
		board->source = GAGE_EXTERNAL;
	}

//	board->srindex = best_sample_rate (board_info.board_type, board->opmode, external_clock_in_use);
	gage_select_board (1);	/* Select Master. */

	if (!gage_capture_mode (board->opmode, sample_rate_table[board->srindex].rate, sample_rate_table[board->srindex].mult))  {
		wsprintf (str, "Error while setting capture mode!  Error Code = %d", gage_get_error_code ());
		MessageBox(NULL, str, "Error", MB_OK);
	}
	
    if (!gage_input_control (GAGE_CHAN_A, GAGE_INPUT_ENABLE, board->couple_a, (int16)(board->range_a | board->imped_a)))  {
		wsprintf (str, "Error while setting channel A input control in master!  Error Code = %d", gage_get_error_code ());
		MessageBox(NULL, str, "Error", MB_OK);
	}
	if ( (board->opmode == GAGE_SINGLE_CHAN) && (board_info.board_type != GAGE_ASSUME_CS8500) && (board_info.board_type != GAGE_ASSUME_CS3200) )  {
		if (!gage_input_control (GAGE_CHAN_B, GAGE_INPUT_ENABLE, board->couple_a, (int16)(board->range_a | board->imped_a)))  {
			wsprintf (str, "Error while setting channel B input control in master!  Error Code = %d", gage_get_error_code ());
			MessageBox(NULL, str, "Error", MB_OK);
		}
	}else if ( (board_info.board_type != GAGE_ASSUME_CS8500) && (board_info.board_type != GAGE_ASSUME_CS3200) ){
	    if (!gage_input_control (GAGE_CHAN_B, GAGE_INPUT_ENABLE, board->couple_b, (int16)(board->range_b | board->imped_b)))  {
			wsprintf (str, "Error while setting channel B input control in master!  Error Code = %d", gage_get_error_code ());
			MessageBox(NULL, str, "Error", MB_OK);
		}
	}
	
	for (i = 2; i <= boards_in_system; i++) {
		gage_select_board (i);		/* Select slaves. */
		
		if ((board_info.board_type & (GAGE_ASSUME_CS1012 | GAGE_ASSUME_CS6012 | GAGE_ASSUME_CS512 | GAGE_ASSUME_CS8012)) && (board->srindex != SRTI_EXTERNAL)) {
			if (!gage_capture_mode (board->opmode, sample_rate_table[board->srindex].rate, sample_rate_table[board->srindex].mult)) {
				wsprintf (str, "Error setting capture mode on slave %d! Error code = %d", i, gage_get_error_code ());
				MessageBox (NULL, str, "Error", MB_OK);
			}	
		}else{
			if (!gage_capture_mode (board->opmode, 0, GAGE_EXT_CLK))  {
				wsprintf (str, "Error while setting EXT_CLK capture mode!  Error Code = %d", gage_get_error_code ());
				MessageBox(NULL, str, "Error", MB_OK);
			}
		}
		if (!gage_input_control (GAGE_CHAN_A, GAGE_INPUT_ENABLE, board->couple_a, (int16)(board->range_a | board->imped_a))) {
			wsprintf (str, "Error while setting Channel A input control in slave! Error Code = %d", gage_get_error_code ());
			MessageBox (NULL, str, "Error", MB_OK);
		}
		if ( (board->opmode == GAGE_SINGLE_CHAN) && (board_info.board_type != GAGE_ASSUME_CS8500))  {
			if (!gage_input_control (GAGE_CHAN_B, GAGE_INPUT_ENABLE, board->couple_a, (int16)(board->range_a | board->imped_a)))  {
				wsprintf (str, "Error while setting channel B input control in slave!  Error Code = %d", gage_get_error_code ());
				MessageBox(NULL, str, "Error", MB_OK);
			}
		}else if (board_info.board_type != GAGE_ASSUME_CS8500){
			if (!gage_input_control (GAGE_CHAN_B, GAGE_INPUT_ENABLE, board->couple_b, (int16)(board->range_b | board->imped_b)))  {
				wsprintf (str, "Error while setting channel B input control in slave!  Error Code = %d", gage_get_error_code ());
				MessageBox(NULL, str, "Error", MB_OK);
			}
		}
#define USE_TRIGGER_CONTROL_2
#ifdef	USE_TRIGGER_CONTROL_2

	if (board_info.board_type & (GAGE_ASSUME_CS1016 | GAGE_ASSUME_CS6012 | GAGE_ASSUME_CS1012 | GAGE_ASSUME_CS512 | GAGE_ASSUME_CS8012))	/*	12 bit boards have 12 bit trigger levels.	*/
		ret = gage_trigger_control_2 (board->source, board->source_2, board->couple_e, board->range_e, board->slope, board->slope_2, (int16)(board->level << 4), (int16)(board->level_2 << 4), board->depth, 1, 1);
	else
		ret = gage_trigger_control_2 (board->source, board->source_2, board->couple_e, board->range_e, board->slope, board->slope_2, board->level, board->level_2, board->depth, 1, 1);

#else	/*	USE_TRIGGER_CONTROL_2	*/

		ret = gage_trigger_control (GAGE_SOFTWARE, board->couple_e, board->range_e, board->slope, board->level, board->depth);

#endif	/*	USE_TRIGGER_CONTROL_2	*/

		if (!ret)  {
			wsprintf (str, "Error while setting trigger control in slave!  Error Code = %d", gage_get_error_code ());
			MessageBox(NULL, str, "Error", MB_OK);
		}

	}

	gage_select_board (1);	/* Select Master. */

#ifdef	USE_TRIGGER_CONTROL_2

	if (board_info.board_type & (GAGE_ASSUME_CS1016 | GAGE_ASSUME_CS6012 | GAGE_ASSUME_CS1012 | GAGE_ASSUME_CS512 | GAGE_ASSUME_CS8012))	/*	12 bit boards have 12 bit trigger levels.	*/
		ret = gage_trigger_control_2 (board->source, board->source_2, board->couple_e, board->range_e, board->slope, board->slope_2, (int16)(board->level << 4), (int16)(board->level_2 << 4), board->depth, 1, 1);
	else
		ret = gage_trigger_control_2 (board->source, board->source_2, board->couple_e, board->range_e, board->slope, board->slope_2, board->level, board->level_2, board->depth, 1, 1);

#else	/*	USE_TRIGGER_CONTROL_2	*/

		ret = gage_trigger_control (board->source, board->couple_e, board->range_e, board->slope, board->level, board->depth);

#endif	/*	USE_TRIGGER_CONTROL_2	*/

	if (!ret)  {
		wsprintf (str, "Error while setting trigger control in master!  Error Code = %d", gage_get_error_code ());
		MessageBox(NULL, str, "Error", MB_OK);
	}


	/* Get updated board_info in case max_available_memory has changed due to changing the mode */
	gage_get_driver_info (&board_info);
	Sleep(10);

//	channel_ram_size = board_info.max_available_memory;

}	/*	End of prepare_for_capture ().	*/

/*-------------------------------------------------------------------------*/

void	InitMemPtrs (void)
{
	int32			i;
	int16		channel;
	

	for (i = 0; i < (boards_in_system * 2); i += (1 + (board.opmode == GAGE_SINGLE_CHAN))) {
		gage_select_board ((int16)((i >> 1) + 1));
		channel = (int16)((i & 1) + GAGE_CHAN_A);

    /* When not using interpolated trigger, use this line. */
     	gage_calculate_addresses (channel, board.opmode,sample_rate_table[board.srindex].sr_calc,
     		&trigger_address[i], &starting_address[i], &ending_address[i]);
   }
}       /*  End of InitMemPtrs ().  */

/*-------------------------------------------------------------------------*/

void	AcquireStart (void)
{
	uInt32 rtn = 0;
	
    gage_select_board(1);

	gage_start_capture ((int16)(board.source == GAGE_SOFTWARE));
    CapturingData = TRUE;
	number_of_timer_events = 0;

}       /*      End of AcquireStart ().  */


/*----------------mydata--------------------------------------------------------------*/
//void	TransferMRData_MRAdjust (HWND hWnd)
//{
//	int32	i, count, group, trig, start, end;
//	int32	j;
//	int16	data, bits;		/*	New for multiple record adjust on CS1012-16M.	*/
/***********/
//	FILE		*stream1;
//	stream1 = fopen( "GAGE.DAT", "w" );
//		MessageBox(NULL, "IIII", "My Message", MB_OK);
/***********/
//	for (i = 0; i < boards_in_system; i++) {
//		gage_select_board ((int16)(i + 1));
//		gage_multiple_record(use_multiple_record);
//		gage_need_ram (TRUE);
//		if (board.opmode == GAGE_SINGLE_CHAN) {
//			group = 1;
//
//			while (group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, board.opmode, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) {
//
//				data = gage_mem_read_single (trig);					
//
//#if 0
//				if (data & 0x0800)									
//					bits = (int16)(((data >> 10) & 0x1));			
//				else												
//					bits = (int16)(((data >> 10) & 0x1) | 0x02);	
//				
//				trig  += (int32)(4 + bits);							
//				start += (int32)(4);								
//				end   -= (int32)(8);								
//#endif 
//				count = trig;
//				adjusted_depth =  end-start;
//
//				if (board_info.sample_bits == (int16)12) {
//					for (j = 0; j < board.depth; j++) {
//
//						a_buffer16[i][j] = (int16)gage_mem_read_single (count);
//
/**************/
//
//		fprintf( stream1, "\t%f\n", a_buffer[i][j], "\n" );			
//			wsprintf (str, "a_buffer[i][j]= %d\n",a_buffer[i][j] );
//		MessageBox(NULL, str, "My Message", MB_OK);
//
/**************/
//
//
//						if (count >= end)								
//							break;
//						
//						if (++count >= board_info.max_available_memory)
//							count = 0;
//					}
//				}
//				else {
//					for (j = 0; j < board.depth; j++) {
//						a_buffer[i][j] = (int8)gage_mem_read_single (count);
/**************/
//
//		fprintf( stream1, "\t%f\n", a_buffer[i][j], "\n" );
//						wsprintf (str, "a_buffer[i][j]= %d\n",a_buffer[i][j] );
//		MessageBox(NULL, str, "My Message", MB_OK);
//			
//
/**************/
//						
//						if (count >= end)								
//							break;										
//						if (++count >= board_info.max_available_memory)
//							count = 0;
//					}
//
//
//				}
/**************/				
//				fclose( stream1 );
//wsprintf (str, "Acquisition complete! Data saved to file GAGE_A2D.DAT");
//		MessageBox(NULL, str, "GAGE_A2D", MB_OK);
/**************/
//					if (!CheckMessageQueue ())
//					break;
//				Display (hWnd);
//				group++;
//			}
//
//		}else{
//			group = 1;
//			while (group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, board.opmode, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) {
//				data = gage_mem_read_chan_a (trig);					
//#if 0
//				if (data & 0x0800)									
//					bits = (int16)(((data >> 10) & 0x1));			
//				else												
//					bits = (int16)(((data >> 10) & 0x1) | 0x02);	
//				trig  += (int32)(2 + (bits >> 1));					
//				start += (int32)(2);								
//				end   -= (int32)(4);								
//#endif
//				count = trig;
//				adjusted_depth =  end-start;
//				if (board_info.sample_bits == (int16)12) {
//					for (j = 0; j < board.depth; j++) {
//						a_buffer16[i][j] = (int16)gage_mem_read_chan_a (count);
//						b_buffer16[i][j] = (int16)gage_mem_read_chan_b (count);
//						if (count >= end)							
//							break;									
//						if (++count >= board_info.max_available_memory)
//							count = 0;
//					}
//				}
//				else {
//					for (j = 0; j < board.depth; j++) {
//						a_buffer[i][j] = (int8)gage_mem_read_chan_a (count);
//						b_buffer[i][j] = (int8)gage_mem_read_chan_b (count);
//						if (count >= end)						
//							break;								
//						if (++count >= board_info.max_available_memory)
//							count = 0;
//					}
//				}
//					if (!CheckMessageQueue ())
//					break;
//				Display(hWnd);
//				group++; 
//			}
//		}
//		gage_need_ram (FALSE);
//	}
//}

/*-------------------------------------------------------------------------*/

//void	TransferMRData_M (HWND hWnd)
//{
//	int32	i, count, group, trig, start, end;
//	int32 j;
//
//
//	for (i = 0; i < boards_in_system; i++) {
//		gage_select_board ((int16)(i + 1));   	 	
//		gage_multiple_record(use_multiple_record);
//		gage_need_ram (TRUE);
//
//		if (board.opmode == GAGE_SINGLE_CHAN) {
//			group = 1;
//			while (group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, board.opmode, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) {
//				count = trig;
//				for (j = 0; j < board.depth; j++) {
//					if (board_info.sample_bits == 12) {
//						a_buffer16[i][j] = (int16)gage_mem_read_single (count);
//					}
//					else {
//						a_buffer[i][j] = (int8)gage_mem_read_single (count);
//					}
//					if (++count >= board_info.max_available_memory)
//						count = 0;
//				}
//				if (!CheckMessageQueue ())
//					break;
//				Display (hWnd);
  //         		group++;
	//		}
	//		
	//	}else{
	//		group = 1;
	//		while (group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, board.opmode, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) {
	//			count = trig;
	//			for (j = 0; j < board.depth; j++) {
	//				if (board_info.sample_bits == 12) {
	//					a_buffer16[i][j] = (int16)gage_mem_read_chan_a (count);
	//					b_buffer16[i][j] = (int16)gage_mem_read_chan_b (count);
	//				}
	//				else {
	//					a_buffer[i][j] = (int8)gage_mem_read_chan_a (count);
	//					b_buffer[i][j] = (int8)gage_mem_read_chan_b (count);
	//				}
	//				if (++count >= board_info.max_available_memory)
	//					count = 0;
//				}
//				if (!CheckMessageQueue ())
//					break;
//				Display(hWnd);
//				group++; 
//			}
//		}
//		gage_need_ram (FALSE);
//	}
//}

//void	TransferMRData (HWND hWnd)
//{
//	if(AdjustFlag)
	  //TransferMRData_MRAdjust(hWnd);
//	{}
//	else
//	  TransferMRData_M (hWnd);
//}

/*-------------------------------------------------------------------------*/

void	TransferMRTBData (HWND hWnd)
{
	int32	group, start, end, trig;
	int16	boards;
	uInt32	transfer_depth;
	BOOL 	bQuit;
	MSG		msg;

	transfer_depth = board.depth;

	bQuit = FALSE;
	
	for (boards = 0; boards < boards_in_system; boards++) 
	{
   		gage_select_board ((int16)(boards + 1));   	 	
		gage_multiple_record(use_multiple_record);
		if (board.opmode == GAGE_SINGLE_CHAN) 
		{
			group = 1;
			while ((group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, GAGE_SINGLE_CHAN, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) && (!bQuit)) 
			{
				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
				{
					if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) || ((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) 
					{
						bQuit = TRUE;
						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
					}
					else if (msg.message == WM_CHAR)
						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
					else 
					{
						if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) 
						{
							TranslateMessage(&msg);
							DispatchMessage(&msg);
						}
					}
				}

				if( board_type == GAGE_ASSUME_CS8500)
				{
					gage_transfer_buffer(trig, GAGE_CHAN_A, (void *)a_buffer[boards], board.depth);
				}
				else
				{ 
					if ((board_info.sample_bits == 12 ) || (board_info.sample_bits == 16))
					{
						gage_transfer_buffer(trig, GAGE_CHAN_A, (void *)a_buffer16[boards], board.depth >> 1);
						gage_transfer_buffer(trig, GAGE_CHAN_B, (void *)b_buffer16[boards], board.depth >> 1);
					}
					else 
					{
						gage_transfer_buffer(trig, GAGE_CHAN_A, (void *)a_buffer[boards], board.depth);
						gage_transfer_buffer(trig, GAGE_CHAN_B, (void *)b_buffer[boards], board.depth);
					}
			
					{ 
						register i, j;
						if ((board_info.sample_bits == 12 )  || (board_info.sample_bits == 16))
						{
							for (i = transfer_depth - 1, j = (transfer_depth >> 1) - 1; j > 0; ) 
							{
								a_buffer16[boards][i--] = b_buffer16[boards][j];
								a_buffer16[boards][i--] = a_buffer16[boards][j--];
							}
							a_buffer16[boards][1] = b_buffer16[boards][0];
						} 
						else 
						{
							for (i = transfer_depth - 1, j = (transfer_depth >> 1) - 1; j > 0; ) 
							{
								a_buffer[boards][i--] = b_buffer[boards][j];
								a_buffer[boards][i--] = a_buffer[boards][j--];
							}
							a_buffer[boards][1] = b_buffer[boards][0];
						} 
					}
				}

				Display (hWnd);
				group++;
			} 
			
		}
		else
		{

			group = 1;
			while ((group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, GAGE_DUAL_CHAN, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) && (!bQuit)) 
			{
				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
				{
					if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) ||
						((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) 
					{
						bQuit = TRUE;
						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
					}else if (msg.message == WM_CHAR)
						SendMessage (GetParent(hWnd), WM_CHAR, 0, 0);
					else {
						if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) 
						{
							TranslateMessage(&msg);
							DispatchMessage(&msg);
						}
					}
				}
		
				if ((board_info.sample_bits == 12)  || (board_info.sample_bits == 16))
				{
					gage_transfer_buffer(trig, GAGE_CHAN_A,(void *)a_buffer16[boards], board.depth);
					gage_transfer_buffer(trig, GAGE_CHAN_B,(void *)b_buffer16[boards], board.depth);
				} 
				else {
					gage_transfer_buffer(trig, GAGE_CHAN_A,(void *)a_buffer[boards], board.depth);
					gage_transfer_buffer(trig, GAGE_CHAN_B,(void *)b_buffer[boards], board.depth);
				}
				Display(hWnd);
				group++; 
			}
		}
	}
}

/*---------------------------------------------------------------------------------------------*/

//void	TransferMRTVTData (HWND hWnd)
//{
//	int32	group, start, end, trig;
//	int16	boards;
//	uInt32	transfer_depth;
//	BOOL 	bQuit;
//	MSG		msg;
//
//	transfer_depth = board.depth;
//
//	bQuit = FALSE;
//	
//	for (boards = 0; boards < boards_in_system; boards++) {
//
//		gage_select_board ((int16)(boards + 1));   	 	
//		gage_multiple_record(use_multiple_record);
//		if (board.opmode == GAGE_SINGLE_CHAN) 
//		{
//
//			group = 1;
//			while ((group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, GAGE_SINGLE_CHAN, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) && (!bQuit)) 
//			{
//				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
//				{
//					if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) || ((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) 
//					{
//						bQuit = TRUE;
//						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
//					}
//					else if (msg.message == WM_CHAR)
//						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
//					else 
//					{
//						if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) 
//						{
//							TranslateMessage(&msg);
//							DispatchMessage(&msg);
//						}
//					}
//				}
//
//				if( board_type == GAGE_ASSUME_CS8500)
//				{
//					gage_trigger_view_transfer(GAGE_CHAN_A, (uInt8 *)a_buffer[boards], (uInt16)(board.depth ));
//				}
//				else
//				{ 
//					if ((board_info.sample_bits == 12 ) || (board_info.sample_bits == 16))
//					{
//						if (trigger_address[boards]&1)
//						{
//							gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)a_buffer[boards], (int16)(board.depth>>1));
//							gage_set_trigger_view_offset(1);
//							gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)b_buffer[boards], (int16)(board.depth>>1));
//							gage_set_trigger_view_offset(0);
//						}
//						else
//						{
//							gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)a_buffer[boards], (uInt16)(board.depth>>1));
//							gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)b_buffer[boards], (uInt16)(board.depth>>1));
//						}
//					}
//					else 
//					{
//						gage_trigger_view_transfer(GAGE_CHAN_A, (uInt8 *)a_buffer[boards], (uInt16)(board.depth));
//						gage_trigger_view_transfer(GAGE_CHAN_B, (uInt8 *)b_buffer[boards], (uInt16)(board.depth));
//					}
//			
//					{ 
//						register i, j;
//						if ((board_info.sample_bits == 12 ) || (board_info.sample_bits == 16))
//						{
//							for (i = transfer_depth - 1, j = (transfer_depth >> 1) - 1; j > 0; ) 
//							{
//								a_buffer16[boards][i--] = b_buffer16[boards][j];
//								a_buffer16[boards][i--] = a_buffer16[boards][j--];
//							}
//							a_buffer16[boards][1] = b_buffer16[boards][0];
//						} 
//						else 
//						{
//							for (i = transfer_depth - 1, j = (transfer_depth >> 1) - 1; j > 0; ) 
//							{
//								a_buffer[boards][i--] = b_buffer[boards][j];
//								a_buffer[boards][i--] = a_buffer[boards][j--];
//							}
//							a_buffer[boards][1] = b_buffer[boards][0];
//						} 
//					}
//				}
//
//				Display (hWnd);
//				group++;
//			} 
//			
//		}
//		else
//		{
//
//			group = 1;
//			while ((group == gage_calculate_mr_addresses(group, board_info.board_type, board.depth, board_info.max_available_memory, 0, GAGE_DUAL_CHAN, sample_rate_table[board.srindex].sr_calc, &trig, &start, &end)) && (!bQuit)) 
//			{
//				if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
//				{
//					if ((msg.message == WM_QUIT) || (msg.message == WM_CLOSE) ||
//						((msg.message == WM_SYSCOMMAND) && (msg.wParam == SC_CLOSE))) 
//					{
//						bQuit = TRUE;
//						PostMessage(msg.hwnd, msg.message, msg.wParam, msg.lParam);
//					}else if (msg.message == WM_CHAR)
//						SendMessage (GetParent(hWnd), WM_CHAR, 0, 0);
//					else {
//						if (!TranslateAccelerator(msg.hwnd, hAccel, &msg)) 
//						{
//							TranslateMessage(&msg);
//							DispatchMessage(&msg);
//						}
//					}
//				}
//		
//				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//				{
//					gage_trigger_view_transfer(GAGE_CHAN_A,(uInt8 *)a_buffer16[boards], (uInt16)(board.depth ));
//					gage_trigger_view_transfer(GAGE_CHAN_B,(uInt8 *)b_buffer16[boards], (uInt16)(board.depth ));
//				} 
//				else {
//					gage_trigger_view_transfer(GAGE_CHAN_A,(uInt8 *) a_buffer[boards], (uInt16)(board.depth));
//					gage_trigger_view_transfer(GAGE_CHAN_B,(uInt8 *)b_buffer[boards], (uInt16)(board.depth));
//				}
//				Display(hWnd);
//				group++; 
//			}
//		}
//	}
//}

/*-----transfer_type == USE_REAL_TIME----------------------------------------------------------------------------------------*/

//void	TransferRTData (HWND hWnd)
//{
//} //end TransferRTData()

//uInt32 times = 0;


/*-------transfer_type == USE_MEM_READ------------------------------------------------------------------*/

//void	TransferData (HWND hWnd)
//{
//	int32	i, count;
//	int32	j;
//	LARGE_INTEGER count1, count2, counter;
//	BOOL bool1;
/***********/
//	FILE		*stream1;
//	stream1 = fopen( "GAGE.DAT", "w+" );
/*		MessageBox(NULL, "IIII", "My Message", MB_OK);
/***********/
//
//	for (i = 0; i < boards_in_system; i++) 
//	{
//		gage_select_board ((int16)(i + 1));   	 	
//		gage_need_ram (TRUE);
//		if (board.opmode == GAGE_SINGLE_CHAN)
//		{
//
//			bool1 = QueryPerformanceFrequency(&counter);
//			bool1 = QueryPerformanceCounter(&count1);
//			count = trigger_address[i << 1];
//			for (j = 0; j < board.depth; j++) 
//			{
//				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//				{
//					a_buffer16[i][j] = (int16)gage_mem_read_single (count);
//
//					
/***didn't work***********/
//
//		fprintf( stream1, "\t%f\n", a_buffer16[i][j], "\n" );	
//		
/*			wsprintf (str, "a_buffer[i][j]= %d\n",a_buffer[i][j] );
//		MessageBox(NULL, str, "My Message", MB_OK);
//
/**************/				
//				}
//				else 
//				{
//					a_buffer[i][j] = (int8)gage_mem_read_single (count);
/****save to a_buffer[i][j]=128 **********/
//
//		fprintf( stream1, "\t%d\n", a_buffer[i][j]);			
/*
		wsprintf (str, "a_buffer[i][j]= %d\n",a_buffer[i][j] );
		MessageBox(NULL, str, "My Message", MB_OK);

/**************/
//				
//				}
//				if (++count >= board_info.max_available_memory)
//					count = 0;
//			} 
//			bool1 = QueryPerformanceCounter(&count2);
//			time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//		}
//		else
//		{
//			count = trigger_address[i << 1];
//			trigger_trace = count;
//
//			bool1 = QueryPerformanceFrequency(&counter);
//			bool1 = QueryPerformanceCounter(&count1);
//
//			for (j = 0; j < board.depth; j++) 
//			{
//				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//				{
//					a_buffer16[i][j] = (int16)gage_mem_read_chan_a (count);
//				}
//				else 
//				{
//					a_buffer[i][j] = (int8)gage_mem_read_chan_a (count);
//				}
//				if (++count >= board_info.max_available_memory)
//					count = 0;
//			}
//			count = trigger_address[(i << 1) + 1];
//			for (j = 0; j < board.depth; j++) 
//			{
//				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//				{
//					b_buffer16[i][j] = (int16)gage_mem_read_chan_b (count);
//				}
//				else 
//				{
//					b_buffer[i][j] = (int8)gage_mem_read_chan_b (count);
//				wsprintf (str, "b_buffer[i][j]= %d\n",b_buffer[i][j] );
//				}
//				if (++count >= board_info.max_available_memory)
//					count = 0;
//			}
//			bool1 = QueryPerformanceCounter(&count2);
//			time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//		}
//
/**************/				
//				fclose( stream1 );
/*wsprintf (str, "Acquisition complete! Data saved to file GAGE_A2D.DAT");
		MessageBox(NULL, str, "GAGE_A2D", MB_OK);
/**************/
//		
//		gage_need_ram (FALSE);
//
//	    Display (hWnd); 
//	}
//} // end TransferData()

/*-------------transfer_type == USE_TRIGGER_VIEW------------------------------------------------------------*/

//void TransferTVTData (HWND hWnd)
//{
//	int16 boards;
//	int32 offset = 0, ta=0;
//    uInt32 depth;
//	LARGE_INTEGER count1, count2, counter;
//	BOOL bool1;
//
//	depth = board.depth;
//
/* wsprintf (str, "depth,boards,boards_in_system01 = %d,%d,%d\n",depth,boards,boards_in_system );
//				MessageBox(NULL, str, "My Message", MB_OK);   */
//
//	for (boards = 0; boards < boards_in_system; boards++)
//	{
//		gage_select_board ((int16)(boards + 1));   	 	
//		gage_set_trigger_view_offset (0);
//	    if (board.opmode == GAGE_SINGLE_CHAN)
//		{
//			if(board_type == GAGE_ASSUME_CS8500)
//			{
//				bool1 = QueryPerformanceFrequency(&counter);
//				bool1 = QueryPerformanceCounter(&count1);
//				gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 *)a_buffer[boards],(int16)(board.depth));
//				bool1 = QueryPerformanceCounter(&count2);
//				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//			}
//			else if (board_type == GAGE_ASSUME_CS3200)
//			{
//				bool1 = QueryPerformanceFrequency(&counter);
//				bool1 = QueryPerformanceCounter(&count1);
//				gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)a_buffer[boards],(int16)(board.depth));
//				bool1 = QueryPerformanceCounter(&count2);
//				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//			}
//			else
//			{
//				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//				{
//					bool1 = QueryPerformanceFrequency(&counter);
//					bool1 = QueryPerformanceCounter(&count1);
//					ta=trigger_address[boards];
//					if (ta&1)
//					{
//						
//						gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)a_buffer[boards], (int16)(board.depth>>1));
//						gage_set_trigger_view_offset(1);
//						gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)b_buffer[boards], (int16)(board.depth>>1));
//						gage_set_trigger_view_offset(0);
//					}
//					else
//					{
//						gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)a_buffer[boards], (uInt16)(board.depth>>1));
//						gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)b_buffer[boards], (uInt16)(board.depth>>1));
//					}
//					bool1 = QueryPerformanceCounter(&count2);
//					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//				}
//				else
//				{
//					gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)a_buffer[boards], (uInt16)(board.depth >> 1));
//					gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)b_buffer[boards], (uInt16)(board.depth >> 1));
//				}
//
//				{
//					register i, j;
//
//					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//					{
//						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; j--)
//						{
//							a_buffer16[boards][i--] = b_buffer16[boards][j];
//							a_buffer16[boards][i--] = a_buffer16[boards][j];
//						}
//						a_buffer16[boards][1] = b_buffer16[boards][0];
//					}
//					else
//					{
//						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; )
//						{
//							a_buffer[boards][i--] = b_buffer[boards][j];
//							a_buffer[boards][i--] = a_buffer[boards][j--];
//						}
//						a_buffer[boards][1] = b_buffer[boards][0];
//					}
//				}
//			}
//		}
//		else
//		{ 
//			if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
//			{
//				bool1 = QueryPerformanceFrequency(&counter);
//				bool1 = QueryPerformanceCounter(&count1);
//				gage_trigger_view_transfer ((int16)GAGE_CHAN_A, (uInt8 far *)(a_buffer16[boards] + offset), (int16)(board.depth));
//				gage_trigger_view_transfer ((int16)GAGE_CHAN_B, (uInt8 far *)(b_buffer16[boards] + offset), (int16)(board.depth));
//				bool1 = QueryPerformanceCounter(&count2);
//				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//			}
//			else if (board_type == GAGE_ASSUME_CS3200)
//			{
//				bool1 = QueryPerformanceFrequency(&counter);
//				bool1 = QueryPerformanceCounter(&count1);
//				gage_trigger_view_transfer((int16)GAGE_CHAN_A, (uInt8 far *)a_buffer[boards],(int16)(board.depth));
//				gage_trigger_view_transfer((int16)GAGE_CHAN_B, (uInt8 far *)a_buffer[boards],(int16)(board.depth));
//				bool1 = QueryPerformanceCounter(&count2);
//				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
//			}
//			else
//			{
//				gage_trigger_view_transfer ((int16)GAGE_CHAN_A, (uInt8 far *)(a_buffer[boards] + offset), (int16)(board.depth));
//				gage_trigger_view_transfer ((int16)GAGE_CHAN_B, (uInt8 far *)(b_buffer[boards] + offset), (int16)(board.depth));
//			}
//		}
//	}
//
/*	Display (hWnd);*/
//	Display (hWnd);
//} // end TransferTVTData()

/*-------------transfer_type == USE_TRIGGER_VIEW_2------------------------------------------------------------*/

void	TransferTVT2Data (HWND hWnd)
{
	int16	boards;
	int32	offset = 0;
    uInt32	depth;
	LARGE_INTEGER count1, count2, counter;
	BOOL bool1;

	depth = board.depth;
	for (boards = 0; boards < boards_in_system; boards++)
	{
		gage_select_board ((int16)(boards + 1));   	 	
		gage_set_trigger_view_offset (0);
		if (board.opmode == GAGE_SINGLE_CHAN)
		{
			if((board_type == GAGE_ASSUME_CS8500) || (board_type == GAGE_ASSUME_CS3200) )
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT2_ptr[boards] = gage_trigger_view_transfer_2(GAGE_CHAN_A, (void *)(a_buffer[boards]), board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{ 
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					bool1 = QueryPerformanceFrequency(&counter);
					bool1 = QueryPerformanceCounter(&count1);
					if (trigger_address[boards]&1)
					{
						TVT2_ptr[boards] =(uInt16*)gage_trigger_view_transfer_2(GAGE_CHAN_B, (void *)(a_buffer16[boards]+ offset), board.depth>>1);
						gage_set_trigger_view_offset (1);
						TVT2_ptrB[boards]=(uInt16*)gage_trigger_view_transfer_2(GAGE_CHAN_A, (void *)(b_buffer16[boards]+ offset), board.depth>>1);
						gage_set_trigger_view_offset (0);

					}
					else
					{
						TVT2_ptr[boards] =(uInt16*)gage_trigger_view_transfer_2(GAGE_CHAN_A, (void *)(a_buffer16[boards]+ offset), board.depth>>1);
						TVT2_ptrB[boards] =(uInt16*)gage_trigger_view_transfer_2(GAGE_CHAN_B, (void *)(b_buffer16[boards]+ offset), board.depth>>1);
					}
					bool1 = QueryPerformanceCounter(&count2);
					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				}
				else
				{
					gage_trigger_view_transfer_2(GAGE_CHAN_A, (void *)(a_buffer[boards]+ offset), board.depth>>1);
					gage_trigger_view_transfer_2(GAGE_CHAN_B, (void *)(b_buffer[boards]+ offset), board.depth>>1);
				}

				{
					register i, j;
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; j--)
						{
							a_buffer16[boards][i--] = TVT2_ptrB[boards][j];
							a_buffer16[boards][i--] = TVT2_ptr[boards][j];
						}
						a_buffer16[boards][1] = TVT2_ptrB[boards][0];
					}
					else
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; )
						{
							a_buffer[boards][i--] = b_buffer[boards][j];
							a_buffer[boards][i--] = a_buffer[boards][j--];
						}
						a_buffer[boards][1] = b_buffer[boards][0];
					}
				}
			}
		}
		else
		{
			if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT2_ptr[boards] =(uInt16*)gage_trigger_view_transfer_2 (GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), board.depth);
				TVT2_ptrB[boards]=(uInt16*)gage_trigger_view_transfer_2 (GAGE_CHAN_B, (void far *)(b_buffer16[boards]+ offset), board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{
				gage_trigger_view_transfer_2 (GAGE_CHAN_A, (void far *)(a_buffer[boards]+ offset), board.depth>>1);
				gage_trigger_view_transfer_2 (GAGE_CHAN_B, (void far *)(b_buffer[boards]+ offset), board.depth>>1);
			}
		}
	}

	Display (hWnd);

} // end TransferTVT2Data()

/*------transfer_type == USE_TRIGGER_VIEW_3-------------------------------------------------------------------*/

void	TransferTVT3Data (HWND hWnd)
{
	int16	boards;
	int32	offset = 0;
    uInt32	depth;
	LARGE_INTEGER count1, count2, counter;
	BOOL bool1;

	depth = board.depth;
	for (boards = 0; boards < boards_in_system; boards++)
	{
		gage_select_board ((int16)(boards + 1));   	 	
		gage_set_trigger_view_offset (0);
		if (board.opmode == GAGE_SINGLE_CHAN)
		{
			if((board_type == GAGE_ASSUME_CS8500) || (board_type == GAGE_ASSUME_CS3200) )
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);

				TVT3_offset[boards]= gage_trigger_view_transfer_3((int16)(GAGE_CHAN_A+boards), (void far *)TVTBufferPtr[boards], (int32)board.depth);

				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{ 
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					bool1 = QueryPerformanceFrequency(&counter);
					bool1 = QueryPerformanceCounter(&count1);
					TVT3_offset[boards]=gage_trigger_view_transfer_3((int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), (int32)board.depth);
					bool1 = QueryPerformanceCounter(&count2);
					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				}
				else
				{
					TVT3_offset[boards]=gage_trigger_view_transfer_3((int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), (int32)board.depth);
				}
			}
		}
		else
		{
			if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT3_offset[boards]=gage_trigger_view_transfer_3 ((int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), (int32)board.depth);
				TVT3_offsetB[boards]=gage_trigger_view_transfer_3 ((int16)GAGE_CHAN_B, (void far *)(b_buffer16[boards]+ offset), (int32)board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{
				gage_trigger_view_transfer_3 ((int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), (int32)board.depth>>1);
				gage_trigger_view_transfer_3 ((int16)GAGE_CHAN_B, (void far *)(b_buffer16[boards]+ offset), (int32)board.depth>>1);
			}
		}
	}

	Display (hWnd);

} // end TransferTVT3Data()


/*----NO 1-----transfer_type == USE_TRANSFER_BUFFER (Slave Mode)------------------------------------------------------------------------------------*/

void TransferTBData (HWND hWnd)
{
	int16 boards;
	int32 offset = 0, ta=0;
    uInt32 depth;
	LARGE_INTEGER count1, count2, counter;
	BOOL bool1;
	


	depth = board.depth;
	for (boards = 0; boards < boards_in_system; boards++)
	{
		gage_select_board ((int16)(boards + 1));   	 	
	    if (board.opmode == GAGE_SINGLE_CHAN)
		{
/********************************************************/
			if(board_type == GAGE_ASSUME_CS8500)
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
/*The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. */
/*The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists. */
				
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_A, (void *)a_buffer[boards],board.depth);
/***** No reture value ******/			
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);

			}

			else if (board_type == GAGE_ASSUME_CS3200)
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);

			
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_A, (void *)a_buffer[boards],board.depth);

				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}

			else
			{
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					bool1 = QueryPerformanceFrequency(&counter);
					bool1 = QueryPerformanceCounter(&count1);
					ta=trigger_address[boards];
					if (ta&1)
					{
						gage_transfer_buffer(ta,GAGE_CHAN_B,(void *)a_buffer16[boards], board.depth>>1);
						gage_transfer_buffer(ta+1,GAGE_CHAN_A, (void *)b_buffer16[boards], board.depth>>1);
					}
					else
					{
						gage_transfer_buffer(ta,GAGE_CHAN_A, (void *)a_buffer16[boards], board.depth>>1);
						gage_transfer_buffer(ta,GAGE_CHAN_B, (void *)b_buffer16[boards], board.depth>>1);
					}
					bool1 = QueryPerformanceCounter(&count2);
					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				}
				else
				{
					gage_transfer_buffer(ta,GAGE_CHAN_A, (void *)a_buffer16[boards], board.depth>>1);
					gage_transfer_buffer(ta,GAGE_CHAN_B, (void *)b_buffer16[boards], board.depth>>1);
				}

				{
					register i, j;

					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; j--)
						{
							a_buffer16[boards][i--] = b_buffer16[boards][j];
							a_buffer16[boards][i--] = a_buffer16[boards][j];
						}
						a_buffer16[boards][1] = b_buffer16[boards][0];
					}
					else
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; )
						{
							a_buffer[boards][i--] = b_buffer[boards][j];
							a_buffer[boards][i--] = a_buffer[boards][j--];
						}
						a_buffer[boards][1] = b_buffer[boards][0];
					}
				}
			}
		}
		else
		{ 
			if (board_type == GAGE_ASSUME_CS3200)
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_A, (void far *)a_buffer[boards],board.depth);
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_B, (void far *)a_buffer[boards],board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_A, (void far *)(a_buffer16[boards] + offset), board.depth);
				gage_transfer_buffer(trigger_address[boards],GAGE_CHAN_B, (void far *)(b_buffer16[boards] + offset), board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{
				gage_transfer_buffer (trigger_address[boards],GAGE_CHAN_A, (void far *)(a_buffer[boards] + offset), board.depth);
				gage_transfer_buffer (trigger_address[boards],GAGE_CHAN_B, (void far *)(b_buffer[boards] + offset), board.depth);
			}
		}
	}

	Display (hWnd);

} // end TransferTBData()

/*-----transfer_type == USE_TRANSFER_BUFFER_2--------------------------------------------------------------------*/

void	TransferTB2Data (HWND hWnd)
{
	int16	boards;
	int i;
	int32	offset = 0;
    uInt32	depth;
	LARGE_INTEGER count1, count2, counter;
	BOOL bool1;
	FILE *stream;
	location = gage_trigger_address;   
	points = board.depth;


	/* Using gage_transfer_buffer_2 Poutines: used to copy nsamples point 
	from the specified channel to the supplied buffer beginning from address ta. **/

	depth = board.depth;
	for (boards = 0; boards < boards_in_system; boards++)
	{
		gage_select_board ((int16)(boards + 1));   	 	
		gage_set_trigger_view_offset (0);
		if (board.opmode == GAGE_SINGLE_CHAN)
		{
			if((board_type == GAGE_ASSUME_CS8500) || (board_type == GAGE_ASSUME_CS3200) )
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
/*The QueryPerformanceFrequency function retrieves the frequency of the high-resolution performance counter, if one exists. */
/*The QueryPerformanceCounter function retrieves the current value of the high-resolution performance counter, if one exists. */

				TVT2_ptr[boards] = gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_A, (void *)(a_buffer[boards]), board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
                	
/************my save data try*********
	/* 8 bits board */
	if(board_info.sample_bits == 8)
	{
		if((chan_a_8 = (uInt8 *) malloc(board.depth + 8)) == NULL)
		{
			wsprintf (str, "   Malloc Failed!   ");
			MessageBox(NULL, str, "Error", MB_OK);
			exit(1);
		}
		else
			/* Transfer Chan A 8 bits board single mode */
			offset_a = gage_transfer_buffer_3(location,GAGE_CHAN_A,chan_a_8, points);
	}
stream = fopen( "new.dat", "w+" );
for (i = 0; i < points; i++)  
			fprintf(stream, "\t%d\n", (255 - chan_a_8[offset_a + i])); 
		
		fclose( stream );

	if(chan_a_8 != NULL)
		free(chan_a_8);
/********************/
			}
			else
			{ 
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					bool1 = QueryPerformanceFrequency(&counter);
					bool1 = QueryPerformanceCounter(&count1);
					if (trigger_address[boards]&1)
					{
						TVT2_ptr[boards] =(uInt16*)gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_B, (void *)(a_buffer16[boards]+ offset), board.depth>>1);
						TVT2_ptrB[boards]=(uInt16*)gage_transfer_buffer_2((trigger_address[boards]+1),GAGE_CHAN_A, (void *)(b_buffer16[boards]+ offset), board.depth>>1);
					}
					else
					{
						TVT2_ptr[boards] =(uInt16*)gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_A, (void *)(a_buffer16[boards]+ offset), board.depth>>1);
						TVT2_ptrB[boards] =(uInt16*)gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_B, (void *)(b_buffer16[boards]+ offset), board.depth>>1);
					}
					bool1 = QueryPerformanceCounter(&count2);
					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				}
				else
				{
					gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_A, (void *)(a_buffer[boards]+ offset), board.depth>>1);
					gage_transfer_buffer_2(trigger_address[boards],GAGE_CHAN_B, (void *)(b_buffer[boards]+ offset), board.depth>>1);
				}

				{
					register i, j;
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; j--)
						{
							a_buffer16[boards][i--] = TVT2_ptrB[boards][j];
							a_buffer16[boards][i--] = TVT2_ptr[boards][j];
						}
						a_buffer16[boards][1] = TVT2_ptrB[boards][0];
					}
					else
					{
						for (i = board.depth - 1, j = (board.depth >> 1) - 1; j > 0; )
						{
							a_buffer[boards][i--] = b_buffer[boards][j];
							a_buffer[boards][i--] = a_buffer[boards][j--];
						}
						a_buffer[boards][1] = b_buffer[boards][0];
					}
				}
			}
		}
		else
		{
			if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT2_ptr[boards] =(uInt16*)gage_transfer_buffer_2 (trigger_address[boards],GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), board.depth);
				TVT2_ptrB[boards]=(uInt16*)gage_transfer_buffer_2 (trigger_address[boards],GAGE_CHAN_B, (void far *)(b_buffer16[boards]+ offset), board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{
				gage_transfer_buffer_2 (trigger_address[boards],GAGE_CHAN_A, (void far *)(a_buffer[boards]+ offset), board.depth>>1);
				gage_transfer_buffer_2 (trigger_address[boards],GAGE_CHAN_B, (void far *)(b_buffer[boards]+ offset), board.depth>>1);
			}
		}
	}

	Display (hWnd);

} // end TransferTB2Data()

/*------transfer_type == USE_TRANSFER_BUFFER_3-------------------------------------------------------------------*/

void	TransferTB3Data (HWND hWnd)
{
	int16	boards;
	int i;
	int32	offset = 0;
    uInt32	depth;
	LARGE_INTEGER count1, count2, counter;
	BOOL bool1;

	location = gage_trigger_address;   
	points = board.depth;
	depth = board.depth;

	for (boards = 0; boards < boards_in_system; boards++)
	{
		gage_select_board ((int16)(boards + 1));   	 	
		if (board.opmode == GAGE_SINGLE_CHAN)
		{
			if((board_type == GAGE_ASSUME_CS8500) || (board_type == GAGE_ASSUME_CS3200) )
			{	
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT3_offset[boards]= gage_transfer_buffer_3(trigger_address[boards],(int16)(GAGE_CHAN_A+boards), (void *)TVTBufferPtr[boards], (int32)board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				/************my save data try*********
				/* 8 bits board */
				if(board_info.sample_bits == 8)
				{
					if((chan_a_8 = (uInt8 *) malloc(board.depth + 8)) == NULL)
					{
						wsprintf (str, "   Malloc Failed!   ");
						MessageBox(NULL, str, "Error", MB_OK);
						exit(1);
					}
					else
						/* Transfer Chan A 8 bits board single mode */
						offset_a = gage_transfer_buffer_3(location,GAGE_CHAN_A,chan_a_8, points);
				}
				for (i = 0; i < points; i++)  
					fprintf(stream, "\t%d\n", (255 - chan_a_8[offset_a + i])); 
				if(chan_a_8 != NULL)
					free(chan_a_8);	
				/********************/	
			}
			else
			{ 
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					bool1 = QueryPerformanceFrequency(&counter);
					bool1 = QueryPerformanceCounter(&count1);
					TVT3_offset[boards]=gage_transfer_buffer_3(trigger_address[boards],(int16)GAGE_CHAN_A, (void *)(a_buffer16[boards]+ offset), (int32)board.depth);
					bool1 = QueryPerformanceCounter(&count2);
					time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
				}
				else
				{
					TVT3_offset[boards]=gage_transfer_buffer_3(trigger_address[boards],(int16)GAGE_CHAN_A, (void *)(a_buffer16[boards]+ offset), (int32)board.depth);
				}
			}
		}
		else
		{
			if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
			{
				bool1 = QueryPerformanceFrequency(&counter);
				bool1 = QueryPerformanceCounter(&count1);
				TVT3_offset[boards]=gage_transfer_buffer_3(trigger_address[boards],(int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+offset), (int32)board.depth);
				TVT3_offsetB[boards]=gage_transfer_buffer_3(trigger_address[boards],(int16)GAGE_CHAN_B, (void far *)(b_buffer16[boards]+offset), (int32)board.depth);
				bool1 = QueryPerformanceCounter(&count2);
				time3 = (count2.LowPart - count1.LowPart)/((counter.LowPart)/1000);
			}
			else
			{
				gage_transfer_buffer_3 (trigger_address[boards],(int16)GAGE_CHAN_A, (void far *)(a_buffer16[boards]+ offset), (int32)board.depth>>1);
				gage_transfer_buffer_3 (trigger_address[boards],(int16)GAGE_CHAN_B, (void far *)(b_buffer16[boards]+ offset), (int32)board.depth>>1);
			}
		}
	}	
	fclose( stream );
	Display (hWnd);
} /* TransferTB3Data()

/*-------------AcquireCheck------------------------------------------------------------*/


void	AcquireCheck (HWND hWnd)
{   
	int16			bd;
	int i;
	static int32	triggered = 0;
//	char tmpbuf1[128];
//	char tmpbuf2[128];
	
//  public boolean mkdir ();
  
//	char filename[20];
//	char filepath[30];

	if (!CapturingData)
		return;
    if (!gage_triggered ())  
		{
		if (number_of_timer_events > TRIG_TIMEOUT)  
			{
			gage_forced_trigger_capture (board.source);
			number_of_timer_events = 0;
			}
		
		return;
		}
		else if (!triggered)
			{
  //			wsprintf (str, "triggered = %d\n",triggered );
  //			MessageBox(NULL, str, "My Message", MB_OK);  
				triggered = 1;
				number_of_timer_events = 0;
			}
	for (bd = 1; bd <= boards_in_system; bd++)
		{
		gage_select_board (bd);
		while (gage_busy ()) 
			{
		   	number_of_timer_events++;
			}
		}

	number_of_timer_events = 0;
    triggered = 0;
	gage_multiple_record(use_multiple_record);
/********************
	    wsprintf (str, "%s not found!", (LPSTR)board_loc_file);
		wsprintf (str, "%s transfer_type", (LPSTR)(transfer_type));
				MessageBox(NULL, str, "My Message", MB_OK);   
/********************/
	if (use_multiple_record)
	{
//		if (transfer_type == USE_TRIGGER_VIEW)
//		{
//			TransferMRTVTData (hWnd);
//		}
//		else if(transfer_type == USE_TRIGGER_VIEW_2)
//		{
//			MessageBox(hWnd, "Trigger_View_Transfer_2 Method Not Available Yet,\nTrigger_View_Transfer Will Be Used ","Note", MB_OK);
//		    TransferMRTVTData (hWnd);
//		}
//		else if(transfer_type == USE_TRIGGER_VIEW_3)
//		{
//			MessageBox(hWnd, "Trigger_View_Transfer_3 Method Not Available Yet,\nTrigger_View_Transfer Will Be Used","Note", MB_OK);
//		    TransferMRTVTData (hWnd);
//		}
//		else if (transfer_type == USE_TRANSFER_BUFFER)
//		{
//			TransferMRTBData (hWnd);
//		}
//		else if (transfer_type == USE_TRANSFER_BUFFER_2)
//		{
//			MessageBox(hWnd, "Trigger_Transfer_Buffer_2 Method Not Available Yet,\nTrigger_Transfer_Buffer Will Be Used","Note", MB_OK);
//			TransferMRTBData (hWnd);
//		}
//		else 
		if (transfer_type == USE_TRANSFER_BUFFER_3)
		{
			MessageBox(hWnd, "Trigger_Transfer_Buffer_3 Method Not Available Yet,\nTrigger_Transfer_Buffer Will Be Used","Note", MB_OK);
			TransferMRTBData (hWnd);
		}
//		else if(transfer_type == USE_MEM_READ)
//		{
//		    TransferMRData (hWnd);
//		}
//		else if(transfer_type == USE_REAL_TIME)
//		{
//		    TransferRTData(hWnd);
//		}
	}
	else
	{
		InitMemPtrs ();
	//	if (transfer_type == USE_TRIGGER_VIEW)
	//	{
	//		TransferTVTData (hWnd);
	//	}
	//	else 
		if (transfer_type == USE_TRIGGER_VIEW_2)
		{
			TransferTVT2Data (hWnd);
		}
		else if (transfer_type == USE_TRIGGER_VIEW_3)
		{
			TransferTVT3Data (hWnd);
		}
		else if (transfer_type == USE_TRANSFER_BUFFER)
		{
			TransferTBData (hWnd);
		}
		else if (transfer_type == USE_TRANSFER_BUFFER_2)
		{
			TransferTB2Data (hWnd);
		}
		else if (transfer_type == USE_TRANSFER_BUFFER_3)
		{
			if (ContinuousMode)
			{
				if ( k == 1 ) /* k=1 CONTINUOUS MODE*/
				{
					filename[0]=0;
					_mkdir(".\\dat");
					strcpy(filepath, ".\\dat\\");
					itoa( j, filename, 10);
					strcat(filename, ".dat"); 
					strcat(filepath, filename);
					stream = fopen(filepath, "w+" );
					save_data(stream);
					j++; 
					TransferTB3Data (hWnd);
				}								
				else if ( j < nxx ) /* k=0 */
				{
					filename[0]=0;
					_mkdir(".\\dat");
					strcpy(filepath, ".\\dat\\");
					itoa( j, filename, 10);
					strcat(filename, ".dat"); 
					strcat(filepath, filename);
					stream = fopen(filepath, "w+" );
					j++; 
					save_data(stream);
					TransferTB3Data (hWnd);
				    _tzset();
					_strtime( tmpbuf1 );
					_strdate( tmpbuf2 );
					directoryname[0] = tmpbuf2[0];
					directoryname[1] = tmpbuf2[1];
					directoryname[2] = tmpbuf2[3];
					directoryname[3] = tmpbuf2[4];
					directoryname[4] = tmpbuf2[6];
					directoryname[5] = tmpbuf2[7];
					directoryname[6] = tmpbuf1[0];
					directoryname[7] = tmpbuf1[1];
					directoryname[8] = tmpbuf1[3];
					directoryname[9] = tmpbuf1[4];
					directoryname[10] = tmpbuf1[6];
					directoryname[11] = tmpbuf1[7];
					directoryname[12]='\0';
					if ( j == nxx )
					{ 
						_mkdir(directoryname);
  						sprintf(line, "copy dat\\*.* %s", directoryname);	
						system(line);
						sprintf(linezip, "pkzip %s.zip %s\\*.*", directoryname, directoryname);	
						system(linezip);
						system("del .\\dat\\*.*");	
					}
				}
			}
		}
/****************************/			
		//else if (transfer_type == USE_MEM_READ)
		//{
		//	TransferData (hWnd);
		//}
		//else if (transfer_type == USE_REAL_TIME)
		//{
		//	TransferRTData(hWnd);
		//}
	}
    if (ContinuousMode)  {
		prepare_for_capture(boards_in_system, &board);
		AcquireStart ();
    }else
		CapturingData = FALSE;
}  /*      End of AcquireCheck ().  */

/*-------------------------------------------------------------------------*/

void	Abort (HWND hWnd)
{
	if (ContinuousMode)  
	{

		ContinuousMode = FALSE;

		if(stream != NULL)
			fclose( stream );
		else
			MessageBox(hWnd,"NO FILE OPEN CANT CLOSE","FUNCTION Abort",1);
	}
	else
	{
		if (!gage_triggered ())  
		{
			gage_forced_trigger_capture (board.source);
		}
		gage_abort_capture (board.source);
        if (!gage_triggered ())  
		{
			MessageBox(hWnd, "CompuScope hardware still reports it has not triggered!","Error", MB_OK);
	    }
	    if (gage_busy ())  
		{
			MessageBox(hWnd, "CompuScope hardware still reports it is busy!","Error", MB_OK);
	    }
	    if (!gage_triggered () || gage_busy ())  
		{
			CapturingData = FALSE;
			ContinuousMode = FALSE;
	    }
	}

}       /*      End of Abort ().  */

/*-------------------------------------------------------------------------*/





/*-------------------------------------------------------------------------*/

void	Display (HWND hWnd)

{
	InvalidateRect(hWnd, NULL, FALSE);
	UpdateWindow(hWnd);

}       /*  End of Display ().  */

/*-------------------------------------------------------------------------*/


int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow)

{
 /***********************************************************************/
 /* HANDLE hInstance;       handle for this instance                    */
 /* HANDLE hPrevInstance;   handle for possible previous instances      */
 /* LPSTR  lpszCmdLine;     long pointer to exec command line           */
 /* int32    nCmdShow;        Show code for main window display           */
 /***********************************************************************/

 	MSG        msg;             /* MSG structure to store your messages        */
 	int32        nRc;           /* return value from Register Classes          */
 	int32        ret;               /* return value from InitBoard.  */

    lpszCmdLine = lpszCmdLine;
 	strcpy(szAppName, "GSDLLDEM");
 	hInst = hInstance;

	//Compare the flag ...
	if(	!strcmp(lpszCmdLine,"mulrec_adjust_1012_16M"))
		AdjustFlag=1;
	
	if(!hPrevInstance) {
   	    /* register window classes if first instance of application         */
    	    if ((nRc = nCwRegisterClasses()) == -1) {
      	    /* registering one of the windows failed		*/
       		CwUnRegisterClasses();                         
       		LoadString(hInst, IDS_ERR_REGISTER_CLASS, szString, sizeof(szString));
       		MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
       		return nRc;
      	    }
   	}

 /* create application's Main window                                    */
 	hWndMain = CreateWindow(
		szAppName,               /* Window class name           */
		"SPLAT VER 0.1",         /* Window's title              */
		WS_CAPTION      |        /* Title and Min/Max           */
		WS_SYSMENU      |        /* Add system menu box         */
		WS_MINIMIZEBOX  |        /* Add minimize box            */
		WS_MAXIMIZEBOX  |        /* Add maximize box            */
		WS_THICKFRAME   |        /* thick sizeable frame        */
		WS_CLIPCHILDREN |        /* don't draw in child windows areas */
		WS_OVERLAPPED,
		CW_USEDEFAULT,           /* Use default X, Y            */
		CW_USEDEFAULT,           /* Use default X, Y            */
		600,                     /* Force the size of X         */
		400,                     /* Force the size of Y         */
		NULL,                    /* Parent window's handle      */
		NULL,                    /* Default to Class Menu       */
		hInst,                   /* Instance of window          */
		NULL);                   /* Create struct for WM_CREATE */

 	if(hWndMain == NULL) {
   	   	LoadString(hInst, IDS_ERR_CREATE_WINDOW, szString, sizeof(szString));
    	MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
    	return IDS_ERR_CREATE_WINDOW;
   	}
	
 	if ((ret = InitBoard (hWndMain)) != 0)
		return (FALSE);
	splatisa();
	if (bio == 0)
	{
		MessageBox(NULL, "SPLAT NOT FOUND", "ERROR", MB_OK);
		return (FALSE);
	}
	hAccel = LoadAccelerators(hInst, szAppName);
 	ShowWindow(hWndMain, nCmdShow);            /* display main window      */	
    while(GetMessage(&msg, NULL, 0, 0)) {       /* Until WM_QUIT message    */

		AcquireCheck (hDisplayWnd);
    	if (!TranslateAccelerator(hWndMain, hAccel, &msg))
		if (!IsDialogMessage(SRateDlg, &msg))
		if (!IsDialogMessage(IRangeDlg, &msg))
		if (!IsDialogMessage(PmtDlg, &msg))
		if (!IsDialogMessage(TriggerDlg, &msg))
		{
      		TranslateMessage(&msg);
    		DispatchMessage(&msg);
		}
   	    SendMessage(hWndMain,&msg, 0, 0);
	}
	
	gage_driver_remove();
 	/* Do clean up before exiting from the application                     */
 	return msg.wParam;
} /*  End of WinMain                                                    */

/*-------------------------------------------------------------------------*/

LRESULT CALLBACK SRateDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static	boarddef temp_board;
	int32		i, j;
	DWORD	nIndex;

	switch (msg) {

		case WM_INITDIALOG:

			memcpy (&temp_board, &board, sizeof(board));

			if (temp_board.opmode == GAGE_DUAL_CHAN)
			{
				SendDlgItemMessage (hWnd, IDC_DUAL, BM_SETCHECK, BST_CHECKED, 0);
				SendDlgItemMessage (hWnd, IDC_SINGLE, BM_SETCHECK, BST_UNCHECKED, 0);
			}
			else
			{
				SendDlgItemMessage (hWnd, IDC_SINGLE, BM_SETCHECK, BST_CHECKED, 0);
				if (board_type == GAGE_ASSUME_CS8500)
				{
					EnableWindow(GetDlgItem(hWnd, IDC_DUAL),FALSE);
				}
				else
				{
					SendDlgItemMessage (hWnd, IDC_DUAL, BM_SETCHECK, BST_UNCHECKED, 0);
				}
			}

			for (i = 0; i < SRTI_EXTERNAL; i++) {
				if (temp_board.opmode == GAGE_DUAL_CHAN) {	
					if ((sample_rate_table[i].sr_flag & B_T_DUAL) & current_board_type) {
						nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_ADDSTRING, 0, (LPARAM)((LPCTSTR)(sample_rate_table[i].sr_text)));
						SendDlgItemMessage (hWnd, IDC_SRATE, CB_SETITEMDATA, nIndex, (LPARAM)i);
						j = i; 
					}
				}
				else {
					if ((sample_rate_table[i].sr_flag & B_T_SINGLE) & current_board_type) {
						nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_ADDSTRING, 0, (LPARAM)((LPCTSTR)(sample_rate_table[i].sr_text)));
						SendDlgItemMessage (hWnd, IDC_SRATE, CB_SETITEMDATA, nIndex, (LPARAM)i);
						j = i;
					}
				}				
			}

			SendDlgItemMessage (hWnd, IDC_MULREC, BM_SETCHECK, (WPARAM)use_multiple_record, 0);
			SendDlgItemMessage (hWnd, IDC_EXTCLK, BM_SETCHECK, (WPARAM)external_clock_in_use, 0);
			
			if(external_clock_in_use){
				EnableWindow(GetDlgItem(hWnd, IDC_EXT_CLK_VALUE),TRUE);
				SetDlgItemInt(	hWnd,	// handle of dialog box
								IDC_EXT_CLK_VALUE,	// identifier of control
								(uInt32)external_clock_rate,	// value to set
								FALSE 	// signed or unsigned indicator
				);
		   	}else
			   EnableWindow(GetDlgItem(hWnd, IDC_EXT_CLK_VALUE),FALSE);
			
			if(external_clock_in_use)		
				nIndex = SRTI_EXTERNAL;
			else   
				nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_FINDSTRINGEXACT, (WPARAM)0, (LPARAM)((LPCTSTR)(sample_rate_table[board.srindex].sr_text)));

			SendDlgItemMessage (hWnd, IDC_SRATE, CB_SETCURSEL, (WPARAM)nIndex, 0);

			return (TRUE);

				
		case WM_COMMAND:

			switch (LOWORD(wParam)) {

				case IDC_SR_APPLY:
					{
					nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_GETCURSEL, 0, 0);
					temp_board.srindex =  (int16)(SendDlgItemMessage (hWnd, IDC_SRATE, CB_GETITEMDATA, (WPARAM)nIndex, 0));
					use_multiple_record = (int16)SendDlgItemMessage (hWnd, IDC_MULREC,BM_GETCHECK, 0, 0);
					external_clock_in_use = (int16)SendDlgItemMessage (hWnd, IDC_EXTCLK,BM_GETCHECK, 0, 0);
					if (!multiple_record_avail && use_multiple_record)
					{
						MessageBox(NULL, "MultipleRecord Is Not Available On Your Board", "Error", MB_OK);
						use_multiple_record = 0;
					}
					if(external_clock_in_use)
						external_clock_rate=(float)GetDlgItemInt (hWnd, IDC_EXT_CLK_VALUE, NULL, FALSE);
					else{
						EnableWindow(GetDlgItem(hWnd, IDC_EXT_CLK_VALUE),FALSE);
					    SetDlgItemInt(hWnd,	// handle of dialog box
                             IDC_EXT_CLK_VALUE,	// identifier of control
                             (uInt32)external_clock_rate,	// value to set
                             FALSE 	// signed or unsigned indicator
                        );
					}
					
					board.srindex = temp_board.srindex;
					board.opmode = temp_board.opmode;

					SetFocus (GetParent (hWnd));
					prepare_for_capture (boards_in_system, &temp_board);
					}
					break;

				case IDC_SR_CLOSE:
					DestroyWindow (SRateDlg);
					SRateDlg = 0;
					break;

				case IDC_SINGLE:
					for (i = 0; i < SRTI_EXTERNAL; i++) {
						if ((sample_rate_table[i].sr_flag & B_T_SINGLE) & current_board_type) {
							nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_ADDSTRING, 0, (LPARAM)((LPCTSTR)(sample_rate_table[i].sr_text)));
							SendDlgItemMessage (hWnd, IDC_SRATE, CB_SETITEMDATA, nIndex, (LPARAM)i);
							j = i;
						}
					}
					temp_board.opmode = GAGE_SINGLE_CHAN;
					temp_board.srindex = best_sample_rate (board_info.board_type, GAGE_SINGLE_CHAN, 0);
					break;
				
				case IDC_DUAL:

					for (i = 0; i < SRTI_EXTERNAL; i++) {
						if ((sample_rate_table[i].sr_flag & B_T_DUAL) & current_board_type) {
							nIndex = SendDlgItemMessage (hWnd, IDC_SRATE, CB_ADDSTRING, 0, (LPARAM)((LPCTSTR)(sample_rate_table[i].sr_text)));
							SendDlgItemMessage (hWnd, IDC_SRATE, CB_SETITEMDATA, nIndex, (LPARAM)i);
							j = i; 
						}
					}
					temp_board.opmode = GAGE_DUAL_CHAN;
					temp_board.srindex = best_sample_rate (board_info.board_type, GAGE_DUAL_CHAN, 0);
					break;

				case IDC_EXTCLK:
                    external_clock_in_use = (int16)SendDlgItemMessage (hWnd, IDC_EXTCLK,BM_GETCHECK, 0, 0);
				    if(external_clock_in_use){
					   EnableWindow(GetDlgItem(hWnd, IDC_EXT_CLK_VALUE),TRUE);
					   SetDlgItemInt(hWnd,	// handle of dialog box
                             IDC_EXT_CLK_VALUE,	// identifier of control
                            (uInt32) external_clock_rate,	// value to set
                             FALSE 	// signed or unsigned indicator
                       );
			        } 
					else{
                       EnableWindow(GetDlgItem(hWnd, IDC_EXT_CLK_VALUE),FALSE);
					  
					}
					break;
		}
			
		default:
			break;
	}

	return (FALSE);
}

LRESULT CALLBACK IRangeDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static	boarddef temp_board;
	int32	i;
	DWORD	nIndex;
	
	switch (msg) {
		
		case WM_INITDIALOG:

			memcpy (&temp_board, &board, sizeof(board));
			if (temp_board.couple_a == GAGE_AC)
				SendDlgItemMessage (hWnd, IDC_IRAC, BM_SETCHECK, 1, 0);
			else
				SendDlgItemMessage (hWnd, IDC_IRDC, BM_SETCHECK, 1, 0);
			
			
			for (i = 0; i < 7; i++) {
				if (ranges[i].ir_flag & current_board_type) {
					nIndex = SendDlgItemMessage (hWnd, IDC_IRANGE, CB_ADDSTRING, 0, (LPARAM)((LPCTSTR)(ranges[i].ir_text)));
					SendDlgItemMessage (hWnd, IDC_IRANGE, CB_SETITEMDATA, nIndex, (LPARAM)i);
				}	
			}
			nIndex = SendDlgItemMessage (hWnd, IDC_IRANGE, CB_FINDSTRINGEXACT, (WPARAM)0, (LPARAM)((LPCTSTR)(ranges[board.range_a].ir_text)));
			SendDlgItemMessage (hWnd, IDC_IRANGE, CB_SETCURSEL, (WPARAM)nIndex, 0);
			
			return (TRUE);

		case WM_COMMAND:

			switch (LOWORD(wParam)) {

				case IDC_IRAPPLY:
					
					nIndex = SendDlgItemMessage (hWnd, IDC_IRANGE, CB_GETCURSEL, 0, 0);
					temp_board.range_a = temp_board.range_b =  (int16)(SendDlgItemMessage (hWnd, IDC_IRANGE, CB_GETITEMDATA, (WPARAM)nIndex, 0));
					board.range_a = temp_board.range_a;
					board.range_b = temp_board.range_b;
					board.couple_a = temp_board.couple_a;
					board.couple_b = temp_board.couple_b;

					SetFocus (GetParent (hWnd));
					prepare_for_capture (boards_in_system, &board);
					break;

				case IDC_IRCLOSE:

					DestroyWindow (IRangeDlg);
					IRangeDlg = 0;
					break;

				case IDC_IRAC:

					temp_board.couple_a = temp_board.couple_b = GAGE_AC;
					break;

				case IDC_IRDC:

					temp_board.couple_a = temp_board.couple_b = GAGE_DC;
					break;
			}

		default:
			break;

	}
	return (FALSE);
}


LRESULT CALLBACK TriggerDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	static	boarddef temp_board;
	uInt32 jj;

	switch (msg) {
		
		case WM_INITDIALOG:

			memcpy (&temp_board, &board, sizeof(board));
			if (temp_board.source == GAGE_CHAN_A)
				SendDlgItemMessage (hWnd, IDC_TRIGCHANA, BM_SETCHECK, 1, 0);
			else if (temp_board.source == GAGE_CHAN_B)
				SendDlgItemMessage (hWnd, IDC_TRIGCHANB, BM_SETCHECK, 1, 0);
			else if (temp_board.source == GAGE_SOFTWARE)
				SendDlgItemMessage (hWnd, IDC_TRIGSOFTWARE, BM_SETCHECK, 1, 0);
			else
				SendDlgItemMessage (hWnd, IDC_TRIGEXTERNAL, BM_SETCHECK, 1, 0);

			if (temp_board.slope == GAGE_POSITIVE)
				SendDlgItemMessage (hWnd, IDC_TRIGPOS, BM_SETCHECK, 1, 0);
			else
				SendDlgItemMessage (hWnd, IDC_TRIGNEG, BM_SETCHECK, 1, 0);
			
			if ( temp_board.range_e == GAGE_PM_1_V )
				SendDlgItemMessage (hWnd, IDC_TRIG1V, BM_SETCHECK, 1, 0);
			if ( temp_board.range_e == GAGE_PM_5_V )
				SendDlgItemMessage (hWnd, IDC_TRIG5V, BM_SETCHECK, 1, 0);

			if ( temp_board.couple_e == GAGE_AC)
				SendDlgItemMessage (hWnd, IDC_TRIGAC, BM_SETCHECK, 1, 0);
			else
				SendDlgItemMessage (hWnd, IDC_TRIGDC, BM_SETCHECK, 1, 0);

			SetDlgItemInt (hWnd, IDC_TRIGGERLEVEL, board.level, FALSE);						
			SetDlgItemInt (hWnd, IDC_TRIGGERDEPTH, (int32)board.depth, FALSE);
			SetDlgItemInt (hWnd, IDC_TRIGGERTIMEOUT, TRIG_TIMEOUT, FALSE);	
			SetDlgItemInt (hWnd, IDC_BUSYIMEOUT, BUSY_TIMEOUT, FALSE);
			SetDlgItemInt (hWnd, IDC_NUMBEROFSAMPLES, nxx, FALSE);
			return (TRUE);
		case WM_COMMAND:
			switch (LOWORD(wParam)) {
				case IDC_TRIGAPPLY:
					temp_board.level = (int16)GetDlgItemInt (hWnd, IDC_TRIGGERLEVEL, NULL, FALSE);
					temp_board.depth = GetDlgItemInt (hWnd, IDC_TRIGGERDEPTH, NULL, FALSE);
					nxx = GetDlgItemInt (hWnd, IDC_NUMBEROFSAMPLES, NULL, FALSE);

					TRIG_TIMEOUT == GetDlgItemInt (hWnd, IDC_TRIGGERTIMEOUT, NULL, FALSE);
					BUSY_TIMEOUT == GetDlgItemInt (hWnd, IDC_BUSYIMEOUT, NULL, FALSE);

					if ((board_type & GAGE_ASSUME_CS8500) && (transfer_type == USE_TRIGGER_VIEW_2)) {
						if (temp_board.depth > bsize) {
							wsprintf (str, "Trigger depth exceeds allocated size of %d bytes", bsize);
							MessageBox(hWnd, str, "CS8500: Gage_Trigger_View_Transfer_2", MB_OK | MB_ICONINFORMATION);
							break;
						}
					}

					if (transfer_type == USE_TRIGGER_VIEW) {
						if (temp_board.depth >= 32*1024) {
							wsprintf (str, "Trigger depth exceeds allowed size of %d bytes", (32*1024)-1);
							MessageBox(hWnd, str, "Gage_Trigger_View_Transfer", MB_OK | MB_ICONINFORMATION);
							break;
						}
					}

					board.source = temp_board.source;
					board.slope = temp_board.slope;
					board.couple_e = temp_board.couple_a;
					board.range_e = temp_board.range_e;
					
					if((board.depth != temp_board.depth)&&(temp_board.depth>=8192)){
						for(jj=0;jj<boards_in_system;jj++){
							VirtualFree(
										a_buffer[jj],	// address of region of committed pages  
										0,	// size of region 
										MEM_RELEASE 	// type of free operation 
							);
							a_buffer[jj] = VirtualAlloc(
										NULL,	// address of region to reserve or commit  
										(DWORD)(80+temp_board.depth * ((board_info.sample_bits >= 12) + 1)),	// size of region 
										MEM_COMMIT,	// type of allocation 
										PAGE_READWRITE 	// type of access protection 
							);
							TVTBufferPtr[jj] = a_buffer[jj]; 
							a_buffer16[jj] = (int16  *)a_buffer[jj];
							VirtualFree(
										b_buffer[jj],	// address of region of committed pages  
										0,	// size of region 
										MEM_RELEASE 	// type of free operation 
							);
							b_buffer[jj] = VirtualAlloc(
										NULL,	// address of region to reserve or commit  
										(DWORD)(80+temp_board.depth * ((board_info.sample_bits >= 12) + 1)),	// size of region 
										MEM_COMMIT,	// type of allocation 
										PAGE_READWRITE 	// type of access protection 
							);					
							b_buffer16[jj] = (int16  *)b_buffer[jj];
						}
					}
					board.depth = temp_board.depth;
					board.level = temp_board.level;
					SetFocus (GetParent (hWnd));
					prepare_for_capture (boards_in_system, &board);
					break;

				case IDC_TRIGCLOSE:

					DestroyWindow (TriggerDlg);
					TriggerDlg = 0;
					break;

				case IDC_TRIGAC:

					temp_board.couple_e = GAGE_AC;
					break;

				case IDC_TRIGDC:

					temp_board.couple_e = GAGE_DC;
					break;

				case IDC_TRIGNEG:
					
					temp_board.slope = GAGE_NEGATIVE;
					break;

				case IDC_TRIGPOS:

					temp_board.slope = GAGE_POSITIVE;
					break;

				case IDC_TRIG5V:

					temp_board.range_e = GAGE_PM_5_V;
					break;

				case IDC_TRIG1V:

					temp_board.range_e = GAGE_PM_1_V;
					break;

				case IDC_TRIGCHANA:

					temp_board.source = GAGE_CHAN_A;
					break;

				case IDC_TRIGCHANB:

					temp_board.source = GAGE_CHAN_B;
					break;

				case IDC_TRIGSOFTWARE:

					temp_board.source = GAGE_SOFTWARE;
					break; 

				case IDC_TRIGEXTERNAL:

					temp_board.source = GAGE_EXTERNAL;
					break;
				
			}

		default:
			break;

	}
	return (FALSE);
}

/*-------------------------------------------------------------------------*/						
LRESULT CALLBACK AboutDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch (msg) 
	{
		case WM_COMMAND:
			switch (LOWORD(wParam)) 
			{
				case IDOK:
					DestroyWindow (AboutDlg);
					AboutDlg = 0;
				break;
			}
		break;
	}
	return (FALSE);
}

/*-------------------------------------------------------------------------*/						
LRESULT CALLBACK PmtDlgProc (HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{ char temp[50];
  unsigned int  num;

	switch (msg) 
	{
		case WM_INITDIALOG:
		reg_read();
		sprintf(temp,"%#-5d",regs.pmt_trs);
		SetDlgItemText(hWnd, IDC_PTRS,temp);
		sprintf(temp,"%#-5d",regs.pmt_sat*25);
		SetDlgItemText(hWnd, IDC_PSAT,temp);
		sprintf(temp,"%#-5d",regs.pmt_wnd*25);
		SetDlgItemText(hWnd, IDC_PWND,temp);
		sprintf(temp,"%#-9f",(regs.errc/4096.0));
		SetDlgItemText(hWnd, IDC_ERRC,temp);
		sprintf(temp,"%#-5d",regs.chn_trs);
		SetDlgItemText(hWnd, IDC_CTRS,temp);
		sprintf(temp,"%#-5d",regs.chn_wnd*25);
		SetDlgItemText(hWnd, IDC_CWND,temp);
		sprintf(temp,"%#-5X",regs.star);
		SetDlgItemText(hWnd, IDC_STAR,temp);
		sprintf(temp,"%#-5d",regs.t1_wait*25);
		SetDlgItemText(hWnd, IDC_T1WAIT,temp);
		sprintf(temp,"%#-5d",regs.t2_wait*25);
		SetDlgItemText(hWnd, IDC_T2WAIT,temp);
		sprintf(temp,"%#-5d",regs.t3_wait*25);
		SetDlgItemText(hWnd, IDC_T3WAIT,temp);
		sprintf(temp,"%#-5d",regs.pmt_del*25);
		SetDlgItemText(hWnd, IDC_PMTDEL,temp);
		sprintf(temp,"%#-5f",regs.ltimeout*1.6);
		SetDlgItemText(hWnd, IDC_LTIMEOUT,temp);
		sprintf(temp,"%#-5d",regs.ptrvlt);
		SetDlgItemText(hWnd, IDC_PMT,temp);
		SetDlgItemText(hWnd, IDC_CHN,temp);
		if (cont_run == 1)
			SendDlgItemMessage (hWnd, IDC_C_CONT, BM_SETCHECK, BST_CHECKED, 0);
		else
			SendDlgItemMessage (hWnd, IDC_C_CONT, BM_SETCHECK, BST_UNCHECKED, 0);

	    if ((regs.star && 0x1) != 0)
		{
				SendDlgItemMessage (hWnd, IDC_START, BM_SETCHECK, BST_CHECKED, 0);
				SendDlgItemMessage (hWnd, IDC_STOP, BM_SETCHECK, BST_UNCHECKED, 0);
		}
		else
		{
				SendDlgItemMessage (hWnd, IDC_STOP, BM_SETCHECK, BST_CHECKED, 0);
				SendDlgItemMessage (hWnd, IDC_START, BM_SETCHECK,BST_UNCHECKED, 0);
		}


// set the spin range
		SendDlgItemMessage(hWnd,IDC_SPIN_PWND,UDM_SETRANGE32,1,0xff);
		SendDlgItemMessage(hWnd,IDC_SPIN_PSAT,UDM_SETRANGE32,1,0xff);
		SendDlgItemMessage(hWnd,IDC_SPIN_ERRC,UDM_SETRANGE32,1,0xffff);
		SendDlgItemMessage(hWnd,IDC_SPIN_CWND,UDM_SETRANGE32,1,0xff);
		SendDlgItemMessage(hWnd,IDC_SPIN_T1WAIT,UDM_SETRANGE32,1,0xffff);
		SendDlgItemMessage(hWnd,IDC_SPIN_T2WAIT,UDM_SETRANGE32,1,0xffff);
		SendDlgItemMessage(hWnd,IDC_SPIN_T3WAIT,UDM_SETRANGE32,0,0xffff);
		SendDlgItemMessage(hWnd,IDC_SPIN_PMTDEL,UDM_SETRANGE32,0,0xffff);
		SendDlgItemMessage(hWnd,IDC_SPIN_LTIMEOUT,UDM_SETRANGE32,0,0xffff);

// set the spin position
		SendDlgItemMessage(hWnd,IDC_SPIN_PWND,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.pmt_wnd,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_PSAT,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.pmt_sat,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_ERRC,UDM_SETPOS,0,(LPARAM) MAKELONG( regs.errc,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_CWND,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.chn_wnd,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_T1WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t1_wait,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_T2WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t2_wait,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_T3WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t3_wait,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_PMTDEL,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.pmt_del,0));
		SendDlgItemMessage(hWnd,IDC_SPIN_LTIMEOUT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.ltimeout,0));

		return (TRUE);
		case WM_CLOSE:
				DestroyWindow (PmtDlg);
				PmtDlg = 0;
	 	break; /* End of WM_CLOSE                                     */
		case WM_NOTIFY:
			switch (LOWORD(wParam)) 
			{
			case IDC_SPIN_PWND:
				regs.pmt_wnd = SendDlgItemMessage(hWnd,IDC_SPIN_PWND,UDM_GETPOS,0,0) & 0xff;
				sprintf(temp,"%#-5d",regs.pmt_wnd*25);
				SetDlgItemText(hWnd, IDC_PWND,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_PSAT:
				regs.pmt_sat = SendDlgItemMessage(hWnd,IDC_SPIN_PSAT,UDM_GETPOS,0,0) &0xff;
				sprintf(temp,"%#-5d",regs.pmt_sat*25);
				SetDlgItemText(hWnd, IDC_PSAT,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_ERRC:
				regs.errc = SendDlgItemMessage(hWnd,IDC_SPIN_ERRC,UDM_GETPOS,0,0) & 0xffff;
				sprintf(temp,"%#-5f",(regs.errc/4096.0));
				SetDlgItemText(hWnd, IDC_ERRC,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_CWND:
				regs.chn_wnd = SendDlgItemMessage(hWnd,IDC_SPIN_CWND,UDM_GETPOS,0,0) & 0xff;
				sprintf(temp,"%#-5d",regs.chn_wnd*25);
				SetDlgItemText(hWnd, IDC_CWND,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_T1WAIT:
				regs.t1_wait = SendDlgItemMessage(hWnd,IDC_SPIN_T1WAIT,UDM_GETPOS,0,0) &0xffff;
				sprintf(temp,"%#-5d",regs.t1_wait*25);
				SetDlgItemText(hWnd, IDC_T1WAIT,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_T2WAIT:
				regs.t2_wait = SendDlgItemMessage(hWnd,IDC_SPIN_T2WAIT,UDM_GETPOS,0,0) &0xffff;
				sprintf(temp,"%#-5d",regs.t2_wait*25);
				SetDlgItemText(hWnd, IDC_T2WAIT,temp);
				reg_bwrite();
			return(TRUE);

			case IDC_SPIN_T3WAIT:
				regs.t3_wait = SendDlgItemMessage(hWnd,IDC_SPIN_T3WAIT,UDM_GETPOS,0,0) &0xffff;
				sprintf(temp,"%#-5d",regs.t3_wait*25);
				SetDlgItemText(hWnd, IDC_T3WAIT,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_PMTDEL:
				regs.pmt_del = SendDlgItemMessage(hWnd,IDC_SPIN_PMTDEL,UDM_GETPOS,0,0) &0xffff;
				sprintf(temp,"%#-5d",regs.pmt_del*25);
				SetDlgItemText(hWnd, IDC_PMTDEL,temp);
				reg_bwrite();
			return(TRUE);
			case IDC_SPIN_LTIMEOUT:
				regs.ltimeout = SendDlgItemMessage(hWnd,IDC_SPIN_LTIMEOUT,UDM_GETPOS,0,0) &0xffff;
				sprintf(temp,"%#-5f",regs.ltimeout*1.6);
				SetDlgItemText(hWnd, IDC_LTIMEOUT,temp);
				reg_bwrite();
			return(TRUE);
			}
			return(TRUE);
		case WM_COMMAND:
			switch (LOWORD(wParam)) 
			{
				case IDC_PMTCLOSE:
					DestroyWindow (PmtDlg);
					PmtDlg = 0;
				break;
				case IDC_EVENT:
					single_trigger();
					reg_read();
					sprintf(temp,"%#-5d",regs.ptrvlt);
					SetDlgItemText(hWnd, IDC_PMT,temp);
					SetDlgItemText(hWnd, IDC_CHN,temp);
					GetDlgItemText(hWnd,IDC_STAR,temp,10);
					sscanf(temp,"%X",&num); regs.star = num;
					if (regs.star > 65536) regs.star = 65535;
				break;
				case IDC_START:
					reg_read();
					if ((cont_run) == 1)
						cont_trigger();
			    	else
						single_trigger();
					reg_read();
			  		sprintf(temp,"%#-5X",regs.star);
					SetDlgItemText(hWnd, IDC_STAR,temp); 
				break;
				case IDC_STOP:
					reset_board();
					reg_read();
					sprintf(temp,"%#-5X",regs.star);
					SetDlgItemText(hWnd, IDC_STAR,temp);
				break;
				case IDC_C_CONT:
					 if(cont_run == 1)
						cont_run = 0;
					 else
						cont_run = 1;
				break;
				case IDC_PMTAPPLY:
					GetDlgItemText(hWnd,IDC_PTRS,temp,10);
					regs.pmt_trs =  strtoul(temp,NULL,10);
					if (regs.pmt_trs > 65536) regs.pmt_trs = 65535;
					GetDlgItemText(hWnd,IDC_PSAT,temp,10);
					regs.pmt_sat =  strtoul(temp,NULL,10)/25;
					if (regs.pmt_sat > 256) regs.pmt_sat = 256;
					GetDlgItemText(hWnd,IDC_PWND,temp,10);
					regs.pmt_wnd =  strtoul(temp,NULL,10)/25;
					if (regs.pmt_wnd > 256) regs.pmt_wnd = 256;
					GetDlgItemText(hWnd,IDC_ERRC,temp,10);
					regs.errc  = (int) ((atof(temp) + 0.00001)*4096.0);
					if (regs.errc > 65535) regs.pmt_wnd = 256;
					GetDlgItemText(hWnd,IDC_CTRS,temp,10);
					regs.chn_trs =  strtoul(temp,NULL,10);
					if (regs.chn_trs > 65536) regs.chn_trs = 65535;
					GetDlgItemText(hWnd,IDC_CWND,temp,10);
					regs.chn_wnd =  strtoul(temp,NULL,10)/25;
					if (regs.chn_wnd > 256) regs.chn_wnd = 256;
					GetDlgItemText(hWnd,IDC_STAR,temp,10);
					sscanf(temp,"%X",&num); regs.star = num;
					if (regs.star > 65536) regs.star = 65535;
					GetDlgItemText(hWnd,IDC_T1WAIT,temp,15);
					regs.t1_wait=  strtoul(temp,NULL,10)/25;
					if (regs.t1_wait > 65536) regs.t1_wait = 65535;
					GetDlgItemText(hWnd,IDC_T2WAIT,temp,15);
					regs.t2_wait =  strtoul(temp,NULL,10)/25;
					if (regs.t2_wait > 65536) regs.t2_wait = 65535;
					GetDlgItemText(hWnd,IDC_T3WAIT,temp,15);
					regs.t3_wait =  strtoul(temp,NULL,10)/25;
					if (regs.t3_wait > 65536) regs.t3_wait = 65535;
					GetDlgItemText(hWnd,IDC_PMTDEL,temp,15);
					regs.pmt_del =  strtoul(temp,NULL,10)/25;
					if (regs.pmt_del > 65536) regs.pmt_del = 65535;
					GetDlgItemText(hWnd,IDC_LTIMEOUT,temp,15);
					regs.ltimeout  =(int) (strtod(temp,NULL,30)/1.6);
					if (regs.ltimeout > 65536) regs.ltimeout = 65535;
					reg_bwrite();
					reg_read();
					sprintf(temp,"%#-5d",regs.pmt_trs);
					SetDlgItemText(hWnd, IDC_PTRS,temp);
					sprintf(temp,"%#-5d",regs.pmt_sat*25);
					SetDlgItemText(hWnd, IDC_PSAT,temp);
					sprintf(temp,"%#-5d",regs.pmt_wnd*25);
					SetDlgItemText(hWnd, IDC_PWND,temp);
					sprintf(temp,"%#-5f",(regs.errc/4096.0));
					SetDlgItemText(hWnd, IDC_ERRC,temp);
					sprintf(temp,"%#-5d",regs.chn_trs);
					SetDlgItemText(hWnd, IDC_CTRS,temp);
					sprintf(temp,"%#-5d",regs.chn_wnd*25);
					SetDlgItemText(hWnd, IDC_CWND,temp);
					sprintf(temp,"%#-5X",regs.star);
					SetDlgItemText(hWnd, IDC_STAR,temp);
					sprintf(temp,"%#-5d",regs.t1_wait*25);
					SetDlgItemText(hWnd, IDC_T1WAIT,temp);
					sprintf(temp,"%#-5d",regs.t2_wait*25);
					SetDlgItemText(hWnd, IDC_T2WAIT,temp);
					sprintf(temp,"%#-5d",regs.t3_wait*25);
					SetDlgItemText(hWnd, IDC_T3WAIT,temp);
					sprintf(temp,"%#-5d",regs.pmt_del*25);
					SetDlgItemText(hWnd, IDC_PMTDEL,temp);
					sprintf(temp,"%#-5f",regs.ltimeout*1.6);
					SetDlgItemText(hWnd, IDC_LTIMEOUT,temp);
					sprintf(temp,"%#-5d",regs.ptrvlt);
					SetDlgItemText(hWnd, IDC_PMT,temp);
					SetDlgItemText(hWnd, IDC_CHN,temp);

	    if ((regs.star && 0x1) != 0)
		{		SendDlgItemMessage (hWnd, IDC_START, BM_SETCHECK, BST_CHECKED, 0);
				SendDlgItemMessage (hWnd, IDC_STOP, BM_SETCHECK, BST_UNCHECKED, 0);
		}
		else
		{		SendDlgItemMessage (hWnd, IDC_STOP, BM_SETCHECK, BST_CHECKED, 0);
				SendDlgItemMessage (hWnd, IDC_START, BM_SETCHECK,BST_UNCHECKED, 0);
		}
		if (cont_run == 1)
			SendDlgItemMessage (hWnd, IDC_C_CONT, BM_SETCHECK, BST_CHECKED, 0);
		else
			SendDlgItemMessage (hWnd, IDC_C_CONT, BM_SETCHECK, BST_UNCHECKED, 0);
					SendDlgItemMessage(hWnd,IDC_SPIN_PWND,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.pmt_wnd,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_PSAT,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.pmt_sat,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_ERRC,UDM_SETPOS,0,(LPARAM) MAKELONG( regs.errc,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_CWND,UDM_SETPOS,0,(LPARAM) MAKELONG((short) regs.chn_wnd,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_T1WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t1_wait,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_T2WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t2_wait,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_T3WAIT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.t3_wait,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_PMTDEL,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.pmt_del,0));
					SendDlgItemMessage(hWnd,IDC_SPIN_LTIMEOUT,UDM_SETPOS,0,(LPARAM) MAKELONG(regs.ltimeout,0));

				break;
			}
		break;
	}
	return (FALSE);
}


/*-------------------------------------------------------------------------*/			
			
void	CalcGr(double TimeBase, int32 ngrp,float *incell)
{ 
	double tbs;
	double srs;     
    
	gage_get_driver_info(&board_info); 
	tbs = TimeBase * 10;
	srs = (double)(ngrp) / (double)( board_info.rate * pow ( 1000, board_info.multiplier - 1));
	*incell = (float)(tbs/srs);
}    

/*-------------------------------------------------------------------------*/

void	CalcScale(int32 offset, int32 size_y, double vc, int32 range, double s_o, double s_r, double * coef, double * off) 
{
	double vcs;
    
	vcs = vc * 4;
	*coef = (double)(size_y * ranges[range].ir_calc  /(vcs * s_r));
	*off = (double)offset + (double)(size_y * ( 1 -  (ranges[range].ir_calc *  s_o/(s_r * vcs))));
}    

/*-----------user selects a command item from a menu--------------------------------------------------------------*/

void	Cls_OnCommand (HWND hWnd, int32 id, HWND hWndCtl, UINT codeNotify)

{

	int i;
	switch (id) {

/**NO1******************************************/

		case IDM_C_ACQUIRE:
			k=0;
			j=0;
//			prepare_for_capture(boards_in_system, &board);
			AcquireStart (); 
			ContinuousMode = TRUE;
			AcquireCheck(hWnd);
			break;
/********************************************/
	    case IDM_C_CONTINUOUS:
			k=1;
            j=0;
			prepare_for_capture(boards_in_system, &board);
			AcquireStart (); 
			ContinuousMode = TRUE;			
//				MessageBox(NULL, str, "My Message", MB_OK);
			break;
/********************************************/
		
		
		case ID_FILE_ZIP_AS:
		    
			system( "zipfile.bat" );

			break;
/********************************************/
					
		
		case ID_FILE_UNZIP_AS:
		    
			system( "unzipfile.bat" );

			break;

/********************************************/		
		
		case IDM_C_ABORT:

//	MessageBox(NULL, "save data in CONTINUOUS ", "My Message", MB_OK);
			
			Abort (hWnd);

	_tzset();
	_strtime( tmpbuf1 );
	_strdate( tmpbuf2 );

 directoryname[0] = tmpbuf2[0];
 directoryname[1] = tmpbuf2[1];
 directoryname[2] = tmpbuf2[3];
 directoryname[3] = tmpbuf2[4];
 directoryname[4] = tmpbuf2[6];
 directoryname[5] = tmpbuf2[7];

 directoryname[6] = tmpbuf1[0];
 directoryname[7] = tmpbuf1[1];
 directoryname[8] = tmpbuf1[3];
 directoryname[9] = tmpbuf1[4];
 directoryname[10] = tmpbuf1[6];
 directoryname[11] = tmpbuf1[7];
 directoryname[12]='\0';

	 _mkdir(directoryname);
   	sprintf(line, "copy dat\\*.* %s", directoryname);	
    system(line);
	sprintf(linezip, "pkzip %s.zip %s\\*.*", directoryname, directoryname);	
	system(linezip);
	system("del .\\dat\\*.*");
			break;

/********************************************/
	    case IDM_C_ABOUT:
			{
			FARPROC lpfnDIALOGAMsgProc;
			int32 nRc = 0;
			lpfnDIALOGAMsgProc = MakeProcInstance((FARPROC)DIALOGAMsgProc, hInst);
			nRc = DialogBox(hInst, MAKEINTRESOURCE(100), hWnd, (DLGPROC)lpfnDIALOGAMsgProc);
			FreeProcInstance(lpfnDIALOGAMsgProc);
			}
			break;
/************************************************************/
	    case IDM_C_EXIT:
		    		 
			SendMessage (hWnd, WM_CLOSE, 0, 0L);
			break;
/************************************************************/
		case IDM_C_BUILD:			// Build up SIGNAL for Display

			buildup_signal = 1;
			break;
/************************************************************/
		case IDM_C_CLEAR:			// Clear Signal after build

			buildup_signal = 0;
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_HOME:

			view_start = 0L;
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_END:
			{
			long	temp = (((long)(scrn_x) * (long)(zoom_x2)) / (long)(zoom_x));
			view_start = adjusted_depth - temp + ptm_start;
			}
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_ALTLEFTKEY:

			view_start -= 1L;
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_CTLLEFTKEY:

			view_start -= 25L * zoom_x2;
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_ALTRIGHTKEY:

			view_start += 1L;
			Display (hWnd);
			break;
/************************************************************/
		case IDM_C_CTLRIGHTKEY:

			view_start += 25L * zoom_x2;
			Display (hWnd);

			break;
/************************************************************/
		case IDM_TB3_TRANSFER:

			if (transfer_type != USE_TRANSFER_BUFFER_3) {
				transfer_type = USE_TRANSFER_BUFFER_3;
				CheckMenuItem (GetMenu(hWnd), IDM_MEM_READ_TRANSFER, MF_UNCHECKED);	
				CheckMenuItem (GetMenu(hWnd), IDM_RT_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TV_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TV2_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TV3_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TB_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TB2_TRANSFER,	MF_UNCHECKED);
				CheckMenuItem (GetMenu(hWnd), IDM_TB3_TRANSFER,	MF_CHECKED);
			}
			break;
/************************************************************/
        case IDM_RT_TRANSFER:

			if (transfer_type != USE_REAL_TIME) {
			}
			break;
/************************************************************/
        case IDM_XFER_TIME:

			if (!xfer_time) {
				xfer_time = 1;
			    CheckMenuItem (GetMenu(hWnd), IDM_XFER_TIME, MF_CHECKED);
			}
			else {
				xfer_time = 0;
			    CheckMenuItem (GetMenu(hWnd), IDM_XFER_TIME, MF_UNCHECKED);
			}
			break;
/************************************************************/
		case IDM_SRATE:

			if (!SRateDlg) { // check if instance exists already first before create
				SRateDlg = CreateDialog (hInst, (LPCSTR)MAKEINTRESOURCE(IDD_CAPTURE), hWnd, (DLGPROC)SRateDlgProc);
				ShowWindow (SRateDlg, SW_SHOW);
			}
			break;
/************************************************************/
		case IDM_IRANGE:

			if (!IRangeDlg) { // check if instance exists already first before create
				IRangeDlg = CreateDialog (hInst, (LPCSTR)MAKEINTRESOURCE(IDD_INPUT), hWnd, (DLGPROC)IRangeDlgProc);
				ShowWindow (IRangeDlg, SW_SHOW);
			}
			break;
/************************************************************/
		case IDM_TRIGGER:
			
			if (!TriggerDlg) {
				TriggerDlg = CreateDialog (hInst, (LPCSTR)MAKEINTRESOURCE(IDD_TRIGGER), hWnd, (DLGPROC)TriggerDlgProc);
				ShowWindow (TriggerDlg, SW_SHOW);
			}
			break;

/************************************************************/
		case IDM_ABOUT:
			
			if (!AboutDlg) {
				AboutDlg = CreateDialog (hInst, (LPCSTR)MAKEINTRESOURCE(IDD_ABOUT), hWnd, (DLGPROC)AboutDlgProc);
				ShowWindow (AboutDlg, SW_SHOW);
			}
			break;
/************************************************************/
		case IDM_T_PMT:
			
			if (!PmtDlg) {
				PmtDlg = CreateDialog (hInst, (LPCSTR)MAKEINTRESOURCE(IDD_PMT), hWnd, (DLGPROC)PmtDlgProc);
				ShowWindow (PmtDlg,SW_SHOW);
			}
			break;

/************************************************************/
		case IDM_T_START:
				CheckMenuItem (GetMenu(hWnd), IDM_T_START, MF_CHECKED);	
				CheckMenuItem (GetMenu(hWnd), IDM_T_STP,  MF_UNCHECKED);
				cont_trigger();
			break;
/************************************************************/
/************************************************************/
		case IDM_T_STP:
				CheckMenuItem (GetMenu(hWnd), IDM_T_START, MF_UNCHECKED);	
				CheckMenuItem (GetMenu(hWnd), IDM_T_STP,  MF_CHECKED);
				reset_board();
			break;
/************************************************************/



		default:
			FORWARD_WM_COMMAND(hWnd, id, hWndCtl, codeNotify, DefWindowProc);
	}
}

/*-------------------------------------------------------------------------*/

void Cls_OnChar(HWND hWnd, TCHAR ch, int32 cRepeat) 
{
 	switch (ch) {
		case '-':
			if (zoom_x > 1)  {
					zoom_x >>= 1;
			}else if (board.depth > ((uInt32)(scrn_x) * zoom_x2))  {
					zoom_x2 <<= 1;
			}
			break;

		case '+':
			if (zoom_x2 > 1)  {
					zoom_x2 >>= 1;
			}else{
				if (zoom_x < 64)
					zoom_x <<= 1;
			}
			break;

		default:
			FORWARD_WM_CHAR(hWnd, ch, cRepeat, DefWindowProc);
	}
	Display (hWnd); // this call will allow to zoom on an existing display
}
			
/*------when the system or another application makes a request to paint 
a portion of an application's window---------------------------------------------------------*/

void	Cls_OnPaint(HWND hWnd) 
{
	HDC			hDC;                /* handle for the display device       */
	PAINTSTRUCT ps;                 /* holds PAINT information             */
	int32		i, j, points, size_y, offset_y; /* Vars used by paint code.*/
	RECT		rect, rect_mr;                  /* Vars used by paint code.*/
	int32		centre_line;
	int32		times;
/*	double		TimeBase = 0.000005; */
	double		TimeBase = 0.0005; 
	float		incell;      
	double		VertScale = 0.2;
	uInt16		trace_x, channel = GAGE_CHAN_A;
	uInt32		start;
	uInt32		count;
	int16		x, min, max, val, opmode ;
    uInt8		t=0;
	uInt8		tmp[20];
	uInt8		tmp2[64]; // for measurement info
	uInt32		Depth;

	// make local copies of the a_buffer, a_buffer16, b_buffer, and b_buffer16
	// to similar ptrs which will be used in this paint routine only
	
	uInt8	far *dsp_a_buffer[GAGE_B_L_MAX_CARDS];
	uInt8	far *dsp_b_buffer[GAGE_B_L_MAX_CARDS];

	int16	far *dsp_a_buffer16[GAGE_B_L_MAX_CARDS];
	int16	far *dsp_b_buffer16[GAGE_B_L_MAX_CARDS];

/*	stream = fopen( "Mydata.dat", "w+" ); */

	for (j = 0; j < boards_in_system; j++)
	{

		if ((transfer_type == USE_TRIGGER_VIEW_3) || (transfer_type == USE_TRANSFER_BUFFER_3))
		{
			dsp_a_buffer[j]=a_buffer[j]+(TVT3_offset[j]);
			dsp_b_buffer[j]=b_buffer[j]+(TVT3_offset[j]);
			dsp_a_buffer16[j]=a_buffer16[j]+(TVT3_offset[j]>>1);
			dsp_b_buffer16[j]=b_buffer16[j]+(TVT3_offsetB[j]>>1);
		}
		else if ((transfer_type == USE_TRIGGER_VIEW_2) || (transfer_type == USE_TRANSFER_BUFFER_2))
		{
			TVT3_offset[j] = 0;
			TVT3_offsetB[j] = 0;
			if (board.opmode == GAGE_DUAL_CHAN) 
			{
				dsp_a_buffer16[j]=TVT2_ptr[j];
				dsp_b_buffer[j]=b_buffer[j];
				dsp_b_buffer16[j]=TVT2_ptrB[j];
			}
			else
			{
				TVT3_offset[j] = 0;
				TVT3_offsetB[j] = 0;
				dsp_a_buffer[j]=a_buffer[j];
				dsp_b_buffer[j]=b_buffer[j];
				dsp_a_buffer16[j]=a_buffer16[j];
				dsp_b_buffer16[j]=b_buffer16[j];


			}
		}
		else
		{
			dsp_a_buffer[j]=a_buffer[j];
			dsp_b_buffer[j]=b_buffer[j];
			dsp_a_buffer16[j]=a_buffer16[j];
			dsp_b_buffer16[j]=b_buffer16[j];
		}

	}

	
	opmode = board.opmode & 0x03;
	memset(&ps, 0x00, sizeof(PAINTSTRUCT));
	hDC = BeginPaint(hWnd, &ps);

	SetBkMode(hDC, TRANSPARENT);
   	GetClientRect (hWnd, &rect);

	/* if buildup_signal then do not clear only bottom 20 chars */
	if (!buildup_signal)
		FillRect(hDC, &rect, hWhiteBrush);
	else {
		rect_mr.right = 160; //20 chars
		rect_mr.left = 1;
		rect_mr.top = rect.bottom - 16;
		rect_mr.bottom = rect.bottom;
		FillRect(hDC, &rect_mr, hWhiteBrush);
	}
	SelectObject (hDC, hDotPen);
	
	if(TimeBase == 0 ) 
/* my setting TimeBase = 0.000005 ******/
		TimeBase = 0.000005;

	CalcGr(TimeBase, rect.right - rect.left,&incell );
	
	for(i = 0; i <= 10; i++) {
		MoveToEx (hDC, i * (rect.right - rect.left) / 10, rect.top, NULL); 	
		LineTo (hDC, i * (rect.right - rect.left) / 10, rect.bottom ); 
	}

	times = max(boards_in_system, 1);	
	size_y = (rect.bottom - rect.top) / (times * 2 * opmode );	 	
	
	for(i = 0; i <= (times * opmode << 3); i++) {
		MoveToEx (hDC, rect.left, i * size_y / 4, NULL); 	
		LineTo (hDC, rect.right,  i * size_y / 4);
	}                     
	points = rect.right - rect.left;
	scrn_x = (int16)rect.right;
	
	sprintf(tmp,"%08Ld",acgtimes++);
	TextOut(hDC,0,rect.bottom-16,tmp,8);

	if (xfer_time) 
	{ // check if transfer time set
		if (transfer_type == USE_TRIGGER_VIEW) 
		{
			sprintf(tmp2,"TVT   : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		} 
		else if(transfer_type == USE_TRIGGER_VIEW_2) 
		{
			sprintf(tmp2,"TVT2  : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		}
		else if(transfer_type == USE_TRIGGER_VIEW_3) 
		{
			sprintf(tmp2,"TVT3  : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		}
		else if(transfer_type == USE_MEM_READ) 
		{
			sprintf(tmp2,"Mem Rd: %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		}
		else if (transfer_type == USE_TRANSFER_BUFFER) 
		{
			sprintf(tmp2,"TB    : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		} 
		else if(transfer_type == USE_TRANSFER_BUFFER_2) 
		{
			sprintf(tmp2,"TB2  : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		}
		else if(transfer_type == USE_TRANSFER_BUFFER_3) 
		{
			sprintf(tmp2,"TB3   : %04LdKSamples in %04Ld msecs",board.depth/1024, time3);
		}
		TextOut(hDC,70,rect.bottom-16,tmp2,strlen(tmp2));
	}
	for (j = 0; j < boards_in_system; j++) 
	{

		Depth = board.depth;
		start = 0;

		if (opmode == GAGE_SINGLE_CHAN) 
		{
			SelectObject (hDC, hBlackPen);
			offset_y = (size_y * j * 2) + (size_y * sample_offset);
			centre_line = size_y * j * 2;	
#ifdef TRIGGER_TRACE
			sprintf(hTriggPenStr,"%08Lx",trigger_trace);
			TextOut(hDC,0,rect.bottom-16,hTriggPenStr,sizeof(hTriggPenStr)-2);
#endif

			if (view_start < 0)
				view_start = 0;
			{
			long	temp = (((long)(scrn_x) * (long)(zoom_x2)) / (long)(zoom_x));
			if (view_start > Depth - temp + (int32)start)
				view_start = Depth - temp;
			}

			count = start + view_start;
			if (count < 0) 
			{
				count = 0;
			}

			{
				long	temp = (((long)(scrn_x) * (long)(zoom_x2)) / (long)(zoom_x));
				if (count > Depth - temp + (int32)start)
				{
					count = Depth - temp + start ;
					view_start = count;
				}
			}

			if (zoom_x2 > 1)  
			{
				trace_x = 0;
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					min = max = dsp_a_buffer16[j][count];
				}
				else {
					min = max = dsp_a_buffer[j][count];
				}
				while ((trace_x < (scrn_x - zoom_x)) && (count < (Depth - 1))) 
				{
					for (x = 0 ; x < zoom_x2 ; x++)  {
						
						if (count >= (Depth - 1))
							break;
						if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
						{
							if ((val = dsp_a_buffer16[j][++count]) > max)  
							{
								max = val;
							}
							else if (val < min)
								min = val;
						}
						else 
						{
							if ((val = dsp_a_buffer[j][++count]) > max)  
							{
								max = val;
							}
							else if (val < min)
								min = val;
						}
					}
					MoveToEx (hDC, trace_x, offset_y + (int16)(((float)(min) / sample_resolution) * (float)(size_y)), NULL);
/********************* Line To***********/
					LineTo (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))));
					SetPixel (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))), 0x0);
					trace_x += zoom_x;
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						min = max = dsp_a_buffer16[j][count];
					}
					else 
					{
						min = max = dsp_a_buffer[j][count];
					}
				}
			}
			else
			{
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_a_buffer16[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
					while (trace_x < (scrn_x - zoom_x))  
					{
/********************* Line To***********/
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_a_buffer16[j][count]) / sample_resolution) * (float)(size_y))));
						if (count++ >= (Depth - 1))
							break;
					} 
				}
				else 
				{
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_a_buffer[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
					while (trace_x < (scrn_x - zoom_x))  
					{
/********************* Line To***********/
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_a_buffer[j][count]) / sample_resolution) * (float)(size_y))));
						if (count++ >= (Depth - 1))
							break;
					} 
				} 
			}
			
			SelectObject (hDC, hZeroPen);
			MoveToEx (hDC, 0, centre_line + size_y, NULL);
/********************* Line To***********/
			LineTo (hDC, points, centre_line + size_y);
			if (!CheckMessageQueue ())
				break;
		}
		else 
		{
			/*  Do Channel A, dual channel mode.  */
   			SelectObject (hDC, hBlackPen);
			size_y = (rect.bottom - rect.top) / (2 * boards_in_system * 2);
			offset_y = (size_y * j * 4) + (size_y * sample_offset);
			centre_line = size_y * j * 4;

			count = start;
#ifdef TRIGGER_TRACE		
			sprintf(hTriggPenStr,"%08Lx",trigger_trace);
			TextOut(hDC,0,rect.bottom-16,hTriggPenStr,sizeof(hTriggPenStr)-2);
#endif
			
			if (zoom_x2 > 1)  
			{
				trace_x = 0;
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					min = max = dsp_a_buffer16[j][count];
				}
				else 
				{
					min = max = dsp_a_buffer[j][count];
				}
				while ((trace_x < (scrn_x - zoom_x)) && (count < (Depth - 1)))  
				{
					for (x = 0 ; x < zoom_x2 ; x++)  
					{
						if (count >= (Depth - 1))
							break;

						if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
						{
							if ((val = dsp_a_buffer16[j][++count]) > max)  
							{
								max = val;
							}
							else if (val < min)
								min = val;
						}
						else 
						{
							if ((val = dsp_a_buffer[j][count++]) > max)  
							{
								max = val;
							}
							else if (val < min)
								min = val;
						}
					}

					MoveToEx (hDC, trace_x, offset_y + (int16)(((float)(min) / sample_resolution) * (float)(size_y)), NULL);

/********************* Line To***********/
					LineTo (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))));
					
					SetPixel (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))), 0x0);
					
					trace_x += zoom_x;
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						min = max = dsp_a_buffer16[j][count];
					}
					else 
					{
						min = max = dsp_a_buffer[j][count];
					}
				}
			}
			else
			{
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_a_buffer16[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
				}
				else {
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_a_buffer[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
				}
				while (trace_x < (scrn_x - zoom_x))  
				{
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_a_buffer16[j][count]) / sample_resolution) * (float)(size_y))));
					}
					else {
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_a_buffer[j][count]) / sample_resolution) * (float)(size_y))));
					} 

#ifdef TRIGGER_TRACE					
					if(trigger_trace<count)
			           SelectObject (hDC, hTriggPen);
#endif					
					if (count++ >= (Depth - 1))
						break;
				}
			}
			SelectObject (hDC, hZeroPen);
			MoveToEx (hDC, 0, centre_line + size_y, NULL);
/*  Display********/
			LineTo (hDC, points, centre_line + size_y);

			if (!CheckMessageQueue ())
				break;

			/*  Do Channel B, dual channel mode.  */
   			SelectObject (hDC, hBlackPen);
			offset_y = (size_y * j * 4) + (size_y * 2) + (size_y * sample_offset);
			centre_line = (size_y * j * 4) + (size_y * 2);
			
			count = start;
			if (zoom_x2 > 1)  
			{
				trace_x = 0;
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					min = max = (int16)dsp_b_buffer16[count];
				}
				else 
				{
					min = max = (int16)dsp_b_buffer[count];
				}
				while ((trace_x < (scrn_x - zoom_x)) && (count < (Depth - 1)))  
				{
					for (x = 0 ; x < zoom_x2 ; x++)  
					{
						if (count >= (Depth - 1))
							break;
						if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
						{
							if ((val = dsp_b_buffer16[j][++count]) > max)  
							{
								max = val;
							}else if (val < min)
								min = val;
						}
						else 
						{
							if ((val = dsp_b_buffer[j][++count]) > max)  
								max = val;
							else if (val < min)
								min = val;
						}
					}
#ifdef TRIGGER_TRACE					
					if(trigger_trace<count)
			           SelectObject (hDC, hTriggPen);
#endif			

					MoveToEx (hDC, trace_x, offset_y + (int16)(((float)(min) / sample_resolution) * (float)(size_y)), NULL);
					LineTo (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))));
					SetPixel (hDC, trace_x, offset_y + (int16)((((float)(max) / sample_resolution) * (float)(size_y))), 0x0);
					trace_x += zoom_x;
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						min = max = dsp_b_buffer16[j][count];
					}
					else 
					{
						min = max = dsp_b_buffer[j][count];
					}
				}
			}
			else
			{
				if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
				{
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_b_buffer16[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
				}
				else 
				{
					MoveToEx (hDC, trace_x = 0, offset_y + (int16)(((float)(dsp_b_buffer[j][count++]) / sample_resolution) * (float)(size_y)), NULL);
				}
				while (trace_x < (scrn_x - zoom_x))  
				{
					if ((board_info.sample_bits == 12) || (board_info.sample_bits == 16))
					{
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_b_buffer16[j][count]) / sample_resolution) * (float)(size_y))));
					}
					else 
					{
						LineTo (hDC, trace_x += zoom_x, offset_y + (int16)((((float)(dsp_b_buffer[j][count]) / sample_resolution) * (float)(size_y))));
					}
					if (count++ >= (Depth - 1))
						break;
				}
			}
			SelectObject (hDC, hZeroPen);
			MoveToEx (hDC, 0, centre_line + size_y, NULL);
			LineTo (hDC, points, centre_line + size_y);
			if (!CheckMessageQueue ())
				break;
		}
   	}
	EndPaint(hWnd, &ps);
/*	fclose( stream ); */
}
	 		
/*-------------------------------------------------------------------------*/
void	Cls_OnClose (HWND hWnd)
{
	int16 tmp;

	// Add abort here in case card not aborted yet.
	gage_abort_capture (GAGE_SOFTWARE);
	if (board_info.board_type & GAGE_ASSUME_CS2125)
		tmp = gage_power_control (GAGE_POWER_OFF);
	KillTimer(hWnd, wTimerID);
	DeleteObject(hZeroPen);
	DeleteObject(hDotPen);
	DestroyWindow (SRateDlg);
    DestroyWindow (IRangeDlg);
	DestroyWindow (TriggerDlg);
	DestroyWindow (AboutDlg);
	DestroyWindow (PmtDlg);
	DestroyWindow (hWnd);
}

void	Cls_OnQuit (HWND hWnd)
{
	int16 tmp;

	if (board_info.board_type & GAGE_ASSUME_CS2125)
		tmp = gage_power_control (GAGE_POWER_OFF);
	KillTimer(hWnd, wTimerID);
	DeleteObject(hZeroPen);
	DeleteObject(hDotPen);
	DestroyWindow (SRateDlg);
    DestroyWindow (IRangeDlg);
	DestroyWindow (TriggerDlg);
	DestroyWindow (hWnd);
}

/*-------------------------------------------------------------------------*/

void	Cls_OnDestroy(HWND hWnd) 
{
	PostQuitMessage(0);
}

/*-------------------------------------------------------------------------*/

BOOL	Cls_OnCreate(HWND hWnd, LPCREATESTRUCT lpCreateStruct) 
{
	wTimerID = SetTimer (hWnd, 1, wTimerInterval, (TIMERPROC) NULL);
	hBlackPen = GetStockObject (BLACK_PEN);

#ifdef TRIGGER_TRACE	
	hTriggPen = CreatePen (PS_SOLID, 3, 0x0000ff00);
#else
	hTriggPen = hBlackPen;
#endif

	hZeroPen = CreatePen (PS_DASH, 1, 0x000000ff);
	hDotPen = CreatePen (PS_DOT, 1, 0x0000ff00);           
	hWhiteBrush = GetStockObject (WHITE_BRUSH);
	hDisplayWnd = hWnd;
	return (TRUE);
}

/*-------------------------------------------------------------------------*/

void	Cls_OnTimer(HWND hWnd, UINT id) 
{
	number_of_timer_events++;
}

/*-------------------------------------------------------------------------*/

void	Cls_OnMove(HWND hWnd, int32 x, int32 y) 
{
	Display (hWnd);
}


/************************************************************************/
/*                                                                      */
/* Main Window Procedure                                                */
/*                                                                      */
/************************************************************************/

LRESULT CALLBACK WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
 	switch (Message)
   	{
		HANDLE_MSG(hWnd, WM_COMMAND, Cls_OnCommand);
/* The WM_COMMAND message is sent when the user selects 
a command item from a menu, when a control sends a 
notification message to its parent window, or when an 
accelerator keystroke is translated. */


		HANDLE_MSG(hWnd, WM_PAINT, Cls_OnPaint);  
/* An application sends the WM_PAINT message when the 
system or another application makes a request to paint 
a portion of an application's window. */
		
		HANDLE_MSG(hWnd, WM_DESTROY, Cls_OnDestroy);
/*The WM_DESTROY message is sent when a window is being 
destroyed. It is sent to the window procedure of the 
window being destroyed after the window is removed from 
the screen. */


		HANDLE_MSG(hWnd, WM_TIMER, Cls_OnTimer);
/*The WM_TIMER message is posted to the installing 
thread's message queue when a timer expires. You can 
process the message by providing a WM_TIMER case in the 
window procedure. Otherwise, the default window procedure 
will call the TimerProc callback function specified in 
the call to the SetTimer function used to install the 
timer. */

		HANDLE_MSG(hWnd, WM_MOVE, Cls_OnMove);
/*The WM_MOVE message is sent after a window has been 
moved. */

		HANDLE_MSG(hWnd, WM_CREATE, Cls_OnCreate);
/*The WM_CREATE message is sent when an application 
requests that a window be created by calling the CreateWindowEx 
or CreateWindow function. The window procedure of the 
new window receives this message after the window is 
created, but before the window becomes visible. 
The message is sent before the CreateWindowEx or 
CreateWindow function returns. */

		HANDLE_MSG(hWnd, WM_CHAR, Cls_OnChar);
/*The WM_CHAR message is posted to the window with the 
keyboard focus when a WM_KEYDOWN message is translated 
by the TranslateMessage function. WM_CHAR contains the 
character code of the key that was pressed. */

/****************************************************/

		// Add new handler for WM_QUIT in case of system shutting down before app is closed
		// Cls_OnQuit is same as Cls_OnClose but also call gage_bufaccess_destroy()
		case WM_QUIT:
			return HANDLE_WM_QUIT(hWnd, wParam, lParam, Cls_OnQuit);

		// Add new handler for WM_ENDSESSION in case of system shutting down before app
		//is closed. (Happens in WinNT) This avoid blue screen when closing WinNT before app.
		case WM_CLOSE:
		case WM_ENDSESSION:
			return HANDLE_WM_CLOSE(hWnd, wParam, lParam, Cls_OnClose);

	default:
			return(DefWindowProc(hWnd, Message, wParam, lParam));
  	}
	return 0L;
}     /* End of WndProc                                         	*/

/************************************************************************/
/*                                                                      */
/* Dialog Window Procedure                                              */
/*                                                                      */
/************************************************************************/


LRESULT CALLBACK DIALOGAMsgProc(HWND hWndDlg, UINT Message, WPARAM wParam, LPARAM lParam)
{

	lParam = lParam;
	switch(Message)
   	{
    	case WM_INITDIALOG:
	 	cwCenter(hWndDlg, 0);
	 	/* initialize working variables                               */
	 	break; /* End of WM_INITDIALOG                                */

		case WM_CLOSE:
	 	/* Closing the Dialog behaves the same as Cancel              */
	 	PostMessage(hWndDlg, WM_COMMAND, IDCANCEL, 0L);
	 	break; /* End of WM_CLOSE                                     */

		case WM_COMMAND:
			switch(wParam)
	   		{
	    		case IDOK:
		 			EndDialog(hWndDlg, TRUE);
		 			break;
		 		
	    		case IDCANCEL:
		 		/* Ignore data values entered into the controls        */
		 		/* and dismiss the dialog window returning FALSE       */
		 			EndDialog(hWndDlg, FALSE);
		 			break;
	   		}
	 		break;    /* End of WM_COMMAND                             */

	  	default:
			return FALSE;
   	}
 	return TRUE;
} /* End of DIALOGAMsgProc                                      */


/************************************************************************/
/*                                                                      */
/* nCwRegisterClasses Function                                          */
/*                                                                      */
/* The following function registers all the classes of all the windows  */
/* associated with this application. The function returns an error code */
/* if unsuccessful, otherwise it returns 0.                             */
/*                                                                      */
/************************************************************************/

int		nCwRegisterClasses(void)
{
 	WNDCLASS   wndclass;    /* struct to define a window class             */
 	memset(&wndclass, 0x00, sizeof(WNDCLASS));
     /* load WNDCLASS with window's characteristics                         */
 	wndclass.style = CS_HREDRAW | CS_VREDRAW | CS_BYTEALIGNWINDOW;
 	wndclass.lpfnWndProc = (WNDPROC)WndProc;
 	/* Extra storage for Class and Window objects                          */
 	wndclass.cbClsExtra = 0;
 	wndclass.cbWndExtra = 0;
 	wndclass.hInstance = hInst;
 	wndclass.hIcon = LoadIcon(hInst, "GSDLLDEM");
 	wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
 	/* Create brush for erasing background                                 */
 	wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    wndclass.lpszMenuName = szAppName;   /* Menu Name is App Name */
 	wndclass.lpszClassName = szAppName; /* Class Name is App Name */
 	if(!RegisterClass(&wndclass))
   		return -1;
    return(0);
} /* End of nCwRegisterClasses                                          */

/************************************************************************/
/*  cwCenter Function                                                   */
/*                                                                      */
/*  centers a window based on the client area of its parent             */
/*                                                                      */
/************************************************************************/

void	cwCenter(HWND hWnd, int top)
{
 	POINT      pt;
 	RECT       swp;
 	RECT       rParent;
 	int32      iwidth;
 	int32      iheight;

 	/* get the rectangles for the parent and the child                     */
 	GetWindowRect(hWnd, &swp);
 	GetClientRect(hWndMain, &rParent);

 	/* calculate the height and width for MoveWindow                       */
 	iwidth = swp.right - swp.left;
 	iheight = swp.bottom - swp.top;

 	/* find the center point and convert to screen coordinates             */
 	pt.x = (rParent.right - rParent.left) / 2;
 	pt.y = (rParent.bottom - rParent.top) / 2;
 	ClientToScreen(hWndMain, &pt);

 	/* calculate the new x, y starting point                               */
 	pt.x = pt.x - (iwidth / 2);
 	pt.y = pt.y - (iheight / 2);

 	/* top will adjust the window position, up or down                     */
 	if(top)
   		pt.y = pt.y + top;

 	/* move the window                                                     */
 	MoveWindow(hWnd, pt.x, pt.y, iwidth, iheight, FALSE);
}

/************************************************************************/
/*  CwUnRegisterClasses Function                                        */
/*                                                                      */
/*  Deletes any refrences to windows resources created for this         */
/*  application, frees memory, deletes instance, handles and does       */
/*  clean up prior to exiting the window                                */
/*                                                                      */
/************************************************************************/

void	CwUnRegisterClasses(void)
{
 	WNDCLASS   wndclass;    /* struct to define a window class             */
 	memset(&wndclass, 0x00, sizeof(WNDCLASS));
    UnregisterClass(szAppName, hInst);
}    /* End of CwUnRegisterClasses    */             