// Kurt Rosenfeld 2007
// public domain
// This code was tested with a PCI-DAS4020/12
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include "..\cbw.h"
#define BUFPOINTS  67108864  // 64M
#define CAPTURESECS 60
#define SAMPLERATE (20 * 1000 * 1000)

void cleanup(int BoardNum, WORD *ADData);

void main () {
	int BoardNum=0, bufcount = 0, ULStat=0, LowChan=0, HighChan=0, Gain=BIP1VOLTS;
	short Status = RUNNING;
	long CurCount, CurIndex, Count=BUFPOINTS, Rate=SAMPLERATE;
	unsigned int Options = BACKGROUND + CONTINUOUS; // + CONVERTDATA;	
	float revision = (float)CURRENTREVNUM;
	int OldIndex = 0, first_data = 1; 
	INT64 sum=0, samp_count=0;
	WORD *ADData; 
	FILE *out;

	out = fopen("d:\\4020.out", "wb"); // drive must handle data rate
	if (NULL == out) { printf("out open failed\n"); exit(1); }
	ULStat = cbDeclareRevision(&revision);
	ADData = (WORD*)cbWinBufAlloc(BUFPOINTS); 
	if (!ADData) { fprintf(stderr, "cbWinBufAlloc failed\n");  exit(1); }  
	cbErrHandling (PRINTALL, DONTSTOP);
	ULStat = cbAInScan(BoardNum, LowChan, HighChan, Count, &Rate, Gain, ADData, Options);
    
	while (!kbhit() && Status==RUNNING && samp_count<Rate*CAPTURESECS) {
		int r=0, wrote_this_pass=0;
		fflush(stdout);
		Sleep(50); // msec
		ULStat = cbGetStatus (BoardNum, &Status, &CurCount, &CurIndex,AIFUNCTION);
		if (CurIndex < 0) { // no valid data yet 
			printf("CurIndex < 0\n"); 
			continue;
		}
		if (CurIndex >= 0 && first_data) { // we'll wait for more data
			printf("seeing first data\n");
			first_data = 0;		
			OldIndex = CurIndex;
			continue;
		}
		if (CurIndex == OldIndex) { 
			printf("CurIndex didn't advance since last pass\n");
		}
		if (CurIndex > OldIndex) { // write out the region from old to cur-1
			printf("case A, ");
			r = fwrite(&(ADData[OldIndex]), sizeof(WORD), CurIndex - OldIndex, out); 
			if (r != CurIndex - OldIndex) { 
				fprintf(stderr, "A: wrote %d instead of %d\n", r, CurIndex - OldIndex);  
				cleanup(BoardNum, ADData);
			}
			wrote_this_pass += r;
		}
		if (CurIndex < OldIndex) { // write out old to end and start to cur-1 
			printf("case B, ");
			r = fwrite(&(ADData[OldIndex]), sizeof(WORD), BUFPOINTS - OldIndex, out);
			if (r != BUFPOINTS - OldIndex) { 
				fprintf(stderr, "B1: wrote %d instead of %d\n", r, BUFPOINTS - OldIndex);  
				cleanup(BoardNum, ADData);
			}
			wrote_this_pass += r;
			r = fwrite(&(ADData[0]), sizeof(WORD), CurIndex, out);
			if (r != CurIndex) { 
				fprintf(stderr, "B2: wrote %d instead of %d\n", CurIndex);  
				cleanup(BoardNum, ADData);
			}
			wrote_this_pass += r;
		}
		printf("wrote %d words, CurIndex = %d\n", wrote_this_pass, CurIndex);
		OldIndex = CurIndex;
		samp_count += wrote_this_pass;		
	}
	printf("wrote %I64d words, %I64d bytes foo total\n", samp_count, 2*samp_count);
	cleanup(BoardNum, ADData);
	fclose(out);
}

void cleanup(int BoardNum, WORD *ADData) {
	fprintf(stderr, "cleaning up\n");
	cbStopBackground(BoardNum,AIFUNCTION);
	cbWinBufFree(ADData);
}	